index.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. * @Description:
  3. * @Author: 欧阳承珺
  4. * @LastEditors: 欧阳承珺
  5. * @Date: 2022-10-25 17:19:19
  6. * @LastEditTime: 2022-11-16 13:51:41
  7. */
  8. import { setToken, cookieParse } from '~/utils/cookie'
  9. export const state = () => ({
  10. userInfo: {},
  11. token: '',
  12. globalLoginVisiable: false,
  13. globalInterfaceType: false
  14. })
  15. export const mutations = {
  16. setUser(state, data) {
  17. state.userInfo = data
  18. },
  19. SET_TOKEN(state, data) {
  20. state.token = data
  21. },
  22. DEL_TOKEN(state,data) {
  23. state.token = data
  24. },
  25. OPEN_LOGIN(state, data) {
  26. state.globalLoginVisiable = data
  27. },
  28. SET_INTERFACE(state, data) {
  29. state.globalInterfaceType = data
  30. }
  31. }
  32. export const actions = {
  33. nuxtServerInit ({commit, state}, {req}) {
  34. let token = null
  35. let userInfo = null
  36. if (req && req.headers && req.headers.cookie) {
  37. const parsed = cookieParse(req.headers.cookie)
  38. try {
  39. token = parsed?.Token || ''
  40. userInfo = JSON.parse(decodeURIComponent(parsed.vuex)).userInfo
  41. } catch (err) {
  42. }
  43. commit('SET_TOKEN', token)
  44. commit('setUser', userInfo)
  45. }
  46. },
  47. login ({ commit }, params) {
  48. return new Promise((resolve, reject) => {
  49. this.$axios.get(`auth/getToken?code=${params}`).then(res => {
  50. const token = res.result.accessToken
  51. setToken('Token', token)
  52. localStorage.setItem('token',token)
  53. commit('SET_TOKEN', token)
  54. resolve(token)
  55. }).catch(err => {
  56. reject(err)
  57. })
  58. })
  59. },
  60. }