更新密码找回uniapp

This commit is contained in:
hero920103 committed 2026-11-16 17:22:50 +08:00
1 parent 7f9a66da0f
commit 3fdda89833
1304 files changed
+3503 -26132

No files matched your search

-48
View File
@@ -1,10 +1,7 @@
import { defineStore } from 'pinia'
import { ref, computed } from 'vue'
// import { getUserInfo } from '@/utils/auth'
import { getMenus } from '@/api/menu';
// 是否属于「登录态失效」类错误。
// request.js 已给鉴权错误打上 status=401,同时兼容历史文案与 axios 原始错误。
const isAuthError = (err) =>
err?.status === 401 ||
err?.response?.status === 401 ||
@@ -12,21 +9,14 @@ const isAuthError = (err) =>
/token已过期|token无效|无效的token/.test(err?.message || '');
export const useMenuStore = defineStore('menu', () => {
// 菜单数据
const menus = ref([]);
// 加载状态
const loading = ref(false);
// 加载错误
const error = ref(null);
// 正在加载的 Promise(用于避免重复请求)
let loadingPromise = null;
// 菜单缓存 key(基于用户类型、当前企业与角色ID)
// 必须带 tid:菜单是按企业下发的,key 里没有企业会让「切换企业」后
// 直接复用上一家的菜单,导致新企业未开通的模块仍能进入。
const getCacheKey = () => {
try {
const userInfo = JSON.parse(localStorage.getItem('userInfo') || '{}');
@@ -39,14 +29,12 @@ export const useMenuStore = defineStore('menu', () => {
}
};
// 从缓存加载菜单
const loadFromCache = () => {
try {
const cacheKey = getCacheKey();
const cached = localStorage.getItem(cacheKey);
if (cached) {
const menuData = JSON.parse(cached);
// 检查缓存是否过期(5分钟过期)
if (menuData.timestamp && Date.now() - menuData.timestamp < 5 * 60 * 1000) {
return menuData.menus;
}
@@ -57,7 +45,6 @@ export const useMenuStore = defineStore('menu', () => {
return null;
};
// 保存菜单到缓存
const saveToCache = (menuData) => {
try {
const cacheKey = getCacheKey();
@@ -70,19 +57,16 @@ export const useMenuStore = defineStore('menu', () => {
}
};
// 清除菜单缓存
const clearCache = () => {
try {
const cacheKey = getCacheKey();
localStorage.removeItem(cacheKey);
// 也清除其他可能的缓存key(兼容旧代码)
localStorage.removeItem('menu_cache');
} catch (e) {
console.warn('清除菜单缓存失败:', e);
}
};
// 获取用户信息
const getUserInfo = () => {
try {
return JSON.parse(localStorage.getItem('userInfo') || '{}');
@@ -91,14 +75,11 @@ export const useMenuStore = defineStore('menu', () => {
}
};
// 从 API 加载菜单(核心方法,确保只请求一次)
const fetchMenus = async (forceRefresh = false) => {
// 如果已经有正在加载的请求,直接返回该 Promise
if (loadingPromise && !forceRefresh) {
return loadingPromise;
}
// 如果不强制刷新,先尝试从缓存加载
if (!forceRefresh) {
const cachedMenus = loadFromCache();
if (cachedMenus && cachedMenus.length > 0) {
@@ -107,12 +88,10 @@ export const useMenuStore = defineStore('menu', () => {
}
}
// 如果正在加载且不是强制刷新,返回现有的 Promise
if (loading.value && !forceRefresh) {
return loadingPromise;
}
// 创建新的加载 Promise
loadingPromise = (async () => {
loading.value = true;
error.value = null;
@@ -124,39 +103,28 @@ export const useMenuStore = defineStore('menu', () => {
let res;
// 检查用户ID是否存在
if (!userInfo.id) {
throw new Error('用户ID不存在,请重新登录');
}
// 用户登录,使用 getMenus 接口
res = await getMenus(userInfo.id);
// 检查响应格式:HTTP 200 但 body 为空(后端/网关异常)会走到这里,
// 打印原始值便于区分「服务挂了」和「返回了空内容」
if (!res) {
console.error('菜单接口返回空响应:', { res, url: `/backend/menu/${userInfo.id}` });
throw new Error('获取菜单失败:接口返回空响应(后端服务或网关异常)');
}
// 检查后端返回的 code 字段
if (res.code !== 200) {
throw new Error(res.msg || '获取菜单失败');
}
// 如果 code 为 200,检查 data
if (res.code === 200) {
// data 可能是空数组,这也是有效的
if (res.data !== undefined && res.data !== null) {
// 确保 data 是数组
const menuData = Array.isArray(res.data) ? res.data : [];
// 直接使用后端返回的树形结构数据,不需要额外过滤
menus.value = menuData;
// 保存到缓存
saveToCache(menuData);
return menuData;
} else {
// data 为 null 或 undefined,使用空数组
console.warn('菜单数据为空,使用空数组');
menus.value = [];
saveToCache([]);
@@ -164,7 +132,6 @@ export const useMenuStore = defineStore('menu', () => {
}
}
// 如果响应格式不符合预期,尝试直接使用 res.data
if (res.data !== undefined) {
const menuData = Array.isArray(res.data) ? res.data : [];
const filtered = menuData.filter(m => (m.isShow ?? 1) !== 0);
@@ -173,15 +140,10 @@ export const useMenuStore = defineStore('menu', () => {
return filtered;
}
// 如果都不符合,抛出错误
throw new Error(res.message || '获取菜单失败:响应格式错误');
} catch (err) {
error.value = err.message || '获取菜单失败';
// 鉴权失败(令牌过期/无效、会话被吊销):清掉缓存并继续抛出,
// 由 request.js 静默续期或跳认证中心。
// 不能回退到缓存:否则令牌过期时会用旧菜单把登录态问题掩盖掉,
// 用户看到的是一份已经失效的权限菜单。
if (isAuthError(err)) {
clearCache();
menus.value = [];
@@ -196,7 +158,6 @@ export const useMenuStore = defineStore('menu', () => {
stack: err.stack
});
// 如果出错,尝试使用缓存数据
const cachedMenus = loadFromCache();
if (cachedMenus && cachedMenus.length > 0) {
console.warn('使用缓存的菜单数据');
@@ -204,7 +165,6 @@ export const useMenuStore = defineStore('menu', () => {
return cachedMenus;
}
// 如果连缓存都没有,设置空数组,避免页面崩溃
menus.value = [];
throw err;
} finally {
@@ -216,14 +176,10 @@ export const useMenuStore = defineStore('menu', () => {
return loadingPromise;
};
// 刷新菜单(强制从 API 获取)
// 不预先清缓存:接口异常时 fetchMenus 还能回退到上一份菜单,避免整个应用没菜单可用;
// 请求成功后 saveToCache 会直接覆盖旧缓存。
const refreshMenus = async () => {
return await fetchMenus(true);
};
// 重置菜单 store(登出时使用)
const resetMenus = () => {
menus.value = [];
loading.value = false;
@@ -232,20 +188,16 @@ export const useMenuStore = defineStore('menu', () => {
clearCache();
};
// 计算属性:获取菜单列表
const menuList = computed(() => menus.value);
// 计算属性:菜单是否已加载
const isLoaded = computed(() => menus.value.length > 0);
return {
// 状态
menus: menuList,
loading,
error,
isLoaded,
// 方法
fetchMenus,
refreshMenus,
resetMenus,