2019-04-30 11:11:25 +08:00
|
|
|
const DB = require('./sample.db.js');
|
|
|
|
|
2021-01-10 11:40:26 +08:00
|
|
|
module.exports = function (req, res) {
|
|
|
|
const pathname = (req.originalUrl || req.url).replace(
|
|
|
|
/^\/(api\/mock2|api)\/|\?.*$/g,
|
|
|
|
''
|
|
|
|
);
|
|
|
|
|
|
|
|
if (
|
|
|
|
pathname === 'sample' &&
|
|
|
|
(req.method === 'POST' || req.method === 'PUT')
|
|
|
|
) {
|
|
|
|
return store(req, res);
|
|
|
|
} else if (/sample\/(\d+(?:,\d+)*)?$/.test(pathname)) {
|
|
|
|
if (req.method === 'POST' || req.method === 'PUT') {
|
|
|
|
return update(req, res, RegExp.$1);
|
|
|
|
} else if (req.method === 'DELETE') {
|
|
|
|
return del(req, res, RegExp.$1);
|
|
|
|
} else if (req.method === 'GET') {
|
|
|
|
return show(req, res, RegExp.$1);
|
2019-04-30 11:11:25 +08:00
|
|
|
}
|
2021-01-10 11:40:26 +08:00
|
|
|
} else if (pathname === 'sample/bulkUpdate') {
|
|
|
|
return bulkUpdate(req, res);
|
|
|
|
} else if (pathname === 'sample/bulkUpdate2') {
|
|
|
|
return bulkUpdate2(req, res);
|
|
|
|
}
|
2019-04-30 11:11:25 +08:00
|
|
|
|
2021-01-10 11:40:26 +08:00
|
|
|
return index(req, res);
|
2019-10-31 15:39:32 +08:00
|
|
|
};
|
2019-04-30 11:11:25 +08:00
|
|
|
|
|
|
|
function index(req, res) {
|
2021-01-10 11:40:26 +08:00
|
|
|
const perPage = parseInt(req.query.perPage, 10);
|
|
|
|
const page = req.query.page || 1;
|
|
|
|
let items = DB.concat();
|
|
|
|
|
|
|
|
if (req.query.keywords) {
|
|
|
|
const keywords = req.query.keywords;
|
|
|
|
items = items.filter(function (item) {
|
|
|
|
return ~JSON.stringify(item).indexOf(keywords);
|
|
|
|
});
|
|
|
|
}
|
2019-04-30 11:11:25 +08:00
|
|
|
|
2021-01-10 11:40:26 +08:00
|
|
|
if (req.query.engine) {
|
|
|
|
const keywords = req.query.engine;
|
|
|
|
items = items.filter(function (item) {
|
|
|
|
return ~JSON.stringify(item.engine).indexOf(keywords);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (req.query.orderBy) {
|
|
|
|
const field = req.query.orderBy;
|
|
|
|
const direction = req.query.orderDir === 'desc' ? -1 : 1;
|
|
|
|
items = items.sort(function (a, b) {
|
|
|
|
a = String(a[field]);
|
|
|
|
b = String(b[field]);
|
|
|
|
|
|
|
|
if (/^\d+$/.test(a) && /^\d+$/.test(b)) {
|
|
|
|
a = parseInt(a, 10);
|
|
|
|
b = parseInt(b, 10);
|
|
|
|
return (a > b ? 1 : a < b ? -1 : 0) * direction;
|
|
|
|
}
|
|
|
|
|
|
|
|
return a.localeCompare(b) * direction;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
let response = () =>
|
|
|
|
res.json({
|
|
|
|
status: 0,
|
|
|
|
msg: 'ok',
|
|
|
|
data: {
|
|
|
|
count: items.length,
|
|
|
|
rows: perPage
|
|
|
|
? items.splice((page - 1) * perPage, perPage)
|
|
|
|
: items.concat()
|
|
|
|
}
|
|
|
|
});
|
2019-04-30 11:11:25 +08:00
|
|
|
|
2021-01-10 11:40:26 +08:00
|
|
|
if (req.query.waitSeconds) {
|
|
|
|
return setTimeout(response, parseInt(req.query.waitSeconds, 10) * 1000);
|
|
|
|
}
|
2019-10-31 15:39:32 +08:00
|
|
|
|
2021-01-10 11:40:26 +08:00
|
|
|
return response();
|
2019-04-30 11:11:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function store(req, res) {
|
2021-01-10 11:40:26 +08:00
|
|
|
const data = Object.assign({}, req.body);
|
|
|
|
|
|
|
|
data.id = DB.length ? DB[DB.length - 1].id + 1 : 1;
|
2019-04-30 11:11:25 +08:00
|
|
|
|
2021-01-10 11:40:26 +08:00
|
|
|
DB.push(data);
|
2019-04-30 11:11:25 +08:00
|
|
|
|
2021-01-10 11:40:26 +08:00
|
|
|
return res.json({
|
|
|
|
status: 0,
|
|
|
|
msg: '新增成功'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function show(req, res, id) {
|
|
|
|
const idx = DB.findIndex(function (item) {
|
|
|
|
return item.id == id;
|
|
|
|
});
|
2019-04-30 11:11:25 +08:00
|
|
|
|
2021-01-10 11:40:26 +08:00
|
|
|
if (!~idx) {
|
2019-04-30 11:11:25 +08:00
|
|
|
return res.json({
|
2021-01-10 11:40:26 +08:00
|
|
|
status: 404,
|
|
|
|
msg: '保存失败,数据可能已被删除!'
|
2019-04-30 11:11:25 +08:00
|
|
|
});
|
2021-01-10 11:40:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const item = Object.assign({}, DB[idx], req.body);
|
|
|
|
res.json({
|
|
|
|
status: 0,
|
|
|
|
data: item
|
|
|
|
});
|
2019-04-30 11:11:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function update(req, res, id) {
|
2021-01-10 11:40:26 +08:00
|
|
|
const ids = id.split(',');
|
|
|
|
|
|
|
|
if (
|
|
|
|
!ids.every(function (id) {
|
|
|
|
const idx = DB.findIndex(function (item) {
|
|
|
|
return item.id == id;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!~idx) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const item = Object.assign({}, DB[idx], req.body);
|
|
|
|
item.id = id;
|
|
|
|
DB.splice(idx, 1, item);
|
|
|
|
return true;
|
|
|
|
})
|
|
|
|
) {
|
2019-04-30 11:11:25 +08:00
|
|
|
return res.json({
|
2021-01-10 11:40:26 +08:00
|
|
|
status: 404,
|
|
|
|
msg: '保存失败,数据可能已被删除!'
|
2019-04-30 11:11:25 +08:00
|
|
|
});
|
2021-01-10 11:40:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return res.json({
|
|
|
|
status: 0,
|
|
|
|
msg: '保存成功'
|
|
|
|
});
|
2019-04-30 11:11:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function del(req, res, id) {
|
2021-01-10 11:40:26 +08:00
|
|
|
const ids = id.split(',');
|
|
|
|
console.log(ids);
|
|
|
|
|
|
|
|
if (
|
|
|
|
!ids.every(function (id) {
|
|
|
|
const idx = DB.findIndex(function (item) {
|
|
|
|
return item.id == id;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!~idx) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
DB.splice(idx, 1);
|
|
|
|
return true;
|
|
|
|
})
|
|
|
|
) {
|
2019-04-30 11:11:25 +08:00
|
|
|
return res.json({
|
2021-01-10 11:40:26 +08:00
|
|
|
status: 404,
|
|
|
|
msg: '保存失败,数据可能已被删除!'
|
2019-04-30 11:11:25 +08:00
|
|
|
});
|
2021-01-10 11:40:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return res.json({
|
|
|
|
status: 0,
|
|
|
|
msg: '删除成功'
|
|
|
|
});
|
2019-04-30 11:11:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function bulkUpdate(req, res) {
|
2021-01-10 11:40:26 +08:00
|
|
|
const rowDiff = req.body.rowsDiff;
|
|
|
|
const ids = req.body.ids ? req.body.ids.split(',') : [];
|
|
|
|
if (
|
|
|
|
!ids.length ||
|
|
|
|
!ids.every(function (id, index) {
|
|
|
|
const idx = DB.findIndex(function (item) {
|
|
|
|
return item.id == id;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!~idx) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const item = Object.assign({}, DB[idx], rowDiff[index]);
|
|
|
|
item.id = id;
|
|
|
|
DB.splice(idx, 1, item);
|
|
|
|
return true;
|
|
|
|
})
|
|
|
|
) {
|
2019-04-30 11:11:25 +08:00
|
|
|
return res.json({
|
2021-01-10 11:40:26 +08:00
|
|
|
status: 404,
|
|
|
|
msg: '保存失败,数据可能已被删除!'
|
2019-04-30 11:11:25 +08:00
|
|
|
});
|
2021-01-10 11:40:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return res.json({
|
|
|
|
status: 0,
|
|
|
|
msg: '保存成功'
|
|
|
|
});
|
2019-04-30 11:11:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function bulkUpdate2(req, res) {
|
2021-01-10 11:40:26 +08:00
|
|
|
const data = Object.assign({}, req.body);
|
|
|
|
|
|
|
|
delete data.ids;
|
|
|
|
|
|
|
|
const ids = req.body.ids ? req.body.ids.split(',') : [];
|
|
|
|
if (
|
|
|
|
!ids.length ||
|
|
|
|
!ids.every(function (id, index) {
|
|
|
|
const idx = DB.findIndex(function (item) {
|
|
|
|
return item.id == id;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!~idx) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const item = Object.assign({}, DB[idx], data);
|
|
|
|
item.id = id;
|
|
|
|
DB.splice(idx, 1, item);
|
|
|
|
return true;
|
|
|
|
})
|
|
|
|
) {
|
2019-04-30 11:11:25 +08:00
|
|
|
return res.json({
|
2021-01-10 11:40:26 +08:00
|
|
|
status: 404,
|
|
|
|
msg: '保存失败,数据可能已被删除!'
|
2019-04-30 11:11:25 +08:00
|
|
|
});
|
2021-01-10 11:40:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return res.json({
|
|
|
|
status: 0,
|
|
|
|
msg: '保存成功'
|
|
|
|
});
|
2019-04-30 11:11:25 +08:00
|
|
|
}
|