.roadhogrc.mock.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. profile: '简单的介绍下自己',
  30. notifyCount: 12,
  31. country: 'China',
  32. geographic: {
  33. province: {
  34. label: '浙江省',
  35. key: '330000',
  36. },
  37. city: {
  38. label: '杭州市',
  39. key: '330100',
  40. },
  41. },
  42. address: '西湖区工专路 77 号',
  43. phone: '0752-268888888',
  44. },
  45. },
  46. // GET POST 可省略
  47. 'GET /api/users': [
  48. {
  49. key: '1',
  50. name: 'John Brown',
  51. age: 32,
  52. address: 'New York No. 1 Lake Park',
  53. },
  54. {
  55. key: '2',
  56. name: 'Jim Green',
  57. age: 42,
  58. address: 'London No. 1 Lake Park',
  59. },
  60. {
  61. key: '3',
  62. name: 'Joe Black',
  63. age: 32,
  64. address: 'Sidney No. 1 Lake Park',
  65. },
  66. ],
  67. 'GET /api/project/notice': getNotice,
  68. 'GET /api/activities': getActivities,
  69. 'GET /api/rule': getRule,
  70. 'POST /api/rule': {
  71. $params: {
  72. pageSize: {
  73. desc: '分页',
  74. exp: 2,
  75. },
  76. },
  77. $body: postRule,
  78. },
  79. 'POST /api/forms': (req, res) => {
  80. res.send({ message: 'Ok' });
  81. },
  82. 'GET /api/tags': mockjs.mock({
  83. 'list|100': [{ name: '@city', 'value|1-100': 150, 'type|0-2': 1 }],
  84. }),
  85. 'GET /api/fake_list': getFakeList,
  86. 'POST /api/fake_list': postFakeList,
  87. 'GET /api/fake_chart_data': getFakeChartData,
  88. 'GET /api/profile/basic': getProfileBasicData,
  89. 'GET /api/profile/advanced': getProfileAdvancedData,
  90. 'POST /api/login/account': (req, res) => {
  91. const { password, userName, type } = req.body;
  92. if (password === '888888' && userName === 'admin') {
  93. res.send({
  94. status: 'ok',
  95. type,
  96. currentAuthority: 'admin',
  97. });
  98. return;
  99. }
  100. if (password === '123456' && userName === 'user') {
  101. res.send({
  102. status: 'ok',
  103. type,
  104. currentAuthority: 'user',
  105. });
  106. return;
  107. }
  108. res.send({
  109. status: 'error',
  110. type,
  111. currentAuthority: 'guest',
  112. });
  113. },
  114. 'POST /api/register': (req, res) => {
  115. res.send({ status: 'ok', currentAuthority: 'user' });
  116. },
  117. 'GET /api/notices': getNotices,
  118. 'GET /api/500': (req, res) => {
  119. res.status(500).send({
  120. timestamp: 1513932555104,
  121. status: 500,
  122. error: 'error',
  123. message: 'error',
  124. path: '/base/category/list',
  125. });
  126. },
  127. 'GET /api/404': (req, res) => {
  128. res.status(404).send({
  129. timestamp: 1513932643431,
  130. status: 404,
  131. error: 'Not Found',
  132. message: 'No message available',
  133. path: '/base/category/list/2121212',
  134. });
  135. },
  136. 'GET /api/403': (req, res) => {
  137. res.status(403).send({
  138. timestamp: 1513932555104,
  139. status: 403,
  140. error: 'Unauthorized',
  141. message: 'Unauthorized',
  142. path: '/base/category/list',
  143. });
  144. },
  145. 'GET /api/401': (req, res) => {
  146. res.status(401).send({
  147. timestamp: 1513932555104,
  148. status: 401,
  149. error: 'Unauthorized',
  150. message: 'Unauthorized',
  151. path: '/base/category/list',
  152. });
  153. },
  154. 'GET /api/geographic/province': getProvince,
  155. 'GET /api/geographic/city/:province': getCity,
  156. };
  157. export default (noProxy ? {} : delay(proxy, 1000));