.roadhogrc.mock.js 4.6 KB

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