user.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. success: true,
  40. data: {
  41. name: 'Serati Ma',
  42. avatar: 'https://gw.alipayobjects.com/zos/antfincdn/XAosXuNZyF/BiazfanxmamNRoxxVxka.png',
  43. userid: '00000001',
  44. email: 'antdesign@alipay.com',
  45. signature: '海纳百川,有容乃大',
  46. title: '交互专家',
  47. group: '蚂蚁金服-某某某事业群-某某平台部-某某技术部-UED',
  48. tags: [
  49. {
  50. key: '0',
  51. label: '很有想法的',
  52. },
  53. {
  54. key: '1',
  55. label: '专注设计',
  56. },
  57. {
  58. key: '2',
  59. label: '辣~',
  60. },
  61. {
  62. key: '3',
  63. label: '大长腿',
  64. },
  65. {
  66. key: '4',
  67. label: '川妹子',
  68. },
  69. {
  70. key: '5',
  71. label: '海纳百川',
  72. },
  73. ],
  74. notifyCount: 12,
  75. unreadCount: 11,
  76. country: 'China',
  77. access: getAccess(),
  78. geographic: {
  79. province: {
  80. label: '浙江省',
  81. key: '330000',
  82. },
  83. city: {
  84. label: '杭州市',
  85. key: '330100',
  86. },
  87. },
  88. address: '西湖区工专路 77 号',
  89. phone: '0752-268888888',
  90. },
  91. });
  92. },
  93. // GET POST 可省略
  94. 'GET /api/users': [
  95. {
  96. key: '1',
  97. name: 'John Brown',
  98. age: 32,
  99. address: 'New York No. 1 Lake Park',
  100. },
  101. {
  102. key: '2',
  103. name: 'Jim Green',
  104. age: 42,
  105. address: 'London No. 1 Lake Park',
  106. },
  107. {
  108. key: '3',
  109. name: 'Joe Black',
  110. age: 32,
  111. address: 'Sidney No. 1 Lake Park',
  112. },
  113. ],
  114. 'POST /api/login/account': async (req: Request, res: Response) => {
  115. const { password, username, type } = req.body;
  116. await waitTime(2000);
  117. if (password === 'ant.design' && username === 'admin') {
  118. res.send({
  119. status: 'ok',
  120. type,
  121. currentAuthority: 'admin',
  122. });
  123. access = 'admin';
  124. return;
  125. }
  126. if (password === 'ant.design' && username === 'user') {
  127. res.send({
  128. status: 'ok',
  129. type,
  130. currentAuthority: 'user',
  131. });
  132. access = 'user';
  133. return;
  134. }
  135. if (type === 'mobile') {
  136. res.send({
  137. status: 'ok',
  138. type,
  139. currentAuthority: 'admin',
  140. });
  141. access = 'admin';
  142. return;
  143. }
  144. res.send({
  145. status: 'error',
  146. type,
  147. currentAuthority: 'guest',
  148. });
  149. access = 'guest';
  150. },
  151. 'POST /api/login/outLogin': (req: Request, res: Response) => {
  152. access = '';
  153. res.send({ data: {}, success: true });
  154. },
  155. 'POST /api/register': (req: Request, res: Response) => {
  156. res.send({ status: 'ok', currentAuthority: 'user', success: true });
  157. },
  158. 'GET /api/500': (req: Request, res: Response) => {
  159. res.status(500).send({
  160. timestamp: 1513932555104,
  161. status: 500,
  162. error: 'error',
  163. message: 'error',
  164. path: '/base/category/list',
  165. });
  166. },
  167. 'GET /api/404': (req: Request, res: Response) => {
  168. res.status(404).send({
  169. timestamp: 1513932643431,
  170. status: 404,
  171. error: 'Not Found',
  172. message: 'No message available',
  173. path: '/base/category/list/2121212',
  174. });
  175. },
  176. 'GET /api/403': (req: Request, res: Response) => {
  177. res.status(403).send({
  178. timestamp: 1513932555104,
  179. status: 403,
  180. error: 'Forbidden',
  181. message: 'Forbidden',
  182. path: '/base/category/list',
  183. });
  184. },
  185. 'GET /api/401': (req: Request, res: Response) => {
  186. res.status(401).send({
  187. timestamp: 1513932555104,
  188. status: 401,
  189. error: 'Unauthorized',
  190. message: 'Unauthorized',
  191. path: '/base/category/list',
  192. });
  193. },
  194. 'GET /api/login/captcha': getFakeCaptcha,
  195. };