import { config } from "../config/config"; import request from "../config/rerquest"; // 封装promise对象,避免报错,返回[err, data] export function to(promise: Promise) { return promise .then((data) => { return [null, data]; }) .catch((err) => [err]); } // 输入同步事件 export function inputSync(event) { // console.log(event) let { detail: { value }, currentTarget: { dataset: { name, fm, subindex, subindexname, maxlength, value: dataValue, }, }, } = event; if (!name) { name = event.detail.name; fm = event.detail.fm; maxlength = event.detail.maxlength; } if (value == undefined && dataValue != undefined) { value = dataValue; } if (name) { if (maxlength) { value = value.substr(0, parseInt(maxlength)); } const fmKey = fm ? fm : "fmData"; let data = {}; data[fmKey] = this.data[fmKey]; if (subindex !== undefined && subindexname) { data[fmKey][name][subindex][subindexname] = value; } else { data[fmKey][name] = value; } if (typeof this.$beforeUpdateData === "function") { if (this.$beforeUpdateData(fmKey, name, value)) { this.setData(data); } else { return; } } else { this.setData(data); } if (typeof this.$afterUpdateData === "function") { this.$afterUpdateData(fmKey, name, value); } } } export async function appLogin() { const app = getApp(); const [err, data] = await to(app.login()); if (err) { console.error("登陆 app.login()函数报错", err); return false; } else { console.log("初始化登陆返回码", data); if (data === 1) { return true; } else { return false; } } } // 登录后才会继续执行 export async function doAfterLogin() { let app = getApp(); return new Promise(async (reslove, reject) => { let i = 1; while ( app.globalData.token == "" || app.globalData.gridId == "" || app.globalData.currentGridName == "" ) { // 等了一会还不行就重新登录 i += 1; if (i == 10 || i == 100 || i == 250 || i == 600 || i % 3000 == 0) { if (await appLogin()) { return reslove(); } } await nextTick(100); } return reslove(); }); } // 获取组件 export const getComponent = (selector: string) => { const ctx = getCurrentPages()[getCurrentPages().length - 1]; const componentCtx = ctx.selectComponent(selector); if (!componentCtx) { throw new Error("无法找到对应的组件,请按文档说明使用组件"); } return componentCtx; }; /** * 函数柯里化 * * @example * * var abc = function(a, b, c) { * return [a, b, c]; * }; * * var curried = curry(abc); * * curried(1)(2)(3); * // => [1, 2, 3] * */ export function curryN(fn, n = null) { const needLen = typeof n === "number" ? n : fn.length; function warp(...arg) { let argLen = arg.length; if (needLen - argLen <= n) { return fn(...arg); } else { return (...newArg) => warp(...arg, ...newArg); } } return warp; } export function curry(fn) { return curryN(fn); } /** * 下一次循环 * * 注:返回Promise对象,结合await修饰符使用更佳 * * Number -> Promise */ export const nextTick = (cd = 0) => new Promise((reslove) => setTimeout(reslove, cd)); // 检测最新版本 export function checkVersionUpdate() { const updateManager = wx.getUpdateManager(); updateManager.onCheckForUpdate(function (res) { // 请求完新版本信息的回调 console.log(res.hasUpdate); }); updateManager.onUpdateReady(function () { wx.showModal({ title: "更新提示", content: "新版本已经准备好,是否重启应用?", success(res) { if (res.confirm) { // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启 updateManager.applyUpdate(); } }, }); }); updateManager.onUpdateFailed(function () { // 新版本下载失败 }); } // 获取元素的位置大小等个信息 export function getNodeRect(id, that = wx) { return new Promise((resolve) => { that .createSelectorQuery() .select("#" + id) .boundingClientRect(function (rect) { // console.log(rect); // rect.id // 节点的ID // rect.dataset // 节点的dataset // rect.left // 节点的左边界坐标 // rect.right // 节点的右边界坐标 // rect.top // 节点的上边界坐标 // rect.bottom // 节点的下边界坐标 // rect.width // 节点的宽度 // rect.height // 节点的高度 resolve(rect); }) .exec(); }); } //重新进入网格,刷新用户角色 export async function loginGrid() { const app = getApp(); let gid = app.globalData.gridId; let cid = app.globalData.customerId; return new Promise(async (reslove, reject) => { return request({ method: "POST", url: "resi/guide/user/entergrid", options: { customerId: cid, gridId: gid, }, ifToken: true, }).then(function (res: { data: any }) { // 更新网格缓存数据 console.log("重新进网格,更新数据"); wx.setStorageSync("userRoleList", res.data.userRoleList); wx.setStorageSync("gridName", res.data.currentGridName); app.globalData.userRoleList = res.data.userRoleList; app.globalData.currentGridName = res.data.currentGridName; app.globalData.userInfo.nickname = res.data.nickname; app.globalData.userInfo.userHeadPhoto = res.data.userHeadPhoto; app.globalData.gridId = gid; return reslove(); }); }); } //处理后端传来的富文本 export function cookRichContent(content) { if ( content.startsWith("\n\n\n\n\n") && content.endsWith("\n\n") ) { content = content.slice(45, -16); } content = content .replace(/ /g, "\xa0") .replace(/