feat: mock

This commit is contained in:
baiqi 2023-07-17 15:16:54 +08:00 committed by fit2-zhao
parent 63894ee4c8
commit 197c020a59
20 changed files with 299 additions and 457 deletions

View File

@ -413,55 +413,36 @@ export interface LoginRes {
提供全局接口数据 mock 功能,避免因为调试而修改请求代码导致出现问题,按功能模块划分文件,通过`index.ts`暴露,示例如下: 提供全局接口数据 mock 功能,避免因为调试而修改请求代码导致出现问题,按功能模块划分文件,通过`index.ts`暴露,示例如下:
```typescript ```typescript
import Mock from 'mockjs'; import MOCK from '@/utils/setup-mock';
import './user';
import '@/views/dashboard/workplace/mock';
Mock.setup({ import './user';
timeout: '600-1000',
});
``` ```
```typescript ```typescript
import Mock from 'mockjs'; import { mock } from '@/utils/setup-mock';
import setupMock, { successResponseWrap, failResponseWrap } from '@/utils/setup-mock'; import { RequestEnum } from '@/enums/httpEnum';
import { isLogin } from '@/utils/auth';
import { MockParams } from '@/types/mock'; const getProjectList = () => {
return [
{
id: '0283f238hf2',
num: 0,
organizationId: 'v3v4h434c3',
name: '发了多少',
description: 'string',
createTime: 0,
updateTime: 0,
updateUser: 'string',
createUser: 'string',
deleteTime: 0,
deleted: true,
deleteUser: 'string',
enable: true,
},
];
};
setupMock({ mock(RequestEnum.GET, '/system/project/list', getProjectList(), 200);
setup() {
// 用户信息
Mock.mock(new RegExp('/api/user/info'), () => {
if (isLogin()) {
const role = window.localStorage.getItem('userRole') || 'admin';
return successResponseWrap({
name: '王立群',
avatar: '//lf1-xgcdn-tos.pstatp.com/obj/vcloud/vadmin/start.8e0e4855ee346a46ccff8ff3e24db27b.png',
email: 'wangliqun@email.com',
job: 'frontend',
jobName: '前端艺术家',
organization: 'Frontend',
organizationName: '前端',
location: 'beijing',
locationName: '北京',
introduction: '人潇洒,性温存',
personalWebsite: 'https://www.arco.design',
phone: '150****0000',
registrationDate: '2013-05-10 12:10:00',
accountId: '15012312300',
certification: 1,
role,
});
}
return failResponseWrap(null, '未登录', 50008);
});
// 登出
Mock.mock(new RegExp('/api/user/logout'), () => {
return successResponseWrap(null);
});
},
});
``` ```
<a name="QgxDQ"></a> <a name="QgxDQ"></a>
@ -557,10 +538,11 @@ export default i18n;
<a name="RwVcu"></a> <a name="RwVcu"></a>
## -theme 主题配置 ## -theme 主题配置
1. 去Desing Lab 创建主题 https://arco.design/themes/home
1. 去 Desing Lab 创建主题 https://arco.design/themes/home
2. 以`ms-theme-` 命名为开头 2. 以`ms-theme-` 命名为开头
3. 点击页面的配置主题 3. 点击页面的配置主题
**“CSS 变量” + “Tailwind 配置变量” + “基于 css 变量自行计算混合色覆盖 arco-theme 变量”** **“CSS 变量” + “Tailwind 配置变量” + “基于 css 变量自行计算混合色覆盖 arco-theme 变量”**
## -.env.\*环境变量配置 ## -.env.\*环境变量配置

View File

@ -36,7 +36,7 @@
"dependencies": { "dependencies": {
"@7polo/kity": "2.0.8", "@7polo/kity": "2.0.8",
"@7polo/kityminder-core": "1.4.53", "@7polo/kityminder-core": "1.4.53",
"@arco-design/web-vue": "^2.47.0", "@arco-design/web-vue": "^2.48.0",
"@arco-themes/vue-ms-theme-default": "^0.0.17", "@arco-themes/vue-ms-theme-default": "^0.0.17",
"@form-create/arco-design": "^3.1.21", "@form-create/arco-design": "^3.1.21",
"@vueuse/core": "^9.13.0", "@vueuse/core": "^9.13.0",
@ -49,6 +49,7 @@
"lodash": "^4.17.21", "lodash": "^4.17.21",
"lodash-es": "^4.17.21", "lodash-es": "^4.17.21",
"mitt": "^3.0.0", "mitt": "^3.0.0",
"monaco-editor": "^0.39.0",
"nprogress": "^0.2.0", "nprogress": "^0.2.0",
"pinia": "^2.1.3", "pinia": "^2.1.3",
"pinia-plugin-persistedstate": "^3.1.0", "pinia-plugin-persistedstate": "^3.1.0",
@ -67,7 +68,6 @@
"@commitlint/config-conventional": "^17.6.5", "@commitlint/config-conventional": "^17.6.5",
"@types/lodash": "^4.14.195", "@types/lodash": "^4.14.195",
"@types/lodash-es": "^4.17.7", "@types/lodash-es": "^4.17.7",
"@types/mockjs": "^1.0.7",
"@types/nprogress": "^0.2.0", "@types/nprogress": "^0.2.0",
"@types/sortablejs": "^1.15.1", "@types/sortablejs": "^1.15.1",
"@typescript-eslint/eslint-plugin": "^5.59.9", "@typescript-eslint/eslint-plugin": "^5.59.9",
@ -78,6 +78,7 @@
"@vue/babel-plugin-jsx": "^1.1.1", "@vue/babel-plugin-jsx": "^1.1.1",
"@vue/test-utils": "^2.3.2", "@vue/test-utils": "^2.3.2",
"autoprefixer": "^10.4.14", "autoprefixer": "^10.4.14",
"axios-mock-adapter": "^1.21.5",
"consola": "^2.15.3", "consola": "^2.15.3",
"cross-env": "^7.0.3", "cross-env": "^7.0.3",
"eslint": "^8.42.0", "eslint": "^8.42.0",
@ -93,7 +94,6 @@
"less": "^4.1.3", "less": "^4.1.3",
"less-loader": "^11.1.2", "less-loader": "^11.1.2",
"lint-staged": "^13.2.2", "lint-staged": "^13.2.2",
"mockjs": "^1.1.0",
"postcss": "^8.4.24", "postcss": "^8.4.24",
"postcss-html": "^1.5.0", "postcss-html": "^1.5.0",
"postcss-import": "^15.1.0", "postcss-import": "^15.1.0",
@ -122,6 +122,7 @@
"vite-plugin-compression": "^0.5.1", "vite-plugin-compression": "^0.5.1",
"vite-plugin-eslint": "^1.8.1", "vite-plugin-eslint": "^1.8.1",
"vite-plugin-imagemin": "^0.6.1", "vite-plugin-imagemin": "^0.6.1",
"vite-plugin-monaco-editor": "^1.1.0",
"vite-plugin-svg-icons": "^2.0.1", "vite-plugin-svg-icons": "^2.0.1",
"vite-plugin-vue-setup-extend": "^0.4.0", "vite-plugin-vue-setup-extend": "^0.4.0",
"vite-svg-loader": "^3.6.0", "vite-svg-loader": "^3.6.0",

View File

@ -14,7 +14,7 @@ export * from './axiosTransform';
* @description: axios请求 * @description: axios请求
*/ */
export class MSAxios { export class MSAxios {
private axiosInstance: AxiosInstance; public axiosInstance: AxiosInstance;
private readonly options: CreateAxiosOptions; private readonly options: CreateAxiosOptions;

View File

@ -1,7 +1,7 @@
import { MSAxios } from './Axios'; import { MSAxios } from './Axios';
import checkStatus from './checkStatus'; import checkStatus from './checkStatus';
import { Message, Modal } from '@arco-design/web-vue'; import { Message, Modal } from '@arco-design/web-vue';
import { RequestEnum, ContentTypeEnum, ResultEnum } from '@/enums/httpEnum'; import { RequestEnum, ContentTypeEnum } from '@/enums/httpEnum';
import { isString } from '@/utils/is'; import { isString } from '@/utils/is';
import { getToken } from '@/utils/auth'; import { getToken } from '@/utils/auth';
import { setObjToUrlParams, deepMerge } from '@/utils'; import { setObjToUrlParams, deepMerge } from '@/utils';
@ -31,7 +31,7 @@ const transform: AxiosTransform = {
config.params = Object.assign(params || {}, joinTimestamp(joinTime, false)); config.params = Object.assign(params || {}, joinTimestamp(joinTime, false));
} else { } else {
// 兼容restful风格 // 兼容restful风格
config.url = `${config.url}${params}${joinTimestamp(joinTime, true)}`; config.url = `${config.url}/${params}${joinTimestamp(joinTime, true)}`;
config.params = undefined; config.params = undefined;
} }
} else if (isString(params)) { } else if (isString(params)) {
@ -76,32 +76,33 @@ const transform: AxiosTransform = {
throw new Error(t('api.apiRequestFailed')); throw new Error(t('api.apiRequestFailed'));
} }
// 这里 coderesultmessage为 后台统一的字段 // 这里 coderesultmessage为 后台统一的字段
const { code, data: dataResult, message } = data; const { data: dataResult } = data;
// TODO:定义完成功code后需要重写 // 这里直接返回正常结果,因为拦截器已经拦截了非 200 的请求
const hasSuccess = data && Reflect.has(data, 'code') && Number(code) === ResultEnum.SUCCESS; return dataResult;
if (hasSuccess) { // const hasSuccess = data && Reflect.has(data, 'code') && Number(code) === ResultEnum.SUCCESS;
return dataResult; // if (hasSuccess) {
} // return dataResult;
// }
// 在此处根据自己项目的实际情况对不同的code执行不同的操作 // // 在此处根据自己项目的实际情况对不同的code执行不同的操作
// 如果不希望中断当前请求请return数据否则直接抛出异常即可 // // 如果不希望中断当前请求请return数据否则直接抛出异常即可
let timeoutMsg = ''; // let timeoutMsg = '';
if (Number(code) === ResultEnum.TIMEOUT) { // if (Number(code) === ResultEnum.TIMEOUT) {
timeoutMsg = t('api.timeoutMessage'); // timeoutMsg = t('api.timeoutMessage');
} else if (message) { // } else if (message) {
timeoutMsg = message; // timeoutMsg = message;
} // }
// errorMessageMode=modal的时候会显示modal错误弹窗而不是消息提示用于一些比较重要的错误 // // errorMessageMode=modal的时候会显示modal错误弹窗而不是消息提示用于一些比较重要的错误
// errorMessageMode='none' 一般是调用时明确表示不希望自动弹出错误提示 // // errorMessageMode='none' 一般是调用时明确表示不希望自动弹出错误提示
if (options.errorMessageMode === 'modal') { // if (options.errorMessageMode === 'modal') {
Modal.error({ title: t('api.errorTip'), content: timeoutMsg }); // Modal.error({ title: t('api.errorTip'), content: timeoutMsg });
} else if (options.errorMessageMode === 'message') { // } else if (options.errorMessageMode === 'message') {
Message.error(timeoutMsg); // Message.error(timeoutMsg);
} // }
throw new Error(timeoutMsg || t('api.apiRequestFailed')); // throw new Error(timeoutMsg || t('api.apiRequestFailed'));
}, },
/** /**

View File

@ -3,7 +3,7 @@ import { ProjectListUrl } from '@/api/requrls/system/project';
import type { ProjectListItem } from '@/models/system/project'; import type { ProjectListItem } from '@/models/system/project';
export function getProjectList(organizationId: string) { export function getProjectList(organizationId: string) {
return MSR.get<ProjectListItem[]>({ url: `${ProjectListUrl}/${organizationId}` }); return MSR.get<ProjectListItem[]>({ url: ProjectListUrl, params: organizationId });
} }
export default {}; export default {};

View File

@ -1,12 +1,24 @@
import MSR from '@/api/http/index'; import MSR from '@/api/http/index';
import { PoolListUrl, UpdatePoolUrl } from '@/api/requrls/system/resourcePool'; import { PoolListUrl, UpdatePoolUrl, AddPoolUrl, DetailPoolUrl } from '@/api/requrls/system/resourcePool';
import type { ResourcePoolItem } from '@/models/system/resourcePool'; import type { ResourcePoolItem, ResourcePoolInfo } from '@/models/system/resourcePool';
import type { TableQueryParams } from '@/models/common'; import type { TableQueryParams } from '@/models/common';
// 获取资源池列表
export function getPoolList(data: TableQueryParams) { export function getPoolList(data: TableQueryParams) {
return MSR.post<ResourcePoolItem[]>({ url: PoolListUrl, data }); return MSR.post<ResourcePoolItem[]>({ url: PoolListUrl, data });
} }
// 更新资源池信息
export function updatePoolInfo(data: ResourcePoolItem) { export function updatePoolInfo(data: ResourcePoolItem) {
return MSR.post({ url: UpdatePoolUrl, data }); return MSR.post({ url: UpdatePoolUrl, data });
} }
// 添加资源池
export function addPool(data: ResourcePoolInfo) {
return MSR.post({ url: AddPoolUrl, data });
}
// 获取资源池详情
export function getPoolInfo(poolId: string) {
return MSR.get<ResourcePoolItem>({ url: DetailPoolUrl, params: poolId });
}

View File

@ -2,3 +2,4 @@ export const PoolListUrl = '/test/resource/pool/page';
export const UpdatePoolUrl = '/test/resource/pool/update'; export const UpdatePoolUrl = '/test/resource/pool/update';
export const AddPoolUrl = '/test/resource/pool/add'; export const AddPoolUrl = '/test/resource/pool/add';
export const DeletePoolUrl = '/test/resource/pool/delete'; export const DeletePoolUrl = '/test/resource/pool/delete';
export const DetailPoolUrl = '/test/resource/pool/detail';

View File

@ -2,7 +2,7 @@
* *
*/ */
export enum ResultEnum { export enum ResultEnum {
SUCCESS = 0, SUCCESS = 100200,
ERROR = 1, ERROR = 1,
TIMEOUT = 401, TIMEOUT = 401,
TYPE = 'success', TYPE = 'success',

View File

@ -2,6 +2,7 @@ import { createApp } from 'vue';
import FormCreate from '@form-create/arco-design'; import FormCreate from '@form-create/arco-design';
import ArcoVueIcon from '@arco-design/web-vue/es/icon'; import ArcoVueIcon from '@arco-design/web-vue/es/icon';
import SvgIcon from '@/components/pure/svg-icon/index.vue'; import SvgIcon from '@/components/pure/svg-icon/index.vue';
import MSIcon from '@/components/pure/ms-icon-font/index.vue';
import router from './router'; import router from './router';
import store from './store'; import store from './store';
import { setupI18n } from './locale'; import { setupI18n } from './locale';
@ -23,6 +24,7 @@ async function bootstrap() {
await setupI18n(app); await setupI18n(app);
app.use(ArcoVueIcon); app.use(ArcoVueIcon);
app.component('MsIcon', MSIcon);
app.component('SvgIcon', SvgIcon); app.component('SvgIcon', SvgIcon);
app.use(router); app.use(router);

View File

@ -1,5 +1,5 @@
import Mock from 'mockjs'; import { mock } from '@/utils/setup-mock';
import setupMock, { successResponseWrap } from '@/utils/setup-mock'; import { RequestEnum } from '@/enums/httpEnum';
const getList = () => { const getList = () => {
return { return {
@ -108,10 +108,4 @@ const getList = () => {
}; };
}; };
setupMock({ mock(RequestEnum.POST, '/mock/api-test/define/list/', getList(), 200);
setup: () => {
Mock.mock(new RegExp('/mock/api-test/define/list/'), () => {
return successResponseWrap(getList());
});
},
});

View File

@ -1,4 +1,4 @@
import Mock from 'mockjs'; import MOCK from '@/utils/setup-mock';
import './user'; import './user';
import './message-box'; import './message-box';
@ -9,6 +9,4 @@ import './system/resourcePool';
import './system/member'; import './system/member';
import './system/pluginManger'; import './system/pluginManger';
Mock.setup({ MOCK.onAny().passThrough();
timeout: '600-1000',
});

View File

@ -1,5 +1,5 @@
import Mock from 'mockjs'; import { mock } from '@/utils/setup-mock';
import setupMock, { successResponseWrap } from '@/utils/setup-mock'; import { RequestEnum } from '@/enums/httpEnum';
const haveReadIds: number[] = []; const haveReadIds: number[] = [];
const getMessageList = () => { const getMessageList = () => {
@ -70,16 +70,14 @@ const getMessageList = () => {
})); }));
}; };
setupMock({ mock(RequestEnum.GET, '/api/message/list', getMessageList(), 200);
setup: () => { mock(
Mock.mock(new RegExp('/api/message/list'), () => { RequestEnum.GET,
return successResponseWrap(getMessageList()); '/api/message/read',
}); (params: { body: string }) => {
const { ids } = JSON.parse(params.body);
Mock.mock(new RegExp('/api/message/read'), (params: { body: string }) => { haveReadIds.push(...(ids || []));
const { ids } = JSON.parse(params.body); return true;
haveReadIds.push(...(ids || []));
return successResponseWrap(true);
});
}, },
}); 200
);

View File

@ -1,5 +1,5 @@
import Mock from 'mockjs'; import { mock } from '@/utils/setup-mock';
import setupMock, { successTableResponseWrap } from '@/utils/setup-mock'; import { RequestEnum } from '@/enums/httpEnum';
const getMemberList = () => { const getMemberList = () => {
return [ return [
@ -250,10 +250,4 @@ const getMemberList = () => {
]; ];
}; };
setupMock({ mock(RequestEnum.POST, '/member/page', getMemberList(), 200, true);
setup: () => {
Mock.mock(new RegExp('/member/page'), () => {
return successTableResponseWrap(getMemberList());
});
},
});

View File

@ -1,5 +1,5 @@
import Mock from 'mockjs'; import { mock } from '@/utils/setup-mock';
import setupMock, { successTableResponseWrap } from '@/utils/setup-mock'; import { RequestEnum } from '@/enums/httpEnum';
const getPluginList = () => { const getPluginList = () => {
return [ return [
@ -176,10 +176,4 @@ const getPluginList = () => {
]; ];
}; };
setupMock({ mock(RequestEnum.POST, '/plugin/page', getPluginList(), 200, true);
setup: () => {
Mock.mock(new RegExp('/plugin/page'), () => {
return successTableResponseWrap(getPluginList());
});
},
});

View File

@ -1,5 +1,5 @@
import Mock from 'mockjs'; import { mock } from '@/utils/setup-mock';
import setupMock, { successResponseWrap } from '@/utils/setup-mock'; import { RequestEnum } from '@/enums/httpEnum';
const getProjectList = () => { const getProjectList = () => {
return [ return [
@ -51,10 +51,4 @@ const getProjectList = () => {
]; ];
}; };
setupMock({ mock(RequestEnum.GET, '/system/project/list', getProjectList(), 200);
setup: () => {
Mock.mock(new RegExp('/system/project/list'), () => {
return successResponseWrap(getProjectList());
});
},
});

View File

@ -1,166 +1,160 @@
import Mock from 'mockjs'; // import { mock } from '@/utils/setup-mock';
import setupMock, { makeMockUrl, successTableResponseWrap } from '@/utils/setup-mock'; // import { PoolListUrl } from '@/api/requrls/system/resourcePool';
import { PoolListUrl } from '@/api/requrls/system/resourcePool'; // import { RequestEnum } from '@/enums/httpEnum';
const getPoolList = () => { // const getPoolList = () => {
return [ // return [
{ // {
id: '938uh9', // id: '938uh9',
name: 'ksajdfkas', // name: 'ksajdfkas',
type: 'Node', // type: 'Node',
description: '年卡是健康的就是肯德基富士康', // description: '年卡是健康的就是肯德基富士康',
enable: true, // enable: true,
createTime: 0, // createTime: 0,
updateTime: 0, // updateTime: 0,
createUser: 'hShdf', // createUser: 'hShdf',
apiTest: true, // apiTest: true,
loadTest: true, // loadTest: true,
uiTest: true, // uiTest: true,
serverUrl: 'fldskfsl.sdjks.com', // serverUrl: 'fldskfsl.sdjks.com',
deleted: false, // deleted: false,
configuration: 'akjfhbgkdjfsokdsdfs', // configuration: 'akjfhbgkdjfsokdsdfs',
organizationList: [ // organizationList: [
{ // {
id: 'string', // id: 'string',
num: 0, // num: 0,
name: '组织 1', // name: '组织 1',
description: 'blabla', // description: 'blabla',
createTime: 0, // createTime: 0,
updateTime: 0, // updateTime: 0,
createUser: 'string', // createUser: 'string',
updateUser: 'string', // updateUser: 'string',
deleted: true, // deleted: true,
deleteUser: 'string', // deleteUser: 'string',
deleteTime: 0, // deleteTime: 0,
enable: true, // enable: true,
}, // },
{ // {
id: 'string', // id: 'string',
num: 0, // num: 0,
name: '组织 2', // name: '组织 2',
description: 'blabla', // description: 'blabla',
createTime: 0, // createTime: 0,
updateTime: 0, // updateTime: 0,
createUser: 'string', // createUser: 'string',
updateUser: 'string', // updateUser: 'string',
deleted: true, // deleted: true,
deleteUser: 'string', // deleteUser: 'string',
deleteTime: 0, // deleteTime: 0,
enable: true, // enable: true,
}, // },
{ // {
id: 'string', // id: 'string',
num: 0, // num: 0,
name: '组织 3', // name: '组织 3',
description: 'blabla', // description: 'blabla',
createTime: 0, // createTime: 0,
updateTime: 0, // updateTime: 0,
createUser: 'string', // createUser: 'string',
updateUser: 'string', // updateUser: 'string',
deleted: true, // deleted: true,
deleteUser: 'string', // deleteUser: 'string',
deleteTime: 0, // deleteTime: 0,
enable: true, // enable: true,
}, // },
{ // {
id: 'string', // id: 'string',
num: 0, // num: 0,
name: '组织 4', // name: '组织 4',
description: 'blabla', // description: 'blabla',
createTime: 0, // createTime: 0,
updateTime: 0, // updateTime: 0,
createUser: 'string', // createUser: 'string',
updateUser: 'string', // updateUser: 'string',
deleted: true, // deleted: true,
deleteUser: 'string', // deleteUser: 'string',
deleteTime: 0, // deleteTime: 0,
enable: true, // enable: true,
}, // },
], // ],
resources: ['192.168.1.1', '192.168.11.23'], // resources: ['192.168.1.1', '192.168.11.23'],
}, // },
{ // {
id: 'dfopi03wif3', // id: 'dfopi03wif3',
name: 'ksajdfkas', // name: 'ksajdfkas',
type: 'K8s', // type: 'K8s',
description: '生动地很多次私董会佛山的', // description: '生动地很多次私董会佛山的',
enable: false, // enable: false,
createTime: 0, // createTime: 0,
updateTime: 0, // updateTime: 0,
createUser: 'hShdf', // createUser: 'hShdf',
apiTest: true, // apiTest: true,
loadTest: true, // loadTest: true,
uiTest: true, // uiTest: true,
serverUrl: 'fldskfsl.sdjks.com', // serverUrl: 'fldskfsl.sdjks.com',
deleted: false, // deleted: false,
configuration: 'akjfhbgkdjfsokdsdfs', // configuration: 'akjfhbgkdjfsokdsdfs',
organizationList: [ // organizationList: [
{ // {
id: 'string', // id: 'string',
num: 0, // num: 0,
name: '组织 1', // name: '组织 1',
description: 'blabla', // description: 'blabla',
createTime: 0, // createTime: 0,
updateTime: 0, // updateTime: 0,
createUser: 'string', // createUser: 'string',
updateUser: 'string', // updateUser: 'string',
deleted: true, // deleted: true,
deleteUser: 'string', // deleteUser: 'string',
deleteTime: 0, // deleteTime: 0,
enable: true, // enable: true,
}, // },
{ // {
id: 'string', // id: 'string',
num: 0, // num: 0,
name: '组织 2', // name: '组织 2',
description: 'blabla', // description: 'blabla',
createTime: 0, // createTime: 0,
updateTime: 0, // updateTime: 0,
createUser: 'string', // createUser: 'string',
updateUser: 'string', // updateUser: 'string',
deleted: true, // deleted: true,
deleteUser: 'string', // deleteUser: 'string',
deleteTime: 0, // deleteTime: 0,
enable: true, // enable: true,
}, // },
{ // {
id: 'string', // id: 'string',
num: 0, // num: 0,
name: '组织 3', // name: '组织 3',
description: 'blabla', // description: 'blabla',
createTime: 0, // createTime: 0,
updateTime: 0, // updateTime: 0,
createUser: 'string', // createUser: 'string',
updateUser: 'string', // updateUser: 'string',
deleted: true, // deleted: true,
deleteUser: 'string', // deleteUser: 'string',
deleteTime: 0, // deleteTime: 0,
enable: true, // enable: true,
}, // },
{ // {
id: 'string', // id: 'string',
num: 0, // num: 0,
name: '组织 4', // name: '组织 4',
description: 'blabla', // description: 'blabla',
createTime: 0, // createTime: 0,
updateTime: 0, // updateTime: 0,
createUser: 'string', // createUser: 'string',
updateUser: 'string', // updateUser: 'string',
deleted: true, // deleted: true,
deleteUser: 'string', // deleteUser: 'string',
deleteTime: 0, // deleteTime: 0,
enable: true, // enable: true,
}, // },
], // ],
resources: ['192.168.1.1', '192.168.11.23'], // resources: ['192.168.1.1', '192.168.11.23'],
}, // },
]; // ];
}; // };
setupMock({ // mock(RequestEnum.POST, PoolListUrl, getPoolList(), 200, true);
setup: () => {
Mock.mock(makeMockUrl(PoolListUrl), () => {
return successTableResponseWrap(getPoolList());
});
},
});

View File

@ -1,5 +1,5 @@
import Mock from 'mockjs'; import { mock } from '@/utils/setup-mock';
import setupMock, { successTableResponseWrap } from '@/utils/setup-mock'; import { RequestEnum } from '@/enums/httpEnum';
const getUserList = () => { const getUserList = () => {
return [ return [
@ -250,10 +250,4 @@ const getUserList = () => {
]; ];
}; };
setupMock({ mock(RequestEnum.POST, '/user/page', getUserList(), 200, true);
setup: () => {
Mock.mock(new RegExp('/user/page'), () => {
return successTableResponseWrap(getUserList());
});
},
});

View File

@ -1,143 +1,6 @@
import Mock from 'mockjs'; import { mock } from '@/utils/setup-mock';
import setupMock, { successResponseWrap, failResponseWrap, makeMockUrl } from '@/utils/setup-mock'; import { RequestEnum } from '@/enums/httpEnum';
import { LogoutUrl, LoginUrl } from '@/api/requrls/user';
import { GetMenuListUrl, LogoutUrl, LoginUrl } from '@/api/requrls/user'; // mock(RequestEnum.POST, LoginUrl, {}, 200);
import { isLogin } from '@/utils/auth'; mock(RequestEnum.POST, LogoutUrl, null, 200);
setupMock({
mock: false,
setup() {
// 用户信息
Mock.mock(makeMockUrl('/api/userinfo'), async () => {
if (await isLogin()) {
const role = window.localStorage.getItem('userRole') || 'admin';
return successResponseWrap({
name: '王立群',
avatar: '//lf1-xgcdn-tos.pstatp.com/obj/vcloud/vadmin/start.8e0e4855ee346a46ccff8ff3e24db27b.png',
email: 'wangliqun@email.com',
job: 'frontend',
jobName: '前端艺术家',
organization: 'Frontend',
organizationName: '前端',
location: 'beijing',
locationName: '北京',
introduction: '人潇洒,性温存',
personalWebsite: 'https://www.arco.design',
phone: '150****0000',
registrationDate: '2013-05-10 12:10:00',
accountId: '15012312300',
certification: 1,
role,
});
}
return failResponseWrap(null, '未登录', 50008);
});
// 登陆
Mock.mock(makeMockUrl(LoginUrl), () => {
return successResponseWrap({});
});
// 登出
Mock.mock(makeMockUrl(LogoutUrl), () => {
return successResponseWrap(null);
});
// 用户的服务端菜单
Mock.mock(makeMockUrl(GetMenuListUrl), () => {
const menuList = [
{
path: '/api-test',
name: 'apiTest',
meta: {
locale: 'menu.apiTest',
icon: 'icon-dashboard',
order: 0,
},
children: [
{
path: 'list',
name: 'apiTest',
meta: {
locale: 'menu.apiTest',
roles: ['*'],
icon: 'icon-computer',
},
},
],
},
{
path: '/setting',
name: 'setting',
meta: {
locale: 'menu.settings',
icon: 'icon-dashboard',
order: 0,
},
children: [
{
path: 'system',
name: 'settingSystem',
redirect: '/setting/system/user',
meta: {
locale: 'menu.settings.system',
roles: ['*'],
hideChildrenInMenu: true,
},
children: [
{
path: 'user',
name: 'settingSystemUser',
meta: {
locale: 'menu.settings.system.user',
roles: ['*'],
isTopMenu: true,
},
},
{
path: 'usergroup',
name: 'settingSystemUsergroup',
meta: {
locale: 'menu.settings.system.usergroup',
roles: ['*'],
isTopMenu: true,
},
},
{
path: 'resourcePool',
name: 'settingSystemResourcePool',
meta: {
locale: 'menu.settings.system.resourcePool',
roles: ['*'],
isTopMenu: true,
},
},
{
path: 'resourcePoolDetail',
name: 'settingSystemResourcePoolDetail',
meta: {
locale: 'menu.settings.system.resourcePoolDetail',
roles: ['*'],
},
},
],
},
],
},
{
path: '/personal',
name: 'personal',
meta: {},
children: [
{
path: '/personal/info',
name: 'personalInfo',
meta: {},
},
],
},
];
return successResponseWrap(menuList);
});
},
});

View File

@ -1,8 +1,8 @@
import debug from './env'; import MSR from '@/api/http';
import { RequestEnum } from '@/enums/httpEnum';
import MockAdapter from 'axios-mock-adapter';
export default ({ mock, setup }: { mock?: boolean; setup: () => void }) => { const MOCK = new MockAdapter(MSR.axiosInstance, { onNoMatch: 'throwException' });
if (mock !== false && debug) setup();
};
/** /**
* mock- * mock-
@ -39,15 +39,35 @@ export const successTableResponseWrap = (data: unknown) => {
* @param code * @param code
* @returns * @returns
*/ */
export const failResponseWrap = (data: unknown, message: string, code = 50000) => { export const failResponseWrap = (data: unknown, message?: string, messageDetail?: string) => {
return { return {
data, data,
status: 'fail', message: message || '请求失败',
message, messageDetail,
code,
}; };
}; };
export const mock = (
method: RequestEnum,
url: string | RegExp,
data: unknown,
code: number,
isTable?: boolean,
message?: string,
messageDetail?: string
) => {
const methodMap = {
[RequestEnum.GET]: MOCK.onGet(url),
[RequestEnum.POST]: MOCK.onPost(url),
[RequestEnum.PUT]: MOCK.onPut(url),
[RequestEnum.DELETE]: MOCK.onDelete(url),
};
if (code === 200) {
return methodMap[method].reply(code, isTable ? successTableResponseWrap(data) : successResponseWrap(data));
}
return methodMap[method].reply(code, failResponseWrap(data, message, messageDetail));
};
/** /**
* mock url * mock url
* @param url * @param url
@ -57,3 +77,5 @@ export const makeMockUrl = (url: string) => {
const mockOrigin = window.location.origin; const mockOrigin = window.location.origin;
return `${mockOrigin}/front${url}`; return `${mockOrigin}/front${url}`;
}; };
export default MOCK;

View File

@ -12,10 +12,8 @@
"build/**/*.d.ts", "build/**/*.d.ts",
"mock/**/*.ts", "mock/**/*.ts",
"__test__/**/*.ts", "__test__/**/*.ts",
"node_modules/monaco-editor/monaco.d.ts"
], // TS ], // TS
"exclude": [
"node_modules"
],
"compilerOptions": { "compilerOptions": {
"allowJs": true, // JSJSX "allowJs": true, // JSJSX
"noEmit": true, "noEmit": true,