index.js 1.4 KB

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