baizonghao
2023-08-04 8fee5b265eaa379b7a1cc51cd060a368c046de46
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
import Cookies from 'js-cookie'
import userApi from '@/api/user'
// initial state
const state = {
  userName: Cookies.get('XzsStudentUserName'),
  userInfo: Cookies.get('XzsStudentUserInfo'),
  imagePath: Cookies.get('XzsStudentImagePath'),
  messageCount: 0
}
 
// actions
const actions = {
  initUserInfo ({ commit }) {
    userApi.getCurrentUser().then(re => {
      commit('setUserInfo', re.response)
    })
  },
  getUserMessageInfo ({ commit }) {
    userApi.getMessageCount().then(re => {
      commit('setMessageCount', re.response)
    })
  }
}
 
// mutations
const mutations = {
  setUserName (state, userName) {
    state.userName = userName
    Cookies.set('XzsStudentUserName', userName, { expires: 30 })
  },
  setUserInfo (state, userName) {
    state.userName = userName
    Cookies.set('XzsStudentUserName', userName, { expires: 30 })
  },
  setImagePath: (state, imagePath) => {
    state.imagePath = imagePath
    Cookies.set('XzsStudentImagePath', imagePath, { expires: 30 })
  },
  setMessageCount: (state, messageCount) => {
    state.messageCount = messageCount
  },
  messageCountSubtract: (state, num) => {
    state.messageCount = state.messageCount - num
  },
  clearLogin (state) {
    Cookies.remove('XzsStudentUserName')
    Cookies.remove('XzsStudentUserInfo')
  }
}
 
export default {
  namespaced: true,
  state,
  mutations,
  actions
}