.roadhogrc.mock.js 3.3 KB

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