user.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. import { Request, Response } from 'express';
  2. const waitTime = (time: number = 100) => {
  3. return new Promise((resolve) => {
  4. setTimeout(() => {
  5. resolve(true);
  6. }, time);
  7. });
  8. };
  9. async function getFakeCaptcha(req: Request, res: Response) {
  10. await waitTime(2000);
  11. return res.json('captcha-xxx');
  12. }
  13. const { ANT_DESIGN_PRO_ONLY_DO_NOT_USE_IN_YOUR_PRODUCTION } = process.env;
  14. /**
  15. * 当前用户的权限,如果为空代表没登录
  16. * current user access, if is '', user need login
  17. * 如果是 pro 的预览,默认是有权限的
  18. */
  19. let access = ANT_DESIGN_PRO_ONLY_DO_NOT_USE_IN_YOUR_PRODUCTION === 'site' ? 'admin' : '';
  20. const getAccess = () => {
  21. return access;
  22. };
  23. // 代码中会兼容本地 service mock 以及部署站点的静态数据
  24. export default {
  25. // 支持值为 Object 和 Array
  26. 'GET /api/currentUser': (req: Request, res: Response) => {
  27. if (!getAccess()) {
  28. res.status(401).send({
  29. data: {
  30. isLogin: false,
  31. },
  32. errorCode: '401',
  33. errorMessage: '请先登录!',
  34. success: true,
  35. });
  36. return;
  37. }
  38. res.send({
  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. // GET POST 可省略
  91. 'GET /api/users': [
  92. {
  93. key: '1',
  94. name: 'John Brown',
  95. age: 32,
  96. address: 'New York No. 1 Lake Park',
  97. },
  98. {
  99. key: '2',
  100. name: 'Jim Green',
  101. age: 42,
  102. address: 'London No. 1 Lake Park',
  103. },
  104. {
  105. key: '3',
  106. name: 'Joe Black',
  107. age: 32,
  108. address: 'Sidney No. 1 Lake Park',
  109. },
  110. ],
  111. 'POST /api/login/account': async (req: Request, res: Response) => {
  112. const { password, username, type } = req.body;
  113. await waitTime(2000);
  114. if (password === 'ant.design' && username === 'admin') {
  115. res.send({
  116. status: 'ok',
  117. type,
  118. currentAuthority: 'admin',
  119. });
  120. access = 'admin';
  121. return;
  122. }
  123. if (password === 'ant.design' && username === 'user') {
  124. res.send({
  125. status: 'ok',
  126. type,
  127. currentAuthority: 'user',
  128. });
  129. access = 'user';
  130. return;
  131. }
  132. if (type === 'mobile') {
  133. res.send({
  134. status: 'ok',
  135. type,
  136. currentAuthority: 'admin',
  137. });
  138. access = 'admin';
  139. return;
  140. }
  141. res.send({
  142. status: 'error',
  143. type,
  144. currentAuthority: 'guest',
  145. });
  146. access = 'guest';
  147. },
  148. 'GET /api/login/outLogin': (req: Request, res: Response) => {
  149. access = '';
  150. res.send({ data: {}, success: true });
  151. },
  152. 'POST /api/register': (req: Request, res: Response) => {
  153. res.send({ status: 'ok', currentAuthority: 'user', success: true });
  154. },
  155. 'GET /api/500': (req: Request, res: Response) => {
  156. res.status(500).send({
  157. timestamp: 1513932555104,
  158. status: 500,
  159. error: 'error',
  160. message: 'error',
  161. path: '/base/category/list',
  162. });
  163. },
  164. 'GET /api/404': (req: Request, res: Response) => {
  165. res.status(404).send({
  166. timestamp: 1513932643431,
  167. status: 404,
  168. error: 'Not Found',
  169. message: 'No message available',
  170. path: '/base/category/list/2121212',
  171. });
  172. },
  173. 'GET /api/403': (req: Request, res: Response) => {
  174. res.status(403).send({
  175. timestamp: 1513932555104,
  176. status: 403,
  177. error: 'Unauthorized',
  178. message: 'Unauthorized',
  179. path: '/base/category/list',
  180. });
  181. },
  182. 'GET /api/401': (req: Request, res: Response) => {
  183. res.status(401).send({
  184. timestamp: 1513932555104,
  185. status: 401,
  186. error: 'Unauthorized',
  187. message: 'Unauthorized',
  188. path: '/base/category/list',
  189. });
  190. },
  191. 'GET /api/login/captcha': getFakeCaptcha,
  192. };