refactor: ♻️ 我的页面重构

This commit is contained in:
ray
2024-10-15 08:31:03 +08:00
parent 3dec53af38
commit c21b15d793
22 changed files with 6888 additions and 5319 deletions

View File

@@ -1,19 +0,0 @@
const TOKEN_KEY = "app-token";
function getToken(): string {
return uni.getStorageSync(TOKEN_KEY) || "";
}
function setToken(token: string) {
return uni.setStorageSync(TOKEN_KEY, token);
}
function clearToken() {
return uni.removeStorageSync(TOKEN_KEY);
}
function isLogin(): boolean {
return !!getToken();
}
export { getToken, setToken, clearToken, isLogin };

38
src/utils/cache.ts Normal file
View File

@@ -0,0 +1,38 @@
const TOKEN_KEY = "app-token";
const USER_INFO_KEY = "user-info";
// 设置 token
export function setToken(token: string) {
uni.setStorageSync(TOKEN_KEY, token);
}
// 获取 token
export function getToken(): string {
return uni.getStorageSync(TOKEN_KEY) || "";
}
// 清除 token
export function clearToken() {
uni.removeStorageSync(TOKEN_KEY);
}
// 设置用户信息
export function setUserInfo(userInfo: any) {
uni.setStorageSync(USER_INFO_KEY, userInfo);
}
// 获取用户信息
export function getUserInfo(): any {
return uni.getStorageSync(USER_INFO_KEY) || null;
}
// 清除用户信息
export function clearUserInfo() {
uni.removeStorageSync(USER_INFO_KEY);
}
// 清除所有缓存信息
export function clearAll() {
clearToken();
clearUserInfo();
}

View File

@@ -1,4 +1,4 @@
import { getToken, clearToken } from "@/utils/auth";
import { getToken } from "@/utils/cache";
import { ResultCodeEnum } from "@/enums/ResultCodeEnum";
export default function request<T>(options: UniApp.RequestOptions): Promise<T> {
@@ -14,7 +14,7 @@ export default function request<T>(options: UniApp.RequestOptions): Promise<T> {
url: `${baseApi}${options.url}`,
header: {
...options.header,
Authorization: getToken() ? `Bearer ${getToken()}` : "", // 添加 Bearer 前缀
Authorization: getToken() ? `Bearer ${getToken()}` : "",
},
success: (response) => {
console.log("success response", response);
@@ -32,8 +32,6 @@ export default function request<T>(options: UniApp.RequestOptions): Promise<T> {
duration: 2000,
});
clearToken();
// 此处不强制跳转到登录页,可以让调用方决定下一步操作
reject({
message: resData.msg || "令牌无效或过期",
@@ -46,7 +44,6 @@ export default function request<T>(options: UniApp.RequestOptions): Promise<T> {
uni.showToast({
title: resData.msg || "业务处理失败",
icon: "none",
duration: 2000,
});
reject({
message: resData.msg || "业务处理失败",