权限调整

This commit is contained in:
hb 2025-08-02 10:47:16 +08:00
parent d114fe8c8f
commit 102882e3fd
6 changed files with 393 additions and 25 deletions

17
src/api/base/notice.ts Normal file
View File

@ -0,0 +1,17 @@
import { get } from "@/utils/request";
// 通知公告分页查询参数接口
export interface NoticePageParams {
page: number;
rows: number;
appCode: string;
fbfw?: string;
xqId?: string;
fbNjmcId?: string;
releaseFlag?: string;
}
// 获取通知公告列表
export function getNoticeListApi(params: NoticePageParams) {
return get("/api/cms/article/list", params);
}

View File

@ -27,6 +27,7 @@
<view
v-for="(item, index) in menuItems"
:key="index"
v-show="hasPermissionDirect(item.permissionKey)"
class="grid-item"
@click="handleMenuClick(item)"
>
@ -79,8 +80,11 @@ import { ref, computed, onMounted, watch } from "vue";
import { onShow } from "@dcloudio/uni-app";
import XsPicker from "@/pages/base/components/XsPicker/index.vue"
import { cmsArticlePageApi, getUserLatestInfoApi } from "@/api/base/server";
import { getNoticeListApi } from "@/api/base/notice";
import { useUserStore } from "@/store/modules/user";
import { useDataStore } from "@/store/modules/data";
import { hasPermission } from "@/utils/permission";
const { getCurXs } = useUserStore();
const { setData, getAppCode } = useDataStore();
@ -88,6 +92,37 @@ const { setData, getAppCode } = useDataStore();
const { getLastRefreshTime, getRefreshInterval, setLastRefreshTime, updateStudentInfo, updateStudentList } = useUserStore();
const REFRESH_INTERVAL = 10 * 60 * 1000; // 10
// changeTime
const getCurrentChangeTime = () => {
try {
const userDataStr = uni.getStorageSync('app-user');
if (!userDataStr) return null;
const userData = typeof userDataStr === 'string' ? JSON.parse(userDataStr) : userDataStr;
return userData?.changeTime || null;
} catch (error) {
console.error('获取changeTime失败:', error);
return null;
}
};
// changeTime
const checkPermission = (permissionKey: string) => {
const changeTime = getCurrentChangeTime();
return hasPermission(permissionKey, changeTime);
};
//
const hasPermissionDirect = (permissionKey: string) => {
if (!permissionKey) return true;
const userStore = useUserStore();
const permissions = userStore.getAuth;
if (!permissions || permissions.length === 0) return false;
const uniquePermissions = [...new Set(permissions)];
return uniquePermissions.includes(permissionKey);
};
//
const checkAndRefreshStudentInfo = async () => {
const lastRefreshTime = getLastRefreshTime;
@ -135,37 +170,44 @@ const menuItems = ref([
title: "班级课表",
icon: "/static/base/home/book-read-line.png",
path: "/pages/base/class-schedule/index",
permissionKey: "school-bjkb", //
},
{
title: "成绩查询",
icon: "/static/base/home/file-search-line.png",
path: "/pages/base/grades/list",
permissionKey: "school-cjcx", //
},
{
title: "在线请假",
icon: "/static/base/home/draft-line.png",
path: "/pages/base/leave-request/index",
permissionKey: "school-zxqj", // 线
},
// TODO:
// {
// title: "",
// icon: "/static/base/home/file-transfer-line.png",
// path: "/pages/base/campus-access/index",
// permissionKey: "school-jcxy", //
// },
{
title: "家校沟通",
icon: "/static/base/home/file-transfer-line.png",
path: "/pages/base/jl/index",
permissionKey: "school-jxgt", //
},
{
title: "兴趣课",
icon: "/static/base/home/file-text-line.png",
path: "/pages/base/interest-class/index",
permissionKey: "school-xqk", //
},
{
title: "俱乐部",
icon: "/static/base/home/contacts-book-3-line.png",
path: "/pages/base/club/index",
permissionKey: "school-jlb", //
},
]);
@ -229,13 +271,21 @@ function goToDetail(notice: any) {
const getArticleList = async () => {
if (curXs.value && curXs.value.njmcId) {
const params = Object.assign({}, pageParams.value, { njmcId: curXs.value.njmcId });
const params = {
page: pageParams.value.page,
rows: pageParams.value.rows,
appCode: getAppCode,
fbfw: 'JZ', //
xqId: curXs.value.xqId || curXs.value.xq_id, //
fbNjmcId: curXs.value.njmcId, //
releaseFlag: 'A' //
};
cmsArticlePageApi(params).then(res => {
announcements.value = res.rows;
})
.catch((error) => {
//
getNoticeListApi(params).then(res => {
announcements.value = res.rows;
})
.catch((error) => {
//
});
}
};
@ -255,6 +305,29 @@ onMounted(async () => {
//
await checkAndRefreshStudentInfo();
//
const userStore = useUserStore();
const changeTime = userStore.getChangeTime;
if (changeTime) {
// changeTime
const { PermissionCacheManager } = await import('@/utils/permission');
const cacheInfo = PermissionCacheManager.getCacheInfo();
if (cacheInfo.hasCache && cacheInfo.changeTime) {
const serverTime = new Date(changeTime).getTime();
const cacheTime = new Date(cacheInfo.changeTime).getTime();
if (serverTime > cacheTime) {
//
const { refreshPermissionCache } = await import('@/utils/permission');
const currentPermissions = userStore.getAuth;
if (currentPermissions && currentPermissions.length > 0) {
refreshPermissionCache(currentPermissions, changeTime);
}
}
}
}
});
//

View File

@ -16,6 +16,7 @@ import {onLoad} from "@dcloudio/uni-app";
import {useDataStore} from "@/store/modules/data";
import {useUserStore} from "@/store/modules/user";
import {checkOpenId} from "@/api/system/login";
import {refreshPermissionCache} from "@/utils/permission";
const { setGlobal } = useDataStore();
const { afterLoginAction } = useUserStore();
@ -36,24 +37,34 @@ function toHome(data: any) {
onLoad(async (data: any) => {
setGlobal(data);
if (data && data.openId) {
checkOpenId({ openId: data.openId, appCode: "JZ" })
.then(async (res) => {
if (res.resultCode == 1) {
if (res.result) {
afterLoginAction(res.result);
toHome(data);
return;
try {
const res = await checkOpenId({ openId: data.openId, appCode: "JZ" });
if (res.resultCode == 1 && res.result) {
//
afterLoginAction(res.result);
// changeTime
if (data.changeTime) {
const userStore = useUserStore();
const currentPermissions = userStore.getAuth;
if (currentPermissions && currentPermissions.length > 0) {
refreshPermissionCache(currentPermissions, data.changeTime);
}
}
uni.reLaunch({
url: "/pages/system/login/login",
});
})
.catch((err) => {
uni.reLaunch({
url: "/pages/system/login/login",
});
});
toHome(data);
} else {
uni.reLaunch({
url: "/pages/system/login/login",
});
}
} catch (err) {
uni.reLaunch({
url: "/pages/system/login/login",
});
}
} else {
uni.reLaunch({
url: "/pages/system/login/login",

View File

@ -124,6 +124,7 @@ import { loginRegisterJzApi } from "@/api/base/server";
import { useUserStore } from "@/store/modules/user";
import { useDataStore } from "@/store/modules/data";
import {imagUrl} from "@/utils";
import {refreshPermissionCache} from "@/utils/permission";
const dicOptions = ref<any>([[[]]]);
const dicPickerRef = ref();
@ -305,6 +306,17 @@ async function submit() {
hideLoading();
if (res.resultCode == 1) {
afterLoginAction(res.result);
// changeTime
if (res.result && res.result.changeTime) {
const userStore = useUserStore();
const currentPermissions = userStore.getAuth;
if (currentPermissions && currentPermissions.length > 0) {
refreshPermissionCache(currentPermissions, res.result.changeTime);
}
}
toHome();
} else {
showToast({ title: res.message || "提交失败", icon: "none" });

View File

@ -15,6 +15,7 @@ interface UserState {
auth: string[];
lastRefreshTime: number; // 上次刷新时间
refreshInterval: number; // 刷新间隔(毫秒)
changeTime: string; // 权限变更时间
ws: any;
wsCallback: any;
}
@ -32,6 +33,7 @@ export const useUserStore = defineStore({
auth: [],
lastRefreshTime: 0, // 上次刷新时间
refreshInterval: 7 * 24 * 60 * 60 * 1000, // 刷新间隔(毫秒)
changeTime: '', // 权限变更时间
ws: null,
wsCallback: defWsCallback
}),
@ -53,6 +55,9 @@ export const useUserStore = defineStore({
},
getRefreshInterval(): number {
return this.refreshInterval;
},
getChangeTime(): string {
return this.changeTime;
}
},
actions: {
@ -74,6 +79,9 @@ export const useUserStore = defineStore({
setRefreshInterval(interval: number) {
this.refreshInterval = interval;
},
setChangeTime(changeTime: string) {
this.changeTime = changeTime;
},
// 更新学生信息
updateStudentInfo(studentInfo: any) {
this.setCurXs(studentInfo);

View File

@ -2,11 +2,258 @@ import {ISROUTERINTERCEPT} from "@/config";
import {getRouter} from "@/utils/uniapp";
import {useUserStore} from "@/store/modules/user";
export function _auth(autd: string) {
const {getAuth} = useUserStore()
return getAuth.includes(autd);
// 权限缓存接口
interface PermissionCache {
permissions: string[];
timestamp: number;
userId: string;
changeTime: string;
}
// 存储键名
const PERMISSION_CACHE_KEY = 'permission_cache';
const CHANGE_TIME_KEY = 'change_time';
// 存储操作
function setStorage(key: string, value: any): void {
try {
const jsonValue = JSON.stringify(value);
uni.setStorageSync(key, jsonValue);
} catch (error) {
console.error('存储权限缓存失败:', error);
}
}
function getStorage(key: string): any {
try {
const value = uni.getStorageSync(key);
if (value) {
const parsedValue = JSON.parse(value);
return parsedValue;
} else {
return null;
}
} catch (error) {
console.error('获取权限缓存失败:', error);
return null;
}
}
// 从app-user获取changeTime
export function getChangeTimeFromAppUser(): string | null {
try {
const userDataStr = uni.getStorageSync('app-user');
if (!userDataStr) return null;
const userData = typeof userDataStr === 'string' ? JSON.parse(userDataStr) : userDataStr;
return userData?.changeTime || null;
} catch (error) {
console.error('获取changeTime失败:', error);
return null;
}
}
// 设置changeTime到app-user
function setChangeTimeToAppUser(changeTime: string): void {
try {
const userDataStr = uni.getStorageSync('app-user');
if (!userDataStr) return;
const userData = typeof userDataStr === 'string' ? JSON.parse(userDataStr) : userDataStr;
userData.changeTime = changeTime;
uni.setStorageSync('app-user', JSON.stringify(userData));
} catch (error) {
console.error('设置changeTime失败:', error);
}
}
// 获取权限缓存
function getPermissionCache(): PermissionCache | null {
return getStorage(PERMISSION_CACHE_KEY);
}
// 设置权限缓存
function setPermissionCache(permissions: string[], userId: string, changeTime?: string): void {
const cache: PermissionCache = {
permissions,
timestamp: Date.now(),
userId,
changeTime: changeTime || ''
};
setStorage(PERMISSION_CACHE_KEY, cache);
if (changeTime) {
setChangeTimeToAppUser(changeTime);
}
}
// 清除权限缓存
function clearPermissionCache(): void {
try {
uni.removeStorageSync(PERMISSION_CACHE_KEY);
} catch (error) {
console.error('清除权限缓存失败:', error);
}
}
// 检查缓存是否有效
function isCacheValid(cache: PermissionCache, currentUserId: string): boolean {
if (!cache || !cache.permissions || cache.userId !== currentUserId) {
return false;
}
// 检查缓存是否过期7天
const now = Date.now();
const cacheAge = now - cache.timestamp;
const maxAge = 7 * 24 * 60 * 60 * 1000; // 7天
return cacheAge < maxAge;
}
// 获取用户权限(带缓存)
function getUserPermissionsWithCache(currentChangeTime?: string): string[] {
const userStore = useUserStore();
const currentUser = userStore.getUser;
const currentUserId = currentUser?.userId || currentUser?.id;
if (!currentUserId) {
return userStore.getAuth;
}
const cache = getPermissionCache();
if (cache && isCacheValid(cache, currentUserId)) {
if (currentChangeTime) {
const serverTime = new Date(currentChangeTime).getTime();
const cacheTime = new Date(cache.changeTime).getTime();
if (serverTime > cacheTime) {
const permissions = userStore.getAuth;
if (permissions && permissions.length > 0) {
setPermissionCache(permissions, currentUserId, currentChangeTime);
}
return permissions;
} else {
return cache.permissions;
}
} else {
return cache.permissions;
}
}
const permissions = userStore.getAuth;
if (permissions && permissions.length > 0) {
setPermissionCache(permissions, currentUserId, currentChangeTime);
}
return permissions;
}
// 刷新权限缓存
export function refreshPermissionCache(permissions?: string[], changeTime?: string): void {
const userStore = useUserStore();
const currentUser = userStore.getUser;
const currentUserId = currentUser?.userId || currentUser?.id;
if (!currentUserId) {
return;
}
const permissionList = permissions || userStore.getAuth;
const currentCache = getPermissionCache();
if (currentCache && currentCache.permissions && permissionList) {
const isSame = JSON.stringify(currentCache.permissions.sort()) === JSON.stringify(permissionList.sort());
if (isSame && currentCache.changeTime === changeTime) {
return;
}
}
setPermissionCache(permissionList, currentUserId, changeTime);
if (changeTime) {
userStore.setChangeTime(changeTime);
}
}
// 清除权限缓存(供外部调用)
export function clearPermissionCachePublic(): void {
clearPermissionCache();
}
// 权限检查函数
export function _auth(autd: string, changeTime?: string) {
const permissions = getUserPermissionsWithCache(changeTime);
return permissions.includes(autd);
}
export function hasPermission(permissionKey: string, changeTime?: string): boolean {
if (!permissionKey) return true;
const permissions = getUserPermissionsWithCache(changeTime);
// 去重处理,避免重复权限影响判断
const uniquePermissions = [...new Set(permissions)];
return uniquePermissions.includes(permissionKey);
}
export function hasAnyPermission(permissionKeys: string[], changeTime?: string): boolean {
if (!permissionKeys || permissionKeys.length === 0) return true;
const permissions = getUserPermissionsWithCache(changeTime);
// 去重处理,避免重复权限影响判断
const uniquePermissions = [...new Set(permissions)];
return permissionKeys.some(key => uniquePermissions.includes(key));
}
export function hasAllPermissions(permissionKeys: string[], changeTime?: string): boolean {
if (!permissionKeys || permissionKeys.length === 0) return true;
const permissions = getUserPermissionsWithCache(changeTime);
// 去重处理,避免重复权限影响判断
const uniquePermissions = [...new Set(permissions)];
return permissionKeys.every(key => uniquePermissions.includes(key));
}
// 获取用户权限
export function getUserPermissions(changeTime?: string): string[] {
return getUserPermissionsWithCache(changeTime);
}
// 权限缓存管理器
export const PermissionCacheManager = {
getCacheInfo() {
const cache = getPermissionCache();
const changeTime = getChangeTimeFromAppUser();
return {
cache,
changeTime,
hasCache: !!cache,
cacheAge: cache ? Date.now() - cache.timestamp : 0
};
},
debugCache() {
const info = this.getCacheInfo();
console.log('权限缓存信息:', info);
},
forceRefresh() {
clearPermissionCache();
const userStore = useUserStore();
const permissions = userStore.getAuth;
if (permissions && permissions.length > 0) {
const currentUser = userStore.getUser;
const currentUserId = currentUser?.userId || currentUser?.id;
if (currentUserId) {
setPermissionCache(permissions, currentUserId);
}
}
},
clear() {
clearPermissionCache();
}
};
function loginPage(url: string) {
uni.redirectTo({
url: "/pages/system/login/login?redirect=" + url,