user.ts 4.5 KB

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