import request from "@/utils/request.js"; export default { audioStorageKey: "audioStorageKey", pcmStorageKey: "pcmStorageKey", guid: function generateUUID() { return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { let r = Math.random() * 16 | 0; let v = c === 'x' ? r : (r & 0x3 | 0x8); return v.toString(16); }); }, alert: function(title, content, callback) { if (!title) { title = '提示' } if (!content) { content = title; } uni.showModal({ title: title, content: content, success: function(res) { if (res.confirm) { console.log('用户点击确定'); } else if (res.cancel) { console.log('用户点击取消'); } if (callback) { callback(res); } } }); }, showLoading: function(title, mask) { uni.showLoading({ title: title, mask: mask, }) }, hideLoading: function() { uni.hideLoading(); }, showToast: function(title, mask, duration, callback) { if (!duration) { duration = 1500; } if (mask == undefined) { mask = false; } uni.showToast({ title: title, mask: mask, duration: duration, callback: callback, icon: 'none' }) }, GetData: function(url, data, method, contentType, succ, err, complete) { var these = this; if (!url) { console.error("url为空"); return; } if (url.toLowerCase().indexOf('http://') == -1 || url.toLowerCase().indexOf('https://') == -1) { if (url.indexOf('/') == 0) { url = url.substr(1, url.length - 1); } let ServerPath = these.DevApi; if (these.Version === 'Dev') { ServerPath = these.DevApi; } else if (these.Version === 'Uat') { ServerPath = these.UatApi; } else if (these.Version === 'Relese') { ServerPath = these.ReleseApi; } else { these.DevApi } url = ServerPath + url; } var these = this; if (!method) { method = 'POST'; } method = method.toUpperCase(); if (!contentType) { contentType = 'application/json;charset=UTF-8'; } these.checkLAN( function() { these.showLoading('请稍候..', true); setTimeout(function() { uni.request({ url: url, data: data, header: { "Content-Type": contentType }, method: method, timeout: 60000, dataType: 'json', success: function(json) { if (succ) { succ(json); } }, fail: function(ex) { if (err) { err(ex); } }, complete: function() { if (complete) { complete(); } } }); }, 0); } , function() { these.showToast('无网络连接'); }); }, checkLAN: function(succ, error) { uni.getNetworkType({ success: (res) => { let networkType = res.networkType; // 判断网络是否连接 if (networkType === 'none') { console.error('无网络连接') if (error) { error(); } } else { if (succ) { succ(); } } }, fail: (err) => { console.error('获取网络状态失败:', err); if (error) { error(); } } }); }, DateFormat: function(date, format) { if (!date) { date = new Date(); } if (!format) { format = 'yyyy-MM-dd HH:mm:ss'; } // 处理参数默认值 if (typeof date === 'string' || typeof date === 'number') { date = new Date(date); } date = date instanceof Date ? date : new Date(); format = format || 'yyyy-MM-dd'; // 检查日期是否有效 if (isNaN(date.getTime())) { return 'Invalid Date'; } // 定义格式化映射 let formatMap = { 'yyyy': date.getFullYear(), 'MM': String(date.getMonth() + 1).padStart(2, '0'), 'dd': String(date.getDate()).padStart(2, '0'), 'HH': String(date.getHours()).padStart(2, '0'), 'mm': String(date.getMinutes()).padStart(2, '0'), 'ss': String(date.getSeconds()).padStart(2, '0'), 'SSS': String(date.getMilliseconds()).padStart(3, '0'), 'M': date.getMonth() + 1, 'd': date.getDate(), 'H': date.getHours(), 'm': date.getMinutes(), 's': date.getSeconds(), 'S': date.getMilliseconds() }; // 替换格式字符串中的占位符 return format.replace(/(yyyy|MM|dd|HH|mm|ss|SSS|M|d|H|m|s|S)/g, (match) => { return formatMap[match]; }); }, getdeviceShareId(id) { //获取设备分享信息 return request({ url: `/app/deviceShare/${id}`, method: 'get', }) }, getPermissions(type) { if (!type) { type = '6170'; } let array = [{ value: "1", label: "灯光模式", checked: false, type: ['6170', '670', '102', '6155', '650', '7305', '6075'] }, { value: "2", label: "激光模式", checked: false, type: ['6170', '6075'] }, { value: "3", label: "开机画面", checked: false, type: ['210', '6170', '670', '6155', '650', '7305', '6075'] }, { value: "4", label: "人员信息登记", checked: false, type: ['210', '6170', '670', '6155', '650', '7305', '6075'] }, { value: "5", label: "发送信息", checked: false, type: ['210', '6170', '670', '6075'] }, { value: "6", label: "产品信息", checked: false, type: ['210', '6170', '670'] }, { value: "41", label: "静电探测", checked: false, type: ['670', '650'] }, { value: "42", label: "SOS", checked: false, type: ['670', '4877', '6075'] }, { value: "43", label: "联机设备", checked: false, type: ['210'] }, { value: "44", label: "报警声音", checked: false, type: ['210'] }, { value: "45", label: "自动报警", checked: false, type: ['210'] }, { value: "46", label: "手动报警", checked: false, type: ['210', '102', '6170'] }, { value: "47", label: "报警时长", checked: false, type: ['210'] }, { value: "48", label: "物体感应", checked: false, type: ['102'] }, { value: "49", label: "联机模式", checked: false, type: ['102'] }, { value: "50", label: "报警模式", checked: false, type: ['100', '100J'] }, { value: "51", label: "警示灯", checked: false, type: ['100', '100J'] }, { value: "52", label: "语音管理", checked: false, type: ['100', '100J'] }, { value: "53", label: "箭头模式", checked: false, type: ['4877'] }, { value: "54", label: "配组设置", checked: false, type: ['4877'] }, { value: "55", label: "修改信道", checked: false, type: ['4877', '102'] }, { value: "56", label: "灯光类型设置", checked: false, type: ['100J'] }, ] let arr = []; for (let i = 0; i < array.length; i++) { let item = array[i]; if (!item) { continue; } if (!item.type) { continue; } let typeContais = item.type.find(v => { return v.includes(type); }); if (typeContais) { let json = {}; Object.assign(json, item); arr.push(json); } } return arr; }, //10进制转换为16进制字符串 decimalToHexLittleEndian(num, byteCount, revers) { // 处理负数(如果需要支持负数,可先转为补码) if (num < 0) { num = 0xFFFFFFFF + num + 1; } // 转为16进制,去除前缀0x,转为大写 let hex = num.toString(16).toUpperCase(); // 计算需要补充的0的数量,确保每个字节占2位 let padLength = (byteCount || Math.ceil(hex.length / 2) * 2) - hex.length; if (padLength > 0) { hex = '0'.repeat(padLength) + hex; } // 分割为字节数组(每2位一个字节) let bytes = []; for (let i = 0; i < hex.length; i += 2) { bytes.push(hex.substr(i, 2)); } // 是否反转字节顺序(低位在前)并拼接 if (revers) { return bytes.reverse().join(''); } return bytes.join(''); }, //将相对路径的文件移动到_downloads文件夹中 moveFileToDownloads(tempFilePath) { return new Promise((resolve, reject) => { if (!tempFilePath) { console.log("无文件需要移动"); resolve(tempFilePath); return; } //本来就在此目录时直接返回 if (tempFilePath.indexOf("_downloads") === 0) { console.log("文件已存在,无需移动"); resolve(tempFilePath); return; } //不是app直接返回 if (!uni.getSystemInfoSync().uniPlatform.includes('app')) { resolve('仅支持 App 端操作'); return; } // console.log("tempFilePath=", tempFilePath); var srcPath = plus.io.convertLocalFileSystemURL(tempFilePath); // console.log("srcPath=", srcPath); plus.io.resolveLocalFileSystemURL(srcPath, (fileEntry) => { plus.io.requestFileSystem(plus.io.PUBLIC_DOWNLOADS, (fs) => { // console.log("fs=",fs.name); // console.log("fs=",fs.root.fullPath); fileEntry.moveTo(fs.root, fileEntry.name, (entry) => { console.log("entry=", entry); let relativePath = `_downloads/${entry.name}`; resolve(relativePath); }, (ex) => { reject(ex) }); }, (e) => { console.error("请求download目录失败 " + e); reject(e); }); }, (error) => { console.log('文件不存在/路径错误:', error.message); reject(error || new Error('resolveLocalFileSystemURL 失败')); } ); }); }, getOSAndUpload() { let os = uni.getSystemInfoSync().platform; let url = '' if (os === 'ios') { url = 'https://apps.apple.com/cn/app/星汉物联/id6752555460' } else if (os === 'android') { url = 'https://www.pgyer.com/xhwl'; } return { os: os, url: url }; }, //将点阵数据转换成RGB565 convertToRGB565(pixels, type) { if (!type) { type = 'rgb'; } const result = new Uint16Array(pixels.length / 4); let index = 0; for (let i = 0; i < pixels.length; i += 4) { let r = pixels[i]; let g = pixels[i + 1]; let b = pixels[i + 2]; let a = pixels[i + 3]; if (type == 'bgr') { result[index++] = ((b & 0xF8) << 8) | ((g & 0xFC) << 3) | (r >> 3); } else { result[index++] = ((r & 0xF8) << 8) | ((g & 0xFC) << 3) | (b >> 3); } } return result; }, formatTime(value, unit = 'minute') { // 边界处理:空值、0或负数 if (!value || value <= 0) return '0秒'; // 将所有单位统一转换为分钟和总秒数 let minutes; let totalSeconds; switch (unit) { case 'second': // 秒 minutes = value / 60; totalSeconds = value; break; case 'minute': // 分钟(默认) minutes = value; totalSeconds = value * 60; break; case 'hour': // 小时 minutes = value * 60; totalSeconds = value * 3600; break; case 'day': // 天 minutes = value * 24 * 60; totalSeconds = value * 86400; break; default: minutes = value; totalSeconds = value * 60; } // 定义时间单位(分钟) const units = [ { label: '年', minutes: 365 * 24 * 60 }, { label: '月', minutes: 30 * 24 * 60 }, // 按30天简化计算 { label: '天', minutes: 24 * 60 }, { label: '小时', minutes: 60 }, { label: '分', minutes: 1 } ]; let remaining = minutes; const parts = []; for (const unitObj of units) { const val = Math.floor(remaining / unitObj.minutes); if (val > 0) { parts.push(`${val}${unitObj.label}`); remaining %= unitObj.minutes; } } // 处理剩余的秒数(remaining 现在是小于1分钟的分钟数) if (remaining > 0) { const seconds = Math.round(remaining * 60); if (seconds > 0) { parts.push(`${seconds}秒`); } } // 如果没有任何部分,返回总秒数 if (parts.length === 0) { return `${Math.floor(totalSeconds)}秒`; } return parts.join(''); }, regeo(lon, lat) { return new Promise((resolve, reject) => { let url = 'https://restapi.amap.com/v3/geocode/regeo?key=ca3af8a20d628897020893892bda5ae4&location=' + lon + ',' + lat; request({ url: url, method: 'GET', isAuthen: false }).then(res => { if (res) { if (res.infocode == '10000' && res.info == 'OK') { resolve(res); return; } } reject(res); }).catch(ex => { reject(ex); }); }); } }