124 lines
2.9 KiB
TypeScript
124 lines
2.9 KiB
TypeScript
import { useUserStore } from "@/store/modules/user";
|
||
import { Storage } from "./storage";
|
||
import { ACCESS_TOKEN_KEY, REFRESH_TOKEN_KEY } from "@/constants";
|
||
|
||
/**
|
||
* 认证工具函数
|
||
*
|
||
* 使用示例:
|
||
*
|
||
* 1. 检查登录状态并自动跳转:
|
||
* if (!checkLogin()) return; // 未登录会自动跳转到登录页
|
||
*
|
||
* 2. 静默检查登录状态:
|
||
* if (!isLoggedIn()) {
|
||
* // 处理未登录逻辑,不会自动跳转
|
||
* }
|
||
*
|
||
* 3. 强制要求登录:
|
||
* requireLogin(); // 清除无效状态并跳转到登录页
|
||
*/
|
||
|
||
/**
|
||
* 获取访问令牌
|
||
* @returns 返回访问令牌,如果不存在则返回null
|
||
*/
|
||
export function getAccessToken(): string | null {
|
||
return Storage.get<string>(ACCESS_TOKEN_KEY) || null;
|
||
}
|
||
|
||
/**
|
||
* 设置访问令牌
|
||
* @param token 访问令牌
|
||
*/
|
||
export function setAccessToken(token: string): void {
|
||
Storage.set(ACCESS_TOKEN_KEY, token);
|
||
}
|
||
|
||
/**
|
||
* 获取刷新令牌
|
||
* @returns 返回刷新令牌,如果不存在则返回null
|
||
*/
|
||
export function getRefreshToken(): string | null {
|
||
return Storage.get<string>(REFRESH_TOKEN_KEY) || null;
|
||
}
|
||
|
||
/**
|
||
* 设置刷新令牌
|
||
* @param token 刷新令牌
|
||
*/
|
||
export function setRefreshToken(token: string): void {
|
||
Storage.set(REFRESH_TOKEN_KEY, token);
|
||
}
|
||
|
||
/**
|
||
* 清除所有令牌
|
||
*/
|
||
export function clearTokens(): void {
|
||
Storage.remove(ACCESS_TOKEN_KEY);
|
||
Storage.remove(REFRESH_TOKEN_KEY);
|
||
}
|
||
|
||
function getCurrentPagePath(): string {
|
||
const pages = getCurrentPages();
|
||
if (pages.length === 0) return "/pages/index/index";
|
||
|
||
const currentPage = pages[pages.length - 1];
|
||
const route = currentPage.route || "";
|
||
const options = (currentPage as any).options || {};
|
||
|
||
const query = Object.entries(options)
|
||
.map(([key, value]) => `${key}=${value}`)
|
||
.join("&");
|
||
|
||
return query ? `/${route}?${query}` : `/${route}`;
|
||
}
|
||
|
||
/**
|
||
* 检查用户登录状态,未登录则跳转到登录页面
|
||
* @param silent 是否静默检查,不跳转登录页面
|
||
* @returns 返回用户是否已登录
|
||
*/
|
||
export function checkLogin(silent: boolean = false): boolean {
|
||
if (getAccessToken()) return true;
|
||
|
||
if (!silent) {
|
||
const redirect = encodeURIComponent(getCurrentPagePath());
|
||
uni.navigateTo({
|
||
url: `/pages/login/index?redirect=${redirect}`,
|
||
fail: () => {
|
||
uni.reLaunch({ url: "/pages/login/index" });
|
||
},
|
||
});
|
||
}
|
||
|
||
return false;
|
||
}
|
||
|
||
/**
|
||
* 检查用户是否已登录(静默检查,不跳转)
|
||
* @returns 返回用户是否已登录
|
||
*/
|
||
export function isLoggedIn(): boolean {
|
||
return !!getAccessToken();
|
||
}
|
||
|
||
/**
|
||
* 强制用户登录,清除无效的登录状态
|
||
*/
|
||
export function requireLogin(): void {
|
||
const userStore = useUserStore();
|
||
const accessToken = getAccessToken();
|
||
|
||
if (!accessToken || !userStore.userInfo) {
|
||
// 清除可能存在的无效状态
|
||
clearTokens();
|
||
userStore.logout();
|
||
|
||
// 跳转到登录页
|
||
uni.reLaunch({
|
||
url: "/pages/login/index",
|
||
});
|
||
}
|
||
}
|