mirror of
				https://github.com/YunaiV/ruoyi-vue-pro.git
				synced 2025-11-04 08:06:12 +08:00 
			
		
		
		
	feat: add vue3(element-plus)
This commit is contained in:
		
							
								
								
									
										56
									
								
								yudao-ui-admin-vue3/src/store/modules/user.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										56
									
								
								yudao-ui-admin-vue3/src/store/modules/user.ts
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,56 @@
 | 
			
		||||
import { store } from '../index'
 | 
			
		||||
import { defineStore } from 'pinia'
 | 
			
		||||
import { getInfoApi } from '@/api/login'
 | 
			
		||||
import { getAccessToken } from '@/utils/auth'
 | 
			
		||||
import { useCache } from '@/hooks/web/useCache'
 | 
			
		||||
 | 
			
		||||
interface UserInfoVO {
 | 
			
		||||
  permissions: []
 | 
			
		||||
  roles: []
 | 
			
		||||
  user: {
 | 
			
		||||
    avatar: string
 | 
			
		||||
    id: number
 | 
			
		||||
    nickname: string
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
const { wsCache } = useCache()
 | 
			
		||||
 | 
			
		||||
export const useUserStore = defineStore({
 | 
			
		||||
  id: 'admin-user',
 | 
			
		||||
  state: (): UserInfoVO => ({
 | 
			
		||||
    permissions: [],
 | 
			
		||||
    roles: [],
 | 
			
		||||
    user: {
 | 
			
		||||
      id: 0,
 | 
			
		||||
      avatar: '',
 | 
			
		||||
      nickname: ''
 | 
			
		||||
    }
 | 
			
		||||
  }),
 | 
			
		||||
  actions: {
 | 
			
		||||
    // TODO 设置store刷新页面就消失
 | 
			
		||||
    async getUserInfoAction() {
 | 
			
		||||
      if (!getAccessToken()) {
 | 
			
		||||
        this.resetState()
 | 
			
		||||
        return null
 | 
			
		||||
      }
 | 
			
		||||
      const res = await getInfoApi()
 | 
			
		||||
      this.permissions = res.permissions
 | 
			
		||||
      this.roles = res.roles
 | 
			
		||||
      this.user = res.user
 | 
			
		||||
      wsCache.set('user', res)
 | 
			
		||||
    },
 | 
			
		||||
    resetState() {
 | 
			
		||||
      this.permissions = []
 | 
			
		||||
      this.roles = []
 | 
			
		||||
      this.user = {
 | 
			
		||||
        id: 0,
 | 
			
		||||
        avatar: '',
 | 
			
		||||
        nickname: ''
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
})
 | 
			
		||||
 | 
			
		||||
export const useUserStoreWithOut = () => {
 | 
			
		||||
  return useUserStore(store)
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user