171 lines
4.4 KiB
JavaScript
171 lines
4.4 KiB
JavaScript
import userApi from './api/user';
|
|
import commonApi from './api/common';
|
|
let token = wx.getStorageSync('accessToken');
|
|
console.log(12);
|
|
App({
|
|
onLaunch(options) {
|
|
console.log(options);
|
|
wx.getStorage({
|
|
key:'accessToken',
|
|
success:(res)=>{
|
|
this.globalData.accessToken = res.data;
|
|
}
|
|
})
|
|
},
|
|
navToLogin(){
|
|
wx.reLaunch({
|
|
url: '/pages/login/login',
|
|
})
|
|
},
|
|
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.info();
|
|
this.globalData.userInfo = data;
|
|
this.globalData.userInfoGetTime = new Date();
|
|
return data;
|
|
},
|
|
getLocation(){
|
|
return new Promise((rs,rj)=>{
|
|
if(this.globalData.locationGetTime&&
|
|
this.globalData.location&&
|
|
new Date()-this.globalData.locationGetTime<1000*60*1){
|
|
rs(this.globalData.location);
|
|
}
|
|
wx.authorize({
|
|
scope: 'scope.userLocation',
|
|
success:(res)=>{
|
|
wx.getLocation({
|
|
success:(_res)=>{
|
|
this.globalData.location = _res;
|
|
this.globalData.locationGetTime = new Date();
|
|
rs(_res)
|
|
},
|
|
fail(res){
|
|
rj();
|
|
}
|
|
});
|
|
},
|
|
fail:()=>{
|
|
rj();
|
|
}
|
|
})
|
|
})
|
|
},
|
|
globalData: {
|
|
userInfo: null,
|
|
accessToken:token,
|
|
community:{
|
|
_list:[],
|
|
lastFetchTime:null,
|
|
getList(){
|
|
let lft = this.lastFetchTime;
|
|
return new Promise((rs,rj)=>{
|
|
if(!lft&&new Date()-lft>30){
|
|
commonApi.community.list().then((data)=>{
|
|
this.lastFetchTime = new Date();
|
|
this._list = data;
|
|
rs(data)
|
|
})
|
|
}else{
|
|
rs(this._list);
|
|
}
|
|
})
|
|
},
|
|
},
|
|
address:{
|
|
_list:[],
|
|
lastFetchTime:null,
|
|
getList(){
|
|
let lft = this.lastFetchTime;
|
|
return new Promise((rs,rj)=>{
|
|
if(!lft&&new Date()-lft>30){
|
|
commonApi.address.list().then((data)=>{
|
|
this.lastFetchTime = new Date();
|
|
this._list = data;
|
|
rs(data)
|
|
})
|
|
}else{
|
|
rs(this._list);
|
|
}
|
|
})
|
|
},
|
|
}
|
|
},
|
|
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{
|
|
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;
|
|
},
|
|
getAddressList(){
|
|
|
|
}
|
|
})
|