/** * 基本缓存方法 */ export function getCookie(key) { let str = uni.getStorageSync(key); return str; } export function setCookie(key, data) { uni.setStorageSync(key, data) } export function removeCookie(key) { uni.removeStorageSync(key); } /** * 复制内容到黏贴板 * @param {Object} str */ copy(str) { uni.setClipboardData({ data: str, success: function() { uni.showToast({ title: "Copy Successfully", duration: 2000, icon: 'none' }); }, fail(err) { uni.showToast({ title: "Replication Failed:" + err, duration: 2000, icon: 'none' }); } }) }, /** * 消息提示 * @param {Object} title */ message(title) { if (title) { uni.showToast({ title: title, duration: 2000, icon: 'none' }); } else { return { ok: function() { uni.showToast({ title: "操作成功", duration: 2000, icon: 'success' }); }, err: function(title) { uni.showToast({ title: "操作失败", duration: 2000, icon: 'error' }); } } } } /** * 获取本地图片 * @param {Object} imagePath */ getImageStatic(imagePath) { return '/static/image/' + imagePath; } /** * 保留小数点 * @value { number|string } 原数据 * @fractionDigits {number} 保留几位数 默认2位 */ round(value, fractionDigits = 2) { var tens = Math.pow(10, fractionDigits); return Math.round(value * tens) / tens; } /** * 检测时间差 * var now = new Date().getTime(); * checkTimeDiff(lastTime, now, 1) * @param {Object} lastTime * @param {Object} now 当前时间 * @param {Object} timeDiffSecondsValues */ checkTimeDiff(lastTime, now, timeDiffSecondsValues) { if (!lastTime) { return true; } if (now - lastTime > timeDiffSecondsValues * 1000) { return true; } return false; } /** * 替换字符串 * @str {string} stringFormat('测试{0}123{1}',"内容","完成") */ stringFormat(str, ...args) { if (!str) { return str; } for (var i = 0; i < args.length; i++) { var reg = new RegExp("({)" + i + "(})", "g"); str = str.replace(reg, args[i]); } return str; } /** * 获取设备信息 * @deviceId 设备 id * @appId manifest 中应用appid,即DCloud appid。 * @appName manifest 中应用名称 * @appVersion manifest 中应用版本名称。 * @appVersionCode manifest 中应用版本名号。 * @screenWidth 屏幕宽度 * @screenHeight 屏幕高度 * @windowWidth 可使用窗口宽度 * @windowHeight 可使用窗口高度 * @windowTop 可使用窗口的顶部位置 * @windowBottom 可使用窗口的底部位置 * @statusBarHeight 手机状态栏的高度 * @deviceType 设备类型。如phone、pad、pc、unknow * @deviceBrand 设备品牌。如:apple、huawei * @deviceModel 设备型号 * @osName 系统名称 ios、android * @osVersion 操作系统版本。如 ios 版本,andriod 版本 */ getSystemInfo() { let systemInfo = {}; uni.getSystemInfo({ success: (e) => { systemInfo.deviceId = e.deviceId; systemInfo.appId = e.appId; systemInfo.appName = e.appName; systemInfo.appVersion = e.appVersion; systemInfo.appVersionCode = e.appVersionCode; systemInfo.screenWidth = e.screenWidth; systemInfo.screenHeight = e.screenHeight; systemInfo.windowWidth = e.windowWidth; systemInfo.windowHeight = e.windowHeight; systemInfo.windowTop = e.windowTop; systemInfo.windowBottom = e.windowBottom; systemInfo.statusBarHeight = e.statusBarHeight; systemInfo.deviceType = e.deviceType; systemInfo.deviceBrand = e.deviceBrand; systemInfo.deviceModel = e.deviceModel; systemInfo.osName = e.osName; systemInfo.osVersion = e.osVersion; } }) return systemInfo; }