core.ts 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. //获取用户信息与权限
  2. import { defineStore } from 'pinia'
  3. import { getUserAuthority } from '@/apis/authority'
  4. import { getFrameUser } from '@/apis/frame'
  5. import { type Result, type FrameUserInfo, type UserAuthorityInfo, SuccessResultCode, setTheme } from '@cacp/ui'
  6. import config from '@/config'
  7. interface CoreState {
  8. isInited: boolean
  9. isAuthInited: boolean
  10. currentUser: FrameUserInfo | undefined
  11. userAuthority: UserAuthorityInfo | undefined
  12. }
  13. export const useCoreStore = defineStore('core', {
  14. state: (): CoreState => ({
  15. isInited: false,
  16. isAuthInited: false,
  17. currentUser: undefined,
  18. userAuthority: undefined
  19. }),
  20. actions: {
  21. async init() {
  22. if (config.NEED_USER_AUTHORITY || !this.isAuthInited) {
  23. const res: Result<UserAuthorityInfo> = await getUserAuthority()
  24. if (res.code === SuccessResultCode) {
  25. this.$patch({
  26. isAuthInited: true,
  27. userAuthority: res.data
  28. })
  29. }
  30. }
  31. if (!this.isInited) {
  32. const res: Result<FrameUserInfo> = await getFrameUser()
  33. if (res.code === SuccessResultCode) {
  34. this.$patch({
  35. isInited: true,
  36. currentUser: res.data
  37. })
  38. setTheme(res.data.theme)
  39. }
  40. }
  41. },
  42. clear() {
  43. this.$patch({
  44. isInited: false,
  45. isAuthInited: false,
  46. currentUser: undefined,
  47. userAuthority: undefined
  48. })
  49. }
  50. }
  51. })