diff --git a/kinit-admin/package.json b/kinit-admin/package.json index 1fac85e..0cc1ca1 100644 --- a/kinit-admin/package.json +++ b/kinit-admin/package.json @@ -42,7 +42,7 @@ "mockjs": "^1.1.0", "moment": "^2.29.4", "nprogress": "^0.2.0", - "pinia": "^2.0.29", + "pinia": "2.0.29", "qrcode": "^1.5.1", "qs": "^6.11.0", "url": "^0.11.0", @@ -96,7 +96,6 @@ "stylelint-order": "^6.0.1", "terser": "^5.16.1", "typescript": "4.9.4", - "unplugin-vue-define-options": "^1.1.4", "vite": "4.0.4", "vite-plugin-ejs": "^1.6.4", "vite-plugin-eslint": "^1.8.1", diff --git a/kinit-admin/src/components/Dialog/src/Dialog.vue b/kinit-admin/src/components/Dialog/src/Dialog.vue index 7913762..163cf62 100644 --- a/kinit-admin/src/components/Dialog/src/Dialog.vue +++ b/kinit-admin/src/components/Dialog/src/Dialog.vue @@ -50,7 +50,6 @@ watch( ) const dialogStyle = computed(() => { - console.log(unref(dialogHeight)) return { height: unref(dialogHeight) } diff --git a/kinit-admin/src/components/UserInfo/src/UserInfo.vue b/kinit-admin/src/components/UserInfo/src/UserInfo.vue index b002f80..b176dbe 100644 --- a/kinit-admin/src/components/UserInfo/src/UserInfo.vue +++ b/kinit-admin/src/components/UserInfo/src/UserInfo.vue @@ -4,6 +4,7 @@ import { useI18n } from '@/hooks/web/useI18n' import { useRouter } from 'vue-router' import { useDesign } from '@/hooks/web/useDesign' import { useAuthStoreWithOut } from '@/store/modules/auth' +import avatar from '@/assets/imgs/avatar.jpg' const { getPrefixCls } = useDesign() @@ -46,7 +47,7 @@ const user = authStore.getUser
diff --git a/kinit-admin/src/config/axios/config.ts b/kinit-admin/src/config/axios/config.ts index a1044a0..6b8e2a3 100644 --- a/kinit-admin/src/config/axios/config.ts +++ b/kinit-admin/src/config/axios/config.ts @@ -3,6 +3,7 @@ const config: { unauthorized_code: number | string default_headers: AxiosHeaders request_timeout: number + token: string } = { /** * 接口成功返回状态码 @@ -22,7 +23,13 @@ const config: { * 默认接口请求类型 * 可选值:application/x-www-form-urlencoded multipart/form-data */ - default_headers: 'application/json' + default_headers: 'application/json', + + /** + * 存储Token字段 + * 关联 config/axios/service/service.interceptors + */ + token: 'Token' } export { config } diff --git a/kinit-admin/src/config/axios/service.ts b/kinit-admin/src/config/axios/service.ts index 0e0c705..44daa25 100644 --- a/kinit-admin/src/config/axios/service.ts +++ b/kinit-admin/src/config/axios/service.ts @@ -1,15 +1,12 @@ -import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse, AxiosError } from 'axios' +import axios, { AxiosInstance, InternalAxiosRequestConfig, AxiosResponse, AxiosError } from 'axios' import { useCache } from '@/hooks/web/useCache' -import { useAppStore } from '@/store/modules/app' -import { useAuthStore } from '@/store/modules/auth' import qs from 'qs' import { config } from './config' import { ElMessage } from 'element-plus' +import { useAuthStore } from '@/store/modules/auth' -const { result_code, unauthorized_code, request_timeout } = config +const { result_code, unauthorized_code, request_timeout, token } = config -const appStore = useAppStore() -const authStore = useAuthStore() const { wsCache } = useCache() // 创建axios实例 @@ -21,10 +18,10 @@ const service: AxiosInstance = axios.create({ // request拦截器 service.interceptors.request.use( - (config: AxiosRequestConfig) => { - const token = wsCache.get(appStore.getToken) - if (token !== '') { - ;(config.headers as any)['Authorization'] = token // 让每个请求携带自定义token 请根据实际情况自行修改 + (config: InternalAxiosRequestConfig) => { + const _token = wsCache.get(token) + if (_token !== '') { + ;(config.headers as any)['Authorization'] = _token // 让每个请求携带自定义token 请根据实际情况自行修改 } if ( config.method === 'post' && @@ -66,6 +63,7 @@ service.interceptors.response.use( } else if (response.data.code === unauthorized_code) { // 请重新登录 ElMessage.error(response.data.message) + const authStore = useAuthStore() authStore.logout() } else { ElMessage.error(response.data.message) @@ -75,11 +73,11 @@ service.interceptors.response.use( console.log('err' + error) let { message } = error if (message == 'Network Error') { - message = '后端接口连接异常' + message = '系统接口连接异常' } else if (message.includes('timeout')) { message = '系统接口请求超时' } else if (message.includes('Request failed with status code')) { - message = '系统接口' + message.substr(message.length - 3) + '异常' + message = '系统接口状态码异常' } ElMessage.error(message) return Promise.reject(error) diff --git a/kinit-admin/src/directives/permission/hasPermi.ts b/kinit-admin/src/directives/permission/hasPermi.ts index 0814c99..28a901d 100644 --- a/kinit-admin/src/directives/permission/hasPermi.ts +++ b/kinit-admin/src/directives/permission/hasPermi.ts @@ -3,16 +3,16 @@ import { useI18n } from '@/hooks/web/useI18n' import { useCache } from '@/hooks/web/useCache' import { intersection } from 'lodash-es' import { isArray } from '@/utils/is' -import { useAppStoreWithOut } from '@/store/modules/app' +import { useAuthStoreWithOut } from '@/store/modules/auth' const { t } = useI18n() const { wsCache } = useCache() -const appStore = useAppStoreWithOut() +const authStore = useAuthStoreWithOut() // 全部权限 const all_permission = ['*.*.*'] const hasPermission = (value: string | string[]): boolean => { - const permissions = wsCache.get(appStore.getUserInfo).permissions as string[] + const permissions = wsCache.get(authStore.getUserInfo).permissions as string[] if (!value) { throw new Error(t('permission.hasPermission')) } diff --git a/kinit-admin/src/permission.ts b/kinit-admin/src/permission.ts index c8322fa..30b811b 100644 --- a/kinit-admin/src/permission.ts +++ b/kinit-admin/src/permission.ts @@ -1,5 +1,4 @@ import router from './router' -import { useAppStoreWithOut } from '@/store/modules/app' import { useCache } from '@/hooks/web/useCache' import type { RouteRecordRaw } from 'vue-router' import { useTitle } from '@/hooks/web/useTitle' @@ -11,7 +10,6 @@ import { useAuthStoreWithOut } from '@/store/modules/auth' const permissionStore = usePermissionStoreWithOut() -const appStore = useAppStoreWithOut() const authStore = useAuthStoreWithOut() const { wsCache } = useCache() @@ -25,14 +23,14 @@ const whiteList = ['/login', '/docs/privacy', '/docs/agreement'] // 不重定向 router.beforeEach(async (to, from, next) => { start() loadStart() - if (wsCache.get(appStore.getUserInfo)) { + if (wsCache.get(authStore.getUserInfo)) { if (to.path === '/login') { next({ path: '/' }) } else if (to.path === '/reset/password') { next() } else { if (!authStore.getIsUser) { - await authStore.getUserInfo() + await authStore.getUserInfoAction() } if (permissionStore.getIsAddRouters) { next() diff --git a/kinit-admin/src/store/modules/app.ts b/kinit-admin/src/store/modules/app.ts index 75b0849..e2cd472 100644 --- a/kinit-admin/src/store/modules/app.ts +++ b/kinit-admin/src/store/modules/app.ts @@ -27,7 +27,6 @@ interface AppState { pageLoading: boolean layout: LayoutType title: string - userInfo: string isDark: boolean currentSize: ElementPlusSize sizeMap: ElementPlusSize[] @@ -38,14 +37,11 @@ interface AppState { logoImage: string footerContent: string icpNumber: string - token: string } export const useAppStore = defineStore('app', { state: (): AppState => { return { - userInfo: 'userInfo', // 登录信息存储字段-建议每个项目换一个字段,避免与其他项目冲突 - token: 'Token', // 存储Token字段 sizeMap: ['default', 'large', 'small'], mobile: false, // 是否是移动端 title: import.meta.env.VITE_APP_TITLE, // 标题 @@ -164,12 +160,6 @@ export const useAppStore = defineStore('app', { getTitle(): string { return this.title }, - getUserInfo(): string { - return this.userInfo - }, - getToken(): string { - return this.token - }, getIsDark(): boolean { return this.isDark }, diff --git a/kinit-admin/src/store/modules/auth.ts b/kinit-admin/src/store/modules/auth.ts index 7f6fce1..c952f9e 100644 --- a/kinit-admin/src/store/modules/auth.ts +++ b/kinit-admin/src/store/modules/auth.ts @@ -2,15 +2,15 @@ import { defineStore } from 'pinia' import { store } from '../index' import { UserLoginType } from '@/api/login/types' import { loginApi } from '@/api/login' -import { useAppStore } from '@/store/modules/app' import { useCache } from '@/hooks/web/useCache' import { getCurrentUserInfo } from '@/api/vadmin/auth/user' import { resetRouter } from '@/router' +import { config } from '@/config/axios/config' import { useTagsViewStore } from '@/store/modules/tagsView' -const appStore = useAppStore() +const { token } = config + const { wsCache } = useCache() -const tagsViewStore = useTagsViewStore() export interface UserState { id?: number @@ -24,6 +24,7 @@ export interface UserState { } export interface AuthState { + userInfo: string user: UserState isUser: boolean } @@ -31,6 +32,7 @@ export interface AuthState { export const useAuthStore = defineStore('auth', { state: (): AuthState => { return { + userInfo: 'userInfo', // 登录信息存储字段-建议每个项目换一个字段,避免与其他项目冲突 user: {}, isUser: false } @@ -41,6 +43,9 @@ export const useAuthStore = defineStore('auth', { }, getIsUser(): boolean { return this.isUser + }, + getUserInfo(): string { + return this.userInfo } }, actions: { @@ -48,10 +53,9 @@ export const useAuthStore = defineStore('auth', { formData.platform = '0' const res = await loginApi(formData) if (res) { - wsCache.set(appStore.getToken, `${res.data.token_type} ${res.data.access_token}`) + wsCache.set(token, `${res.data.token_type} ${res.data.access_token}`) // 存储用户信息 - const auth = useAuthStore() - await auth.getUserInfo() + await this.getUserInfoAction() } return res }, @@ -59,6 +63,7 @@ export const useAuthStore = defineStore('auth', { wsCache.clear() this.user = {} this.isUser = false + const tagsViewStore = useTagsViewStore() tagsViewStore.delAllViews() resetRouter() window.location.href = '/login' @@ -68,11 +73,11 @@ export const useAuthStore = defineStore('auth', { this.user.name = data.name this.user.nickname = data.nickname this.user.telephone = data.telephone - wsCache.set(appStore.getUserInfo, this.user) + wsCache.set(this.userInfo, this.user) }, - async getUserInfo() { + async getUserInfoAction() { const res = await getCurrentUserInfo() - wsCache.set(appStore.getUserInfo, res.data) + wsCache.set(this.userInfo, res.data) this.isUser = true this.user = res.data } diff --git a/kinit-admin/src/views/Dashboard/Workplace.vue b/kinit-admin/src/views/Dashboard/Workplace.vue index 9e686ec..4c7eaab 100644 --- a/kinit-admin/src/views/Dashboard/Workplace.vue +++ b/kinit-admin/src/views/Dashboard/Workplace.vue @@ -6,6 +6,7 @@ import { ref, reactive } from 'vue' import { CountTo } from '@/components/CountTo' import { formatTime } from '@/utils' import { Highlight } from '@/components/Highlight' +import { useAuthStoreWithOut } from '@/store/modules/auth' import { getCountApi, getProjectApi, @@ -21,7 +22,7 @@ import type { Shortcuts } from '@/api/dashboard/workplace/types' import { useCache } from '@/hooks/web/useCache' -import { useAppStoreWithOut } from '@/store/modules/app' +import avatar from '@/assets/imgs/avatar.jpg' const { wsCache } = useCache() @@ -96,9 +97,9 @@ getAllApi() const { t } = useI18n() -const appStore = useAppStoreWithOut() +const authStore = useAuthStoreWithOut() -const user = wsCache.get(appStore.getUserInfo) +const user = wsCache.get(authStore.getUserInfo)