123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433 |
- import mockjs from 'mockjs';
- import { format, delay } from 'roadhog-api-doc';
- import { campusList } from './mock/campus';
- import { terminalList } from './mock/terminal';
- import { merchantList } from './mock/merchant';
- import { resourceList } from './mock/resource';
- import { groupList } from './mock/group';
- import { tagList } from './mock/tag';
- import { wareList } from './mock/ware';
- import { lessonList } from './mock/lesson';
- import { courseList } from './mock/course';
- import { supportList } from './mock/support';
- import { comboList } from './mock/combo';
- import { productList } from './mock/product';
- import { merchantProductList } from './mock/mproduct';
- import { orderList } from './mock/order';
- import { signature } from './mock/signature';
- import { cmsUserList } from './mock/cmsUser';
- import * as api from './src/utils/api';
- // mock数据持久化
- global.campusList = campusList;
- global.terminalList = terminalList;
- global.merchantList = merchantList;
- global.resourceList = resourceList;
- global.groupList = groupList;
- global.tagList = tagList;
- global.wareList = wareList;
- global.lessonList = lessonList;
- global.courseList = courseList; //课程数据,已整合到product接口,废弃
- global.supportList = supportList; //配套数据,已整合到product接口,废弃
- global.comboList = comboList; //课程包数据,已整合到product接口,废弃
- global.productList = productList;
- global.orderList = orderList;
- global.merchantProductList = merchantProductList;
- global.signature = signature;
- global.cmsUserList = cmsUserList;
- // 操作成功响应内容
- const SUCCESS = { code: 200, success: true, message: null };
- // 资源未找到响应内容
- const NOTFOUND = { code: 404, message: '资源未找到!' };
- // 查询
- const query = (dataset, params) => {
- const pageSize = parseInt(params.pageSize) || 10;
- const pageNo = parseInt(params.pageNo) || 1;
- delete params['pageSize'];
- delete params['pageNo'];
- const queryKeys = Object.keys(params);
- const newDataset = dataset.filter(item => {
- let flag = true;
- for (let key of queryKeys) {
- if (item[key] !== params[key]) {
- flag = false;
- break;
- }
- }
- return flag;
- });
- const totalSize = newDataset.length || 0;
- const list = newDataset.slice(pageSize * (pageNo - 1), pageSize * pageNo);
- return {
- ...SUCCESS,
- data: { pageSize, pageNo, totalSize: newDataset.length, list }
- };
- }
- // 添加
- const create = (dataset, params) => {
- const last = dataset[dataset.length - 1];
- const data = { ...params, id: last.id + 1 };
- dataset.push(data);
- return {
- ...SUCCESS,
- data,
- };
- }
- // 更新
- const update = (dataset, params) => {
- for (let index in dataset) {
- if (dataset[index].id === params.id) {
- dataset[index] = { ...dataset[index], ...params };
- }
- }
- return {
- ...SUCCESS,
- }
- }
- const queryArray = (array, key, keyAlias = 'key') => {
- if (!(array instanceof Array)) {
- return null
- }
- let data
- for (let item of array) {
- if (item[keyAlias] === key) {
- data = item
- break
- }
- }
- if (data) {
- return data
- }
- return null
- }
- // 查询一条
- const queryOne = (dataset, id, res) => {
- const target = queryArray(dataset, id, 'id');
- if (target) {
- res.send({ ...SUCCESS, data: { ...target } });
- } else {
- res.send({ ...NOTFOUND, data: {} });
- }
- }
- // 删除
- const remove = (dataset, id, res) => {
- const data = queryArray(dataset, id, 'id');
- if (data) {
- dataset.map(item => { item.id === data.id ? item.status = 'DEL' : null });
- res.send(SUCCESS);
- } else {
- res.send(NOTFOUND);
- }
- }
- // mock数据
- const proxy = {
- // 渠道-产品
- [`GET ${api.mproducts}`]: (req, res) => {
- console.log(`[GET][${api.mproducts}]`, req.query);
- res.send(query(global.merchantProductList, req.query));
- },
- // 资源
- [`POST ${api.resource.replace('/:id', '')}`]: (req, res) => {
- console.log(`[POST][${api.resource}]`, req.body);
- res.send(create(global.resourceList, req.body));
- },
- [`DELETE ${api.resource}`]: (req, res) => {
- console.log(`[DELETE][${api.resource}]`, req.params);
- const { id } = req.params;
- remove(global.resourceList, id, res);
- },
- [`PUT ${api.resource.replace('/:id', '')}`]: (req, res) => {
- console.log(`[PUT][${api.resource}]`, req.body);
- res.send(update(global.resourceList, req.body));
- },
- [`GET ${api.resources}`]: (req, res) => {
- console.log(`[GET][${api.resources}]`, req.query);
- res.send(query(global.resourceList, req.query));
- },
- [`GET ${api.signature}`]: (req, res) => {
- console.log(`[GET][${api.signature}]`, req.query);
- res.send({ ...SUCCESS, data: global.signature });
- },
- // 校区
- [`GET ${api.campuses}`]: (req, res) => {
- res.send(query(global.campusList, req.query));
- },
- [`POST ${api.campus.replace('/:id', '')}`]: (req, res) => {
- res.send(create(global.campusList, req.body));
- },
- [`PUT ${api.campus.replace('/:id', '')}`]: (req, res) => {
- res.send(update(global.campusList, req.body));
- },
- // 终端
- [`GET ${api.terminals}`]: (req, res) => {
- res.send(query(global.terminalList, req.query));
- },
- [`POST ${api.terminal.replace('/:id', '')}`]: (req, res) => {
- res.send(create(global.terminalList, req.body));
- },
- [`PUT ${api.terminal.replace('/:id', '')}`]: (req, res) => {
- res.send(update(global.terminalList, req.body));
- },
- [`DELETE ${api.terminal}`]: (req, res) => {
- const { id } = req.params;
- remove(global.terminalList, id, res);
- },
- // 厂商
- [`POST ${api.merchant.replace('/:id', '')}`]: (req, res) => {
- res.send(create(global.merchantList, req.body));
- },
- [`DELETE ${api.merchant}`]: (req, res) => {
- const { id } = req.params;
- remove(global.merchantList, id, res);
- },
- [`PUT ${api.merchant.replace('/:id', '')}`]: (req, res) => {
- res.send(update(global.merchantList, req.body));
- },
- [`GET ${api.merchants}`]: (req, res) => {
- res.send(query(global.merchantList, req.query));
- },
- [`GET ${api.merchant}`]: (req, res) => {
- const { id } = req.params;
- queryOne(global.merchantList, id, res);
- },
- // 标签组
- [`POST ${api.group.replace('/:id', '')}`]: (req, res) => {
- res.send(create(global.groupList, req.body));
- },
- [`DELETE ${api.group}`]: (req, res) => {
- const { id } = req.params;
- remove(global.groupList, id, res);
- },
- [`PUT ${api.group.replace('/:id', '')}`]: (req, res) => {
- res.send(update(global.groupList, req.body));
- },
- [`GET ${api.groups}`]: (req, res) => {
- console.log(`[${api.groups}]`, req.query);
- res.send(query(global.groupList, req.query));
- },
- [`GET ${api.group}`]: (req, res) => {
- const { id } = req.params;
- queryOne(global.groupList, id, res);
- },
- // 标签
- [`POST ${api.tag.replace('/:id', '')}`]: (req, res) => {
- res.send(create(global.tagList, req.body));
- },
- [`DELETE ${api.tag}`]: (req, res) => {
- const { id } = req.params;
- remove(global.tagList, id, res);
- },
- [`PUT ${api.tag.replace('/:id', '')}`]: (req, res) => {
- res.send(update(global.tagList, req.body));
- },
- [`GET ${api.tags}`]: (req, res) => {
- console.log(`[${api.tags}]`, req.query);
- res.send(query(global.tagList, req.query));
- },
- [`GET ${api.tag}`]: (req, res) => {
- console.log(`[${api.tag}]`, req.params);
- const { id } = req.params;
- queryOne(global.tagList, id, res);
- },
- // 课件
- [`POST ${api.ware.replace('/:id', '')}`]: (req, res) => {
- console.log(`[POST][${api.ware}]`, req.body);
- res.send(create(global.wareList, req.body));
- },
- [`DELETE ${api.ware}`]: (req, res) => {
- console.log(`[DELETE][${api.ware}]`, req.params);
- const { id } = req.params;
- remove(global.wareList, id, res);
- },
- [`PUT ${api.ware.replace('/:id', '')}`]: (req, res) => {
- console.log(`[PUT][${api.ware}]`, req.body);
- res.send(update(global.wareList, req.body));
- },
- [`GET ${api.wares}`]: (req, res) => {
- console.log(`[GET][${api.wares}]`, req.query);
- res.send(query(global.wareList, req.query));
- },
- [`GET ${api.ware}`]: (req, res) => {
- console.log(`[GET][${api.ware}]`, req.params);
- const { id } = req.params;
- queryOne(global.wareList, id, res);
- },
- // 课
- [`POST ${api.lesson.replace('/:id', '')}`]: (req, res) => {
- console.log(`[POST][${api.lesson}]`, req.body);
- res.send(create(global.lessonList, req.body));
- },
- [`DELETE ${api.lesson}`]: (req, res) => {
- console.log(`[DELETE][${api.lesson}]`, req.params);
- const { id } = req.params;
- remove(global.lessonList, id, res);
- },
- [`PUT ${api.lesson.replace('/:id', '')}`]: (req, res) => {
- console.log(`[PUT][${api.lesson}]`, req.body);
- res.send(update(global.lessonList, req.body));
- },
- [`GET ${api.lessons}`]: (req, res) => {
- console.log(`[GET][${api.lessons}]`, req.query);
- res.send(query(global.lessonList, req.query));
- },
- [`GET ${api.lesson}`]: (req, res) => {
- console.log(`[GET][${api.lesson}]`, req.params);
- const { id } = req.params;
- queryOne(global.lessonList, id, res);
- },
- // ################## 已被product接口替代 ###################
- // 课程
- /*
- [`POST ${api.course.replace('/:id', '')}`]: (req, res) => {
- console.log(`[POST][${api.course}]`, req.body);
- res.send(create(global.courseList, req.body));
- },
- [`DELETE ${api.course}`]: (req, res) => {
- console.log(`[DELETE][${api.course}]`, req.params);
- const { id } = req.params;
- remove(global.courseList, id, res);
- },
- [`PUT ${api.course.replace('/:id', '')}`]: (req, res) => {
- console.log(`[PUT][${api.course}]`, req.body);
- res.send(update(global.courseList, req.body));
- },
- [`GET ${api.courses}`]: (req, res) => {
- console.log(`[GET][${api.courses}]`, req.query);
- res.send(query(global.courseList, req.query));
- },
- [`GET ${api.course}`]: (req, res) => {
- console.log(`[GET][${api.course}]`, req.params);
- const { id } = req.params;
- queryOne(global.courseList, id, res);
- },
- // 周边
- [`POST ${api.support.replace('/:id', '')}`]: (req, res) => {
- console.log(`[POST][${api.support}]`, req.body);
- res.send(create(global.supportList, req.body));
- },
- [`DELETE ${api.support}`]: (req, res) => {
- console.log(`[DELETE][${api.support}]`, req.params);
- const { id } = req.params;
- remove(global.supportList, id, res);
- },
- [`PUT ${api.support.replace('/:id', '')}`]: (req, res) => {
- console.log(`[PUT][${api.support}]`, req.body);
- res.send(update(global.supportList, req.body));
- },
- [`GET ${api.supports}`]: (req, res) => {
- console.log(`[GET][${api.supports}]`, req.query);
- res.send(query(global.supportList, req.query));
- },
- [`GET ${api.support}`]: (req, res) => {
- console.log(`[GET][${api.support}]`, req.params);
- const { id } = req.params;
- queryOne(global.supportList, id, res);
- },
- // 课程包
- [`POST ${api.combo.replace('/:id', '')}`]: (req, res) => {
- console.log(`[POST][${api.combo}]`, req.body);
- res.send(create(global.comboList, req.body));
- },
- [`DELETE ${api.combo}`]: (req, res) => {
- console.log(`[DELETE][${api.combo}]`, req.params);
- const { id } = req.params;
- remove(global.comboList, id, res);
- },
- [`PUT ${api.combo.replace('/:id', '')}`]: (req, res) => {
- console.log(`[PUT][${api.combo}]`, req.body);
- res.send(update(global.comboList, req.body));
- },
- [`GET ${api.combos}`]: (req, res) => {
- console.log(`[GET][${api.combos}]`, req.query);
- res.send(query(global.comboList, req.query));
- },
- [`GET ${api.combo}`]: (req, res) => {
- console.log(`[GET][${api.combo}]`, req.params);
- const { id } = req.params;
- queryOne(global.comboList, id, res);
- },
- */
- // ##############################################
- // 产品接口
- [`POST ${api.product}`]: (req, res) => {
- console.log(`[POST][${api.product}]`, req.body);
- res.send(create(global.productList, req.body));
- },
- [`DELETE ${api.product}/:id`]: (req, res) => {
- console.log(`[DELETE][${api.product}]`, req.params);
- const { id } = req.params;
- remove(global.productList, id, res);
- },
- [`PUT ${api.product}/:id`]: (req, res) => {
- console.log(`[PUT][${api.product}]`, req.body);
- res.send(update(global.productList, req.body));
- },
- [`GET ${api.product}`]: (req, res) => {
- console.log(`[GET][${api.product}]`, req.query);
- res.send(query(global.productList, req.query));
- },
- [`GET ${api.product}/:id`]: (req, res) => {
- console.log(`[GET][${api.product}]`, req.params);
- const { id } = req.params;
- queryOne(global.productList, id, res);
- },
- // 订单
- [`POST ${api.order.replace('/:id', '')}`]: (req, res) => {
- console.log(`[POST][${api.order}]`, req.body);
- res.send(create(global.orderList, req.body));
- },
- [`DELETE ${api.order}`]: (req, res) => {
- console.log(`[DELETE][${api.order}]`, req.params);
- const { id } = req.params;
- remove(global.orderList, id, res);
- },
- [`PUT ${api.order.replace('/:id', '')}`]: (req, res) => {
- console.log(`[PUT][${api.order}]`, req.body);
- res.send(update(global.orderList, req.body));
- },
- [`GET ${api.orders}`]: (req, res) => {
- console.log(`[GET][${api.orders}]`, req.query);
- res.send(query(global.orderList, req.query));
- },
- [`GET ${api.order}`]: (req, res) => {
- console.log(`[GET][${api.order}]`, req.params);
- const { id } = req.params;
- queryOne(global.orderList, id, res);
- },
- // cms用户
- [`POST ${api.cmsUser.replace('/:id', '')}`]: (req, res) => {
- console.log(`[POST][${api.cmsUser}]`, req.body);
- res.send(create(global.cmsUserList, req.body));
- },
- [`DELETE ${api.cmsUser}`]: (req, res) => {
- console.log(`[DELETE][${api.cmsUser}]`, req.params);
- const { id } = req.params;
- remove(global.cmsUserList, id, res);
- },
- [`PUT ${api.cmsUser.replace('/:id', '')}`]: (req, res) => {
- console.log(`[PUT][${api.cmsUser}]`, req.body);
- res.send(update(global.cmsUserList, req.body));
- },
- [`GET ${api.cmsUsers}`]: (req, res) => {
- console.log(`[GET][${api.cmsUsers}]`, req.query);
- res.send(query(global.cmsUserList, req.query));
- },
- };
- // 是否禁用代理
- const noProxy = process.env.NO_PROXY === 'true';
- // 根据是否禁用代理来选择是mock数据还是真实接口
- export default noProxy ? {} : delay(proxy, 500);
|