feat: mock拼接 url 方法

This commit is contained in:
baiqi 2023-06-26 11:03:25 +08:00 committed by 刘瑞斌
parent 1776056621
commit 2dc2c8a014
2 changed files with 15 additions and 5 deletions

View File

@ -1,5 +1,5 @@
import Mock from 'mockjs'; import Mock from 'mockjs';
import setupMock, { successResponseWrap, failResponseWrap } from '@/utils/setup-mock'; import setupMock, { successResponseWrap, failResponseWrap, makeMockUrl } from '@/utils/setup-mock';
import { GetMenuListUrl, LogoutUrl, GetUserInfoUrl, LoginUrl } from '@/api/requrls/user'; import { GetMenuListUrl, LogoutUrl, GetUserInfoUrl, LoginUrl } from '@/api/requrls/user';
import { isLogin } from '@/utils/auth'; import { isLogin } from '@/utils/auth';
@ -7,7 +7,7 @@ import { isLogin } from '@/utils/auth';
setupMock({ setupMock({
setup() { setup() {
// 用户信息 // 用户信息
Mock.mock(new RegExp(`${GetUserInfoUrl}`), () => { Mock.mock(makeMockUrl(GetUserInfoUrl), () => {
if (isLogin()) { if (isLogin()) {
const role = window.localStorage.getItem('userRole') || 'admin'; const role = window.localStorage.getItem('userRole') || 'admin';
return successResponseWrap({ return successResponseWrap({
@ -33,17 +33,17 @@ setupMock({
}); });
// 登出 // 登出
Mock.mock(new RegExp(`${LoginUrl}`), () => { Mock.mock(makeMockUrl(LoginUrl), () => {
return successResponseWrap({}); return successResponseWrap({});
}); });
// 登出 // 登出
Mock.mock(new RegExp(`${LogoutUrl}`), () => { Mock.mock(makeMockUrl(LogoutUrl), () => {
return successResponseWrap(null); return successResponseWrap(null);
}); });
// 用户的服务端菜单 // 用户的服务端菜单
Mock.mock(new RegExp(`${GetMenuListUrl}`), () => { Mock.mock(makeMockUrl(GetMenuListUrl), () => {
const menuList = [ const menuList = [
{ {
path: '/api-test', path: '/api-test',

View File

@ -47,3 +47,13 @@ export const failResponseWrap = (data: unknown, message: string, code = 50000) =
code, code,
}; };
}; };
/**
* mock url
* @param url
* @returns url
*/
export const makeMockUrl = (url: string) => {
const mockOrigin = window.location.origin;
return `${mockOrigin}/front${url}`;
};