6-1 修改request.js 拦截401错误

image.png
src/api/config/request.ts

  1. import axios, { AxiosError } from 'axios'
  2. import { ElMessage } from 'element-plus'
  3. import { getToken } from '../../utils/auth'
  4. import store from '@/store'
  5. const service = axios.create({
  6. baseURL: process.env.VUE_APP_BASE_API,
  7. timeout: 100000
  8. })
  9. service.interceptors.request.use(config => {
  10. const token = getToken()
  11. if (token) { // 携带token
  12. config.headers.Authorization = `Bearer ${token}`
  13. }
  14. return config
  15. }, error => {
  16. console.log(error)
  17. return Promise.reject(error)
  18. })
  19. service.interceptors.response.use(response => {
  20. const { code, message } = response.data
  21. if (code !== 0) { // 错误提示
  22. ElMessage.error(message)
  23. return Promise.reject(message)
  24. }
  25. return response.data
  26. }, (error: AxiosError) => {
  27. const res = error?.response
  28. if (res && res.status === 401) { // 401 未登录 token失效
  29. store.dispatch('user/resetToken').then(() => {
  30. window.location.reload()
  31. })
  32. }
  33. ElMessage.error(error.message)
  34. return Promise.reject(error)
  35. })
  36. export default service

6-2 添加resetToken action

清空token

image.png
src/store/modules/user.ts

  1. import { Module, MutationTree, ActionTree } from 'vuex'
  2. import { IRootState } from '@/store'
  3. import { login } from '@/api/user'
  4. import { setToken, removeToken } from '@/utils/auth'
  5. // login params
  6. export interface IUserInfo {
  7. username: string;
  8. password: string;
  9. }
  10. // 定义state类型
  11. export interface IUserState {
  12. token: string;
  13. }
  14. // mutations类型
  15. type IMutations = MutationTree<IUserState>
  16. // actions类型
  17. type IActions = ActionTree<IUserState, IRootState>
  18. // 定义state
  19. const state: IUserState = {
  20. token: ''
  21. }
  22. // 定义mutation
  23. const mutations: IMutations = {
  24. SET_TOKEN(state, token: string) {
  25. state.token = token
  26. }
  27. }
  28. // 定义action
  29. const actions: IActions = {
  30. login({ commit }, userInfo: IUserInfo) {
  31. const { username, password } = userInfo
  32. return new Promise((resolve, reject) => {
  33. login({ username: username.trim(), password }).then(response => {
  34. const { data } = response
  35. commit('SET_TOKEN', data.token)
  36. setToken(data.token) // localStorage中保存token
  37. resolve(data)
  38. }).catch(error => {
  39. reject(error)
  40. })
  41. })
  42. },
  43. logout({ commit, dispatch }) {
  44. // 退出登录接口我这里就不写了
  45. return new Promise<void>((resolve) => {
  46. // 清空store里token
  47. commit('SET_TOKEN', '')
  48. // 清空localStorage里的token
  49. removeToken()
  50. // 清除所有tag views
  51. dispatch('tagsView/delAllViews', null, { root: true })
  52. resolve()
  53. })
  54. },
  55. resetToken({ commit }) {
  56. return new Promise<void>((resolve) => {
  57. // 清空store里token
  58. commit('SET_TOKEN', '')
  59. // 清空localStorage里的token
  60. removeToken()
  61. resolve()
  62. })
  63. }
  64. }
  65. // 定义user module
  66. const user: Module<IUserState, IRootState> = {
  67. namespaced: true,
  68. state,
  69. mutations,
  70. actions
  71. }
  72. export default user