feat: login 接口对接

This commit is contained in:
RubyLiu 2023-06-02 18:28:57 +08:00 committed by rubylliu
parent 9798aa0bb1
commit 31ae4c2ec7
13 changed files with 62 additions and 35 deletions

View File

@ -1,2 +1,2 @@
VITE_API_BASE_URL= 'http://192.168.200.10:8081' VITE_API_BASE_URL= 'front'
VUE_APP_Access_Control_Allow_Origin= '*' VUE_APP_Access_Control_Allow_Origin= '*'

View File

@ -12,9 +12,10 @@ export default mergeConfig(
strict: true, strict: true,
}, },
proxy: { proxy: {
'/api': { '/front': {
target: 'http://192.168.200.10:8081', target: 'http://101.43.186.75:8081/',
changeOrigin: true, changeOrigin: true,
rewrite: (path) => path.replace(/^\/front/, ''),
}, },
}, },
}, },

View File

@ -162,6 +162,8 @@ const transform: AxiosTransform = {
}, },
}; };
const { sessionId, csrfToken } = getToken();
function createAxios(opt?: Partial<CreateAxiosOptions>) { function createAxios(opt?: Partial<CreateAxiosOptions>) {
return new MSAxios( return new MSAxios(
deepMerge( deepMerge(
@ -170,9 +172,9 @@ function createAxios(opt?: Partial<CreateAxiosOptions>) {
// authentication schemese.g: Bearer // authentication schemese.g: Bearer
// authenticationScheme: 'Bearer', // authenticationScheme: 'Bearer',
authenticationScheme: '', authenticationScheme: '',
baseURL: import.meta.env.VITE_API_BASE_URL as string,
timeout: 30 * 1000, timeout: 30 * 1000,
headers: { 'Content-Type': ContentTypeEnum.JSON, 'X-AUTH-TOKEN': sessionId, 'CSRF-TOKEN': csrfToken },
headers: { 'Content-Type': ContentTypeEnum.JSON },
// 如果是form-data格式 // 如果是form-data格式
// headers: { 'Content-Type': ContentTypeEnum.FORM_URLENCODED }, // headers: { 'Content-Type': ContentTypeEnum.FORM_URLENCODED },
// 数据处理方式 // 数据处理方式

View File

@ -1,2 +1,2 @@
export const GetApiTestListUrl = '/mock/list/menu'; export const GetApiTestListUrl = `/mock/list/menu`;
export const GetApiTestList = '/api/definition/page'; export const GetApiTestList = `/api/definition/page`;

View File

@ -1,4 +1,4 @@
export const LoginUrl = '/api/user/login'; export const LoginUrl = '/login';
export const LogoutUrl = '/api/user/logout'; export const LogoutUrl = '/api/user/logout';
export const GetUserInfoUrl = '/api/user/info'; export const GetUserInfoUrl = '/api/user/info';
export const GetMenuListUrl = '/api/user/menu'; export const GetMenuListUrl = '/api/user/menu';

View File

@ -1,7 +1,7 @@
// 核心的封装方法,详细参数看文档 https://arco.design/vue/component/table // 核心的封装方法,详细参数看文档 https://arco.design/vue/component/table
// hook/table-props.ts // hook/table-props.ts
import { ref } from 'vue'; import { ref, reactive } from 'vue';
import { MsTabelProps, MsTableData, MsTableColumn } from './type'; import { MsTabelProps, MsTableData, MsTableColumn } from './type';
import { ApiTestListI } from '@/models/api-test'; import { ApiTestListI } from '@/models/api-test';
@ -20,10 +20,18 @@ export interface QueryParams {
type GetListFunc = (v: QueryParams) => Promise<ApiTestListI>; type GetListFunc = (v: QueryParams) => Promise<ApiTestListI>;
export default function useTbleProps(loadListFunc: GetListFunc, props?: Partial<MsTabelProps>) { export default function useTbleProps(loadListFunc: GetListFunc, props?: Partial<MsTabelProps>) {
// 行选择
const rowSelection = reactive({
type: 'checkbox',
showCheckedAll: true,
onlyCurrent: false,
});
const defaultProps: MsTabelProps = { const defaultProps: MsTabelProps = {
'bordered': true, 'bordered': true,
'size': 'mini', 'size': 'small',
'scroll': { y: 550, x: '1400px' }, 'scroll': { y: '550px', x: '1400px' },
'checkable': true,
'expandable': false, 'expandable': false,
'loading': true, 'loading': true,
'data': [] as MsTableData, 'data': [] as MsTableData,
@ -42,6 +50,11 @@ export default function useTbleProps(loadListFunc: GetListFunc, props?: Partial<
// 属性组 // 属性组
const propsRes = ref(defaultProps); const propsRes = ref(defaultProps);
// 是否可选中
if (propsRes.value.selectable) {
propsRes.value['row-selection'] = rowSelection;
}
// 加载效果 // 加载效果
const setLoading = (status: boolean) => { const setLoading = (status: boolean) => {
propsRes.value.loading = status; propsRes.value.loading = status;

View File

@ -2,9 +2,11 @@
export interface LoginData { export interface LoginData {
username: string; username: string;
password: string; password: string;
authenticate: string;
} }
// 登录返回 // 登录返回
export interface LoginRes { export interface LoginRes {
token: string; sessionId: string;
csrfToken: string;
} }

View File

@ -61,7 +61,7 @@ const useUserStore = defineStore('user', {
async login(loginForm: LoginData) { async login(loginForm: LoginData) {
try { try {
const res = await userLogin(loginForm); const res = await userLogin(loginForm);
setToken(res.token); setToken(res.sessionId, res.csrfToken);
} catch (err) { } catch (err) {
clearToken(); clearToken();
throw err; throw err;

View File

@ -1,19 +1,22 @@
const TOKEN_KEY = 'token'; const SESSION_ID = 'sessionId';
const CSRF_TOKEN = 'csrfToken';
const isLogin = () => { const isLogin = () => {
return !!localStorage.getItem(TOKEN_KEY); return !!localStorage.getItem(SESSION_ID);
}; };
// 获取token
const getToken = () => { const getToken = () => {
return localStorage.getItem(TOKEN_KEY); return { [SESSION_ID]: localStorage.getItem(SESSION_ID), [CSRF_TOKEN]: localStorage.getItem(CSRF_TOKEN) || '' };
}; };
const setToken = (token: string) => { const setToken = (sessionId: string, csrfToken: string) => {
localStorage.setItem(TOKEN_KEY, token); localStorage.setItem(SESSION_ID, sessionId);
localStorage.setItem(CSRF_TOKEN, csrfToken);
}; };
const clearToken = () => { const clearToken = () => {
localStorage.removeItem(TOKEN_KEY); localStorage.removeItem(SESSION_ID);
localStorage.removeItem(CSRF_TOKEN);
}; };
export { isLogin, getToken, setToken, clearToken }; export { isLogin, getToken, setToken, clearToken };

View File

@ -26,6 +26,7 @@
{ {
title: '接口名称', title: '接口名称',
dataIndex: 'name', dataIndex: 'name',
width: 200,
}, },
{ {
title: '请求类型', title: '请求类型',
@ -66,6 +67,7 @@
{ {
title: '创建时间', title: '创建时间',
slotName: 'createTime', slotName: 'createTime',
width: 200,
}, },
{ {
title: '描述', title: '描述',
@ -79,7 +81,7 @@
}, },
]; ];
const { propsRes, propsEvent, loadList } = useTable(getTableList, { columns, scroll: { x: 2000 } }); const { propsRes, propsEvent, loadList } = useTable(getTableList, { columns, scroll: { y: 750, x: 2000 } });
const fetchData = async () => { const fetchData = async () => {
await loadList(); await loadList();

View File

@ -11,9 +11,9 @@
<div class="form mt-20"> <div class="form mt-20">
<a-form ref="formRef" :model="userInfo" @submit="handleSubmit"> <a-form ref="formRef" :model="userInfo" @submit="handleSubmit">
<a-form-item field="radio" hide-label> <a-form-item field="radio" hide-label>
<a-radio-group v-model="userInfo.radio"> <a-radio-group v-model="userInfo.authenticate">
<a-radio value="LDAP">LDAP</a-radio> <a-radio value="LDAP">LDAP</a-radio>
<a-radio value="normal">普通登陆</a-radio> <a-radio value="LOCAL">普通登陆</a-radio>
<a-radio value="OIDC 90">OIDC 90</a-radio> <a-radio value="OIDC 90">OIDC 90</a-radio>
</a-radio-group> </a-radio-group>
</a-form-item> </a-form-item>
@ -23,7 +23,11 @@
:validate-trigger="['change', 'blur']" :validate-trigger="['change', 'blur']"
hide-label hide-label
> >
<a-input v-model="userInfo.username" :placeholder="$t('login.form.userName.placeholder')" class="rounded-3xl"> <a-input
v-model="userInfo.username"
:placeholder="$t('login.form.userName.placeholder')"
style="border-radius: 1.5rem"
>
<template #prefix> <template #prefix>
<icon-user /> <icon-user />
</template> </template>
@ -39,7 +43,7 @@
v-model="userInfo.password" v-model="userInfo.password"
:placeholder="$t('login.form.password.placeholder')" :placeholder="$t('login.form.password.placeholder')"
allow-clear allow-clear
class="rounded-3xl" style="border-radius: 1.5rem"
> >
<template #prefix> <template #prefix>
<icon-lock /> <icon-lock />
@ -47,7 +51,7 @@
</a-input-password> </a-input-password>
</a-form-item> </a-form-item>
<div class="mt-4"> <div class="mt-4">
<a-button class="rounded-3xl" type="primary" html-type="submit" long :loading="loading"> <a-button style="border-radius: 1.5rem" type="primary" html-type="submit" long :loading="loading">
{{ $t('login.form.login') }} {{ $t('login.form.login') }}
</a-button> </a-button>
</div> </div>
@ -75,14 +79,14 @@
const loginConfig = useStorage('login-config', { const loginConfig = useStorage('login-config', {
rememberPassword: true, rememberPassword: true,
username: 'admin', // username: 'admin',
password: 'admin', // demo default value password: 'metersphere',
}); });
const userInfo = reactive({ const userInfo = reactive({
radio: 'normal', authenticate: 'LOCAL',
username: '', username: 'admin',
password: '', password: 'metersphere',
}); });
const handleSubmit = async ({ const handleSubmit = async ({

View File

@ -4,8 +4,8 @@ export default {
'login.form.password.errMsg': 'Password cannot be empty', 'login.form.password.errMsg': 'Password cannot be empty',
'login.form.login.errMsg': 'Login error, refresh and try again', 'login.form.login.errMsg': 'Login error, refresh and try again',
'login.form.login.success': 'welcome to use', 'login.form.login.success': 'welcome to use',
'login.form.userName.placeholder': 'Username: admin', 'login.form.userName.placeholder': 'Username',
'login.form.password.placeholder': 'Password: admin', 'login.form.password.placeholder': 'Password',
'login.form.rememberPassword': 'Remember password', 'login.form.rememberPassword': 'Remember password',
'login.form.forgetPassword': 'Forgot password', 'login.form.forgetPassword': 'Forgot password',
'login.form.login': 'login', 'login.form.login': 'login',

View File

@ -4,8 +4,8 @@ export default {
'login.form.password.errMsg': '密码不能为空', 'login.form.password.errMsg': '密码不能为空',
'login.form.login.errMsg': '登录出错,请刷新重试', 'login.form.login.errMsg': '登录出错,请刷新重试',
'login.form.login.success': '欢迎使用', 'login.form.login.success': '欢迎使用',
'login.form.userName.placeholder': '用户名admin', 'login.form.userName.placeholder': '用户名',
'login.form.password.placeholder': '密码admin', 'login.form.password.placeholder': '密码',
'login.form.rememberPassword': '记住密码', 'login.form.rememberPassword': '记住密码',
'login.form.forgetPassword': '忘记密码', 'login.form.forgetPassword': '忘记密码',
'login.form.login': '登录', 'login.form.login': '登录',