.roadhogrc.mock.js 11 KB

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