import Vue from 'vue'
import Vuex from 'vuex'
import { getInfo } from '@/api'
import { getToken } from '@/utils/auth'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    // 用户信息
    userInfo: {}
  },
  mutations: {
    SET_USER_INFO: (state, userInfo) => {
      state.userInfo = userInfo
    }
  },
  actions: {
    // 获取用户信息
    GetInfo ({ commit, state }) {
      return new Promise((resolve, reject) => {
        getInfo(getToken()).then(res => {
          commit('SET_USER_INFO', res.user)
          resolve(res)
        }).catch(error => {
          reject(error)
        })
      })
    }
  },
  modules: {}
})
