.roadhogrc.mock.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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 { signature } from './mock/signature';
  12. import * as api from './src/utils/api';
  13. // mock数据持久化
  14. global.campusList = campusList;
  15. global.terminalList = terminalList;
  16. global.merchantList = merchantList;
  17. global.resourceList = resourceList;
  18. global.groupList = groupList;
  19. global.tagList = tagList;
  20. global.wareList = wareList;
  21. global.lessonList = lessonList;
  22. global.signature = signature;
  23. // 操作成功响应内容
  24. const SUCCESS = { code: 200, success: true, message: null };
  25. // 资源未找到响应内容
  26. const NOTFOUND = { code: 404, message: 'Not Found!' };
  27. // 查询
  28. const query = (dataset, params) => {
  29. const pageSize = parseInt(params.pageSize) || 10;
  30. const pageNo = parseInt(params.pageNo) || 1;
  31. delete params['pageSize'];
  32. delete params['pageNo'];
  33. const queryKeys = Object.keys(params);
  34. const newDataset = dataset.filter(item => {
  35. let flag = true;
  36. for (let key of queryKeys) {
  37. if (item[key] !== params[key]) {
  38. flag = false;
  39. break;
  40. }
  41. }
  42. return flag;
  43. });
  44. const totalSize = newDataset.length || 0;
  45. const list = newDataset.slice(pageSize * (pageNo - 1), pageSize * pageNo);
  46. return {
  47. ...SUCCESS,
  48. data: { pageSize, pageNo, totalSize: newDataset.length, list }
  49. };
  50. }
  51. // 添加
  52. const create = (dataset, params) => {
  53. const last = dataset[dataset.length - 1];
  54. const data = { ...params, id: last.id + 1 };
  55. dataset.push(data);
  56. return {
  57. ...SUCCESS,
  58. data,
  59. };
  60. }
  61. // 更新
  62. const update = (dataset, params) => {
  63. for (let index in dataset) {
  64. if (dataset[index].id === params.id) {
  65. dataset[index] = { ...dataset[index], ...params };
  66. }
  67. }
  68. return {
  69. ...SUCCESS,
  70. }
  71. }
  72. const queryArray = (array, key, keyAlias = 'key') => {
  73. if (!(array instanceof Array)) {
  74. return null
  75. }
  76. let data
  77. for (let item of array) {
  78. if (item[keyAlias] === key) {
  79. data = item
  80. break
  81. }
  82. }
  83. if (data) {
  84. return data
  85. }
  86. return null
  87. }
  88. // 查询一条
  89. const queryOne = (dataset, id, res) => {
  90. const target = queryArray(dataset, id, 'id');
  91. if (target) {
  92. res.send({ ...SUCCESS, data: { ...target } });
  93. } else {
  94. res.send({ ...NOTFOUND, data: {} });
  95. }
  96. }
  97. // 删除
  98. const remove = (dataset, id, res) => {
  99. const data = queryArray(dataset, id, 'id');
  100. if (data) {
  101. dataset.map(item => { item.id === data.id ? item.status = 'DEL' : null });
  102. res.send(SUCCESS);
  103. } else {
  104. res.send(NOTFOUND);
  105. }
  106. }
  107. // mock数据
  108. const proxy = {
  109. // 资源
  110. [`POST ${api.resource.replace('/:id', '')}`]: (req, res) => {
  111. res.send(create(global.resourceList, req.body));
  112. },
  113. [`DELETE ${api.resource}`]: (req, res) => {
  114. const { id } = req.params;
  115. remove(global.resourceList, id, res);
  116. },
  117. [`PUT ${api.resource.replace('/:id', '')}`]: (req, res) => {
  118. res.send(update(global.resourceList, req.body));
  119. },
  120. [`GET ${api.resources}`]: (req, res) => {
  121. res.send(query(global.resourceList, req.query));
  122. },
  123. [`GET ${api.signature}`]: (req, res) => {
  124. res.send({ ...SUCCESS, data: global.signature });
  125. },
  126. // 校区
  127. [`GET ${api.campuses}`]: (req, res) => {
  128. res.send(query(global.campusList, req.query));
  129. },
  130. [`POST ${api.campus.replace('/:id', '')}`]: (req, res) => {
  131. res.send(create(global.campusList, req.body));
  132. },
  133. [`PUT ${api.campus.replace('/:id', '')}`]: (req, res) => {
  134. res.send(update(global.campusList, req.body));
  135. },
  136. // 终端
  137. [`GET ${api.terminals}`]: (req, res) => {
  138. res.send(query(global.terminalList, req.query));
  139. },
  140. [`POST ${api.terminal.replace('/:id', '')}`]: (req, res) => {
  141. res.send(create(global.terminalList, req.body));
  142. },
  143. [`PUT ${api.terminal.replace('/:id', '')}`]: (req, res) => {
  144. res.send(update(global.terminalList, req.body));
  145. },
  146. [`DELETE ${api.terminal}`]: (req, res) => {
  147. const { id } = req.params;
  148. remove(global.terminalList, id, res);
  149. },
  150. // 厂商
  151. [`POST ${api.merchant.replace('/:id', '')}`]: (req, res) => {
  152. res.send(create(global.merchantList, req.body));
  153. },
  154. [`DELETE ${api.merchant}`]: (req, res) => {
  155. const { id } = req.params;
  156. remove(global.merchantList, id, res);
  157. },
  158. [`PUT ${api.merchant.replace('/:id', '')}`]: (req, res) => {
  159. res.send(update(global.merchantList, req.body));
  160. },
  161. [`GET ${api.merchants}`]: (req, res) => {
  162. res.send(query(global.merchantList, req.query));
  163. },
  164. [`GET ${api.merchant}`]: (req, res) => {
  165. const { id } = req.params;
  166. queryOne(global.merchantList, id, res);
  167. },
  168. // 标签组
  169. [`POST ${api.group.replace('/:id', '')}`]: (req, res) => {
  170. res.send(create(global.groupList, req.body));
  171. },
  172. [`DELETE ${api.group}`]: (req, res) => {
  173. const { id } = req.params;
  174. remove(global.groupList, id, res);
  175. },
  176. [`PUT ${api.group.replace('/:id', '')}`]: (req, res) => {
  177. res.send(update(global.groupList, req.body));
  178. },
  179. [`GET ${api.groups}`]: (req, res) => {
  180. console.log(`[${api.groups}]`, req.query);
  181. res.send(query(global.groupList, req.query));
  182. },
  183. [`GET ${api.group}`]: (req, res) => {
  184. const { id } = req.params;
  185. queryOne(global.groupList, id, res);
  186. },
  187. // 标签
  188. [`POST ${api.tag.replace('/:id', '')}`]: (req, res) => {
  189. res.send(create(global.tagList, req.body));
  190. },
  191. [`DELETE ${api.tag}`]: (req, res) => {
  192. const { id } = req.params;
  193. remove(global.tagList, id, res);
  194. },
  195. [`PUT ${api.tag.replace('/:id', '')}`]: (req, res) => {
  196. res.send(update(global.tagList, req.body));
  197. },
  198. [`GET ${api.tags}`]: (req, res) => {
  199. console.log(`[${api.tags}]`, req.query);
  200. res.send(query(global.tagList, req.query));
  201. },
  202. [`GET ${api.tag}`]: (req, res) => {
  203. console.log(`[${api.tag}]`, req.params);
  204. const { id } = req.params;
  205. queryOne(global.tagList, id, res);
  206. },
  207. // 课件
  208. [`POST ${api.ware.replace('/:id', '')}`]: (req, res) => {
  209. console.log(`[POST][${api.ware}]`, req.body);
  210. res.send(create(global.wareList, req.body));
  211. },
  212. [`DELETE ${api.ware}`]: (req, res) => {
  213. console.log(`[DELETE][${api.ware}]`, req.params);
  214. const { id } = req.params;
  215. remove(global.wareList, id, res);
  216. },
  217. [`PUT ${api.ware.replace('/:id', '')}`]: (req, res) => {
  218. console.log(`[PUT][${api.ware}]`, req.body);
  219. res.send(update(global.wareList, req.body));
  220. },
  221. [`GET ${api.wares}`]: (req, res) => {
  222. console.log(`[GET][${api.wares}]`, req.query);
  223. res.send(query(global.wareList, req.query));
  224. },
  225. [`GET ${api.ware}`]: (req, res) => {
  226. console.log(`[GET][${api.ware}]`, req.params);
  227. const { id } = req.params;
  228. queryOne(global.wareList, id, res);
  229. },
  230. // 课
  231. [`POST ${api.lesson.replace('/:id', '')}`]: (req, res) => {
  232. console.log(`[POST][${api.lesson}]`, req.body);
  233. res.send(create(global.lessonList, req.body));
  234. },
  235. [`DELETE ${api.ware}`]: (req, res) => {
  236. console.log(`[DELETE][${api.lesson}]`, req.params);
  237. const { id } = req.params;
  238. remove(global.lessonList, id, res);
  239. },
  240. [`PUT ${api.lesson.replace('/:id', '')}`]: (req, res) => {
  241. console.log(`[PUT][${api.lesson}]`, req.body);
  242. res.send(update(global.lessonList, req.body));
  243. },
  244. [`GET ${api.lessons}`]: (req, res) => {
  245. console.log(`[GET][${api.lessons}]`, req.query);
  246. res.send(query(global.lessonList, req.query));
  247. },
  248. [`GET ${api.lesson}`]: (req, res) => {
  249. console.log(`[GET][${api.lesson}]`, req.params);
  250. const { id } = req.params;
  251. queryOne(global.lessonList, id, res);
  252. },
  253. };
  254. // 是否禁用代理
  255. const noProxy = process.env.NO_PROXY === 'true';
  256. // 根据是否禁用代理来选择是mock数据还是真实接口
  257. export default noProxy ? {} : delay(proxy, 500);
  258. // export default noProxy ? {} : proxy;