// 用户数据模块
import http from '@/shopro/request/index'
import api from "@/shopro/request/index";
import {
	MALL_ID,
	COMPANY_ID
} from "@/env";
const state = {
	mallId:MALL_ID,
	sessionkey: uni.getStorageSync("sessionkey") || "",
	userInfo: {},
	registerInfo: {//注册信息
		companyId: COMPANY_ID, //开放式传
		mobile: '', //封闭式传
		empcardno: '', //封闭式传
		registerType: 'register' //注册时的类型为普通注册
	},
	wxCode:'',
	todayNum:0,
	sessionStatus:false
}

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

const actions = {
	/***获取用户信息
		无token 走获取用户信息接口。
		有token 走校验authcheck信息
	*/ 
	async getUserInfo({
		dispatch,
		commit,
		state
	}, code) {
		return new Promise((resolve, reject) => {
			
			uni.setStorageSync("mallId", state.mallId);
			http('user.getHomePage', {
				code: code,
				mallId: state.mallId
			}).then(res => {
				if (!res.code) {
					let { sessionKey } = res.data;
					uni.setStorageSync("sessionKey", sessionKey)
					// commit('userInfo', res.data);
					state.userInfo = res.data;
					let token = encodeURI(JSON.stringify(res.data))
					uni.setStorageSync("token", token)
					resolve(1);
				}else{
					uni.showToast({
						title: `${res.code} ${res.msg}`,
						duration: 2000,
						icon:'none'
					});
					resolve(0)
				}
			})
		})
	},
	// 校验接口过期
	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 => {
				console.log("authcheck状态码=========================")
				console.log(res)
				let data = res.data;
				if (res.code === 0) {
					resolve(res.code)
				} else if (res.code == 2001) {
					uni.login({
						success: function(info) { //调用code
							state.wxCode == info.code;
							dispatch('getUserInfo',info.code);
						}
					})
					resolve(res.code);
				} else {
					uni.showToast({
						title: res.msg,
						duration: 2000,
						icon:'none'
					});
					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 if(res.code == 3504){
					uni.showToast({
						title: "抱歉，本应用仅向本单位工会内部授权手机号开放使用，您的手机号暂未授权！",
						duration: 2000,
						icon:'none'
					});
					reject(res.data);
				}else{
					uni.showToast({
						title: res.msg,
						duration: 2000,
						icon:'none'
					});
					reject(res.data);
				}
			})
		})
	},
	// 查询步数
	getWeRunDataFn({state}){
		return new Promise((resolve,reject) => {
			// let wRunEncryptedData = {}
			let data= ""
			wx.getWeRunData({
				success: function(res) {
					let obj = {
						encryptedData:res.encryptedData,
						iv:res.iv,
						sessionKey:uni.getStorageSync("sessionKey")
					}
					data = JSON.stringify(obj)
					// wRunEncryptedData.encryptedData = encodeURIComponent(res.encryptedData);
					// wRunEncryptedData.iv = encodeURIComponent(res.iv);
					// wRunEncryptedData.sessionKey = encodeURIComponent(uni.getStorageSync("sessionKey") );
					http('user.wxRun', {data:data}).then(res => {
						console.log("微信步数++=========================")
						console.log(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'
							});
							resolve(0);
						}
					})
				},
				fail:function(err){
					console.log(err)
					resolve(0);
					uni.showToast({
						title: "您未授权微信步数，请点击右上角...进去设置开启授权",
						duration: 2000,
						icon:'none'
					});
				}
				
			})
		})
	}
	
 
}
const mutations = {
	token(state, payload) {
		state.token = payload;
		uni.setStorageSync("token", payload);
	},
	wxCode(state,code) {
		state.wxCode = code;
	}

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