user.js 634 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. import { getToken, setToken } 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. }
  20. const actions = {
  21. token({ commit }, token) {
  22. commit('SET_TOKEN', token)
  23. },
  24. info({ commit }, info) {
  25. commit('SET_INFO', info)
  26. }
  27. }
  28. export default {
  29. namespaced: true,
  30. state,
  31. mutations,
  32. actions
  33. }