// 用户数据模块
import http from '@/shopro/request/index'
import api from "@/shopro/request/index";
const state = {
	mallId:'106975910000477',
	sessionkey: uni.getStorageSync("sessionkey") || "",
	userInfo: {},
	registerInfo: {//注册信息
		companyId: '999999998', //开放式传
		mobile: '', //封闭式传
		empcardno: '', //封闭式传
		registerType: 'register' //注册时的类型为普通注册
	},
	// weRunData:{//今日步数
	// 	todayNum: 0,
	// 	totalNum: 0,
	// 	updateDate: "",
	// 	yesterdayNum: 0
	// },
	todayNum:0
}

const getters = {
	token: state => state.token,
	userInfo: state => state.userInfo,
	todayNum: state => state.todayNum
}

const actions = {
	/***获取用户信息
		无token 走获取用户信息接口。
		有token 走校验authcheck信息
	 */ 
	async getUserInfo({
		dispatch,
		commit,
		state
	}, code) {
		return new Promise((resolve, reject) => {
			let sessionStatus = false;
			uni.checkSession({ //校验session
				success(res) {
					if (res.errMsg === "checkSession:ok") sessionStatus = true;
				},
				complete() {
					let mallId = '106975910000477'
					uni.setStorageSync("mallId", mallId)
					if (uni.getStorageSync("sessionKey") && sessionStatus) {
						resolve(uni.getStorageSync("sessionKey"));
					} else {
						uni.login({
							success: function(info) {
								let code = info.code;
								http('user.getHomePage', {
									code: code,
									mallId: mallId
								}).then(res => {
									if (!res.code) {
										let { sessionkey } = res.data
										uni.setStorageSync("sessionKey", sessionkey)
										commit('userInfo', res.data);
										let token = encodeURI(JSON.stringify(res.data))
										uni.setStorageSync("token", token)
										dispatch('authCheck')
										resolve(res)
									}
								})
							}
						})
					}
				}
			})
		})
	},
	// 校验接口过期
	authCheck({
		commit,
		dispatch,
		state
	}) {
		return new Promise((resolve, reject) => {
			let token = uni.getStorageSync('token');
			let mallId = uni.getStorageSync('mallId');
			if(token) {
				state.userInfo = JSON.parse(decodeURI(token))
				uni.setStorageSync("sessionKey",state.userInfo.sessionKey)
			}
			api("user.authCheck", {
				mallId: mallId
			}).then(res => {
				let data = res.data;
				if (res.code === 0) {
					resolve(res.code)
				} else if (res.code == 1001) {
					dispatch('getUserInfo');
					reject(res.code)
				} else {
					reject(res.code)
				}
			});
		})
	},
	// 获取手机号
	getPhoneNumber({commit},e){
		return new Promise((resolve,reject) => {
			let code = e.detail.code;
			if(!code){
				uni.showToast({
					title: code,
					duration: 2000,
					icon:'none'
				});
			}else{
				http('user.miniPhone',{code:code,mallId:state.mallId}).then(res=>{
					if (!res.code && res.data) {
						state.registerInfo.mobile = res.data;
						resolve(res.data);
					} else {
						uni.showToast({
							title: res.msg,
							duration: 2000,
							icon:'none'
						});
						reject(0);
					}
				})
			}
		})
	},
	// 注册用户
	registerInfoFn({commit,state},e){
		return new Promise((resolve,reject) => {
			if(!state.registerInfo.mobile){
				uni.showToast({
					title: "手机号为空",
					duration: 2000,
					icon:'none'
				});
			}
			http('user.register', state.registerInfo).then(res => {
				if (res.code==0) {
					resolve(1);
				} else{
					uni.showToast({
						title: res.msg,
						duration: 2000,
						icon:'none'
					});
					reject(res.data);
				}
			})
		})
	},
	// 查询步数
	getWeRunDataFn({state}){
		return new Promise((resolve,reject) => {
			let wRunEncryptedData = {}
			uni.getWeRunData({
				success: function(res) {
					wRunEncryptedData.encryptedData = res.encryptedData;
					wRunEncryptedData.iv = res.iv;
					http('user.wxRun', wRunEncryptedData).then(res => {
						if (!res.code && res.data) {
							state.todayNum = res.data.todayNum;
							resolve(res.data);
						} else {
							state.todayNum = 0;
							uni.showToast({
								title: res.msg,
								duration: 2000,
								icon:'none'
							});
							reject(res.data);
						}
					})
				}
			})
		})
	}
	
 
}
const mutations = {
	token(state, payload) {
		state.token = payload;
		uni.setStorageSync("token", payload);
	}

}
export default {
	state,
	mutations,
	actions,
	getters
}
