135 lines
3.6 KiB
JavaScript
135 lines
3.6 KiB
JavaScript
import userApi from './api/user';
|
|
import commonApi from './api/common';
|
|
const token = wx.getStorageSync('accessToken');
|
|
console.log(1)
|
|
App({
|
|
verifyCodeWaitingTime:60,
|
|
onLaunch() {
|
|
wx.setInnerAudioOption({
|
|
obeyMuteSwitch:false
|
|
});
|
|
if(!token){
|
|
wx.reLaunch({
|
|
url: '/pages/login/index',
|
|
})
|
|
}
|
|
},
|
|
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]||'').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.length<item.minLength||0){
|
|
valid = false
|
|
}
|
|
}
|
|
if(valid){
|
|
page.setData({
|
|
[`${key}Message`]:''
|
|
});
|
|
}else{
|
|
if(item.message){
|
|
page.setData({
|
|
[`${key}Message`]:item.message
|
|
});
|
|
}
|
|
result.push({
|
|
[key]:item.message,autoFocus:item.autoFocus,key:key,shake:item.shake
|
|
})
|
|
}
|
|
})
|
|
}
|
|
const focusInput = result.find((item)=>item.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;
|
|
},
|
|
globalData: {
|
|
userInfo: null,
|
|
accessToken:token,
|
|
summary:null
|
|
}
|
|
})
|