.roadhogrc.mock.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. key: '1',
  32. name: 'John Brown',
  33. age: 32,
  34. address: 'New York No. 1 Lake Park',
  35. }, {
  36. key: '2',
  37. name: 'Jim Green',
  38. age: 42,
  39. address: 'London No. 1 Lake Park',
  40. }, {
  41. key: '3',
  42. name: 'Joe Black',
  43. age: 32,
  44. address: 'Sidney No. 1 Lake Park',
  45. }],
  46. 'GET /api/project/notice': getNotice,
  47. 'GET /api/activities': getActivities,
  48. 'GET /api/rule': getRule,
  49. 'POST /api/rule': {
  50. $params: {
  51. pageSize: {
  52. desc: '分页',
  53. exp: 2,
  54. },
  55. },
  56. $body: postRule,
  57. },
  58. 'POST /api/forms': (req, res) => {
  59. res.send({ message: 'Ok' });
  60. },
  61. 'GET /api/tags': mockjs.mock({
  62. 'list|100': [{ name: '@city', 'value|1-100': 150, 'type|0-2': 1 }]
  63. }),
  64. 'GET /api/fake_list': getFakeList,
  65. 'GET /api/fake_chart_data': getFakeChartData,
  66. 'GET /api/profile/basic': getProfileBasicData,
  67. 'GET /api/profile/advanced': getProfileAdvancedData,
  68. 'POST /api/login/account': (req, res) => {
  69. const { password, username, type } = req.body;
  70. if(password === '888888' && username === 'admin'){
  71. res.send({
  72. status: 'ok',
  73. type,
  74. currentAuthority: 'admin'
  75. });
  76. return ;
  77. }
  78. if(password === '123456' && username === 'user'){
  79. res.send({
  80. status: 'ok',
  81. type,
  82. currentAuthority: 'user'
  83. });
  84. return ;
  85. }
  86. res.send({
  87. status: 'error',
  88. type,
  89. currentAuthority: 'guest'
  90. });
  91. },
  92. 'POST /api/register': (req, res) => {
  93. res.send({ status: 'ok', currentAuthority: 'user' });
  94. },
  95. 'GET /api/notices': getNotices,
  96. 'GET /api/500': (req, res) => {
  97. res.status(500).send({
  98. "timestamp": 1513932555104,
  99. "status": 500,
  100. "error": "error",
  101. "message": "error",
  102. "path": "/base/category/list"
  103. });
  104. },
  105. 'GET /api/404': (req, res) => {
  106. res.status(404).send({
  107. "timestamp": 1513932643431,
  108. "status": 404,
  109. "error": "Not Found",
  110. "message": "No message available",
  111. "path": "/base/category/list/2121212"
  112. });
  113. },
  114. 'GET /api/403': (req, res) => {
  115. res.status(403).send({
  116. "timestamp": 1513932555104,
  117. "status": 403,
  118. "error": "Unauthorized",
  119. "message": "Unauthorized",
  120. "path": "/base/category/list"
  121. });
  122. },
  123. 'GET /api/401': (req, res) => {
  124. res.status(401).send({
  125. "timestamp": 1513932555104,
  126. "status": 401,
  127. "error": "Unauthorized",
  128. "message": "Unauthorized",
  129. "path": "/base/category/list"
  130. });
  131. },
  132. };
  133. export default noProxy ? {} : delay(proxy, 1000);