feat: docking Gf-Vben-Admin backend

This commit is contained in:
haha 2021-04-22 12:41:12 +08:00 committed by JinMao
parent a0165d1eee
commit adfc6688d5
4 changed files with 61 additions and 69 deletions

View File

@ -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<LoginResultModel>(
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<GetUserInfoModel>({ url: Api.GetUserInfo }, { errorMessageMode: 'none' });
return defHttp.get<GetUserInfoByUserIdModel>({
url: Api.GetUserInfo,
});
}
export function getPermCode() {
return defHttp.get<string[]>({ url: Api.GetPermCode });
}
export function doLogout() {
return defHttp.get({ url: Api.Logout });

View File

@ -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<GetUserInfoModel | null> {
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<UserInfo | null> {
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;
},
/**

View File

@ -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<CreateAxiosOptions>) {
// See https://developer.mozilla.org/en-US/docs/Web/HTTP/Authentication#authentication_schemes
// authentication schemese.g: Bearer
// authenticationScheme: 'Bearer',
authenticationScheme: '',
authenticationScheme: 'Bearer',
timeout: 10 * 1000,
// 基础接口地址
// baseURL: globSetting.apiUrl,

View File

@ -1,20 +1,8 @@
<template>
<LoginFormTitle v-show="getShow" class="enter-x" />
<Form
class="p-4 enter-x"
:model="formData"
:rules="getFormRules"
ref="formRef"
v-show="getShow"
@keypress.enter="handleLogin"
>
<Form class="p-4 enter-x" :model="formData" :rules="getFormRules" ref="formRef" v-show="getShow">
<FormItem name="account" class="enter-x">
<Input
size="large"
v-model:value="formData.account"
:placeholder="t('sys.login.userName')"
class="fix-auto-fill"
/>
<Input size="large" v-model:value="formData.account" :placeholder="t('sys.login.userName')" />
</FormItem>
<FormItem name="password" class="enter-x">
<InputPassword