.roadhogrc.mock.js 6.9 KB

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