user.js 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. }
  12. const { ANT_DESIGN_PRO_ONLY_DO_NOT_USE_IN_YOUR_PRODUCTION } = process.env;
  13. /**
  14. * 当前用户的权限,如果为空代表没登录
  15. * current user access, if is '', user need login
  16. * 如果是 pro 的预览,默认是有权限的
  17. */
  18. let access = ANT_DESIGN_PRO_ONLY_DO_NOT_USE_IN_YOUR_PRODUCTION === 'site' ? 'admin' : '';
  19. const getAccess = () => {
  20. return access;
  21. }; // 代码中会兼容本地 service mock 以及部署站点的静态数据
  22. export default {
  23. // 支持值为 Object 和 Array
  24. 'GET /api/currentUser': (req, res) => {
  25. if (!getAccess()) {
  26. res.status(401).send({
  27. data: {
  28. isLogin: false,
  29. },
  30. errorCode: '401',
  31. errorMessage: '请先登录!',
  32. success: true,
  33. });
  34. return;
  35. }
  36. res.send({
  37. success: true,
  38. data: {
  39. name: 'Serati Ma',
  40. avatar: 'https://gw.alipayobjects.com/zos/antfincdn/XAosXuNZyF/BiazfanxmamNRoxxVxka.png',
  41. userid: '00000001',
  42. email: 'antdesign@alipay.com',
  43. signature: '海纳百川,有容乃大',
  44. title: '交互专家',
  45. group: '蚂蚁金服-某某某事业群-某某平台部-某某技术部-UED',
  46. tags: [
  47. {
  48. key: '0',
  49. label: '很有想法的',
  50. },
  51. {
  52. key: '1',
  53. label: '专注设计',
  54. },
  55. {
  56. key: '2',
  57. label: '辣~',
  58. },
  59. {
  60. key: '3',
  61. label: '大长腿',
  62. },
  63. {
  64. key: '4',
  65. label: '川妹子',
  66. },
  67. {
  68. key: '5',
  69. label: '海纳百川',
  70. },
  71. ],
  72. notifyCount: 12,
  73. unreadCount: 11,
  74. country: 'China',
  75. access: getAccess(),
  76. geographic: {
  77. province: {
  78. label: '浙江省',
  79. key: '330000',
  80. },
  81. city: {
  82. label: '杭州市',
  83. key: '330100',
  84. },
  85. },
  86. address: '西湖区工专路 77 号',
  87. phone: '0752-268888888',
  88. },
  89. });
  90. },
  91. // GET POST 可省略
  92. 'GET /api/users': [
  93. {
  94. key: '1',
  95. name: 'John Brown',
  96. age: 32,
  97. address: 'New York No. 1 Lake Park',
  98. },
  99. {
  100. key: '2',
  101. name: 'Jim Green',
  102. age: 42,
  103. address: 'London No. 1 Lake Park',
  104. },
  105. {
  106. key: '3',
  107. name: 'Joe Black',
  108. age: 32,
  109. address: 'Sidney No. 1 Lake Park',
  110. },
  111. ],
  112. 'POST /api/login/account': async (req, res) => {
  113. const { password, username, type } = req.body;
  114. await waitTime(2000);
  115. if (password === 'ant.design' && username === 'admin') {
  116. res.send({
  117. status: 'ok',
  118. type,
  119. currentAuthority: 'admin',
  120. });
  121. access = 'admin';
  122. return;
  123. }
  124. if (password === 'ant.design' && username === 'user') {
  125. res.send({
  126. status: 'ok',
  127. type,
  128. currentAuthority: 'user',
  129. });
  130. access = 'user';
  131. return;
  132. }
  133. if (type === 'mobile') {
  134. res.send({
  135. status: 'ok',
  136. type,
  137. currentAuthority: 'admin',
  138. });
  139. access = 'admin';
  140. return;
  141. }
  142. res.send({
  143. status: 'error',
  144. type,
  145. currentAuthority: 'guest',
  146. });
  147. access = 'guest';
  148. },
  149. 'POST /api/login/outLogin': (req, res) => {
  150. access = '';
  151. res.send({
  152. data: {},
  153. success: true,
  154. });
  155. },
  156. 'POST /api/register': (req, res) => {
  157. res.send({
  158. status: 'ok',
  159. currentAuthority: 'user',
  160. success: true,
  161. });
  162. },
  163. 'GET /api/500': (req, res) => {
  164. res.status(500).send({
  165. timestamp: 1513932555104,
  166. status: 500,
  167. error: 'error',
  168. message: 'error',
  169. path: '/base/category/list',
  170. });
  171. },
  172. 'GET /api/404': (req, res) => {
  173. res.status(404).send({
  174. timestamp: 1513932643431,
  175. status: 404,
  176. error: 'Not Found',
  177. message: 'No message available',
  178. path: '/base/category/list/2121212',
  179. });
  180. },
  181. 'GET /api/403': (req, res) => {
  182. res.status(403).send({
  183. timestamp: 1513932555104,
  184. status: 403,
  185. error: 'Forbidden',
  186. message: 'Forbidden',
  187. path: '/base/category/list',
  188. });
  189. },
  190. 'GET /api/401': (req, res) => {
  191. res.status(401).send({
  192. timestamp: 1513932555104,
  193. status: 401,
  194. error: 'Unauthorized',
  195. message: 'Unauthorized',
  196. path: '/base/category/list',
  197. });
  198. },
  199. 'GET /api/login/captcha': getFakeCaptcha,
  200. };