diff --git a/src/api/sys/user.ts b/src/api/sys/user.ts index 1e883b6d..237b8ba5 100644 --- a/src/api/sys/user.ts +++ b/src/api/sys/user.ts @@ -1,20 +1,24 @@ import { defHttp } from '/@/utils/http/axios'; -import { LoginParams, LoginResultModel, GetUserInfoModel } from './model/userModel'; +import { + LoginParams, + GetUserInfoByUserIdParams, + GetUserInfoByUserIdModel, +} from './model/userModel'; import { ErrorMessageMode } from '/#/axios'; enum Api { Login = '/login', Logout = '/logout', - GetUserInfo = '/getUserInfo', - GetPermCode = '/getPermCode', + GetUserInfo = '/api/user/info', + GetPermCodeByUserId = '/getPermCodeByUserId', } /** * @description: user login api */ export function loginApi(params: LoginParams, mode: ErrorMessageMode = 'modal') { - return defHttp.post( + return defHttp.post( { url: Api.Login, params, @@ -29,12 +33,11 @@ export function loginApi(params: LoginParams, mode: ErrorMessageMode = 'modal') * @description: getUserInfo */ export function getUserInfo() { - return defHttp.get({ url: Api.GetUserInfo }, { errorMessageMode: 'none' }); + return defHttp.get({ + url: Api.GetUserInfo, + }); } -export function getPermCode() { - return defHttp.get({ url: Api.GetPermCode }); -} export function doLogout() { return defHttp.get({ url: Api.Logout }); diff --git a/src/store/modules/user.ts b/src/store/modules/user.ts index 8461efdc..5d369464 100644 --- a/src/store/modules/user.ts +++ b/src/store/modules/user.ts @@ -95,15 +95,8 @@ export const useUserStore = defineStore({ // save token this.setToken(token); - return this.afterLoginAction(goHome); - } catch (error) { - return Promise.reject(error); - } - }, - async afterLoginAction(goHome?: boolean): Promise { - if (!this.getToken) return null; - // get user info - const userInfo = await this.getUserInfoAction(); + // get user info + const userInfo = await this.getUserInfoAction(); const sessionTimeout = this.sessionTimeout; if (sessionTimeout) { @@ -121,19 +114,13 @@ export const useUserStore = defineStore({ goHome && (await router.replace(userInfo?.homePath || PageEnum.BASE_HOME)); } return userInfo; - }, - async getUserInfoAction(): Promise { - if (!this.getToken) return null; + }}, + async getUserInfoAction() { const userInfo = await getUserInfo(); - const { roles = [] } = userInfo; - if (isArray(roles)) { - const roleList = roles.map((item) => item.value) as RoleEnum[]; - this.setRoleList(roleList); - } else { - userInfo.roles = []; - this.setRoleList([]); - } + const { roles } = userInfo; + const roleList = roles.map((item) => item.value) as RoleEnum[]; this.setUserInfo(userInfo); + this.setRoleList(roleList); return userInfo; }, /** diff --git a/src/utils/http/axios/index.ts b/src/utils/http/axios/index.ts index c73af07f..d1277ee3 100644 --- a/src/utils/http/axios/index.ts +++ b/src/utils/http/axios/index.ts @@ -19,7 +19,7 @@ import { useUserStoreWithOut } from '/@/store/modules/user'; const globSetting = useGlobSetting(); const urlPrefix = globSetting.urlPrefix; -const { createMessage, createErrorModal } = useMessage(); +const { createMessage, createErrorModal,notification } = useMessage(); /** * @description: 数据处理,方便区分多种处理方式 @@ -52,35 +52,49 @@ const transform: AxiosTransform = { // 这里逻辑可以根据项目进行修改 const hasSuccess = data && Reflect.has(data, 'code') && code === ResultEnum.SUCCESS; - if (hasSuccess) { + if (!hasSuccess) { + if (message) { + // errorMessageMode=‘modal’的时候会显示modal错误弹窗,而不是消息提示,用于一些比较重要的错误 + if (options.errorMessageMode === 'modal') { + createErrorModal({ title: t('sys.api.errorTip'), content: message }); + } else if (options.errorMessageMode === 'message') { + createMessage.error(message); + } + } + Promise.reject(new Error(message)); + return message; + } + + // 接口请求成功,直接返回结果 + if (code === ResultEnum.SUCCESS) { + if (message) { + notification.success({ message }); + } return result; } - - // 在此处根据自己项目的实际情况对不同的code执行不同的操作 - // 如果不希望中断当前请求,请return数据,否则直接抛出异常即可 - let timeoutMsg = ''; - switch (code) { - case ResultEnum.TIMEOUT: - timeoutMsg = t('sys.api.timeoutMessage'); - const userStore = useUserStoreWithOut(); - userStore.setToken(undefined); - userStore.logout(true); - break; - default: - if (message) { - timeoutMsg = message; - } + // 接口请求错误,统一提示错误信息 + if (code === ResultEnum.ERROR) { + if (message) { + createMessage.error(data.message); + Promise.reject(new Error(message)); + } else { + const msg = t('sys.api.errorMessage'); + createMessage.error(msg); + Promise.reject(new Error(msg)); + } + throw new Error( t('sys.api.apiRequestFailed')); } - - // errorMessageMode=‘modal’的时候会显示modal错误弹窗,而不是消息提示,用于一些比较重要的错误 - // errorMessageMode='none' 一般是调用时明确表示不希望自动弹出错误提示 - if (options.errorMessageMode === 'modal') { - createErrorModal({ title: t('sys.api.errorTip'), content: timeoutMsg }); - } else if (options.errorMessageMode === 'message') { - createMessage.error(timeoutMsg); + // 登录超时 + if (code === ResultEnum.TIMEOUT) { + const timeoutMsg = t('sys.api.timeoutMessage'); + createErrorModal({ + title: t('sys.api.operationFailed'), + content: timeoutMsg, + }); + Promise.reject(new Error(timeoutMsg)); + return message; } - - throw new Error(timeoutMsg || t('sys.api.apiRequestFailed')); + return message; }, // 请求之前处理config @@ -199,7 +213,7 @@ function createAxios(opt?: Partial) { // See https://developer.mozilla.org/en-US/docs/Web/HTTP/Authentication#authentication_schemes // authentication schemes,e.g: Bearer // authenticationScheme: 'Bearer', - authenticationScheme: '', + authenticationScheme: 'Bearer', timeout: 10 * 1000, // 基础接口地址 // baseURL: globSetting.apiUrl, diff --git a/src/views/sys/login/LoginForm.vue b/src/views/sys/login/LoginForm.vue index edc21739..d74e0fac 100644 --- a/src/views/sys/login/LoginForm.vue +++ b/src/views/sys/login/LoginForm.vue @@ -1,20 +1,8 @@