陶杰
2025-01-09 cc1ee1fb090b8344faab02537dec80995cec93fe
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
const state = () => {
  return {
    access_token: '',
    refresh_token: '',
    userInfo: null,
  }
}
 
// getters
const getters = {
  nickName(state) {
    return state.userInfo ? state.userInfo.nickName : ''
  },
  tel(state) {
    return state.userInfo ? state.userInfo.tel : ''
  },
  avatar(state) {
    return state.userInfo ? state.userInfo.image : ''
  },
  loginName(state) {
    return state.userInfo ? state.userInfo.loginName : ''
  },
}
 
// actions
const actions = {
  clearLoginInfo({ commit }) {
    this.$elBusCookie.removeToken()
    commit('SET_USERINFO', null)
  },
  async getUserInfo({ commit }) {
    const { code, data } = await this.$services.auth.getUserInfo()
    if (code === 0) {
      const routes = data.menus || []
      commit('permission/SET_ROUTES', routes, { root: true })
      commit('SET_USERINFO', { ...data })
    }
    return { code, data }
  },
  async login({ commit, dispatch }, payload) {
    const { code, data } = await this.$services.auth.login(payload)
    if (code === 0) {
      this.$elBusCookie.setToken(data.access_token, data.refresh_token)
      return await dispatch('getUserInfo')
    }
    return {
      code,
      data,
    }
  },
  async smsLogin({ commit, dispatch }, payload) {
    const { code, data } = await this.$services.auth.smsLogin(payload)
    if (code === 0) {
      this.$elBusCookie.setToken(data.access_token, data.refresh_token)
      return await dispatch('getUserInfo')
    }
    return {
      code,
      data,
    }
  },
}
 
// mutations
const mutations = {
  SET_USERINFO(state, userInfo) {
    state.userInfo = userInfo
  },
}
 
export default {
  namespaced: true,
  state,
  getters,
  actions,
  mutations,
}