Files
front/src/store/modules/app/index.ts

79 lines
2.4 KiB
TypeScript
Raw Normal View History

2026-03-05 23:45:39 +08:00
import { defineStore } from 'pinia'
import { Notification } from '@arco-design/web-vue'
import type { NotificationReturn } from '@arco-design/web-vue/es/notification/interface'
import type { RouteRecordNormalized } from 'vue-router'
import defaultSettings from '@/config/settings.json'
2026-03-07 20:11:25 +08:00
import { userPmn } from '@/api/module/user'
import { localMenuData } from '@/router/menu-data'
import { buildTree } from '@/utils/tree'
import SafeStorage, { AppStorageKey } from "@/utils/safeStorage";
2026-03-05 23:45:39 +08:00
import { AppState } from './types'
2026-03-07 20:11:25 +08:00
2026-03-05 23:45:39 +08:00
const useAppStore = defineStore('app', {
state: (): AppState => ({ ...defaultSettings }),
getters: {
appCurrentSetting(state: AppState): AppState {
return { ...state }
},
appDevice(state: AppState) {
return state.device
},
appAsyncMenus(state: AppState): RouteRecordNormalized[] {
return state.serverMenu as unknown as RouteRecordNormalized[]
},
},
actions: {
// Update app settings
updateSettings(partial: Partial<AppState>) {
// @ts-ignore-next-line
this.$patch(partial)
},
// Change theme color
toggleTheme(dark: boolean) {
if (dark) {
this.theme = 'dark'
document.body.setAttribute('arco-theme', 'dark')
} else {
this.theme = 'light'
document.body.removeAttribute('arco-theme')
}
},
toggleDevice(device: string) {
this.device = device
},
toggleMenu(value: boolean) {
this.hideMenu = value
},
async fetchServerMenuConfig() {
2026-03-07 20:11:25 +08:00
const userInfo = SafeStorage.get(AppStorageKey.USER_INFO) as any
2026-03-05 23:45:39 +08:00
let notifyInstance: NotificationReturn | null = null
try {
2026-03-07 20:11:25 +08:00
// 使用本地菜单数据(接口未准备好)
// TODO: 接口准备好后,取消下面的注释,使用真实接口数据
const res = await userPmn({ id: userInfo.user_id, workspace: import.meta.env.VITE_APP_WORKSPACE })
console.log('res', res)
if (res.code === 0 && res?.details?.length) {
console.log('buildTree', buildTree(res.details[0].permissions))
}
// this.serverMenu = data
// 使用本地数据
this.serverMenu = localMenuData as unknown as RouteRecordNormalized[]
2026-03-05 23:45:39 +08:00
} catch (error) {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
2026-03-07 20:11:25 +08:00
2026-03-05 23:45:39 +08:00
}
},
clearServerMenu() {
this.serverMenu = []
},
},
})
export default useAppStore