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

Vue.use(Vuex)

const store = new Vuex.Store({
	state: {
		hasLogin: false,
		sessionId:'',
		showModal: false, //登录授权弹窗
		userInfo: {},
	},
	mutations: {
		login(state, provider) {
			state.hasLogin = true;
			state.userInfo = provider;
			uni.setStorage({//缓存用户登陆状态
			    key: 'userInfo',  
			    data: provider  
			}) 
			console.log(state.userInfo);
		},
		
		setOpenid(state, provider) {
			state.sessionId = provider;
			uni.setStorage({//缓存用户登陆状态
			    key: 'sessionId',  
			    data: provider  
			}) 
			
		},
		showDialog(state, status) {
			state.showModal = status;
			uni.setStorage({//缓存用户登陆状态
			    key: 'showModal',  
			    data: status  
			}) 
			console.log(state.showModal);
		},
		
		
		logout(state) {
			state.hasLogin = false;
			state.userInfo = {};
			state.sessionId = '';
			uni.removeStorage({  
		        key: 'userInfo'  
		    })
			uni.removeStorage({
			    key: 'sessionId'  
			})
		}
	},
	// actions: {
	// 	// lazy loading openid
	// 	getUserOpenId: async function ({
	// 		commit,
	// 		state
	// 	}) {
	// 		return await new Promise((resolve, reject) => {
	// 			if (state.openid) {
	// 				resolve(state.openid)
	// 			} else {
	// 				uni.login({
	// 					success: (data) => {
	// 						commit('login')
	// 						setTimeout(function () { //模拟异步请求服务器获取 openid
	// 							const openid = '123456789'
	// 							commit('setOpenid', openid)
	// 							resolve(openid)
	// 						}, 1000)
	// 					},
	// 					fail: (err) => {
	// 						console.log('uni.login 接口调用失败，将无法正常使用开放接口等服务', err)
	// 						reject(err)
	// 					}
	// 				})
	// 			}
	// 		})
	// 	}
	// }
})

export default store

