47 lines
1.1 KiB
TypeScript
47 lines
1.1 KiB
TypeScript
import { info } from "./service/user-api";
|
|
|
|
// app.ts
|
|
App<IAppOption>({
|
|
globalData: {},
|
|
onLaunch() {
|
|
this.updateUserInfo();
|
|
},
|
|
initLoginInfo(resolve, reject) {
|
|
wx.login({
|
|
fail: reject,
|
|
success: (e) => {
|
|
if (e.code) {
|
|
info(e.code).then(userinfo => {
|
|
if (userinfo.token) {
|
|
wx.setStorageSync("user-token", userinfo.token)
|
|
}
|
|
this.globalData.userInfo = userinfo;
|
|
resolve(userinfo);
|
|
}).catch(reject)
|
|
}
|
|
}
|
|
})
|
|
},
|
|
updateUserInfo() {
|
|
return new Promise<UserInfo>((resolve, reject) => {
|
|
// 展示本地存储能力
|
|
const token = wx.getStorageSync('user-token')
|
|
if (token) {
|
|
this.globalData.token = token;
|
|
info('', false).then(userinfo => {
|
|
console.log(userinfo)
|
|
this.globalData.userInfo = userinfo;
|
|
resolve(userinfo);
|
|
}).catch((e) => {
|
|
if (e.code == 403) {
|
|
this.initLoginInfo(resolve, reject)
|
|
return;
|
|
}
|
|
reject(e)
|
|
})
|
|
} else {
|
|
this.initLoginInfo(resolve, reject)
|
|
}
|
|
})
|
|
}
|
|
}) |