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

Vue.use(Vuex);
let store = new Vuex.Store({
	state: {
		loginStatus: 0,
		token: '',
		userName: ''
	},
	mutations: {
		loginSuccess(state) {
			state.userName = db.get('userName');
			state.loginStatus = 1
		},
		loginFail(state) {
			state.loginStatus = 0
		},
		updateToken(state, token) {

			state.token = token;
			db.set('token', token);

		},
		loginOut(state) {
			db.remove('token');
			state.loginStatus = 0;
			state.token = "";
		}
	},
	actions: {
		loginAction({
			commit
		}, token) {

			commit('loginSuccess');
			commit('updateToken', token)
		},
		tokenAction({
			commit
		}, token) {
			commit('updateToken', token)
		},
		loginOutAction({
			commit
		}) {
			commit('loginOut')
		}

	}
})
// 页面刷新时，重新赋值token
let token = db.get('token');
if (token) {
	store.commit('updateToken', token);
	store.commit('loginSuccess')
}

export default store
