his-weapp/utils/auth.js
2026-01-15 15:05:29 +08:00

190 lines
4.2 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// utils/auth.js
const { userApi } = require("./api");
/**
* 认证相关工具函数
*/
class AuthUtil {
/**
* 检查token是否存在
*/
static hasToken() {
const token = wx.getStorageSync("token");
return !!token;
}
/**
* 获取存储的token
*/
static getToken() {
const token = wx.getStorageSync("token");
return token;
}
/**
* 设置token
*/
static setToken(token) {
wx.setStorageSync("token", token);
}
/**
* 清除认证信息
*/
static clearAuth() {
// 停止token自动刷新
this.stopTokenRefresh();
wx.removeStorageSync("token");
wx.removeStorageSync("userInfo");
// 清除全局状态
const app = getApp();
if (app && app.globalData) {
app.globalData.userInfo = null;
app.globalData.isLoggedIn = false;
}
}
/**
* 保存用户信息
*/
static setUserInfo(userInfo) {
wx.setStorageSync("userInfo", userInfo);
// 更新全局状态
const app = getApp();
if (app && app.globalData) {
app.globalData.userInfo = userInfo;
app.globalData.isLoggedIn = true;
}
}
/**
* 获取用户信息
*/
static getUserInfo() {
const userInfo = wx.getStorageSync("userInfo");
return userInfo;
}
/**
* 刷新token
*/
static async refreshToken() {
try {
const res = await userApi.refreshToken();
if ((res.code === 1 || res.code === 200) && res.data && res.data.token) {
// 保存新token
this.setToken(res.data.token);
// 如果返回了用户信息,更新用户信息
if (res.data.user) {
this.setUserInfo(res.data.user);
}
return true;
} else {
return false;
}
} catch (err) {
return false;
}
}
/**
* 验证当前用户状态
*/
static async validateUser() {
try {
const res = await userApi.getCurrentUser();
if ((res.code === 1 || res.code === 200) && res.data && res.data.user) {
// 更新用户信息
this.setUserInfo(res.data.user);
return true;
} else {
// 用户验证失败尝试刷新token
const refreshed = await this.refreshToken();
if (refreshed) {
// 刷新成功,重新验证
return await this.validateUser();
} else {
// 刷新失败,清除认证信息
this.clearAuth();
return false;
}
}
} catch (err) {
// 如果是401错误尝试刷新token
if (err.message && err.message.includes("401")) {
const refreshed = await this.refreshToken();
if (refreshed) {
try {
return await this.validateUser();
} catch (e) {
this.clearAuth();
return false;
}
} else {
this.clearAuth();
return false;
}
} else {
this.clearAuth();
return false;
}
}
}
/**
* 启动token自动刷新机制
*/
static startTokenRefresh() {
// 先停止之前的定时器
this.stopTokenRefresh();
// 设置2天后自动刷新tokentoken有效期3天
const refreshInterval = 2 * 24 * 60 * 60 * 1000; // 2天
const refreshTimer = setTimeout(async () => {
if (this.hasToken()) {
const success = await this.refreshToken();
if (success) {
// 继续设置下一次刷新
this.startTokenRefresh();
} else {
// 清除存储的定时器标记
wx.removeStorageSync("tokenRefreshTimer");
}
}
}, refreshInterval);
// 标记定时器已启动不存储实际ID因为小程序环境限制
wx.setStorageSync("tokenRefreshTimer", Date.now());
// 将定时器ID存储在类的静态属性中
this._refreshTimer = refreshTimer;
}
/**
* 停止token自动刷新
*/
static stopTokenRefresh() {
// 清除实际的定时器
if (this._refreshTimer) {
clearTimeout(this._refreshTimer);
this._refreshTimer = null;
}
// 清除存储的标记
wx.removeStorageSync("tokenRefreshTimer");
}
}
module.exports = AuthUtil;