// 页面路径：store/index.js

import Vue from 'vue'
import Vuex from 'vuex'
import persistedstate from 'vuex-persistedstate'


Vue.use(Vuex); //vue的插件机制

//Vuex.Store 构造器选项

const store = new Vuex.Store({

	actions: {
		//相当于异步的操作,不能直接改变state的值，只能通过触发mutations的方法才能改变
		asyncUpdateUser(context, user) {
			context.commit('updateUser',user);
		}
	},
	mutations: {
		//相当于同步的操作
		updateUser(state, user) {
			state.user = user;
		},
		// 修改头像
		updateAvatar(state, data) {
			state.user.avatar = data;
		},
		// 修改昵称nc
		updateNc(state, data) {
			state.user.nc = data;
		},
		// 添加聊天信息
		addMsg(state, data) {
			state.message.push(data)
		}
	},
	state: {
		//公共的变量，存储数据，这里的变量不能随便修改，只能通过触发mutations的方法才能改变
		user: {
			openid:'',
			avatar: "",
			desc: "",
			email: "",
			phone: "",
			sex: "",
			token: "",
			username: "666",
			__v: 0,
			_id: "6523ac0dd4e4de99a9e86da2"
		},
		// 聊天记录
		message:[]
	},
	getters: {
		getUser(state) {
			return state.user;
		},
		getMsg(state){
			return state.message;
		}
	},
	 plugins: [persistedstate({
	 // 传入参数
	 		storage: {
	 			getItem: (key) => uni.getStorageSync(key),
	 			setItem: (key, val) => uni.setStorageSync(key,val),
	 			removeItem: (key) => uni.removeStorageSync(key)
	 		}
	 })]
})

export default store