dm-wechat-mini/app.js
2025-02-12 02:40:45 +08:00

79 lines
1.8 KiB
JavaScript

import userApi from './api/user';
import commonApi from './api/common';
App({
onLaunch() {
wx.getStorage({
key:'accessToken',
success:(res)=>{
this.globalData.accessToken = res.data;
if(res.data){
this.getUserInfo();
}
}
})
},
navToLogin(){
wx.navigateTo({
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;
},
globalData: {
userInfo: null,
accessToken:null,
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);
}
})
},
}
},
getAddressList(){
}
})