auth.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. userId: '',
  67. userName: '',
  68. userRole: 'user'
  69. };
  70. const isInUser = userModel.some(item => {
  71. const flag = item.token === authorization;
  72. if (flag) {
  73. const { userId: itemUserId, userName, userRole } = item;
  74. Object.assign(userInfo, { userId: itemUserId, userName, userRole });
  75. }
  76. return flag;
  77. });
  78. if (isInUser) {
  79. return {
  80. code: 200,
  81. message: 'ok',
  82. data: userInfo
  83. };
  84. }
  85. return {
  86. code: REFRESH_TOKEN_CODE,
  87. message: '用户信息异常!',
  88. data: null
  89. };
  90. }
  91. },
  92. {
  93. url: '/mock/updateToken',
  94. method: 'post',
  95. response: (options: Service.MockOption): Service.MockServiceResult<ApiAuth.Token | null> => {
  96. const { refreshToken = '' } = options.body;
  97. const findItem = userModel.find(item => item.refreshToken === refreshToken);
  98. if (findItem) {
  99. return {
  100. code: 200,
  101. message: 'ok',
  102. data: {
  103. token: findItem.token,
  104. refreshToken: findItem.refreshToken
  105. }
  106. };
  107. }
  108. return {
  109. code: 3000,
  110. message: '用户已失效或不存在!',
  111. data: null
  112. };
  113. }
  114. }
  115. ];
  116. export default apis;