import Vue from "vue"
import Vuex from "vuex"
Vue.use(Vuex)

import { userInfo } from "../api/index.js"

const store=new Vuex.Store({
	state:{
		cert:"",	//认证状态 0-未认证 1-已认证
		token:"",
		openid:"",
		promise:{	// 查看承诺书
			url:"",
			time:""
		},
		updateUser: false,
		userinfo:{}		// 用户信息
	},
	mutations:{
		change_cert(state,cert){
			state.cert=cert
		},
		change_token(state,token){
			state.token=token
		},
		change_openid(state,openid){
			state.openid=openid
		},
		change_promise(state,promise){
			state.promise=promise
		},
		change_userinfo(state,userinfo){
			state.userinfo=userinfo
		},
		CHANGE_UPDATEUSER(state,updateUser){
			state.updateUser=updateUser
		}
	},
	actions:{
		getUser({commit}){
			return new Promise(async (resolve, reject)=>{
				await userInfo().then(res => {
					if(res.data){
						const { data } = res
						commit("change_userinfo", data)
						uni.$emit('haveUserInfo')
					}
					resolve()
				})
			})
		}
	}
})

export default store;