index.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. if(!data.avatarUrl && Object.keys(data).length !== 0){
  18. if(data.subscriberType === 1){
  19. data.avatarUrl = " http://data-center-mainland-dev.oss-cn-shanghai.aliyuncs.com/avatars/2023/01/03/0577b907be980556bdafa23fb0be0732-jpg.jpg"
  20. }else{
  21. data.avatarUrl = "http://data-center-mainland-dev.oss-cn-shanghai.aliyuncs.com/avatars/2023/01/03/f183029426a996325853fa244e7df661-jpg.jpg"
  22. }
  23. }
  24. state.userInfo = data
  25. },
  26. SET_TOKEN(state, data) {
  27. state.token = data
  28. },
  29. DEL_TOKEN(state,data) {
  30. state.token = data
  31. },
  32. OPEN_LOGIN(state, data) {
  33. state.globalLoginVisiable = data
  34. },
  35. SET_INTERFACE(state, data) {
  36. state.globalInterfaceType = data
  37. }
  38. }
  39. export const actions = {
  40. nuxtServerInit ({commit, state}, {req}) {
  41. let token = null
  42. let userInfo = null
  43. if (req && req.headers && req.headers.cookie) {
  44. const parsed = cookieParse(req.headers.cookie)
  45. try {
  46. token = parsed?.Token || ''
  47. userInfo = JSON.parse(decodeURIComponent(parsed.vuex)).userInfo
  48. } catch (err) {
  49. }
  50. commit('SET_TOKEN', token)
  51. }
  52. },
  53. login ({ commit }, params) {
  54. return new Promise((resolve, reject) => {
  55. this.$axios.get(`auth/getToken?code=${params}`).then(res => {
  56. const token = res.result.accessToken
  57. setToken('Token', token)
  58. localStorage.setItem('token',token)
  59. commit('SET_TOKEN', token)
  60. resolve(token)
  61. }).catch(err => {
  62. reject(err)
  63. })
  64. })
  65. },
  66. }