import userApi from './api/user'; import commonApi from './api/common'; // import Push from '@tencentcloud/donut-push'; const token = wx.getStorageSync('accessToken'); App({ verifyCodeWaitingTime:60, onLaunch() { // this.registerPush = registerPush; wx.setInnerAudioOption({ obeyMuteSwitch:false }); }, resetUserInfoUpdateTag(){ this.globalData.userInfoGetTime = null; }, forceGetUserInfo(){ this.globalData.userInfoGetTime = null; return this.getUserInfo(); }, async getUserInfo(){ if(this.globalData.userInfoGetTime&& this.globalData.userInfo&& new Date()-this.globalData.userInfoGetTime<1000*60*5){ return this.globalData.userInfo; } const data = await userApi.userInfo(); this.globalData.userInfo = data; this.globalData.userInfoGetTime = new Date(); return data; }, async getAppConfig(){ if(!this.globalData.appConfig){ const data = await commonApi.getConfig(); this.globalData.appConfig = {}; data.map((item)=>{ this.globalData.appConfig[item.key] = item.value; }) } return this.globalData.appConfig; }, forceGetSummary(){ this.globalData.summaryGetTime = null; return this.getSummary(); }, async getSummary(){ if(this.globalData.summaryGetTime&& this.globalData.summary&& new Date()-this.globalData.summaryGetTime<1000*60*5){ return this.globalData.summary; } const data = await userApi.summary(); this.globalData.summary = data; this.globalData.summaryGetTime = new Date(); return data; }, validateForm(rules,page){ const result = []; for(var key in rules){ ((rules[key] instanceof Array)?rules[key]:[rules[key]]).map((item)=>{ let valid = true; let value = page.data[key]; if(typeof value=='string'){ value = value.trim(); } //非空 if(item.required){ if(!value){ valid = false; } }else if(item.length){ //绝对长度 if(value.length!=item.length){ valid = false; } }else if(item.type=='phone'){ if(value.length!=11){ valid = false; } }else if(item.maxLength||item.minLength){ if(value.length>(item.maxLength||Infinity)||value.lengthitem.autoFocus); if(focusInput){ page.setData({ [`${focusInput.key}Focus`]:true }) } const shakeInput = result.find((item)=>item.shake); if(shakeInput){ if(!shakeInput.animation){ shakeInput.animation = wx.createAnimation({ duration: 20, }) } shakeInput.animation.translateX(10).step(); shakeInput.animation.translateX(-10).step(); shakeInput.animation.translateX(10).step(); shakeInput.animation.translateX(-10).step(); shakeInput.animation.translateX(5).step(); shakeInput.animation.translateX(0).step(); // needSetData[`${key}Animation`] = item.animation.export(); page.setData({ [`${shakeInput.key}Animation`]:shakeInput.animation.export() }) } return result; }, logout(){ wx.removeStorageSync('accessToken'); this.globalData.accessToken = ''; this.globalData.userInfo = null; const pages = getCurrentPages(); const currentPages = pages[pages.length-1]; if(currentPages&¤tPages.route.indexOf('pages/login/index')>-1){ return; } wx.navigateTo({ url: '/pages/login/index', }) }, globalData: { userInfo: null, accessToken:token, summary:null } }) const registerPush = ()=>{ const sdkAppID = 0; // 消息推送 SDKAppID const appKey = ''; // 消息推送 客户端密钥 return new Promise((resolve) => { Push.setRegistrationID(userid) .then((res) => { console.info("设置注册用户ID", res); return Push.registerPush(sdkAppID, appKey); }).then((res) => { console.info("注册推送服务", res); return Push.getRegistrationID(); }).then((res) => { console.info("完成注册", res); resolve(true) }) .catch((e) => { console.error("注册失败", e); resolve(true) }); Push.addPushListener(Push.EventName.ON_NOTIFICATION_CLICKED, ); }) }