user.js 823 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import { getToken, setToken, removeToken } from '@/utils/auth'
  2. import Cache from '@/utils/cache'
  3. const getDefaultState = () => {
  4. return {
  5. token: getToken(),
  6. info: Cache.get('userInfo')
  7. }
  8. }
  9. const state = getDefaultState()
  10. const mutations = {
  11. SET_TOKEN: (state, token) => {
  12. setToken(token)
  13. state.token = token
  14. },
  15. SET_INFO: (state, info) => {
  16. Cache.set('userInfo', info)
  17. state.info = info
  18. },
  19. CLEAR_INFO: (state) => {
  20. removeToken()
  21. Cache.remove('userInfo')
  22. state.token = null
  23. state.info = null
  24. }
  25. }
  26. const actions = {
  27. token({ commit }, token) {
  28. commit('SET_TOKEN', token)
  29. },
  30. info({ commit }, info) {
  31. commit('SET_INFO', info)
  32. },
  33. clear({ commit }) {
  34. commit('CLEAR_INFO')
  35. }
  36. }
  37. export default {
  38. namespaced: true,
  39. state,
  40. mutations,
  41. actions
  42. }