feat(projects): integration fast-crud

This commit is contained in:
xiaojunnuo
2023-05-15 17:41:17 +08:00
parent f68285fbe5
commit 59af204a4c
24 changed files with 3275 additions and 77 deletions

269
mock/api/crud/base.ts Normal file
View File

@@ -0,0 +1,269 @@
// eslint-disable-next-line max-params
function copyList(originList: any, newList: any, options: any, parentId?: number) {
for (const item of originList) {
const newItem = { ...item, parentId };
newItem.id = options.idGenerator;
options.idGenerator += 1;
newList.push(newItem);
if (item.children) {
newItem.children = [];
copyList(item.children, newItem.children, options, newItem.id);
}
}
}
function delById(req: any, list: any[]) {
for (let i = 0; i < list.length; i += 1) {
const item = list[i];
if (item.id === parseInt(req.params.id, 10)) {
list.splice(i, 1);
break;
}
if (item.children && item.children.length > 0) {
delById(req, item.children);
}
}
}
function findById(id: number, list: any[]): any {
for (const item of list) {
if (item.id === id) {
return item;
}
if (item.children && item.children.length > 0) {
const sub = findById(id, item.children);
if (sub !== null && sub !== undefined) {
return sub;
}
}
}
return null;
}
export default {
buildMock(options: { name: string; copyTimes: number; list: any[]; idGenerator: number }) {
const name = options.name;
if (!options.copyTimes) {
options.copyTimes = 29;
}
const list: any[] = [];
for (let i = 0; i < options.copyTimes; i += 1) {
copyList(options.list, list, options);
}
options.list = list;
return [
{
path: `/mock/${name}/page`,
method: 'post',
handle(req: any) {
let data = [...list];
let limit = 20;
let offset = 0;
for (const item of list) {
if (item.children && item.children.length === 0) {
item.hasChildren = false;
item.lazy = false;
}
}
let orderAsc: any;
let orderProp: any;
if (req && req.body) {
const { page, query, sort } = req.body;
if (page.limit) {
limit = parseInt(page.limit, 10);
}
if (page.offset) {
offset = parseInt(page.offset, 10);
}
orderProp = sort.prop;
orderAsc = sort.asc;
if (Object.keys(query).length > 0) {
// eslint-disable-next-line complexity
data = list.filter(item => {
let allFound = true; // 是否所有条件都符合
// eslint-disable-next-line guard-for-in
for (const key in query) {
// 判定某一个条件
const value = query[key];
if (value === undefined || value === null || value === '') {
// eslint-disable-next-line no-continue
continue;
}
if (value instanceof Array) {
// 如果条件中的value是数组的话只要查到一个就行
if (value.length === 0) {
// eslint-disable-next-line no-continue
continue;
}
let found = false;
for (const i of value) {
if (item[key] instanceof Array) {
// eslint-disable-next-line max-depth
for (const j of item[key]) {
// eslint-disable-next-line max-depth
if (i === j) {
found = true;
break;
}
}
// eslint-disable-next-line max-depth
if (found) {
break;
}
} else if (item[key] === i || (typeof item[key] === 'string' && item[key].indexOf(`${i}`) >= 0)) {
found = true;
break;
}
if (found) {
break;
}
}
if (!found) {
allFound = false;
}
} else if (value instanceof Object) {
// eslint-disable-next-line max-depth,guard-for-in
for (const key2 in value) {
const v = value[key2];
if (v && item[key] && v !== item[key][key2]) {
return false;
}
}
} else if (item[key] !== value) {
allFound = false;
}
}
return allFound;
});
}
}
const start = offset;
let end = offset + limit;
if (data.length < end) {
end = data.length;
}
if (orderProp) {
// 排序
data.sort((a, b) => {
let ret = 0;
if (a[orderProp] > b[orderProp]) {
ret = 1;
} else {
ret = -1;
}
return orderAsc ? ret : -ret;
});
}
const records = data.slice(start, end);
const lastOffset = data.length - (data.length % limit);
if (offset > lastOffset) {
offset = lastOffset;
}
return {
code: 200,
message: 'success',
data: {
records,
total: data.length,
limit,
offset
}
};
}
},
{
path: `/mock/${name}/get`,
method: 'get',
handle(req: any) {
let id = req.params.id;
id = parseInt(id, 10);
let current = null;
for (const item of list) {
if (item.id === id) {
current = item;
break;
}
}
return {
code: 200,
message: 'success',
data: current
};
}
},
{
path: `/mock/${name}/add`,
method: 'post',
handle(req: any) {
req.body.id = options.idGenerator;
options.idGenerator += 1;
list.unshift(req.body);
return {
code: 200,
message: 'success',
data: req.body.id
};
}
},
{
path: `/mock/${name}/update`,
method: 'post',
handle(req: any) {
const item = findById(req.body.id, list);
if (item) {
Object.assign(item, req.body);
}
return {
code: 200,
message: 'success',
data: null
};
}
},
{
path: `/mock/${name}/delete`,
method: 'post',
handle(req: any) {
delById(req, list);
return {
code: 200,
message: 'success',
data: null
};
}
},
{
path: `/mock/${name}/batchDelete`,
method: 'post',
handle(req: any) {
const ids = req.body.ids;
for (let i = list.length - 1; i >= 0; i -= 1) {
const item = list[i];
if (ids.indexOf(item.id) >= 0) {
list.splice(i, 1);
}
}
return {
code: 200,
message: 'success',
data: null
};
}
},
{
path: `/mock/${name}/all`,
method: 'post',
handle() {
return {
code: 200,
message: 'success',
data: list
};
}
}
];
}
};

5
mock/api/crud/index.ts Normal file
View File

@@ -0,0 +1,5 @@
import demo from './modules/demo';
import headerGroup from './modules/header-group';
const crudApis = [...demo, ...headerGroup];
export default crudApis;

View File

@@ -0,0 +1,55 @@
import type { MethodType, MockMethod } from 'vite-plugin-mock';
import mockBase from '../base';
const options: any = {
name: 'crud/demo',
idGenerator: 0
};
const list: any[] = [
{
select: '1',
text: '文本测试',
copyable: '文本可复制',
avatar: 'http://greper.handsfree.work/extends/avatar.jpg',
richtext: '富文本',
datetime: '2023-01-30 11:11:11'
},
{
select: '2'
},
{
select: '0'
}
];
options.list = list;
const mockedApis = mockBase.buildMock(options);
const apis: MockMethod[] = [
{
url: `/mock/${options.name}/dict`,
method: 'get',
response: () => {
return {
code: 200,
message: '',
data: [
{ value: '0', label: '关', color: 'warning' },
{ value: '1', label: '开', color: 'success' },
{ value: '2', label: '停' }
]
};
}
}
];
for (const mockedApi of mockedApis) {
apis.push({
url: mockedApi.path,
method: mockedApi.method as MethodType,
response: (request: any) => {
return mockedApi.handle(request);
}
});
}
export default apis;

View File

@@ -0,0 +1,45 @@
import type { MethodType, MockMethod } from 'vite-plugin-mock';
import mockBase from '../base';
const options: any = {
name: 'crud/header-group',
idGenerator: 0
};
const list: any[] = [
{
name: '张三',
age: 18,
province: '广东省',
city: '深圳市',
county: '南山区',
street: '粤海街道'
},
{
name: '李四',
age: 26,
province: '浙江省',
city: '杭州市',
county: '西湖区',
street: '西湖街道'
},
{
name: '王五',
age: 24
}
];
options.list = list;
const mockedApis = mockBase.buildMock(options);
const apis: MockMethod[] = [];
for (const mockedApi of mockedApis) {
apis.push({
url: mockedApi.path,
method: mockedApi.method as MethodType,
response: (request: any) => {
return mockedApi.handle(request);
}
});
}
export default apis;

View File

@@ -1,5 +1,6 @@
import auth from './auth';
import route from './route';
import management from './management';
import crud from './crud';
export default [...auth, ...route, ...management];
export default [...auth, ...route, ...management, ...crud];