auth.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import type { MockMethod } from 'vite-plugin-mock';
  2. import { userModel } from '../model';
  3. /** 参数错误的状态码 */
  4. const ERROR_PARAM_CODE = 10000;
  5. const ERROR_PARAM_MSG = '参数校验失败!';
  6. const apis: MockMethod[] = [
  7. // 获取验证码
  8. {
  9. url: '/mock/getSmsCode',
  10. method: 'post',
  11. response: (): Service.MockServiceResult<boolean> => {
  12. return {
  13. code: 200,
  14. message: 'ok',
  15. data: true
  16. };
  17. }
  18. },
  19. // 用户+密码 登录
  20. {
  21. url: '/mock/login',
  22. method: 'post',
  23. response: (options: Service.MockOption): Service.MockServiceResult<ApiAuth.Token | null> => {
  24. const { userName = undefined, password = undefined } = options.body;
  25. if (!userName || !password) {
  26. return {
  27. code: ERROR_PARAM_CODE,
  28. message: ERROR_PARAM_MSG,
  29. data: null
  30. };
  31. }
  32. const findItem = userModel.find(item => item.username === userName && item.password === password);
  33. if (findItem) {
  34. return {
  35. code: 200,
  36. message: 'ok',
  37. data: {
  38. token: findItem.token,
  39. refreshToken: findItem.refreshToken
  40. }
  41. };
  42. }
  43. return {
  44. code: 1000,
  45. message: '用户名或密码错误!',
  46. data: null
  47. };
  48. }
  49. },
  50. // 获取用户信息(请求头携带token, 根据token获取用户信息)
  51. {
  52. url: '/mock/getUserInfo',
  53. method: 'get',
  54. response: (options: Service.MockOption): Service.MockServiceResult<ApiAuth.UserInfo | null> => {
  55. // 这里的mock插件得到的字段是authorization, 前端传递的是Authorization字段
  56. const { authorization = '' } = options.headers;
  57. const REFRESH_TOKEN_CODE = 66666;
  58. if (!authorization) {
  59. return {
  60. code: REFRESH_TOKEN_CODE,
  61. message: '用户已失效或不存在!',
  62. data: null
  63. };
  64. }
  65. const userInfo: Auth.UserInfo = {
  66. id: 0,
  67. username: '',
  68. userRole: 'user',
  69. phone: '',
  70. email: '',
  71. permissions: [],
  72. departments: []
  73. };
  74. const isInUser = userModel.some(item => {
  75. const flag = item.token === authorization;
  76. if (flag) {
  77. const { id: itemUserId, username, userRole } = item;
  78. Object.assign(userInfo, { userId: itemUserId, username, userRole });
  79. }
  80. return flag;
  81. });
  82. if (isInUser) {
  83. return {
  84. code: 200,
  85. message: 'ok',
  86. data: userInfo
  87. };
  88. }
  89. return {
  90. code: REFRESH_TOKEN_CODE,
  91. message: '用户信息异常!',
  92. data: null
  93. };
  94. }
  95. },
  96. {
  97. url: '/mock/updateToken',
  98. method: 'post',
  99. response: (options: Service.MockOption): Service.MockServiceResult<ApiAuth.Token | null> => {
  100. const { refreshToken = '' } = options.body;
  101. const findItem = userModel.find(item => item.refreshToken === refreshToken);
  102. if (findItem) {
  103. return {
  104. code: 200,
  105. message: 'ok',
  106. data: {
  107. token: findItem.token,
  108. refreshToken: findItem.refreshToken
  109. }
  110. };
  111. }
  112. return {
  113. code: 3000,
  114. message: '用户已失效或不存在!',
  115. data: null
  116. };
  117. }
  118. }
  119. ];
  120. export default apis;