import Vue from 'vue'
import Vuex from 'vuex'
import http from '../api/httpRequest.js'
Vue.use(Vuex)

const store = new Vuex.Store({
	state: {
		token: '',
		userInfo: uni.getStorageSync('userInfo') ? JSON.parse(uni.getStorageSync('userInfo')) : {},
	},

	getters: {

	},

	mutations: {
		//更新state数据
		setStateAttr(state, param) {
			console.log(11);
			if (param instanceof Array) {
				for (let item of param) {
					state[item.key] = item.val;
				}
			} else {
				state[param.key] = param.val;
				uni.setStorageSync(param.key, JSON.stringify(param.val))
			}
		}
	},

	actions: {
		//更新用户信息
		async getUserInfo({
			state,
			commit
		}) {
			const res = await http.request('user/userInfo');
			console.log(res);
			if (res.code == '200') {
				const userInfo = res.data;
				uni.setStorageSync('userInfo', JSON.stringify(userInfo))
				commit('setStateAttr', {
					key: 'userInfo',
					val: userInfo
				})
			}
		},

	}
})

export default store
