.roadhogrc.mock.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import mockjs from 'mockjs';
  2. import { getRule, postRule } from './mock/rule';
  3. import { getActivities, getNotice, getFakeList } 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. // 是否禁用代理
  10. const noProxy = process.env.NO_PROXY === 'true';
  11. // 代码中会兼容本地 service mock 以及部署站点的静态数据
  12. const proxy = {
  13. // 支持值为 Object 和 Array
  14. 'GET /api/currentUser': {
  15. $desc: '获取当前用户接口',
  16. $params: {
  17. pageSize: {
  18. desc: '分页',
  19. exp: 2,
  20. },
  21. },
  22. $body: {
  23. name: 'Serati Ma',
  24. avatar: 'https://gw.alipayobjects.com/zos/rmsportal/BiazfanxmamNRoxxVxka.png',
  25. userid: '00000001',
  26. notifyCount: 12,
  27. },
  28. },
  29. // GET POST 可省略
  30. 'GET /api/users': [
  31. {
  32. key: '1',
  33. name: 'John Brown',
  34. age: 32,
  35. address: 'New York No. 1 Lake Park',
  36. },
  37. {
  38. key: '2',
  39. name: 'Jim Green',
  40. age: 42,
  41. address: 'London No. 1 Lake Park',
  42. },
  43. {
  44. key: '3',
  45. name: 'Joe Black',
  46. age: 32,
  47. address: 'Sidney No. 1 Lake Park',
  48. },
  49. ],
  50. 'GET /api/project/notice': getNotice,
  51. 'GET /api/activities': getActivities,
  52. 'GET /api/rule': getRule,
  53. 'POST /api/rule': {
  54. $params: {
  55. pageSize: {
  56. desc: '分页',
  57. exp: 2,
  58. },
  59. },
  60. $body: postRule,
  61. },
  62. 'POST /api/forms': (req, res) => {
  63. res.send({ message: 'Ok' });
  64. },
  65. 'GET /api/tags': mockjs.mock({
  66. 'list|100': [{ name: '@city', 'value|1-100': 150, 'type|0-2': 1 }],
  67. }),
  68. 'GET /api/fake_list': getFakeList,
  69. 'GET /api/fake_chart_data': getFakeChartData,
  70. 'GET /api/profile/basic': getProfileBasicData,
  71. 'GET /api/profile/advanced': getProfileAdvancedData,
  72. 'POST /api/login/account': (req, res) => {
  73. const { password, userName, type } = req.body;
  74. if (password === '888888' && userName === 'admin') {
  75. res.send({
  76. status: 'ok',
  77. type,
  78. currentAuthority: 'admin',
  79. });
  80. return;
  81. }
  82. if (password === '123456' && userName === 'user') {
  83. res.send({
  84. status: 'ok',
  85. type,
  86. currentAuthority: 'user',
  87. });
  88. return;
  89. }
  90. res.send({
  91. status: 'error',
  92. type,
  93. currentAuthority: 'guest',
  94. });
  95. },
  96. 'POST /api/register': (req, res) => {
  97. res.send({ status: 'ok', currentAuthority: 'user' });
  98. },
  99. 'GET /api/notices': getNotices,
  100. 'GET /api/500': (req, res) => {
  101. res.status(500).send({
  102. timestamp: 1513932555104,
  103. status: 500,
  104. error: 'error',
  105. message: 'error',
  106. path: '/base/category/list',
  107. });
  108. },
  109. 'GET /api/404': (req, res) => {
  110. res.status(404).send({
  111. timestamp: 1513932643431,
  112. status: 404,
  113. error: 'Not Found',
  114. message: 'No message available',
  115. path: '/base/category/list/2121212',
  116. });
  117. },
  118. 'GET /api/403': (req, res) => {
  119. res.status(403).send({
  120. timestamp: 1513932555104,
  121. status: 403,
  122. error: 'Unauthorized',
  123. message: 'Unauthorized',
  124. path: '/base/category/list',
  125. });
  126. },
  127. 'GET /api/401': (req, res) => {
  128. res.status(401).send({
  129. timestamp: 1513932555104,
  130. status: 401,
  131. error: 'Unauthorized',
  132. message: 'Unauthorized',
  133. path: '/base/category/list',
  134. });
  135. },
  136. };
  137. export default (noProxy ? {} : delay(proxy, 1000));