.roadhogrc.mock.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. import mockjs from 'mockjs';
  2. import { getRule, postRule } from './mock/rule';
  3. import { getActivities, getNotice, getFakeList, postFakeList, getFakeCaptcha } from './mock/api';
  4. import { getFakeChartData } from './mock/chart';
  5. import { getProfileBasicData } from './mock/profile';
  6. import { getProfileAdvancedData } from './mock/profile';
  7. import { getNotices } from './mock/notices';
  8. import { format, delay } from 'roadhog-api-doc';
  9. import { getProvince, getCity } from './mock/geographic';
  10. // 是否禁用代理
  11. const noProxy = process.env.NO_PROXY === 'true';
  12. // 代码中会兼容本地 service mock 以及部署站点的静态数据
  13. const proxy = {
  14. // 支持值为 Object 和 Array
  15. 'GET /api/currentUser': {
  16. $desc: '获取当前用户接口',
  17. $params: {
  18. pageSize: {
  19. desc: '分页',
  20. exp: 2,
  21. },
  22. },
  23. $body: {
  24. name: 'Serati Ma',
  25. avatar: 'https://gw.alipayobjects.com/zos/rmsportal/BiazfanxmamNRoxxVxka.png',
  26. userid: '00000001',
  27. email: 'antdesign@alipay.com',
  28. signature: '海纳百川,有容乃大',
  29. title: '交互专家',
  30. group: '蚂蚁金服-某某某事业群-某某平台部-某某技术部-UED',
  31. tags: [
  32. {
  33. key: '0',
  34. label: '很有想法的',
  35. },
  36. {
  37. key: '1',
  38. label: '专注设计',
  39. },
  40. {
  41. key: '2',
  42. label: '辣~',
  43. },
  44. {
  45. key: '3',
  46. label: '大长腿',
  47. },
  48. {
  49. key: '4',
  50. label: '川妹子',
  51. },
  52. {
  53. key: '5',
  54. label: '海纳百川',
  55. },
  56. ],
  57. notifyCount: 12,
  58. country: 'China',
  59. geographic: {
  60. province: {
  61. label: '浙江省',
  62. key: '330000',
  63. },
  64. city: {
  65. label: '杭州市',
  66. key: '330100',
  67. },
  68. },
  69. address: '西湖区工专路 77 号',
  70. phone: '0752-268888888',
  71. },
  72. },
  73. // GET POST 可省略
  74. 'GET /api/users': [
  75. {
  76. key: '1',
  77. name: 'John Brown',
  78. age: 32,
  79. address: 'New York No. 1 Lake Park',
  80. },
  81. {
  82. key: '2',
  83. name: 'Jim Green',
  84. age: 42,
  85. address: 'London No. 1 Lake Park',
  86. },
  87. {
  88. key: '3',
  89. name: 'Joe Black',
  90. age: 32,
  91. address: 'Sidney No. 1 Lake Park',
  92. },
  93. ],
  94. 'GET /api/project/notice': getNotice,
  95. 'GET /api/activities': getActivities,
  96. 'GET /api/rule': getRule,
  97. 'POST /api/rule': {
  98. $params: {
  99. pageSize: {
  100. desc: '分页',
  101. exp: 2,
  102. },
  103. },
  104. $body: postRule,
  105. },
  106. 'POST /api/forms': (req, res) => {
  107. res.send({ message: 'Ok' });
  108. },
  109. 'GET /api/tags': mockjs.mock({
  110. 'list|100': [{ name: '@city', 'value|1-100': 150, 'type|0-2': 1 }],
  111. }),
  112. 'GET /api/fake_list': getFakeList,
  113. 'POST /api/fake_list': postFakeList,
  114. 'GET /api/fake_chart_data': getFakeChartData,
  115. 'GET /api/profile/basic': getProfileBasicData,
  116. 'GET /api/profile/advanced': getProfileAdvancedData,
  117. 'POST /api/login/account': (req, res) => {
  118. const { password, userName, type } = req.body;
  119. if (password === '888888' && userName === 'admin') {
  120. res.send({
  121. status: 'ok',
  122. type,
  123. currentAuthority: 'admin',
  124. });
  125. return;
  126. }
  127. if (password === '123456' && userName === 'user') {
  128. res.send({
  129. status: 'ok',
  130. type,
  131. currentAuthority: 'user',
  132. });
  133. return;
  134. }
  135. res.send({
  136. status: 'error',
  137. type,
  138. currentAuthority: 'guest',
  139. });
  140. },
  141. 'POST /api/register': (req, res) => {
  142. res.send({ status: 'ok', currentAuthority: 'user' });
  143. },
  144. 'GET /api/notices': getNotices,
  145. 'GET /api/500': (req, res) => {
  146. res.status(500).send({
  147. timestamp: 1513932555104,
  148. status: 500,
  149. error: 'error',
  150. message: 'error',
  151. path: '/base/category/list',
  152. });
  153. },
  154. 'GET /api/404': (req, res) => {
  155. res.status(404).send({
  156. timestamp: 1513932643431,
  157. status: 404,
  158. error: 'Not Found',
  159. message: 'No message available',
  160. path: '/base/category/list/2121212',
  161. });
  162. },
  163. 'GET /api/403': (req, res) => {
  164. res.status(403).send({
  165. timestamp: 1513932555104,
  166. status: 403,
  167. error: 'Unauthorized',
  168. message: 'Unauthorized',
  169. path: '/base/category/list',
  170. });
  171. },
  172. 'GET /api/401': (req, res) => {
  173. res.status(401).send({
  174. timestamp: 1513932555104,
  175. status: 401,
  176. error: 'Unauthorized',
  177. message: 'Unauthorized',
  178. path: '/base/category/list',
  179. });
  180. },
  181. 'GET /api/geographic/province': getProvince,
  182. 'GET /api/geographic/city/:province': getCity,
  183. 'GET /api/captcha': getFakeCaptcha,
  184. };
  185. export default (noProxy ? {} : delay(proxy, 1000));