1404/11/04- first
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
import { type ComputedRef, ref } from 'vue'
|
||||
import type { User } from '@/model/user'
|
||||
import { getMyInformation } from '@/api/user'
|
||||
import { get } from 'lodash'
|
||||
export const useUserStore = defineStore('user', () => {
|
||||
const userInfo = ref<User | undefined>(undefined)
|
||||
|
||||
async function getMyInfo() {
|
||||
try {
|
||||
userInfo.value = await getMyInformation()
|
||||
return userInfo.value
|
||||
} catch (e) {
|
||||
console.log(e)
|
||||
}
|
||||
}
|
||||
|
||||
const permissions: ComputedRef<string[]> = computed(() => {
|
||||
const permissions: string[] = get(userInfo.value, 'permission', [])
|
||||
const excludes: string[] = get(userInfo.value, 'exclude', [])
|
||||
return permissions.filter((item) => !excludes.includes(item))
|
||||
})
|
||||
|
||||
function canAccess(permissionKey: string) {
|
||||
if (isAdmin.value) return true
|
||||
return permissions.value.includes(permissionKey)
|
||||
}
|
||||
|
||||
const isAdmin = computed(() => {
|
||||
return userInfo.value?.is_admin
|
||||
})
|
||||
|
||||
return { userInfo, getMyInfo, permissions, canAccess, isAdmin }
|
||||
})
|
||||
Reference in New Issue
Block a user