refactor: 优化类型定义并重构代码逻辑

This commit is contained in:
Ray.Hao
2026-04-18 16:49:49 +08:00
parent 79ebad0aa1
commit 0288b5a08d
6 changed files with 12 additions and 15 deletions

View File

@@ -180,6 +180,9 @@ export interface UserInfo {
/** 邮箱 */
email?: string;
/** 性别 */
gender?: number;
/** 部门名称 */
deptName?: string;

View File

@@ -102,6 +102,7 @@ import { useRouter } from "uni-mini-router";
import { useUserStore } from "@/store";
import { menuConfig } from "@/config/menu";
import { checkLogin, isLoggedIn } from "@/utils/auth";
import { hasPermission } from "@/utils/permission";
import LogAPI, { type VisitOverview as ApiVisitOverview, type VisitTrend } from "@/api/log";
import NoticeAPI, { type NoticeItem } from "@/api/notice";
@@ -157,12 +158,6 @@ const isLogged = computed(() => isLoggedIn());
const hasAnyPerm = computed(() => userPerms.value.length > 0);
// 检查是否有权限
const hasPermission = (perm: string) => {
if (!perm) return true;
return userPerms.value.includes(perm) || userPerms.value.includes("*:*:*");
};
// 默认菜单(未登录时显示)
const defaultNavList = computed(() => {
const result: { icon: string; title: string; url: string; perm: string }[] = [];

View File

@@ -208,6 +208,7 @@ import { useToast, useMessage } from "wot-design-uni";
import { useUserStore, useThemeStore } from "@/store";
import { useRouter } from "uni-mini-router";
import { useNavbar } from "@/composables/useNavbar";
import type { UserInfo } from "@/api/user";
import { getAccessToken } from "@/utils/auth";
const toast = useToast();
@@ -235,11 +236,11 @@ const appVersion = ref("1.0.0");
const navbar = useNavbar({ hasTabbar: true });
const hasUserProfile = computed(() => {
const info = userInfo.value as any;
return !!(info && (info.id || info.userId || info.username || info.nickname));
const info = userInfo.value;
return !!(info && (info.userId || info.username || info.nickname));
});
const genderValue = computed(() => (userInfo.value as any)?.gender);
const genderValue = computed(() => userInfo.value?.gender);
const normalizedGender = computed(() => {
const v = genderValue.value;
const n = typeof v === "number" ? v : Number(v);
@@ -248,7 +249,7 @@ const normalizedGender = computed(() => {
const deptNameText = computed(() => {
if (!isLogin.value) return "";
return (userInfo.value as any)?.deptName || "";
return userInfo.value?.deptName || "";
});
const genderIconName = computed(() => {
@@ -347,7 +348,7 @@ const openAbout = () => {
};
const clearing = ref(false);
const cacheSize = ref<any>("计算中...");
const cacheSize = ref<string>("计算中...");
const formatBytes = (size: number) => {
if (size < 1024) {

View File

@@ -72,6 +72,4 @@ router.beforeEach(async (to, from, next) => {
next();
});
router.afterEach(() => {});
export default router;

View File

@@ -1,4 +1,4 @@
export {};
export {};
declare module "vue" {
type Hooks = App.AppInstance & Page.PageInstance;

View File

@@ -14,7 +14,7 @@
/**
* localStorage 存储
*/
function set(key: string, value: any): void {
function set<T>(key: string, value: T): void {
uni.setStorageSync(key, JSON.stringify(value));
}