.roadhogrc.mock.js 4.5 KB

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