更新记录

1.1.=4(2025-11-29) 下载此版本

更新使用文档

1.1.3(2025-11-29) 下载此版本

更新使用文档

1.1.2(2025-11-28) 下载此版本

文档新增runAfterLogin使用示例

查看更多

平台兼容性

uni-app(3.6.15)

Vue2 Vue3 Chrome Safari app-vue app-nvue Android Android插件版本 iOS iOS插件版本 鸿蒙
- - - - - - 5.0 1.1.0 12 1.1.0 -
微信小程序 支付宝小程序 抖音小程序 百度小程序 快手小程序 京东小程序 鸿蒙元服务 QQ小程序 飞书小程序 快应用-华为 快应用-联盟
- - - - - - - - - - -

uni-app x(3.6.17)

Chrome Safari Android Android插件版本 iOS iOS插件版本 鸿蒙 微信小程序
- - 5.0 1.1.0 12 1.1.0 - -

t-xtools

基于 uni-app 的通用工具插件,按模块聚合常用功能,支持 Vue3 插件化挂载,一次引入,全局可用。

安装与使用

  1. main.js 引入并挂载:
import tXTools from '@/uni_modules/t-xtools'

export function createApp() {
  const app = createSSRApp(App)
  app.use(tXTools, { mpShare: true })
}
  1. 使用方式:
  • 通过 this.$xtoolsuni.$xtools 访问聚合方法
  • 兼容别名:$imageCache$debounce$throttle

功能一览

  • 配置
    • Default 默认图片地址
    • Domain 相对路径补全域名
  • 图片
    • perfectUrl(url) 补全图片地址
    • isBase64(str) 判断是否为 base64
    • preview(url|Array) 预览图片
    • imageCache(url) APP/小程序端图片缓存
  • 时间
    • getTime(ten=true) 返回时间戳:true 秒(10位),false 毫秒(13位)
    • timeFormat(dateTime, fmt) 格式化为指定字符串,如 yyyy-mm-ddyyyy年mm月dd日 hh时MM分
    • timeFrom(timestamp, fmt|false) 转为“多久之前”,false 时始终返回“xx之前”
  • 登录
    • waitForLogin(loginKey?) 等待登录,就绪条件为本地缓存存在指定键,默认 appUid
    • runAfterLogin(fn, label, loginKey?) 登录就绪后执行回调
    • LoginKey 登录就绪检测的键,默认 appUid
    • setLoginKey(key) 设置登录就绪检测键

示例(runAfterLogin 获取并写入用户信息)

// 在页面或组件内使用
this.$xtools.runAfterLogin(() => {
  return this.$http.getUserInfo().then(result => {
    this.$xtools.get('userInfo', result.data)
  })
}, 'upUserInfo')
  • 导航
    • goNext(url)goBack(n)goTab(url)goAllClose(url)goClose(url)
    • page() 获取当前页面路径
  • 存储
    • get(key, value?)set(key, value)getRemove(key)remove(key)
  • 通知
    • showtt(text, icon)showload()
  • 节流防抖
    • debounce(fn, wait, immediate)throttle(fn, wait, immediate)
  • 小程序
    • openMiniProgram(appId, path, envVersion, allowFullScreen=true) 跳转到其他小程序
  • 分享(小程序)
    • 通过插件参数 { mpShare: true } 开启,提供 uni.$xtools.mpShare
    • 在页面中设置:
onLoad() {
  this.$xtools.mpShare = {
    title: '标题',
    path: '/pages/index/index',
    imageUrl: '/static/share.jpg'
  }
}

示例

<template>
  <uv-button text="去个人中心" @click="$xtools.goNext('/pages/profile/profile')" />
</template>

<script>
export default {
  onLoad() {
    const avatar = this.$xtools.perfectUrl('images/a.png')
    const ts10 = this.$xtools.getTime()      // 10位
    const ts13 = this.$xtools.getTime(false) // 13位
    const fmt = this.$xtools.timeFormat(ts13, 'yyyy年mm月dd日 hh时MM分')
    const from = this.$xtools.timeFrom(ts13)
    // 配置登录就绪键
    this.$xtools.setLoginKey('token')
    // 或直接赋值
    this.$xtools.LoginKey = 'token'

    // 等待登录(可传入自定义键)
    this.$xtools.waitForLogin('token').then(() => {
      // ...
    })
  }
}
</script>

目录结构

uni_modules/t-xtools/
  ├─ index.js           // 插件聚合与挂载
  ├─ share.js           // 小程序分享 mixin
  └─ js_sdk/            // 模块源码
     ├─ image.js        // perfectUrl/isBase64/preview
     ├─ getImageCache.js
     ├─ time.js         // getTime/timeFormat/timeFrom
     ├─ login.js        // waitForLogin/runAfterLogin
     ├─ navigate.js     // goNext/goBack/...
     ├─ storage.js      // get/set/getRemove/remove
     ├─ notify.js       // showtt/showload
     ├─ throttle.js
     ├─ debounce.js
     ├─ miniProgram.js  // openMiniProgram
     └─ router.js       // page

约定

  • 插件挂载后自动提供:this.$xtoolsuni.$xtools
  • 兼容旧用法:$imageCache$debounce$throttle
  • 统一通过 $xtools.perfectUrl() 处理网络图片地址

隐私、权限声明

1. 本插件需要申请的系统权限列表:

2. 本插件采集的数据、发送的服务器地址、以及数据用途说明:

插件不采集任何数据

3. 本插件是否包含广告,如包含需详细说明广告表达方式、展示频率:

许可协议

MIT协议

暂无用户评论。