index.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. }
  45. },
  46. login ({ commit }, params) {
  47. return new Promise((resolve, reject) => {
  48. this.$axios.get(`auth/getToken?code=${params}`).then(res => {
  49. const token = res.result.accessToken
  50. setToken('Token', token)
  51. localStorage.setItem('token',token)
  52. commit('SET_TOKEN', token)
  53. resolve(token)
  54. }).catch(err => {
  55. reject(err)
  56. })
  57. })
  58. },
  59. }