.roadhogrc.mock.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. import mockjs from 'mockjs';
  2. import { format, delay } from 'roadhog-api-doc';
  3. import { campusList } from './mock/campus';
  4. import { terminalList } from './mock/terminal';
  5. import { merchantList } from './mock/merchant';
  6. import { resourceList } from './mock/resource';
  7. import { groupList } from './mock/group';
  8. import { tagList } from './mock/tag';
  9. import { wareList } from './mock/ware';
  10. import { lessonList } from './mock/lesson';
  11. import { courseList } from './mock/course';
  12. import { supportList } from './mock/support';
  13. import { comboList } from './mock/combo';
  14. import { productList } from './mock/product';
  15. import { merchantProductList } from './mock/mproduct';
  16. import { orderList } from './mock/order';
  17. import { signature } from './mock/signature';
  18. import { cmsUserList } from './mock/cmsUser';
  19. import * as api from './src/utils/api';
  20. // mock数据持久化
  21. global.campusList = campusList;
  22. global.terminalList = terminalList;
  23. global.merchantList = merchantList;
  24. global.resourceList = resourceList;
  25. global.groupList = groupList;
  26. global.tagList = tagList;
  27. global.wareList = wareList;
  28. global.lessonList = lessonList;
  29. global.courseList = courseList; //课程数据,已整合到product接口,废弃
  30. global.supportList = supportList; //配套数据,已整合到product接口,废弃
  31. global.comboList = comboList; //课程包数据,已整合到product接口,废弃
  32. global.productList = productList;
  33. global.orderList = orderList;
  34. global.merchantProductList = merchantProductList;
  35. global.signature = signature;
  36. global.cmsUserList = cmsUserList;
  37. // 操作成功响应内容
  38. const SUCCESS = { code: 200, success: true, message: null };
  39. // 资源未找到响应内容
  40. const NOTFOUND = { code: 404, message: '资源未找到!' };
  41. // 查询
  42. const query = (dataset, params) => {
  43. const pageSize = parseInt(params.pageSize) || 10;
  44. const pageNo = parseInt(params.pageNo) || 1;
  45. delete params['pageSize'];
  46. delete params['pageNo'];
  47. const queryKeys = Object.keys(params);
  48. const newDataset = dataset.filter(item => {
  49. let flag = true;
  50. for (let key of queryKeys) {
  51. if (item[key] !== params[key]) {
  52. flag = false;
  53. break;
  54. }
  55. }
  56. return flag;
  57. });
  58. const totalSize = newDataset.length || 0;
  59. const list = newDataset.slice(pageSize * (pageNo - 1), pageSize * pageNo);
  60. return {
  61. ...SUCCESS,
  62. data: { pageSize, pageNo, totalSize: newDataset.length, list }
  63. };
  64. }
  65. // 添加
  66. const create = (dataset, params) => {
  67. const last = dataset[dataset.length - 1];
  68. const data = { ...params, id: last.id + 1 };
  69. dataset.push(data);
  70. return {
  71. ...SUCCESS,
  72. data,
  73. };
  74. }
  75. // 更新
  76. const update = (dataset, params) => {
  77. for (let index in dataset) {
  78. if (dataset[index].id === params.id) {
  79. dataset[index] = { ...dataset[index], ...params };
  80. }
  81. }
  82. return {
  83. ...SUCCESS,
  84. }
  85. }
  86. const queryArray = (array, key, keyAlias = 'key') => {
  87. if (!(array instanceof Array)) {
  88. return null
  89. }
  90. let data
  91. for (let item of array) {
  92. if (item[keyAlias] === key) {
  93. data = item
  94. break
  95. }
  96. }
  97. if (data) {
  98. return data
  99. }
  100. return null
  101. }
  102. // 查询一条
  103. const queryOne = (dataset, id, res) => {
  104. const target = queryArray(dataset, id, 'id');
  105. if (target) {
  106. res.send({ ...SUCCESS, data: { ...target } });
  107. } else {
  108. res.send({ ...NOTFOUND, data: {} });
  109. }
  110. }
  111. // 删除
  112. const remove = (dataset, id, res) => {
  113. const data = queryArray(dataset, id, 'id');
  114. if (data) {
  115. dataset.map(item => { item.id === data.id ? item.status = 'DEL' : null });
  116. res.send(SUCCESS);
  117. } else {
  118. res.send(NOTFOUND);
  119. }
  120. }
  121. // mock数据
  122. const proxy = {
  123. // 渠道-产品
  124. [`GET ${api.mproducts}`]: (req, res) => {
  125. console.log(`[GET][${api.mproducts}]`, req.query);
  126. res.send(query(global.merchantProductList, req.query));
  127. },
  128. // 资源
  129. [`POST ${api.resource.replace('/:id', '')}`]: (req, res) => {
  130. console.log(`[POST][${api.resource}]`, req.body);
  131. res.send(create(global.resourceList, req.body));
  132. },
  133. [`DELETE ${api.resource}`]: (req, res) => {
  134. console.log(`[DELETE][${api.resource}]`, req.params);
  135. const { id } = req.params;
  136. remove(global.resourceList, id, res);
  137. },
  138. [`PUT ${api.resource.replace('/:id', '')}`]: (req, res) => {
  139. console.log(`[PUT][${api.resource}]`, req.body);
  140. res.send(update(global.resourceList, req.body));
  141. },
  142. [`GET ${api.resources}`]: (req, res) => {
  143. console.log(`[GET][${api.resources}]`, req.query);
  144. res.send(query(global.resourceList, req.query));
  145. },
  146. [`GET ${api.signature}`]: (req, res) => {
  147. console.log(`[GET][${api.signature}]`, req.query);
  148. res.send({ ...SUCCESS, data: global.signature });
  149. },
  150. // 校区
  151. [`GET ${api.campuses}`]: (req, res) => {
  152. res.send(query(global.campusList, req.query));
  153. },
  154. [`POST ${api.campus.replace('/:id', '')}`]: (req, res) => {
  155. res.send(create(global.campusList, req.body));
  156. },
  157. [`PUT ${api.campus.replace('/:id', '')}`]: (req, res) => {
  158. res.send(update(global.campusList, req.body));
  159. },
  160. // 终端
  161. [`GET ${api.terminals}`]: (req, res) => {
  162. res.send(query(global.terminalList, req.query));
  163. },
  164. [`POST ${api.terminal.replace('/:id', '')}`]: (req, res) => {
  165. res.send(create(global.terminalList, req.body));
  166. },
  167. [`PUT ${api.terminal.replace('/:id', '')}`]: (req, res) => {
  168. res.send(update(global.terminalList, req.body));
  169. },
  170. [`DELETE ${api.terminal}`]: (req, res) => {
  171. const { id } = req.params;
  172. remove(global.terminalList, id, res);
  173. },
  174. // 厂商
  175. [`POST ${api.merchant.replace('/:id', '')}`]: (req, res) => {
  176. res.send(create(global.merchantList, req.body));
  177. },
  178. [`DELETE ${api.merchant}`]: (req, res) => {
  179. const { id } = req.params;
  180. remove(global.merchantList, id, res);
  181. },
  182. [`PUT ${api.merchant.replace('/:id', '')}`]: (req, res) => {
  183. res.send(update(global.merchantList, req.body));
  184. },
  185. [`GET ${api.merchants}`]: (req, res) => {
  186. res.send(query(global.merchantList, req.query));
  187. },
  188. [`GET ${api.merchant}`]: (req, res) => {
  189. const { id } = req.params;
  190. queryOne(global.merchantList, id, res);
  191. },
  192. // 标签组
  193. [`POST ${api.group.replace('/:id', '')}`]: (req, res) => {
  194. res.send(create(global.groupList, req.body));
  195. },
  196. [`DELETE ${api.group}`]: (req, res) => {
  197. const { id } = req.params;
  198. remove(global.groupList, id, res);
  199. },
  200. [`PUT ${api.group.replace('/:id', '')}`]: (req, res) => {
  201. res.send(update(global.groupList, req.body));
  202. },
  203. [`GET ${api.groups}`]: (req, res) => {
  204. console.log(`[${api.groups}]`, req.query);
  205. res.send(query(global.groupList, req.query));
  206. },
  207. [`GET ${api.group}`]: (req, res) => {
  208. const { id } = req.params;
  209. queryOne(global.groupList, id, res);
  210. },
  211. // 标签
  212. [`POST ${api.tag.replace('/:id', '')}`]: (req, res) => {
  213. res.send(create(global.tagList, req.body));
  214. },
  215. [`DELETE ${api.tag}`]: (req, res) => {
  216. const { id } = req.params;
  217. remove(global.tagList, id, res);
  218. },
  219. [`PUT ${api.tag.replace('/:id', '')}`]: (req, res) => {
  220. res.send(update(global.tagList, req.body));
  221. },
  222. [`GET ${api.tags}`]: (req, res) => {
  223. console.log(`[${api.tags}]`, req.query);
  224. res.send(query(global.tagList, req.query));
  225. },
  226. [`GET ${api.tag}`]: (req, res) => {
  227. console.log(`[${api.tag}]`, req.params);
  228. const { id } = req.params;
  229. queryOne(global.tagList, id, res);
  230. },
  231. // 课件
  232. [`POST ${api.ware.replace('/:id', '')}`]: (req, res) => {
  233. console.log(`[POST][${api.ware}]`, req.body);
  234. res.send(create(global.wareList, req.body));
  235. },
  236. [`DELETE ${api.ware}`]: (req, res) => {
  237. console.log(`[DELETE][${api.ware}]`, req.params);
  238. const { id } = req.params;
  239. remove(global.wareList, id, res);
  240. },
  241. [`PUT ${api.ware.replace('/:id', '')}`]: (req, res) => {
  242. console.log(`[PUT][${api.ware}]`, req.body);
  243. res.send(update(global.wareList, req.body));
  244. },
  245. [`GET ${api.wares}`]: (req, res) => {
  246. console.log(`[GET][${api.wares}]`, req.query);
  247. res.send(query(global.wareList, req.query));
  248. },
  249. [`GET ${api.ware}`]: (req, res) => {
  250. console.log(`[GET][${api.ware}]`, req.params);
  251. const { id } = req.params;
  252. queryOne(global.wareList, id, res);
  253. },
  254. // 课
  255. [`POST ${api.lesson.replace('/:id', '')}`]: (req, res) => {
  256. console.log(`[POST][${api.lesson}]`, req.body);
  257. res.send(create(global.lessonList, req.body));
  258. },
  259. [`DELETE ${api.lesson}`]: (req, res) => {
  260. console.log(`[DELETE][${api.lesson}]`, req.params);
  261. const { id } = req.params;
  262. remove(global.lessonList, id, res);
  263. },
  264. [`PUT ${api.lesson.replace('/:id', '')}`]: (req, res) => {
  265. console.log(`[PUT][${api.lesson}]`, req.body);
  266. res.send(update(global.lessonList, req.body));
  267. },
  268. [`GET ${api.lessons}`]: (req, res) => {
  269. console.log(`[GET][${api.lessons}]`, req.query);
  270. res.send(query(global.lessonList, req.query));
  271. },
  272. [`GET ${api.lesson}`]: (req, res) => {
  273. console.log(`[GET][${api.lesson}]`, req.params);
  274. const { id } = req.params;
  275. queryOne(global.lessonList, id, res);
  276. },
  277. // ################## 已被product接口替代 ###################
  278. // 课程
  279. /*
  280. [`POST ${api.course.replace('/:id', '')}`]: (req, res) => {
  281. console.log(`[POST][${api.course}]`, req.body);
  282. res.send(create(global.courseList, req.body));
  283. },
  284. [`DELETE ${api.course}`]: (req, res) => {
  285. console.log(`[DELETE][${api.course}]`, req.params);
  286. const { id } = req.params;
  287. remove(global.courseList, id, res);
  288. },
  289. [`PUT ${api.course.replace('/:id', '')}`]: (req, res) => {
  290. console.log(`[PUT][${api.course}]`, req.body);
  291. res.send(update(global.courseList, req.body));
  292. },
  293. [`GET ${api.courses}`]: (req, res) => {
  294. console.log(`[GET][${api.courses}]`, req.query);
  295. res.send(query(global.courseList, req.query));
  296. },
  297. [`GET ${api.course}`]: (req, res) => {
  298. console.log(`[GET][${api.course}]`, req.params);
  299. const { id } = req.params;
  300. queryOne(global.courseList, id, res);
  301. },
  302. // 周边
  303. [`POST ${api.support.replace('/:id', '')}`]: (req, res) => {
  304. console.log(`[POST][${api.support}]`, req.body);
  305. res.send(create(global.supportList, req.body));
  306. },
  307. [`DELETE ${api.support}`]: (req, res) => {
  308. console.log(`[DELETE][${api.support}]`, req.params);
  309. const { id } = req.params;
  310. remove(global.supportList, id, res);
  311. },
  312. [`PUT ${api.support.replace('/:id', '')}`]: (req, res) => {
  313. console.log(`[PUT][${api.support}]`, req.body);
  314. res.send(update(global.supportList, req.body));
  315. },
  316. [`GET ${api.supports}`]: (req, res) => {
  317. console.log(`[GET][${api.supports}]`, req.query);
  318. res.send(query(global.supportList, req.query));
  319. },
  320. [`GET ${api.support}`]: (req, res) => {
  321. console.log(`[GET][${api.support}]`, req.params);
  322. const { id } = req.params;
  323. queryOne(global.supportList, id, res);
  324. },
  325. // 课程包
  326. [`POST ${api.combo.replace('/:id', '')}`]: (req, res) => {
  327. console.log(`[POST][${api.combo}]`, req.body);
  328. res.send(create(global.comboList, req.body));
  329. },
  330. [`DELETE ${api.combo}`]: (req, res) => {
  331. console.log(`[DELETE][${api.combo}]`, req.params);
  332. const { id } = req.params;
  333. remove(global.comboList, id, res);
  334. },
  335. [`PUT ${api.combo.replace('/:id', '')}`]: (req, res) => {
  336. console.log(`[PUT][${api.combo}]`, req.body);
  337. res.send(update(global.comboList, req.body));
  338. },
  339. [`GET ${api.combos}`]: (req, res) => {
  340. console.log(`[GET][${api.combos}]`, req.query);
  341. res.send(query(global.comboList, req.query));
  342. },
  343. [`GET ${api.combo}`]: (req, res) => {
  344. console.log(`[GET][${api.combo}]`, req.params);
  345. const { id } = req.params;
  346. queryOne(global.comboList, id, res);
  347. },
  348. */
  349. // ##############################################
  350. // 产品接口
  351. [`POST ${api.product}`]: (req, res) => {
  352. console.log(`[POST][${api.product}]`, req.body);
  353. res.send(create(global.productList, req.body));
  354. },
  355. [`DELETE ${api.product}/:id`]: (req, res) => {
  356. console.log(`[DELETE][${api.product}]`, req.params);
  357. const { id } = req.params;
  358. remove(global.productList, id, res);
  359. },
  360. [`PUT ${api.product}/:id`]: (req, res) => {
  361. console.log(`[PUT][${api.product}]`, req.body);
  362. res.send(update(global.productList, req.body));
  363. },
  364. [`GET ${api.product}`]: (req, res) => {
  365. console.log(`[GET][${api.product}]`, req.query);
  366. res.send(query(global.productList, req.query));
  367. },
  368. [`GET ${api.product}/:id`]: (req, res) => {
  369. console.log(`[GET][${api.product}]`, req.params);
  370. const { id } = req.params;
  371. queryOne(global.productList, id, res);
  372. },
  373. // 订单
  374. [`POST ${api.order.replace('/:id', '')}`]: (req, res) => {
  375. console.log(`[POST][${api.order}]`, req.body);
  376. res.send(create(global.orderList, req.body));
  377. },
  378. [`DELETE ${api.order}`]: (req, res) => {
  379. console.log(`[DELETE][${api.order}]`, req.params);
  380. const { id } = req.params;
  381. remove(global.orderList, id, res);
  382. },
  383. [`PUT ${api.order.replace('/:id', '')}`]: (req, res) => {
  384. console.log(`[PUT][${api.order}]`, req.body);
  385. res.send(update(global.orderList, req.body));
  386. },
  387. [`GET ${api.orders}`]: (req, res) => {
  388. console.log(`[GET][${api.orders}]`, req.query);
  389. res.send(query(global.orderList, req.query));
  390. },
  391. [`GET ${api.order}`]: (req, res) => {
  392. console.log(`[GET][${api.order}]`, req.params);
  393. const { id } = req.params;
  394. queryOne(global.orderList, id, res);
  395. },
  396. // cms用户
  397. [`POST ${api.cmsUser.replace('/:id', '')}`]: (req, res) => {
  398. console.log(`[POST][${api.cmsUser}]`, req.body);
  399. res.send(create(global.cmsUserList, req.body));
  400. },
  401. [`DELETE ${api.cmsUser}`]: (req, res) => {
  402. console.log(`[DELETE][${api.cmsUser}]`, req.params);
  403. const { id } = req.params;
  404. remove(global.cmsUserList, id, res);
  405. },
  406. [`PUT ${api.cmsUser.replace('/:id', '')}`]: (req, res) => {
  407. console.log(`[PUT][${api.cmsUser}]`, req.body);
  408. res.send(update(global.cmsUserList, req.body));
  409. },
  410. [`GET ${api.cmsUsers}`]: (req, res) => {
  411. console.log(`[GET][${api.cmsUsers}]`, req.query);
  412. res.send(query(global.cmsUserList, req.query));
  413. },
  414. };
  415. // 是否禁用代理
  416. const noProxy = process.env.NO_PROXY === 'true';
  417. // 根据是否禁用代理来选择是mock数据还是真实接口
  418. export default noProxy ? {} : delay(proxy, 500);