Files
xk-client-wx/common/utils/auth.js
2026-01-14 20:59:32 +08:00

110 lines
2.4 KiB
JavaScript
Raw 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.
/**
* 未登录体验模式Guest Mode工具函数
* 用于管理未登录用户的访问权限和跳转信息
*/
const GUEST_MODE_KEY = 'guest_mode';
const GUEST_STORE_ID = '11001';
const REDIRECT_INFO_KEY = 'redirect_info';
/**
* 判断是否为未登录体验模式
* @returns {boolean}
*/
export function isGuestMode() {
return uni.getStorageSync(GUEST_MODE_KEY) === true;
}
/**
* 设置未登录体验模式标记
*/
export function setGuestMode() {
uni.setStorageSync(GUEST_MODE_KEY, true);
// 同时设置store_id为11001
// uni.setStorageSync('store_id', GUEST_STORE_ID);
}
/**
* 清除未登录体验模式标记
*/
export function clearGuestMode() {
uni.removeStorageSync(GUEST_MODE_KEY);
}
/**
* 获取Guest模式下的固定诊所ID
* @returns {string}
*/
export function getGuestStoreId() {
return GUEST_STORE_ID;
}
/**
* 保存跳转登录前的页面路径和参数
* @param {string} path - 页面路径
* @param {object} params - 页面参数对象
*/
export function saveRedirectInfo(path, params = {}) {
const redirectInfo = {
path,
params,
timestamp: Date.now()
};
uni.setStorageSync(REDIRECT_INFO_KEY, redirectInfo);
}
/**
* 获取保存的跳转信息
* @returns {object|null} 返回 {path, params} 或 null
*/
export function getRedirectInfo() {
const info = uni.getStorageSync(REDIRECT_INFO_KEY);
if (!info) {
return null;
}
// 检查是否过期24小时
const expireTime = 24 * 60 * 60 * 1000; // 24小时
if (Date.now() - info.timestamp > expireTime) {
clearRedirectInfo();
return null;
}
return {
path: info.path,
params: info.params
};
}
/**
* 清除跳转信息
*/
export function clearRedirectInfo() {
uni.removeStorageSync(REDIRECT_INFO_KEY);
}
/**
* 检查是否已登录
* @returns {boolean}
*/
export function isLoggedIn() {
const token = uni.getStorageSync('token');
const userInfo = uni.getStorageSync('userinfo');
return !!(token && userInfo);
}
/**
* 构建带参数的URL
* @param {string} path - 页面路径
* @param {object} params - 参数对象
* @returns {string}
*/
export function buildUrlWithParams(path, params = {}) {
if (!params || Object.keys(params).length === 0) {
return path;
}
const queryString = Object.keys(params)
.map(key => `${encodeURIComponent(key)}=${encodeURIComponent(params[key])}`)
.join('&');
return `${path}?${queryString}`;
}