user.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. const waitTime = (time = 100) => {
  2. return new Promise((resolve) => {
  3. setTimeout(() => {
  4. resolve(true);
  5. }, time);
  6. });
  7. };
  8. async function getFakeCaptcha(req, res) {
  9. await waitTime(2000);
  10. return res.json('captcha-xxx');
  11. } // 代码中会兼容本地 service mock 以及部署站点的静态数据
  12. export default {
  13. // 支持值为 Object 和 Array
  14. 'GET /api/login/staff': {
  15. code: 0,
  16. data: {
  17. "staffId": "NcDlEdO0O0Oi",
  18. "categoryId": "McTlIdO0O0Oi",
  19. "username": "蔡频",
  20. "categoryTitle": "总部",
  21. "avatar": "/global/avatar/avatar_41",
  22. "qq": "2147422",
  23. "qualifications": "大专"
  24. }
  25. },
  26. // GET POST 可省略
  27. 'GET /api/users': [
  28. {
  29. key: '1',
  30. name: 'John Brown',
  31. age: 32,
  32. address: 'New York No. 1 Lake Park',
  33. },
  34. {
  35. key: '2',
  36. name: 'Jim Green',
  37. age: 42,
  38. address: 'London No. 1 Lake Park',
  39. },
  40. {
  41. key: '3',
  42. name: 'Joe Black',
  43. age: 32,
  44. address: 'Sidney No. 1 Lake Park',
  45. },
  46. ],
  47. 'POST /api/login': async (req, res) => {
  48. const { password, username, type } = req.body;
  49. await waitTime(2000);
  50. if (password === '123456' && username === '蔡频') {
  51. res.send({
  52. code: 0,
  53. data: {
  54. token: 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdGFmZklkIjoiTmNEbEVkTzBPME9pIiwiY2F0ZWdvcnlJZCI6Ik1jVGxJZE8wTzBPaSIsInVzZXJuYW1lIjoiXHU4NTIxXHU5ODkxIiwiY2F0ZWdvcnlUaXRsZSI6Ilx1NjAzYlx1OTBlOCIsImF2YXRhciI6IlwvZ2xvYmFsXC9hdmF0YXJcL2F2YXRhcl80MSIsInFxIjoiMjE0NzQyMiIsInF1YWxpZmljYXRpb25zIjoiXHU1OTI3XHU0ZTEzIiwiYXVkIjoiIiwiZXhwIjoxNjEyODQyNDg3LCJpYXQiOjE2MTI3NTYwODcsImlzcyI6IiIsImp0aSI6IjgyNWRmNTBjNjg3NDQ0ZDJhZDA1ZDNiNzhhOGE0YmVhIiwibmJmIjoxNjEyNzU2MDg3LCJzdWIiOiIifQ.Iexq-ep6YiVM61nEoCDSd72NS-Qh63Ll5vAhlxTgVEo'
  55. },
  56. currentAuthority: 'admin',
  57. });
  58. return;
  59. }
  60. if (password === 'ant.design' && username === 'user') {
  61. res.send({
  62. status: 'ok',
  63. type,
  64. currentAuthority: 'user',
  65. });
  66. return;
  67. }
  68. if (type === 'mobile') {
  69. res.send({
  70. status: 'ok',
  71. type,
  72. currentAuthority: 'admin',
  73. });
  74. return;
  75. }
  76. res.send({
  77. status: 'error',
  78. type,
  79. currentAuthority: 'guest',
  80. });
  81. },
  82. 'POST /api/register': (req, res) => {
  83. res.send({
  84. status: 'ok',
  85. currentAuthority: 'user',
  86. });
  87. },
  88. 'GET /api/500': (req, res) => {
  89. res.status(500).send({
  90. timestamp: 1513932555104,
  91. status: 500,
  92. error: 'error',
  93. message: 'error',
  94. path: '/base/category/list',
  95. });
  96. },
  97. 'GET /api/404': (req, res) => {
  98. res.status(404).send({
  99. timestamp: 1513932643431,
  100. status: 404,
  101. error: 'Not Found',
  102. message: 'No message available',
  103. path: '/base/category/list/2121212',
  104. });
  105. },
  106. 'GET /api/403': (req, res) => {
  107. res.status(403).send({
  108. timestamp: 1513932555104,
  109. status: 403,
  110. error: 'Unauthorized',
  111. message: 'Unauthorized',
  112. path: '/base/category/list',
  113. });
  114. },
  115. 'GET /api/401': (req, res) => {
  116. res.status(401).send({
  117. timestamp: 1513932555104,
  118. status: 401,
  119. error: 'Unauthorized',
  120. message: 'Unauthorized',
  121. path: '/base/category/list',
  122. });
  123. },
  124. 'GET /api/login/captcha': getFakeCaptcha,
  125. };