Просмотр исходного кода

更新http-client.ejs主要修改设置token时自动更新token状态数据
新增用户状态管理token数据
修复文件上传接口过期且自动刷新token后,无法自动使用新token上传文件的问题

zhontai 2 лет назад
Родитель
Сommit
3d5be301a1

+ 2 - 0
gen/templates/http-client.ejs

@@ -5,6 +5,7 @@ const { apiConfig, generateResponses, config } = it;
 import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse, HeadersDefaults, ResponseType, RawAxiosRequestHeaders } from 'axios'
 import { ElLoading, ElMessage, LoadingOptions } from 'element-plus'
 import { Local, Session } from '/@/utils/storage'
+import { useUserInfo } from '/@/stores/userInfo'
 
 export const adminTokenKey = 'admin-token'
 
@@ -14,6 +15,7 @@ export const getToken = () => {
 }
 // 设置token
 export const setToken = (token: any) => {
+  useUserInfo().setToken(token)
   return Local.set(adminTokenKey, token)
 }
 // 清除token

+ 4 - 4
package-lock.json

@@ -18,7 +18,7 @@
         "echarts": "^5.4.1",
         "echarts-gl": "^2.0.9",
         "echarts-wordcloud": "^2.1.0",
-        "element-plus": "^2.2.35",
+        "element-plus": "^2.2.36",
         "js-cookie": "^3.0.1",
         "js-table2excel": "^1.0.3",
         "jsplumb": "^2.15.6",
@@ -1985,9 +1985,9 @@
       }
     },
     "node_modules/element-plus": {
-      "version": "2.2.35",
-      "resolved": "https://registry.npmmirror.com/element-plus/-/element-plus-2.2.35.tgz",
-      "integrity": "sha512-if5QW41hQwVltekRS+avXYtqjRNra/fSgGstXKPR1lSJ81PWQccy+oztw/OP+USqZP/OejFs7rjLPk5MHgT40w==",
+      "version": "2.2.36",
+      "resolved": "https://registry.npmmirror.com/element-plus/-/element-plus-2.2.36.tgz",
+      "integrity": "sha512-9DzLqOVuw8P5Ck8Uqd9XdnDYVg2Z3iosZ1gtk2xDCWNqeoACpldP5gxa/Hbfgp4QeA3xC+f3g+UeoKKu79l28g==",
       "dependencies": {
         "@ctrl/tinycolor": "^3.4.1",
         "@element-plus/icons-vue": "^2.0.6",

+ 1 - 1
package.json

@@ -22,7 +22,7 @@
     "echarts": "^5.4.1",
     "echarts-gl": "^2.0.9",
     "echarts-wordcloud": "^2.1.0",
-    "element-plus": "^2.2.35",
+    "element-plus": "^2.2.36",
     "js-cookie": "^3.0.1",
     "js-table2excel": "^1.0.3",
     "jsplumb": "^2.15.6",

+ 2 - 0
src/api/admin/http-client.ts

@@ -12,6 +12,7 @@
 import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse, HeadersDefaults, RawAxiosRequestHeaders, ResponseType } from 'axios'
 import { ElLoading, ElMessage, LoadingOptions } from 'element-plus'
 import { Local, Session } from '/@/utils/storage'
+import { useUserInfo } from '/@/stores/userInfo'
 
 export const adminTokenKey = 'admin-token'
 
@@ -21,6 +22,7 @@ export const getToken = () => {
 }
 // 设置token
 export const setToken = (token: any) => {
+  useUserInfo().setToken(token)
   return Local.set(adminTokenKey, token)
 }
 // 清除token

+ 1 - 1
src/router/backEnd.ts

@@ -114,7 +114,7 @@ export async function setAddRoute() {
  * @returns 返回后端路由菜单数据
  */
 export async function getBackEndControlRoutes() {
-  const res = await new AuthApi().getUserMenus()
+  const res = await new AuthApi().getUserMenus().catch(() => {})
   if (res?.success && (res?.data?.length as number) > 0) {
     const menus = [] as any
     res.data?.forEach((menu) => {

+ 12 - 5
src/stores/userInfo.ts

@@ -2,7 +2,10 @@ import { defineStore } from 'pinia'
 //import Cookies from 'js-cookie'
 import { Session } from '/@/utils/storage'
 import { AuthApi } from '/@/api/admin/Auth'
-import { clearToken } from '/@/api/admin/http-client'
+import { getToken, clearToken } from '/@/api/admin/http-client'
+import { merge } from 'lodash-es'
+
+const token = getToken()
 
 /**
  * 用户信息
@@ -11,6 +14,7 @@ import { clearToken } from '/@/api/admin/http-client'
 export const useUserInfo = defineStore('userInfo', {
   state: (): UserInfosState => ({
     userInfos: {
+      token: token || '',
       userName: '',
       photo: '',
       time: 0,
@@ -24,16 +28,19 @@ export const useUserInfo = defineStore('userInfo', {
       if (Session.get('userInfo')) {
         this.userInfos = Session.get('userInfo')
       } else {
-        const userInfos: any = await this.getApiUserInfo()
-        this.userInfos = userInfos
+        const userInfos: any = await this.getApiUserInfo().catch(() => {})
+        merge(this.userInfos, userInfos)
       }
     },
-    async setUserName(userName: string) {
+    setUserName(userName: string) {
       this.userInfos.userName = userName
     },
-    async setPhoto(photo: string) {
+    setPhoto(photo: string) {
       this.userInfos.photo = photo
     },
+    setToken(token: string) {
+      this.userInfos.token = token
+    },
     // 模拟接口数据
     async getApiUserInfo() {
       return new Promise((resolve, reject) => {

+ 1 - 0
src/types/pinia.d.ts

@@ -5,6 +5,7 @@
 // 用户信息
 declare interface UserInfosState<T = any> {
   userInfos: {
+    token: string
     authBtnList: string[]
     photo: string
     roles: string[]

+ 6 - 2
src/views/admin/file/components/file-upload.vue

@@ -47,10 +47,14 @@
 
 <script lang="ts" setup name="admin/file/upload">
 import { ref, reactive, computed } from 'vue'
-import { getToken } from '/@/api/admin/http-client'
 import { ElMessage } from 'element-plus'
 import type { UploadInstance, UploadProps, UploadFile } from 'element-plus'
 import eventBus from '/@/utils/mitt'
+import { storeToRefs } from 'pinia'
+import { useUserInfo } from '/@/stores/userInfo'
+
+const stores = useUserInfo()
+const { userInfos } = storeToRefs(stores)
 
 const uploadRef = ref<UploadInstance>()
 
@@ -74,7 +78,7 @@ const uploadAction = computed(() => {
 })
 
 const uploadHeaders = computed(() => {
-  return { Authorization: 'Bearer ' + getToken() }
+  return { Authorization: 'Bearer ' + userInfos.value.token }
 })
 
 // 打开对话框

+ 2 - 2
src/views/admin/login/component/account.vue

@@ -78,7 +78,7 @@ import { formatAxis } from '/@/utils/formatTime'
 import { NextLoading } from '/@/utils/loading'
 import { AuthApi } from '/@/api/admin/Auth'
 import { AuthLoginInput } from '/@/api/admin/data-contracts'
-import { adminTokenKey } from '/@/api/admin/http-client'
+import { setToken, adminTokenKey } from '/@/api/admin/http-client'
 
 // 定义变量内容
 const { t } = useI18n()
@@ -114,7 +114,7 @@ const onSignIn = async () => {
   }
 
   const token = res.data?.token
-  Local.set(adminTokenKey, token)
+  setToken(token)
   Session.set('token', token)
   // 添加完动态路由,再进行 router 跳转,否则可能报错 No match found for location with path "/"
   const isNoPower = await initBackEndControlRoutes()

+ 1 - 2
src/views/admin/personal/index.vue

@@ -143,7 +143,6 @@ import { UserGetBasicOutput } from '/@/api/admin/data-contracts'
 import { useUserInfo } from '/@/stores/userInfo'
 import pinia from '/@/stores/index'
 import { storeToRefs } from 'pinia'
-import { getToken } from '/@/api/admin/http-client'
 import { AxiosResponse } from 'axios'
 
 // 引入组件
@@ -184,7 +183,7 @@ const currentTime = computed(() => {
 
 // 上传头像请求头部
 const avatarHeaders = computed(() => {
-  return { Authorization: 'Bearer ' + getToken() }
+  return { Authorization: 'Bearer ' + userInfos.value.token }
 })
 
 // 头像地址