import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex);

const store = new Vuex.Store({
    state: {
        hasLogin: false,
        accessToken: '',
        userInfo: null,
    },
    mutations: {
        login(state, token) {
            state.hasLogin = true;
            state.accessToken = token;
            localStorage.setItem('accessToken', token)
        },
        logout(state) {
            state.hasLogin = false;
            state.accessToken = "";
            state.userInfo = null;
            localStorage.removeItem('accessToken')
            localStorage.removeItem('userInfo')
        }
    },
    getters: {},
    actions: {
        /**
         * 登录并获取用户基础信息
         * @param dispatch
         * @param commit
         * @param token
         * @returns {Promise<void>}
         */
        async doLogin({commit}, token) {
            commit('login', token)
        },
        /**
         * 登出方法
         * @param commit
         * @returns {Promise<void>}
         */
        async doLogout({commit}) {
            commit('logout')
        }
    }
});

export default store
