当前位置: 首页 > article >正文

14vue3实战-----获取用户信息和用户的菜单树信息

14vue3实战-----获取用户信息和用户的菜单树信息

  • 1.获取用户信息
    • 1.1封装接口
    • 1.2优化
  • 2.获取用户的菜单树信息

1.获取用户信息

1.1封装接口

后端有根据id获取用户信息的接口,前端需要把该接口封装一下:
service/login/login.ts:

import hyRequest from '..'
import type { IAccount } from '@/types'
import { localCache } from '@/utils/cache'
import { LOGIN_TOKEN } from '@/global/constants'
export function getUserInfoById(id: number) {
  return hyRequest.get({
    url: `/users/${id}`
    headers: {
       Authorization: 'Bearer ' + localCache.getCache(LOGIN_TOKEN)
    }
  })
}

store/login/login.ts:

...
async loginAccountAction(account: IAccount) {
      // 1.账号登录, 获取token等信息
      const loginResult = await accountLoginRequest(account)
      const id = loginResult.data.id
      this.token = loginResult.data.token
      localCache.setCache(LOGIN_TOKEN, this.token)

      // 2.获取登录用户的详细信息(role信息)
      const userInfoResult = await getUserInfoById(id)
      const userInfo = userInfoResult.data
      this.userInfo = userInfo

      // 3.根据角色请求用户的权限(菜单menus)
      ...

      // 4.进行本地缓存
      localCache.setCache('userInfo', userInfo)
      ...
      
      // 5.页面跳转(main页面)
      router.push('/main')
    }
  }

1.2优化

如果所有需要token的接口都像上面一样封装,这样子太麻烦,所以最好把token的处理放在请求拦截器中更好。
在这里插入图片描述
service/index.ts:

import { LOGIN_TOKEN } from '@/global/constants'
import { localCache } from '@/utils/cache'
import { BASE_URL, TIME_OUT } from './config'
import HYRequest from './request'

const hyRequest = new HYRequest({
  baseURL: BASE_URL,
  timeout: TIME_OUT,
  interceptors: {
    requestSuccessFn: (config) => {
      // 每一个请求都自动携带token
      const token = localCache.getCache(LOGIN_TOKEN)
      if (config.headers && token) {
        // 类型缩小
        config.headers.Authorization = 'Bearer ' + token
      }
      return config
    }
  }
})
export default hyRequest

2.获取用户的菜单树信息

service/login/login.ts:

import hyRequest from '..'
import type { IAccount } from '@/types'

export function accountLoginRequest(account: IAccount) {
  return hyRequest.post({
    url: '/login',
    data: account
  })
}

export function getUserInfoById(id: number) {
  return hyRequest.get({
    url: `/users/${id}`
  })
}

export function getUserMenusByRoleId(id: number) {
  return hyRequest.get({
    url: `/role/${id}/menu`
  })
}

store/login/login.ts:

import { defineStore } from 'pinia'
import {
  accountLoginRequest,
  getUserInfoById,
  getUserMenusByRoleId
} from '@/service/login/login'
import type { IAccount } from '@/types'
import { localCache } from '@/utils/cache'
import router from '@/router'
import { LOGIN_TOKEN } from '@/global/constants'

interface ILoginState {
  token: string
  //这里userInfo和usermenus也可以定义接口。如果数据很复杂,嵌套很多层,也可以定义为any
  userInfo: any
  userMenus: any
}

const useLoginStore = defineStore('login', {
  // 如何制定state的类型
  state: (): ILoginState => ({
    token: localCache.getCache(LOGIN_TOKEN) ?? '',
    userInfo: localCache.getCache('userInfo') ?? {},
    userMenus: localCache.getCache('userMenus') ?? []
  }),
  actions: {
    async loginAccountAction(account: IAccount) {
      // 1.账号登录, 获取token等信息
      const loginResult = await accountLoginRequest(account)
      const id = loginResult.data.id
      this.token = loginResult.data.token
      localCache.setCache(LOGIN_TOKEN, this.token)

      // 2.获取登录用户的详细信息(role信息)
      const userInfoResult = await getUserInfoById(id)
      const userInfo = userInfoResult.data
      this.userInfo = userInfo

      // 3.根据角色请求用户的权限(菜单menus)
      const userMenusResult = await getUserMenusByRoleId(this.userInfo.role.id)
      const userMenus = userMenusResult.data
      this.userMenus = userMenus

      // 4.进行本地缓存
      localCache.setCache('userInfo', userInfo)
      localCache.setCache('userMenus', userMenus)

      // 5.页面跳转(main页面)
      router.push('/main')
    }
  }
})

export default useLoginStore

获取到用户的详细信息和菜单树信息之后,接下来就是根据这些数据进行前端搭建了。具体搭建比较简单,这里就不多说。文章的主要目的是讲述一些功能的思路和vue3+ts的编码规范(和vue2很多地方不太一样)。


http://www.kler.cn/a/539423.html

相关文章:

  • Centos Ollama + Deepseek-r1+Chatbox运行环境搭建
  • C++字符串相关内容
  • Kong故障转移参数配置
  • 深度学习-利用预训练的 ResNet 和 DenseNet 模型进行医学影像诊断
  • 非递减子序列(力扣491)
  • 利用 Python 爬虫获取按关键字搜索淘宝商品的完整指南
  • 解决Redisson在Kubernetes中连接旧Redis主节点的问题
  • Vue3 进阶-自定义事件用法全解析 ✨
  • 大语言模型需要的可观测性数据的关联方式
  • LeetCode热题100-最大子数组和【JavaScript讲解】
  • webpack配置之---output.filename
  • 【DeepSeek】私有化本地部署图文(Win+Mac)
  • Windows编程:在 VS2010 里面,打开一个项目
  • #渗透测试#批量漏洞挖掘#WookTeam searchinfo SQL注入漏洞
  • Vue 3 中的 reactive 和 ref 有什么区别?
  • IDEA关联Tomcat,部署JavaWeb项目
  • kafka服务端之延时操作实现原理
  • NLP论文速读(ICLR 2025)|在实时机器翻译中对齐人类偏好
  • 基于spring boot的餐厅点餐管理系统设计与实现(LW+源码+讲解)
  • 设计模式
  • 所以尼!什么是边缘计算?这和云计算有什么关系?
  • 移动电视盒MGV2000刷安卓及Armbian笔记
  • vue 中 props 的使用,保姆教程
  • 火语言RPA--网址/图片地址获取
  • 生成式聊天机器人 -- 基于Pytorch + Global Attention + 双向 GRU 实现的SeqToSeq模型 -- 上
  • CodeReview-checkList-Java版