初始化
This commit is contained in:
147
frontend/src/stores/modules/auth.ts
Normal file
147
frontend/src/stores/modules/auth.ts
Normal file
@@ -0,0 +1,147 @@
|
||||
import { defineStore } from 'pinia'
|
||||
import { type AuthState } from '@/stores/interface'
|
||||
import { getAuthButtonListApi, getAuthMenuListApi } from '@/api/user/login'
|
||||
import { getAllBreadcrumbList, getFlatMenuList, getShowMenuList } from '@/utils'
|
||||
import { AUTH_STORE_KEY } from '@/stores/constant'
|
||||
import { getLicense } from '@/api/activate'
|
||||
import type { Activate } from '@/api/activate/interface'
|
||||
|
||||
export const useAuthStore = defineStore(AUTH_STORE_KEY, {
|
||||
state: (): AuthState => ({
|
||||
authButtonList: {},
|
||||
authMenuList: [],
|
||||
routeName: '',
|
||||
showMenuFlag: localStorage.getItem('showMenuFlag') === 'true',
|
||||
activateInfo: {} as Activate.ActivationCodePlaintext,
|
||||
activateInfoLoaded: false
|
||||
}),
|
||||
getters: {
|
||||
authButtonListGet: state => state.authButtonList,
|
||||
authMenuListGet: state => state.authMenuList,
|
||||
showMenuListGet: state => getShowMenuList(state.authMenuList),
|
||||
flatMenuListGet: state => getFlatMenuList(state.authMenuList),
|
||||
breadcrumbListGet: state => getAllBreadcrumbList(state.authMenuList),
|
||||
showMenuFlagGet: state => state.showMenuFlag,
|
||||
activateInfoGet: state => state.activateInfo,
|
||||
activateInfoLoadedGet: state => state.activateInfoLoaded
|
||||
},
|
||||
actions: {
|
||||
async getAuthButtonList() {
|
||||
const { data } = await getAuthButtonListApi()
|
||||
this.authButtonList = data
|
||||
},
|
||||
async getAuthMenuList() {
|
||||
const { data: menuData } = await getAuthMenuListApi()
|
||||
this.authMenuList = filterBusinessMenus(menuData)
|
||||
},
|
||||
async setRouteName(name: string) {
|
||||
this.routeName = name
|
||||
},
|
||||
async resetAuthStore() {
|
||||
this.authButtonList = {}
|
||||
this.authMenuList = []
|
||||
this.routeName = ''
|
||||
this.showMenuFlag = false
|
||||
this.activateInfo = {}
|
||||
this.activateInfoLoaded = false
|
||||
localStorage.removeItem('showMenuFlag')
|
||||
},
|
||||
async setShowMenu() {
|
||||
this.showMenuFlag = true
|
||||
localStorage.setItem('showMenuFlag', 'true')
|
||||
},
|
||||
changeModel() {
|
||||
this.showMenuFlag = false
|
||||
localStorage.removeItem('showMenuFlag')
|
||||
},
|
||||
async setActivateInfo() {
|
||||
const license_result = await getLicense()
|
||||
this.activateInfo = normalizeActivateInfo(license_result.data)
|
||||
this.activateInfoLoaded = true
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
function filterBusinessMenus(menuList: any[]): any[] {
|
||||
const excludedNames = new Set([
|
||||
'home',
|
||||
'plan',
|
||||
'singlePlanList',
|
||||
'preTest',
|
||||
'autoTest',
|
||||
'machine',
|
||||
'testSource',
|
||||
'testScript',
|
||||
'controlSource',
|
||||
'standardDevice',
|
||||
'device',
|
||||
'devType',
|
||||
'errorSystem',
|
||||
'icd',
|
||||
'template',
|
||||
'dictPq',
|
||||
'base'
|
||||
])
|
||||
|
||||
const excludedComponentPatterns = [
|
||||
'/home/',
|
||||
'/plan/',
|
||||
'/machine/',
|
||||
'/system/template/',
|
||||
'/system/dictionary/dictPq/',
|
||||
'/system/base/'
|
||||
]
|
||||
|
||||
const excludedPathPatterns = [
|
||||
'/home',
|
||||
'/plan',
|
||||
'/machine',
|
||||
'/system/template',
|
||||
'/system/dictionary/dictPq',
|
||||
'/system/base'
|
||||
]
|
||||
|
||||
return menuList.filter(menu => {
|
||||
if (Array.isArray(menu.children) && menu.children.length > 0) {
|
||||
menu.children = filterBusinessMenus(menu.children)
|
||||
}
|
||||
|
||||
const component = typeof menu.component === 'string' ? menu.component : ''
|
||||
const path = typeof menu.path === 'string' ? menu.path : ''
|
||||
const hasChildren = Array.isArray(menu.children) && menu.children.length > 0
|
||||
const removedByName = excludedNames.has(menu.name)
|
||||
const removedByComponent = excludedComponentPatterns.some(pattern => component.includes(pattern))
|
||||
const removedByPath = excludedPathPatterns.some(pattern => path.includes(pattern))
|
||||
|
||||
if (removedByName || removedByComponent || removedByPath) {
|
||||
return hasChildren
|
||||
}
|
||||
|
||||
return true
|
||||
})
|
||||
}
|
||||
|
||||
function normalizeActivateInfo(rawData: unknown): Activate.ActivationCodePlaintext {
|
||||
if (!rawData || typeof rawData !== 'object' || Array.isArray(rawData)) {
|
||||
return {}
|
||||
}
|
||||
|
||||
return Object.entries(rawData as Record<string, unknown>).reduce(
|
||||
(modules, [key, value]) => {
|
||||
if (!value || typeof value !== 'object' || Array.isArray(value)) {
|
||||
return modules
|
||||
}
|
||||
|
||||
if (!Object.prototype.hasOwnProperty.call(value, 'permanently')) {
|
||||
return modules
|
||||
}
|
||||
|
||||
const permanentlyValue = (value as { permanently?: unknown }).permanently
|
||||
modules[key] = {
|
||||
permanently: Number(permanentlyValue) === 1 ? 1 : 0
|
||||
}
|
||||
return modules
|
||||
},
|
||||
{} as Activate.ActivationCodePlaintext
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user