// store/index.js
import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    user: null
  },
  mutations: {
    SET_USER(state, user) {
      state.user = user;
      // 同时保存到localStorage
      if (user && user.token) {
        localStorage.setItem('token', user.token);
      }
    },
    LOGOUT(state) {
      state.user = null;
      localStorage.removeItem('token');
    }
  },
  getters: {
    isLoggedIn: state => !!state.user
  }
})
