dm-wechat-mini/app.js
2025-02-13 19:11:45 +08:00

107 lines
2.5 KiB
JavaScript

import userApi from './api/user';
import commonApi from './api/common';
let token = wx.getStorageSync('accessToken');
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;
},
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);
}
})
},
}
},
getAddressList(){
}
})