初始化
This commit is contained in:
73
mock/_util.ts
Normal file
73
mock/_util.ts
Normal file
@@ -0,0 +1,73 @@
|
||||
import Mock from 'mockjs';
|
||||
|
||||
export function resultSuccess(result, { message = 'ok' } = {}) {
|
||||
return Mock.mock({
|
||||
code: 200,
|
||||
result,
|
||||
message,
|
||||
type: 'success',
|
||||
});
|
||||
}
|
||||
|
||||
export function resultPageSuccess<T = any>(
|
||||
page: number,
|
||||
pageSize: number,
|
||||
list: T[],
|
||||
{ message = 'ok' } = {}
|
||||
) {
|
||||
const pageData = pagination(page, pageSize, list);
|
||||
|
||||
return {
|
||||
...resultSuccess({
|
||||
page,
|
||||
pageSize,
|
||||
pageCount: list.length,
|
||||
list: pageData,
|
||||
}),
|
||||
message,
|
||||
};
|
||||
}
|
||||
|
||||
export function resultError(message = 'Request failed', { code = -1, result = null } = {}) {
|
||||
return {
|
||||
code,
|
||||
result,
|
||||
message,
|
||||
type: 'error',
|
||||
};
|
||||
}
|
||||
|
||||
export function pagination<T = any>(pageNo: number, pageSize: number, array: T[]): T[] {
|
||||
const offset = (pageNo - 1) * Number(pageSize);
|
||||
const ret =
|
||||
offset + Number(pageSize) >= array.length
|
||||
? array.slice(offset, array.length)
|
||||
: array.slice(offset, offset + Number(pageSize));
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Number} times 回调函数需要执行的次数
|
||||
* @param {Function} callback 回调函数
|
||||
*/
|
||||
export function doCustomTimes(times: number, callback: any) {
|
||||
let i = -1;
|
||||
while (++i < times) {
|
||||
callback(i);
|
||||
}
|
||||
}
|
||||
|
||||
export interface requestParams {
|
||||
method: string;
|
||||
body: any;
|
||||
headers?: { token?: string };
|
||||
query: any;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 本函数用于从request数据中获取token,请根据项目的实际情况修改
|
||||
*
|
||||
*/
|
||||
export function getRequestToken({ headers }: requestParams): string | undefined {
|
||||
return headers?.token;
|
||||
}
|
||||
44
mock/dashboard/console.ts
Normal file
44
mock/dashboard/console.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import { defineMock } from '@alova/mock';
|
||||
import { faker } from '@faker-js/faker';
|
||||
import { resultSuccess } from '../_util';
|
||||
|
||||
function getRandom(options) {
|
||||
return Number(faker.commerce.price(options));
|
||||
}
|
||||
|
||||
const result = {
|
||||
//访问量
|
||||
visits: {
|
||||
dayVisits: getRandom({ min: 10000, max: 99999, dec: 2 }),
|
||||
rise: getRandom({ min: 10000, max: 99999, dec: 0 }),
|
||||
decline: getRandom({ min: 10000, max: 99999, dec: 0 }),
|
||||
amount: getRandom({ min: 10000, max: 99999, dec: 2 }),
|
||||
},
|
||||
//销售额
|
||||
saleroom: {
|
||||
weekSaleroom: getRandom({ min: 10000, max: 99999, dec: 2 }),
|
||||
amount: getRandom({ min: 10000, max: 99999, dec: 2 }),
|
||||
degree: getRandom({ min: 10000, max: 99999, dec: 0 }),
|
||||
},
|
||||
//订单量
|
||||
orderLarge: {
|
||||
weekLarge: getRandom({ min: 10000, max: 99999, dec: 2 }),
|
||||
rise: getRandom({ min: 10000, max: 99999, dec: 0 }),
|
||||
decline: getRandom({ min: 10000, max: 99999, dec: 0 }),
|
||||
amount: getRandom({ min: 10000, max: 99999, dec: 2 }),
|
||||
},
|
||||
//成交额度
|
||||
volume: {
|
||||
weekLarge: getRandom({ min: 10000, max: 99999, dec: 2 }),
|
||||
rise: getRandom({ min: 10000, max: 99999, dec: 0 }),
|
||||
decline: getRandom({ min: 10000, max: 99999, dec: 0 }),
|
||||
amount: getRandom({ min: 10000, max: 99999, dec: 2 }),
|
||||
},
|
||||
};
|
||||
|
||||
export default defineMock({
|
||||
// 主控台数据
|
||||
'/api/dashboard/console': () => {
|
||||
return resultSuccess(result);
|
||||
},
|
||||
});
|
||||
96
mock/system/menu.ts
Normal file
96
mock/system/menu.ts
Normal file
@@ -0,0 +1,96 @@
|
||||
import { defineMock } from '@alova/mock';
|
||||
import { resultSuccess } from '../_util';
|
||||
|
||||
export interface ListDate {
|
||||
label: string;
|
||||
key: string;
|
||||
type: number;
|
||||
subtitle: string;
|
||||
openType: number;
|
||||
auth: string;
|
||||
path: string;
|
||||
children?: ListDate[];
|
||||
}
|
||||
|
||||
const menuList = () => {
|
||||
const result: ListDate[] = [
|
||||
{
|
||||
label: 'Dashboard',
|
||||
key: 'dashboard',
|
||||
type: 1,
|
||||
subtitle: 'dashboard',
|
||||
openType: 1,
|
||||
auth: 'dashboard',
|
||||
path: '/dashboard',
|
||||
children: [
|
||||
{
|
||||
label: '主控台',
|
||||
key: 'console',
|
||||
type: 1,
|
||||
subtitle: 'console',
|
||||
openType: 1,
|
||||
auth: 'console',
|
||||
path: '/dashboard/console',
|
||||
},
|
||||
{
|
||||
label: '工作台',
|
||||
key: 'workplace',
|
||||
type: 1,
|
||||
subtitle: 'workplace',
|
||||
openType: 1,
|
||||
auth: 'workplace',
|
||||
path: '/dashboard/workplace',
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
label: '表单管理',
|
||||
key: 'form',
|
||||
type: 1,
|
||||
subtitle: 'form',
|
||||
openType: 1,
|
||||
auth: 'form',
|
||||
path: '/form',
|
||||
children: [
|
||||
{
|
||||
label: '基础表单',
|
||||
key: 'basic-form',
|
||||
type: 1,
|
||||
subtitle: 'basic-form',
|
||||
openType: 1,
|
||||
auth: 'basic-form',
|
||||
path: '/form/basic-form',
|
||||
},
|
||||
{
|
||||
label: '分步表单',
|
||||
key: 'step-form',
|
||||
type: 1,
|
||||
subtitle: 'step-form',
|
||||
openType: 1,
|
||||
auth: 'step-form',
|
||||
path: '/form/step-form',
|
||||
},
|
||||
{
|
||||
label: '表单详情',
|
||||
key: 'detail',
|
||||
type: 1,
|
||||
subtitle: 'detail',
|
||||
openType: 1,
|
||||
auth: 'detail',
|
||||
path: '/form/detail',
|
||||
},
|
||||
],
|
||||
},
|
||||
];
|
||||
|
||||
return result;
|
||||
};
|
||||
|
||||
export default defineMock({
|
||||
'/api/menu/list': () => {
|
||||
const list = menuList();
|
||||
return resultSuccess({
|
||||
list,
|
||||
});
|
||||
},
|
||||
});
|
||||
45
mock/system/role.ts
Normal file
45
mock/system/role.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import { defineMock } from '@alova/mock';
|
||||
import { faker } from '@faker-js/faker';
|
||||
import { resultSuccess, doCustomTimes } from '../_util';
|
||||
import dayjs from 'dayjs';
|
||||
function getMenuKeys() {
|
||||
const keys = ['dashboard', 'console', 'workplace', 'basic-form', 'step-form', 'detail'];
|
||||
const newKeys = [];
|
||||
doCustomTimes(parseInt(Math.random() * 6), () => {
|
||||
const key = keys[Math.floor(Math.random() * keys.length)];
|
||||
newKeys.push(key as never);
|
||||
});
|
||||
return Array.from(new Set(newKeys));
|
||||
}
|
||||
|
||||
const roleList = (pageSize) => {
|
||||
const result: any[] = [];
|
||||
doCustomTimes(pageSize, () => {
|
||||
result.push({
|
||||
id: faker.string.numeric(4),
|
||||
name: faker.person.firstName(),
|
||||
explain: faker.lorem.sentence({ min: 2, max: 4 }),
|
||||
isDefault: faker.helpers.arrayElement([true, false]),
|
||||
menu_keys: getMenuKeys(),
|
||||
create_date: dayjs(faker.date.anytime()).format('YYYY-MM-DD HH:mm'),
|
||||
status: faker.helpers.arrayElement(['normal', 'enable', 'disable']),
|
||||
});
|
||||
});
|
||||
return result;
|
||||
};
|
||||
|
||||
export default defineMock({
|
||||
'/api/role/list': ({ query }) => {
|
||||
const { page = 1, pageSize = 10, name } = query;
|
||||
const list = roleList(Number(pageSize));
|
||||
// 并非真实,只是为了模拟搜索结果
|
||||
const count = name ? 30 : 60;
|
||||
return resultSuccess({
|
||||
page: Number(page),
|
||||
pageSize: Number(pageSize),
|
||||
pageCount: count,
|
||||
itemCount: count * Number(pageSize),
|
||||
list,
|
||||
});
|
||||
},
|
||||
});
|
||||
39
mock/table/list.ts
Normal file
39
mock/table/list.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import { defineMock } from '@alova/mock';
|
||||
import { faker } from '@faker-js/faker';
|
||||
import { doCustomTimes, resultSuccess } from '../_util';
|
||||
import dayjs from 'dayjs';
|
||||
function tableList(pageSize: number) {
|
||||
const result: any[] = [];
|
||||
doCustomTimes(pageSize, () => {
|
||||
result.push({
|
||||
id: faker.string.numeric(4),
|
||||
name: faker.person.firstName(),
|
||||
sex: faker.person.sexType(),
|
||||
avatar: `https://picsum.photos/200/200?v=${faker.string.numeric(4)}`,
|
||||
email: faker.internet.email({ firstName: 'admin' }),
|
||||
city: faker.location.city(),
|
||||
status: faker.helpers.arrayElement(['close', 'refuse', 'pass']),
|
||||
type: faker.helpers.arrayElement(['person', 'company']),
|
||||
// createDate: faker.helpers.arrayElement(dateStrs),
|
||||
createDate: dayjs(faker.date.anytime()).format('YYYY-MM-DD HH:mm'),
|
||||
});
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
export default defineMock({
|
||||
// 表格数据列表
|
||||
'/api/table/list': ({ query }) => {
|
||||
const { page = 1, pageSize = 10, name } = query;
|
||||
const list = tableList(Number(pageSize));
|
||||
// 并非真实,只是为了模拟搜索结果
|
||||
const count = name ? 30 : 60;
|
||||
return resultSuccess({
|
||||
page: Number(page),
|
||||
pageSize: Number(pageSize),
|
||||
pageCount: count,
|
||||
itemCount: count * Number(pageSize),
|
||||
list,
|
||||
});
|
||||
},
|
||||
});
|
||||
44
mock/user/index.ts
Normal file
44
mock/user/index.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import Mock from 'mockjs';
|
||||
import { resultSuccess } from '../_util';
|
||||
import { defineMock } from '@alova/mock';
|
||||
|
||||
const Random = Mock.Random;
|
||||
|
||||
const token = Random.string('upper', 32, 32);
|
||||
|
||||
const adminInfo = {
|
||||
userId: '1',
|
||||
username: 'admin',
|
||||
realName: 'Admin',
|
||||
avatar: Random.image(),
|
||||
desc: 'manager',
|
||||
password: Random.string('upper', 4, 16),
|
||||
token,
|
||||
permissions: [
|
||||
{
|
||||
label: '主控台',
|
||||
value: 'dashboard_console',
|
||||
},
|
||||
{
|
||||
label: '监控页',
|
||||
value: 'dashboard_monitor',
|
||||
},
|
||||
{
|
||||
label: '工作台',
|
||||
value: 'dashboard_workplace',
|
||||
},
|
||||
{
|
||||
label: '基础列表',
|
||||
value: 'basic_list',
|
||||
},
|
||||
{
|
||||
label: '基础列表删除',
|
||||
value: 'basic_list_delete',
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
export default defineMock({
|
||||
'[POST]/api/login': () => resultSuccess({ token }),
|
||||
'/api/admin_info': () => resultSuccess(adminInfo),
|
||||
});
|
||||
46
mock/user/menus.ts
Normal file
46
mock/user/menus.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
import { defineMock } from '@alova/mock';
|
||||
import { resultSuccess } from '../_util';
|
||||
|
||||
const menusList = [
|
||||
{
|
||||
path: '/dashboard',
|
||||
name: 'Dashboard',
|
||||
component: 'LAYOUT',
|
||||
redirect: '/dashboard/console',
|
||||
meta: {
|
||||
icon: 'DashboardOutlined',
|
||||
title: 'Dashboard',
|
||||
},
|
||||
children: [
|
||||
{
|
||||
path: 'console',
|
||||
name: 'dashboard_console',
|
||||
component: '/dashboard/console/console',
|
||||
meta: {
|
||||
title: '主控台',
|
||||
},
|
||||
},
|
||||
{
|
||||
path: 'monitor',
|
||||
name: 'dashboard_monitor',
|
||||
component: '/dashboard/monitor/monitor',
|
||||
meta: {
|
||||
title: '监控页',
|
||||
},
|
||||
},
|
||||
{
|
||||
path: 'workplace',
|
||||
name: 'dashboard_workplace',
|
||||
component: '/dashboard/workplace/workplace',
|
||||
meta: {
|
||||
hidden: true,
|
||||
title: '工作台',
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
];
|
||||
|
||||
export default defineMock({
|
||||
'/api/menus': () => resultSuccess(menusList),
|
||||
});
|
||||
Reference in New Issue
Block a user