完成用户登录和全局token

This commit is contained in:
2025-08-11 00:40:21 +08:00
parent f7261e1915
commit 1cc9bfb8ab
15 changed files with 167 additions and 30 deletions

View File

@@ -1,19 +1,31 @@
// app.ts
App<IAppOption>({
globalData: {
token: "",
userInfo: null
},
onLaunch() {
// 展示本地存储能力
const logs = wx.getStorageSync('logs') || []
logs.unshift(Date.now())
wx.setStorageSync('logs', logs)
// 登录
wx.login({
success: res => {
console.log(res.code)
// 发送 res.code 到后台换取 openId, sessionKey, unionId
},
})
const token = wx.getStorageSync("token");
if (token) {
this.globalData.token = token;
}
},
setToken(token: string) {
this.globalData.token = token;
wx.setStorageSync("token", token); // 同步到缓存,持久化存储
},
// 提供清除 token 的方法(退出登录时使用)
clearToken() {
this.globalData.token = "";
wx.removeStorageSync("token");
},
setUserInfo(userInfo: WechatMiniprogram.CustomUserInfo){
this.globalData.userInfo = userInfo;
// 可选:持久化存储到缓存(根据需求决定是否需要)
wx.setStorageSync("userInfo", userInfo);
},
clearUserInfo(){
this.globalData.userInfo = null;
wx.removeStorageSync("userInfo")
}
})