feat: mock拼接 url 方法
This commit is contained in:
parent
1776056621
commit
2dc2c8a014
|
@ -1,5 +1,5 @@
|
|||
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 { isLogin } from '@/utils/auth';
|
||||
|
@ -7,7 +7,7 @@ import { isLogin } from '@/utils/auth';
|
|||
setupMock({
|
||||
setup() {
|
||||
// 用户信息
|
||||
Mock.mock(new RegExp(`${GetUserInfoUrl}`), () => {
|
||||
Mock.mock(makeMockUrl(GetUserInfoUrl), () => {
|
||||
if (isLogin()) {
|
||||
const role = window.localStorage.getItem('userRole') || 'admin';
|
||||
return successResponseWrap({
|
||||
|
@ -33,17 +33,17 @@ setupMock({
|
|||
});
|
||||
|
||||
// 登出
|
||||
Mock.mock(new RegExp(`${LoginUrl}`), () => {
|
||||
Mock.mock(makeMockUrl(LoginUrl), () => {
|
||||
return successResponseWrap({});
|
||||
});
|
||||
|
||||
// 登出
|
||||
Mock.mock(new RegExp(`${LogoutUrl}`), () => {
|
||||
Mock.mock(makeMockUrl(LogoutUrl), () => {
|
||||
return successResponseWrap(null);
|
||||
});
|
||||
|
||||
// 用户的服务端菜单
|
||||
Mock.mock(new RegExp(`${GetMenuListUrl}`), () => {
|
||||
Mock.mock(makeMockUrl(GetMenuListUrl), () => {
|
||||
const menuList = [
|
||||
{
|
||||
path: '/api-test',
|
||||
|
|
|
@ -47,3 +47,13 @@ export const failResponseWrap = (data: unknown, message: string, code = 50000) =
|
|||
code,
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
* 拼接 mock 匹配的 url
|
||||
* @param url 不含前缀的接口地址
|
||||
* @returns 拼接后的完整 url
|
||||
*/
|
||||
export const makeMockUrl = (url: string) => {
|
||||
const mockOrigin = window.location.origin;
|
||||
return `${mockOrigin}/front${url}`;
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue