import Vue from 'vue';
import Vuex from 'vuex';
import { getConfig } from '../api/setting.js';
Vue.use(Vuex);

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

const actions = {
  getUserInfo(context) {
    getConfig().then(res => {
      if (res.code === 200) {
        context.commit('SET_USERINFO', res.data)
      } else {
        this.$message({
          type: 'error',
          message: res.msg
        })
      }
    })
  }
}

const mutations = {
  SET_USERINFO: (state, userInfo) => {
    console.log('进来了', userInfo);
    state.userInfo = userInfo;
    // 防止页面刷新，导致Vuex重启
    localStorage.setItem('userInfo', JSON.stringify(userInfo));
  }
}

const getters = {
  userInfo: (state) => {
    return state.userInfo
  }
}

const store = new Vuex.Store({
  actions,
  mutations,
  state,
  getters
})

export default store;
