更新记录

1.0.11(2025-10-17)

  1. 增加退出回到桌面、闪光灯、震动、截屏、屏幕亮度、电话/短信、隐藏显示状态栏等接口

1.0.10(2025-07-02)

  1. 增加闪光灯、屏幕亮度、横竖屏、系统语言等等

1.0.9(2025-06-30)

  1. 重新调整接口结构
  2. 增加harmony支持
  3. 增肌路径工具类
查看更多

平台兼容性

uni-app(3.6.10)

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

uni-app x(3.6.10)

Chrome Safari Android iOS 鸿蒙 微信小程序
- - 5.0 -

其他

多语言 暗黑模式 宽屏模式

常用工具类,文件IO、图片base64、设备ID、保存删除app后的数据

文件IO工具类(增、删、查、改,copy、move、rename、遍历等等)

图片与Base64相互转换

路径工具类(获取项目资源文件绝对路径、app临时目录)

各种设备ID工具类(deviceID、serial、IMEI、MEID、OAID、AAID)等等

钥匙串保存数据/关键资产存储服务(保存的数据app删除重装后还能获取)

退出回到桌面、状态栏显示/隐藏、闪光灯开关、设置横竖屏、截屏、震动、拨打电话、发短信、设置音量

如需更多常用工具类接口,请点击"进入交流群"联系作者

集成步骤

  • 集成插件步骤请参考https://www.cnblogs.com/wenrisheng/p/18323027
  • harmony需要配置权限ohos.permission.STORE_PERSISTENT_DATA、ohos.permission.APP_TRACKING_CONSENT、ohos.permission.VIBRATE,可参考官网权限配置

常用文件处理插件

  1. 文件预览https://ext.dcloud.net.cn/plugin?id=17167

    • 支持pdf/word/excel/ppt/图片,支持本地或在线文件
  2. 文件增删查改copy遍历https://ext.dcloud.net.cn/plugin?id=14130

    • 文件读写、copy、move、遍历文件夹、是否存在、删除、重命名......等等
  3. 读取常用文件目录(包含U盘)https://ext.dcloud.net.cn/plugin?id=15243

    • uniapp(_www、_doc、_download)
    • Android(数据目录、文件目录、缓存目录、环境environment目录、SDCard目录、外接U盘)
    • iOS(documentDirectory、temporaryDirectory、libraryDirectory、cachesDirectory)
    • 从U盘读取或保存文件
  4. 读取本地相册图片视频音频https://ext.dcloud.net.cn/plugin?id=15711

    • 获取相册图片、视频、音频文件
    • 常用于自定义相册UI

路径工具类(获取项目资源文件绝对路径、app临时目录)


import {
    UTSPathUtils
} from "@/uni_modules/wrs-uts-commonutils"
let pathUtils = new UTSPathUtils()
  • 获取项目资料的绝对路径

// 获取项目里static下logo.png在手机上的绝对路径
let resourcePath = pathUtils.getResourcePath("/static/logo.png")
  • 获取app的缓存路径,该目录一般用来保存临时数据

let cacheDir = pathUtils.getCacheDir()
  • 获取app的fileDir路径,该目录一般用来保存数据库文件、配置文件、下载文件,app重启后数据还在,app删除后数据才会删除

let fileDir = pathUtils.getFileDir()

文件IO工具类(增、删、查、改,copy、move、rename、遍历等等)


    import {
        UTSFileUtils
    } from "@/uni_modules/wrs-uts-commonutils"
    let fileUtils = new UTSFileUtils()
  • 判断文件是否存在

let resourcePath = pathUtils.getResourcePath("/static/loginSys/index.html")
fileUtils.isFileExists(resourcePath, (resp) => {
    let isExist = resp.flag
    this.showMsg("isFileExists:" + resourcePath + "  存在:" + isExist)
})
  • 重命名文件

let newName = "bb.png"
let filePath = pathUtils.getResourcePath("/static/logo.png")
fileUtils.rename(filePath, newName, (resp) => {
    let flag = resp.flag
    if (flag) {
        this.showMsg("rename:" + filePath + "  result:" + flag)
    } else {
        this.showMsg("rename:" + filePath + "  result:" + JSON.stringify(resp))
    }

})
  • 判断是否是目录

let filePath = pathUtils.getResourcePath("/static")
fileUtils.isDir(filePath, (resp) => {
    let isExist = resp.flag
    this.showMsg("isDir:" + filePath + "  目录:" + isExist)
})
  • 判断是否是文件

let filePath = pathUtils.getResourcePath("/static/logo.png")
fileUtils.isFile(filePath, (resp) => {
    let isFile = resp.flag
    this.showMsg("isFile:" + filePath + "  文件:" + isFile)
})
  • 判断目录是否存在,如果不存在则创建目录

let dir = pathUtils.getResourcePath("/testDir")
fileUtils.createOrExistsDir(dir, (resp) => {
    let suc = resp.flag
    if(suc) {
        this.showMsg("createOrExistsDir:" + dir + "  result:" + suc)
    } else {
        this.showMsg("createOrExistsDir:" + dir + "  result:" + JSON.stringify(resp))
    }
})
  • 复制文件或目录

let srcPath = pathUtils.getResourcePath("/static/loginSys")
let destPath = pathUtils.getCacheDir() + "/loginSysCopy"
console.log("copy srcPath:" + srcPath + " destPath:" + destPath)
fileUtils.copy(srcPath, destPath, (resp) => {
    let suc = resp.flag
    if(suc) {
        this.showMsg("copy:" + suc)
    } else {
        this.showMsg("copy:" + JSON.stringify(resp))
    }
})
  • 移动文件或目录

let srcPath = pathUtils.getResourcePath("/static/loginSys")
let destPath = pathUtils.getCacheDir() + "/loginSysMove"
fileUtils.move(srcPath, destPath, (resp) => {
    let suc = resp.flag
    if(suc) {
        this.showMsg("move:" + suc)
    } else {
        this.showMsg("move:" + JSON.stringify(resp))
    }
})
  • 删除文件或目录

// var path = "/sdcard/bb/test.png"
let path = pathUtils.getResourcePath("/static/loginSys")
fileUtils.delete(path, (resp) => {
    let suc = resp.flag
    if(suc) {
        this.showMsg("delete:" + suc)
    } else {
        this.showMsg("delete:" + JSON.stringify(resp))
    }
})
  • 获取目录下所有的文件和文件夹

let path = pathUtils.getResourcePath("/static/loginSys")
fileUtils.listFilesInDir(path, (resp) => {
    let list = resp.fileArray
    // {"fileArray":[{"isFile":true,"name":"index.html"},{"isFile":false,"name":"css"},{"isFile":false,"name":"images"},{"isFile":false,"name":"js"},{"isFile":true,"name":"test.png"},{"isFile":true,"name":"test.pdf"},{"isFile":true,"name":"3d.svf"}]}
    this.showMsg("listFilesInDir:" + path + "  result:" + JSON.stringify(list))
})
  • 获取文件或目录大小

let path = pathUtils.getResourcePath("/static/loginSys")
fileUtils.getLength(path, (resp) => {
    let length = resp.length
    // 单位:字节byte
    this.showMsg("getLength:" + path + "  result:" + length)
})
  • 读取文件文本内容

let path = pathUtils.getResourcePath("/static/loginSys/index.html")
fileUtils.readFile2String(path, (resp) => {
    let str = resp.str
    if(str) {
        this.showMsg("readFile2String:" + path + "  result:" + str)
    } else {
        this.showMsg("readFile2String error:" + JSON.stringify(resp))
    }

})
  • 将字符串文本写入文件

let filePath = pathUtils.getCacheDir() + "/text.txt"
var content = "aaaa" 
var append = false
fileUtils.writeFileFromString(filePath, content, append, (resp) => {
    let suc = resp.flag
    if(suc) {
        this.showMsg("writeFileFromString:" + filePath + "  result:" + suc)
    } else {
        this.showMsg("writeFileFromString error:" + JSON.stringify(resp))
    }

})
  • 获取文件目录

let filePath = pathUtils.getResourcePath("/static/logo.png")
fileUtils.getDirName(filePath, (resp) => {
    let path = resp.path
    this.showMsg("getDirName:" + filePath + "  result:" + path)
})

图片工具类


import {
    UTSImageUtils,
    UTSPathUtils
} from "@/uni_modules/wrs-uts-commonutils"
let imageUtils = new UTSImageUtils()
  • 图片转Base64

注意: 有些android系统转出来的图片会以“data;image/png;base64,”为前缀,iOS没有这个前缀


var filePath = pathUtils.getResourcePath("/static/logo.png")
imageUtils.image2Base64(filePath, (resp) => {
    let str = resp.str
    if (str) {
        // "data:image/png;base64," + base64Str
        // android转出来的图片会以“data;image/png;base64,”为前缀,iOS、harmony没有这个前缀
        this.showMsg("image2Base64 file:" + filePath + " base64Str:" +
            str)
    } else {
        // android转出来的图片会以“data;image/png;base64,”为前缀,iOS没有这个前缀
        this.showMsg("image2Base64 error:" + JSON.stringify(resp))
    }
})
  • Base64转图片

var base64Str =
    "iVBORw0KGgoAAAANSUhEUgAAAEgAAABICAYAAABV7bNHAAAAAXNSR0IArs4c6QAAAARzQklUCAgICHwIZIgAAAFLSURBVHic7dyxbQIxHEbxzxYVNRukDAMQNmCRMAIzMALZgZoNThngWjagpoxThC4Jr0HyRXm/9qTzX0/2VdaVJFke1k8tH/ukbJLM879dk3YqqbtxO5zLV5y8J23Re7JpKZeSrGa3nWOcb9qipe3r7VjpR2VT4zfnnnntPcHUGQgYCBgIGAgYCBgIGAgYCBgIGAgYCBgIzB7xkvF1+PXZ8m39iCW6re0OAgYCBgIGAgYCBgIGAgYCBgIGAgYCBgIGAgYCBgIGAgYCBgIGAgYCBgIGAgYCBgIGAgYCBgIGAgYCBgIGAg+5H3TPvfs7f4E7CBgIGAgYCBgIGAiU58NL6z3ElLmDgIGAgYCBgIGAgYCBgIGAgYCBQE1y7T3EhF1r0k69p5iudqoldZeUS+9RpqdcSuqujtvhXJJV0o7xuCVfvwk8lmQ1bofzJxi+NCeqg7Y3AAAAAElFTkSuQmCC"
let filePath = pathUtils.getCacheDir() + "/aa.png"
imageUtils.base642Image(base64Str, filePath, (resp) => {
    let flag = resp.flag
    if (flag) {
        this.showMsg("base642Image suc")
    } else {
        this.showMsg("base642Image error:" + JSON.stringify(resp))
    }
})

设备工具类


import {
    UTSDeviceUtils
} from "@/uni_modules/wrs-uts-commonutils"
let deviceUtils = new UTSDeviceUtils()
  • 获取设备deviceID,可以当作是设备硬件ID

在iOS里除非系统重置可能会导致设备ID改变,app重装或升级系统都不会改变设备ID,harmony采用的是uuid保存到关键资产存储里面


deviceUtils.getUniqueDeviceId((resp) => {
    let value = resp.value
    if (value) {
        this.showMsg("设备唯一ID:" + value)
    } else {

        this.showMsg("设备唯一ID获取失败:" + JSON.stringify(resp))
    }
})

let params = {}
params.permissions = [
    "ohos.permission.APP_TRACKING_CONSENT"
]
permissionsUtils.requestPermissionsFromUser(params, (resp) => {
    if (resp.authResults[0] == 0) {
        deviceUtils.getOAID((resultResp) => {
            let value = resultResp.value
            if (value) {
                this.showMsg("OAID:" + value)
            } else {

                this.showMsg("OAID获取失败:" + JSON.stringify(resp))
            }
        })
    } else {
        this.showMsg("OAID获取失败,无获取权限:" + JSON.stringify(resp))
    }
})

deviceUtils.getAAID((resultResp) => {
    let value = resultResp.value
    if (value) {
        this.showMsg("AAID:" + value)
    } else {
        this.showMsg("AAID获取失败:" + JSON.stringify(resp))
    }
})

钥匙串保存数据/关键资产存储服务(保存的数据app删除重装后还能获取,仅支持iOS、harmony)

android如果有此业务场景需要方案请联系作者


import {
    UTSKeyChainUtils
} from "@/uni_modules/wrs-uts-commonutils"
let keyChainUtils = new UTSKeyChainUtils()
  • 保存值

例如:保存登录模块账号为zhangsan的密码


let params = {}
params.value = "暗示法在S地方312312"
params.key = "name"
keyChainUtils.setValue(params, (resp) => {
    let flag = resp.flag
    if (flag) {
        this.showMsg("保存成功")
    } else {
        this.showMsg("保存失败:" + JSON.stringify(resp))
    }
})
  • 获取值

let params = {}
params.key = "name"
keyChainUtils.getValue(params, (resp) => {
    let value = resp.value
    if (value) {
        this.showMsg("获取成功:" + value)
    } else {
        this.showMsg("获取失败:" + JSON.stringify(resp))
    }
})
  • 删除值

let params = {}
params.key = "name"
keyChainUtils.deleteValue(params, (resp) => {
    let value = resp.value
    if (value) {
        this.showMsg("删除成功:" + value)
    } else {
        this.showMsg("删除失败:" + JSON.stringify(resp))
    }
})

app工具类

    import {
    UTSAppUtils,
    UTSPathUtils,
    UTSPermissionsUtils
} from "@/uni_modules/wrs-uts-commonutils"
  • 获取UserAgent,仅支持iOS

let ua = appUtils.getUserAgent()
  • 退出app

let ua = appUtils.exitApp()
  • 回到桌面(不支持iOS)

appUtils.toHome()
  • 获取屏幕信息(包含宽高、亮度、密度、锁屏、状态栏等等)

// android,长度单位是px
// {
//  "statusBarHeight": 154,
//  "screenDensity": 3.3125,
//  "isFlashlightOn": false,
//  "isLandscape": false,
//  "screenDensityDpi": 530,
//  "appScreenHeight": 2486,
//  "windowBrightness": 80,
//  "isStatusBarVisible": true,
//  "isStatusBarLightMode": true,
//  "isScreenLock": false,
//  "screenHeight": 2640,
//  "isAutoBrightnessEnabled": true,
//  "brightness": 80,
//  "appScreenWidth": 1200,
//  "isPortrait": true,
//  "screenWidth": 1200,
//  "isFlashlightEnable": true
// }

// ios,长度单位dp
// {
//     "isLandscape": false,
//     "isPortrait": false,
//     "screenWidth": 393,
//     "screenHeight": 852,
//     "statusBarHeight": 54,
//     "scale": 3,
//     "brightness": 0.5
// }
appUtils.getScreenInfo((resp) => {
    this.showMsg("getScreenInfo:" + JSON.stringify(resp))
})
  • 设置状态栏-隐藏(不支持ios)
switch (uni.getSystemInfoSync().platform) {
    case 'android': {
        let params = {}
        params.visible = false
        params.isWindow = true // 窗口还是当前页面
        appUtils.setStatusBar(params)
    }
    break;
    case 'ios': {

    }
    break;
    case 'harmonyos': {
        let params = {}
        params.names = ["navigation"]
        appUtils.setStatusBar(params)
    }
    break;
    default:
        break;
}
  • 设置状态栏-显示(不支持ios)
switch (uni.getSystemInfoSync().platform) {
    case 'android': {
        let params = {}
        params.visible = true
        params.isWindow = true // 窗口还是当前页面
        appUtils.setStatusBar(params)
    }
    break;
    case 'ios': {

    }
    break;
    case 'harmonyos': {
        let params = {}
        params.names = ["status", "navigation"]
        appUtils.setStatusBar(params)
    }
    break;
    default:
        break;
}
  • 设置闪光灯-开(不支持harmony)

  • switch (uni.getSystemInfoSync().platform) {
    case 'harmonyos': {
    
    }
    break;
    default: {
        let params = {}
        params.enable = true
        appUtils.setFlashlight(params)
    }
    break;
    }
  • 设置闪光灯-关(不支持harmony)

switch (uni.getSystemInfoSync().platform) {
    case 'harmonyos': {

    }
    break;
    default: {
        let params = {}
        params.enable = false
        appUtils.setFlashlight(params)
    }
    break;
}
  • 设置屏幕亮度(android需要配置权限android.permission.WRITE_SETTINGS)
            switch (uni.getSystemInfoSync().platform) {
    case 'android': {
        let hasPermission = permissionsUtils.settingCanWrite()
        if (hasPermission) {
            let params = {}
            params.value = 10 // 0~255
            params.isWindow = false // 窗口亮度还是屏幕亮度
            appUtils.setBrightness(params)
        } else {
            permissionsUtils.toManageWriteSetting()
        }
    }
    break;
    case 'ios': {

        let params = {}
        params.value = 0.5 // 0.1~1
        appUtils.setBrightness(params)
    }
    break;
    case 'harmonyos': {
        // 取值范围为[0.0, 1.0]或-1.0。1.0表示最亮,-1.0表示默认亮度。
        let params = {}
        params.value = 0.5 // 0.1~1
        appUtils.setBrightness(params)
    }
    break;
    default:
        break;
}
  • 设置屏幕方向-横屏(不支持iOS)
let params = {}
params.orient = "landscape" // landscape、portrait
appUtils.setScreenOrient(params)
  • 设置屏幕方向-竖屏(不支持iOS)
let params = {}
params.orient = "portrait" // landscape、portrait
appUtils.setScreenOrient(params)
  • 截屏(截取app屏幕)
let params = {}
params.filePath = pathUtils.getCacheDir() + "/screenshot.png"
appUtils.screenShot(params, (resp) => {
    let flag = resp.flag
    if (flag) {
        this.showMsg("截屏成功:" + params.filePath)
    } else {
        this.showMsg("截屏失败:" + JSON.stringify(resp))
    }
})
  • 震动
                let params = {}
switch (uni.getSystemInfoSync().platform) {
    case 'android': {
        const userType1 = true
        if (userType1) {
            // 传入震动持续时间
            params.duration = 2000
        } else {
            // 传入震动频率和重复次数
            // pattern的含义如下,振动500毫秒,停止300毫秒,振动400毫秒,停止200毫秒,以此类推,两个数值一组。
            params.pattern = [500, 300, 400, 200]
            params.repeat = -1 // -1不重复,非-1为从pattern的指定下标开始重复  
        }
    }
    break;
    case 'ios': {
        // 1520:普通短震,3D Touch 中 Pop 震动反馈
        // 1519:普通短震,3D Touch 中 Peek 震动反馈
        // 1521:连续三次短震
        params.systemSoundID = 4095
    }
    break;
    case 'harmonyos': {
        params.duration = 2000
    }
    break;
    default:
        break;
}

appUtils.setVibrate(params)
  • 获取系统语言
    
    let info = appUtils.getLanuage()
- 拨打电话

```javascript
let params = {}
params.tel = "***"
// dial: true 弹出拨号界面
// dial: false 直接拨打电话,需要权限android.permission.CALL_PHONE
params.dial = false
appUtils.call(params)
  • 发送短信
let params = {}
params.to = "***"
params.content = "您好,欢迎来到uniapp插件"
appUtils.sendSMS(params)
  • 设置音量(不支持iOS)

switch (uni.getSystemInfoSync().platform) {
    case 'android': {
        appUtils.getVolumeInfo((result) => {
            let volumes = result.volumes
            if (volumes) {
                let length = volumes.length
                let titleArray = []
                for (let i = 0; i < length; i++) {
                    let model = volumes[i]
                    let title = model.name + " cur:" + model.volume + " max:" + model.maxVolume +
                        " min:" +
                        model
                        .minVolume
                    titleArray.push(title)
                }
                this.showActionSheet(titleArray, (index) => {
                    let model = volumes[index]
                    let params = {}
                    params.streamType = model.streamType
                    params.value = parseInt((model.maxVolume + model.minVolume) / 2)
                    appUtils.setVolume(params)
                })
            } else {
                this.showToast("获取音量数据出错")
            }
        })

    }
    break;
    case 'ios': {

    }
    break;
    case 'harmonyos': {
        appUtils.getVolumeInfo((result) => {
            console.log(JSON.stringify(result))
            let volumes = result.volumes
            if (volumes) {
                let length = volumes.length
                let titleArray = []
                for (let i = 0; i < length; i++) {
                    let model = volumes[i]
                    let title = model.name + " cur:" + model.volume + " max:" + model.maxVolume +
                        " min:" +
                        model
                        .minVolume
                    titleArray.push(title)
                }
                this.showActionSheet(titleArray, (index) => {
                    let model = volumes[index]
                    let params = {}
                    params.streamType = model.streamType
                    params.value = parseInt((model.maxVolume + model.minVolume) / 2) // 整型
                    appUtils.setVolume(params)
                })
            } else {
                this.showToast("获取音量数据出错")
            }
        })
    }
    break;
    default:
        break;
}

隐私、权限声明

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

android: 文件读写权限 harmony: ohos.permission.STORE_PERSISTENT_DATA、ohos.permission.APP_TRACKING_CONSENT

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

插件不采集任何数据

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