const state = {
  userInfo: JSON.parse(localStorage.getItem('userInfo') || '{}')
}

// getters
const getters = {
  userName: (state, getters, rootState) => {
    return state.userInfo.nickName
  },
  userInfo: (state, getters, rootState) => {
    return state.userInfo.hasOwnProperty('id') ? state.userInfo : JSON.parse(localStorage.getItem('userInfo') || '{}')
  },
}

// actions
const actions = {
  setUserInfo ({ state, commit }, user) {
    // 这里可以放异步方法
    commit('setUserInfo', user)
  }
}

// mutations
const mutations = {
  setUserInfo (state, user) {
    console.log(JSON.stringify(state))
    state.userInfo=user
    localStorage.setItem('userInfo', JSON.stringify(user))
  },

  setUserTheme (state, theme) {
    state.userInfo.theme=theme
    localStorage.setItem('userInfo', JSON.stringify(state.userInfo))
  }
}

export default {
  namespaced: true,
  state,
  getters,
  actions,
  mutations
}
