| 123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- import { defineStore } from 'pinia'
- import type { UserInfo, UserState } from './helper'
- import { defaultSetting, getLocalState, setLocalState } from './helper'
- import { fetchMy } from '@/api'
- import { useAuthStore } from '../auth'
- import { useUserMemberStore } from '../memberShip'
- import { useCompanyStore } from '../company'
- export const useUserStore = defineStore('user-store', {
- state: (): UserState => defaultSetting(),
- actions: {
- setUserInfo(userInfo: Partial<UserInfo>) {
- this.userInfo = { ...this.userInfo, ...userInfo }
- this.recordState()
- },
- resetUserInfo() {
- this.userInfo = { ...defaultSetting().userInfo }
- this.recordState()
- },
- recordState() {
- setLocalState(this.$state)
- },
- async fetch() {
- const companyStore = useCompanyStore()
- if (companyStore.company.id !== 0) {
- useAuthStore().changeToken(`SECRET_TOKEN${companyStore.company.id}`)
- }
- const data = await fetchMy<UserInfo>()
- this.setUserInfo(data)
- if (data.id && companyStore.company.id !== 0 && !companyStore.company.code) {
- companyStore.getCompanyInfo(companyStore.company.id)
- }
- await useUserMemberStore().fetchMember()
- },
- logout() {
- this.resetUserInfo()
- useAuthStore().removeToken()
- }
- }
- })
|