import modal from "./modal.plugins"; export default { /** * 参数处理 * @param params 参数 */ tansParams(params) { let result = ""; for (const propName of Object.keys(params)) { const value = params[propName]; var part = encodeURIComponent(propName) + "="; if (value !== null && value !== "" && typeof value !== "undefined") { if (typeof value === "object") { for (const key of Object.keys(value)) { if (value[key] !== null && value[key] !== "" && typeof value[key] !== "undefined") { let params = propName + "[" + key + "]"; var subPart = encodeURIComponent(params) + "="; result += subPart + encodeURIComponent(value[key]) + "&"; } } } else { result += part + encodeURIComponent(value) + "&"; } } } return result; }, /** * 数据映射 * @param reKey 需要返回的key * @param isKey 需要比对的key * @param value 对比值 * @param data 数据集 */ mapping(reKey, isKey, value, data) { if (typeof value == "string" && value.indexOf(",") > -1) { //value为字符串的id集合 let arr = value.split(","); let returnValue = ""; arr.forEach((e, index) => { data.forEach((f) => { if (e == f[isKey]) { returnValue = returnValue ? `${returnValue},${f[reKey]}` : f[reKey]; } }) }) return returnValue; } else { if (!data) return; for (let i = 0; i < data.length; i++) { if (value == data[i][isKey]) { return data[i][reKey]; } } } }, /** * @一键拨号 */ makePhoneCall(phone) { uni.makePhoneCall({ phoneNumber: phone, success: function () { console.log('success'); }, fail: function () { } }); }, /** * @复制粘贴板 * @param {传入值} content * @returns */ uniCopy({ content, success, error }) { if (!content) return error('复制的内容不能为空 !') content = typeof content === 'string' ? content : content.toString() // 复制内容,必须字符串,数字需要转换为字符串 /** * 小程序端 和 app端的复制逻辑 */ //#ifdef APP-PLUS || MP-WEIXIN uni.setClipboardData({ data: content, success: function () { success("复制成功~") console.log('success'); }, fail: function () { success("复制失败~") } }); //#endif /** * H5端的复制逻辑 */ // #ifdef H5 if (!document.queryCommandSupported('copy')) { //为了兼容有些浏览器 queryCommandSupported 的判断 // 不支持 error('浏览器不支持') } let textarea = document.createElement("textarea") textarea.value = content textarea.readOnly = "readOnly" document.body.appendChild(textarea) textarea.select() // 选择对象 textarea.setSelectionRange(0, content.length) //核心 let result = document.execCommand("copy") // 执行浏览器复制命令 if (result) { success("复制成功~") } else { error("复制失败,请检查h5中调用该方法的方式,是不是用户点击的方式调用的,如果不是请改为用户点击的方式触发该方法,因为h5中安全性,不能js直接调用!") } textarea.remove() // #endif }, /** * @根据时间分类数据 * @param {数据集} data * @param {需要处理的时间key} timeKey * @returns */ groupedItems(data, timeKey) { if (data <= 0) { return false } const grouped = {}; data.forEach((item) => { const date = item[timeKey].split("T")[0]; grouped[date] = grouped[date] || []; grouped[date].push(item); }); return grouped; }, /** * @公共获取URL中的参数 */ getUrlList() { // 截取url中的list var url = window.location.href; var theRequest = new Object(); if (url.indexOf("?") != -1) { var str = url.split("?")[1]; var strs = str.split("&"); for (var i = 0; i < strs.length; i++) { theRequest[strs[i].split("=")[0]] = strs[i].split("=")[1]; } } return theRequest; }, /** * @数组对象排序 * @return * @param {数据} data * @param {0 从小到大 1 从大到小} sort */ sortEvent(data, sort) { let arr = []; // 将需要排序的 key, 进行排列 let sortKeys = Object.keys(JSON.parse(JSON.stringify(data))).sort((a, b) => { return sort == 0 ? JSON.parse(JSON.stringify(data))[a].sort - JSON.parse(JSON.stringify(data))[b].sort : JSON.parse(JSON.stringify(data))[b].sort - JSON.parse(JSON.stringify(data))[a].sort; }); // 循环排列好的 key, 重新组成一个新的数组 for (var sortIndex in sortKeys) { arr.push(JSON.parse(JSON.stringify(data))[sortKeys[sortIndex]]); } return arr; }, /** * @数组对象去重 * @methods data 需要去重的数据 * @methods objectName 需要去重的对象名称 */ uniq(data, objectName) { if (!objectName) { var newArr = [...new Set(data)] return newArr; } else { let obj = {}; let peon = data.reduce((cur, next) => { obj[next[objectName]] ? "" : obj[next[objectName]] = true && cur.push(next); return cur; }, []) //设置cur默认类型为数组,并且初始值为空的数组 return peon; } }, /** * @判断当前是否有网络 */ isNetwork() { let status = true // 获取网络状态 uni.getNetworkType({ success: function (res) { if (res.networkType === "none") { modal.msg("网络异常,请稍后重试!"); status = false } else { status = true } }, }); return status }, /** * @判断用户拒绝权限是否超过48小时 */ isExpirationTime() { let sotrTime = uni.getStorageSync("expirationTime"); if (sotrTime) { if (sotrTime + 3600 * 24 * 2 <= Date.parse(new Date()) / 1000) { return true } else { return false } } else { return true } }, /** * @判断是否为微信公众号 */ isWechatMp() { var ua = navigator?.userAgent.toLowerCase(); if (ua?.match(/MicroMessenger/i) == 'micromessenger') { return true; } else { // 不在微信内置浏览器中 return false; } }, /** * @是否显示容器 * @判断是否为微信公众号 */ isVisible() { let visible = true; //#ifdef H5 visible = !this.isWechatMp(); //#endif //#ifdef APP-PLUS || MP-WEIXIN visible = true; //#endif return visible; }, /** * 图片点击放大 * @param {*类型} type (1:string:图片地址 2:array(数组对象)) * @param {*数据} data */ imgEnlarge(type, data) { let param = {} if (type == 1) { param = { urls: [data], current: data } } if (type == 2) { let arr = [] for (let i = 0; i < data.length; i++) { arr.push(data[i].url) } param = { urls: arr, current: arr[0] } } if (data && data.length > 0) { uni.previewImage(param) } } };