import httpsRequest from '@/common/httpRequest.js'
import {
	$cache_config
} from '@/store/cashe';

function getIsPauseServe() {
	return new Promise((reslove, reject) => {
		uni.request({
			url: '',
			method: 'get',
			data: {},
			success() {
				reslove()
			}
		})
	})
}
const $map = {
	882: 'isWxIosPay',
	833: 'checkIosLogin',
	834: 'checkIosPay',
	835: 'checkWxLogin',
	836: 'checkPhoneLogin',
	108: 'isOpenWxWebAutoLogin',
	817: 'zhengbu',
	818: 'danbu',
	252: 'adUnitId',
	821: 'playType',
	251: 'isGuanggao',
	254: 'isGuanggaody',
	202: 'kefu',
	206: 'kefuPhone',
	204: 'kefuUrl',
	203: 'kefuAppId',
	248: 'isVips',
	249: 'moreSearch',
	49: 'AppUrl',
	823: 'OfferID',
	824: 'payEnv',
	825: 'moneyTips',
	855: 'kmPaySel',
	849: 'homeTypeSel',
	856: 'syPaySel',
	857: 'imId',
	858: 'isAccountPay',
	860: 'dyadUnitId',
	881: '',
	109: ''
}

function callback() {
	
	if (key == 'isOpenWxWebAutoLogin' && res.data[i] == '是') {
		// 是否开启公众号自动登陆 108
		let ua = navigator.userAgent.toLowerCase();
		if (ua.indexOf('micromessenger') !== -1) {
			let openid = uni.getStorageSync('openId');
			let userId = uni.getStorageSync('userId');
			if (!userId) {
				if (openid) {
					httpsRequest.get('/app/Login/openid/login?openId=' + openid).then(
						res => {
							if (res.code == 0) {
								uni.setStorageSync('token', res.token)
								uni.setStorageSync('userId', res.user.userId)
								uni.setStorageSync('userName', res.user.userName)
								uni.setStorageSync('phone', res.user.phone)
								uni.setStorageSync('invitationCode', res.user
									.invitationCode)
							}
						});
				}
			}
		}

	}

}
export async function init() {
	if($cache_config.isEmpty()){
		httpsRequest.getT('/app/common/getAppUseKv').then(res => {
			console.log(res);
			if (res.code == 0) {
				for (let i in $map) {
					const key = $map[i]
					if (key) {
						// uni.setStorageSync(key, res.data[i])
						$cache_config.set(key, res.data[i])
						// callback()
					}
				}
			}
		})
	}
	

	return
	// const res=await getIsPauseServe()

	// #ifdef APP
	//微信小程序IOS系统支付功能开关	882
	httpsRequest.getT('/app/common/type/882').then(res => {
		if (res.code == 0 && res.data) {
			let value = res.data.value
			const sysInfo = uni.getSystemInfoSync()
			// #ifdef MP-WEIXIN
			if (sysInfo.osName == 'ios') { //是ios系统
				if (value == '是') { //开启
					uni.setStorageSync('isWxIosPay', true)
				} else { //关闭
					uni.setStorageSync('isWxIosPay', false)
				}
			} else { //不是ios下，直接打开
				uni.setStorageSync('isWxIosPay', true)
			}
			// #endif
			//不在微信小程序也直接打开
			// #ifndef MP-WEIXIN
			uni.setStorageSync('isWxIosPay', true)
			// #endif
		}
	});
	//是否开启苹果登录	833
	httpsRequest.getT('/app/common/type/833').then(res => {
		if (res.code == 0 && res.data) {
			uni.setStorageSync('checkIosLogin', res.data.value)
		}
	});
	//是否开启苹果支付	834
	httpsRequest.getT('/app/common/type/834').then(res => {
		if (res.code == 0 && res.data) {
			uni.setStorageSync('checkIosPay', res.data.value)
		}
	});
	//是否开启微信登录	835
	httpsRequest.getT('/app/common/type/835').then(res => {
		if (res.code == 0 && res.data) {
			uni.setStorageSync('checkWxLogin', res.data.value)
		}
	});
	//是否开启手机号一键登录	836
	httpsRequest.getT('/app/common/type/836').then(res => {
		if (res.code == 0 && res.data) {
			uni.setStorageSync('checkPhoneLogin', res.data.value)
		}
	});

	// #endif

	// #ifdef H5
	// httpsRequest.getT('/app/common/type/108').then(res => { //// 是否开启公众号自动登陆 108
	// 	if (res.code == 0 && res.data) {
	// 		if (res.data.value == '是') {
	// 			let ua = navigator.userAgent.toLowerCase();
	// 			if (ua.indexOf('micromessenger') !== -1) {
	// 				let openid = uni.getStorageSync('openId');
	// 				let userId = uni.getStorageSync('userId');
	// 				if (!userId) {
	// 					if (openid) {
	// 						that.$u.get('/app/Login/openid/login?openId=' + openid).then(
	// 							res => {
	// 								if (res.code == 0) {
	// 									uni.setStorageSync('token', res.token)
	// 									uni.setStorageSync('userId', res.user.userId)
	// 									uni.setStorageSync('userName', res.user.userName)
	// 									uni.setStorageSync('phone', res.user.phone)
	// 									uni.setStorageSync('invitationCode', res.user
	// 										.invitationCode)
	// 								}
	// 							});
	// 					}
	// 				}
	// 			}
	// 		}
	// 	}
	// });

	// #endif


	// configInit()


}

export function configInit() {
	const {
		zhengbu,
		danbu,
		adUnitId,
		playType,
		isGuanggao,
		isGuanggaody,
		kefu,
		kefuPhone
	} = $cache_config
	if (zhengbu === null || zhengbu === undefined) {
		httpsRequest.getT('/app/common/type/817').then(res => { //是否开启购买整部视频	817
			if (res.code == 0 && res.data) {
				uni.setStorageSync('zhengbu', res.data.value)
				$cache_config.set('zhengbu', res.data.value)
			}
		});
	}

	if (danbu === null || danbu === undefined) {
		httpsRequest.getT('/app/common/type/818').then(res => { //是否开启购买单集视频	818
			if (res.code == 0 && res.data) {
				uni.setStorageSync('danbu', res.data.value)
				$cache_config.set('danbu', res.data.value)
			}
		});
	}


	// 微信激励广告id	252
	if (adUnitId === null || adUnitId === undefined) {
		httpsRequest.getT('/app/common/type/252').then(res => {
			if (res.code == 0 && res.data) {
				uni.setStorageSync('adUnitId', res.data.value)
				$cache_config.set('adUnitId', res.data.value)
			}
		});
	}

	// 播放类型 1:抖音云 2:三方云
	if (playType === null || playType === undefined) {
		httpsRequest.getT('/app/common/type/821').then(res => {
			if (res.code == 0 && res.data) {
				uni.setStorageSync('playType', res.data.value)
				$cache_config.set('playType', res.data.value)
			}
		});
	}

	// 是否开启看广告（微信）	251
	if (isGuanggao === null || isGuanggao === undefined) {
		httpsRequest.getT('/app/common/type/251').then(res => {
			if (res.code == 0) {
				if (res.data && res.data.value) {
					uni.setStorageSync('isGuanggao', res.data.value)
					$cache_config.set('isGuanggao', res.data.value)
				}
			}
		});
	}

	// 是否开启看广告（抖音）	254
	if (isGuanggaody === null || isGuanggaody === undefined) {
		httpsRequest.getT('/app/common/type/254').then(res => {
			if (res.code == 0) {
				if (res.data && res.data.value) {
					uni.setStorageSync('isGuanggaody', res.data.value)
					$cache_config.set('isGuanggaody', res.data.value)
				}
			}
		});
	}

	if (kefu === null || kefu === undefined) {
		httpsRequest.getT('/app/common/type/202').then(res => { //客服配置  1  二维码客服 2 微信公众号客服  3电话客服
			if (res.code == 0 && res.data) {
				uni.setStorageSync('kefu', res.data.value)
				$cache_config.set('kefu', res.data.value)
			}
		});
	}

	if (kefuPhone === null || kefuPhone === undefined) {
		httpsRequest.getT('/app/common/type/206').then(res => { //客服电话
			if (res.code == 0 && res.data) {
				uni.setStorageSync('kefuPhone', res.data.value)
				$cache_config.set('kefuPhone', res.data.value)
			}
		});
	}

	if ($cache_config.kefuUrl === null || $cache_config.kefuUrl === undefined) {
		httpsRequest.getT('/app/common/type/204').then(res => { //微信客服链接
			if (res.code == 0 && res.data) {
				uni.setStorageSync('kefuUrl', res.data.value)
				$cache_config.set('kefuUrl', res.data.value)
			}
		});
	}

	if ($cache_config.kefuAppId === null || $cache_config.kefuAppId === undefined) {
		httpsRequest.getT('/app/common/type/203').then(res => { //微信客服appid
			if (res.code == 0 && res.data) {
				uni.setStorageSync('kefuAppId', res.data.value)
				$cache_config.set('kefuAppId', res.data.value)
			}
		});
	}

	//会员开关
	if ($cache_config.isVips === null || $cache_config.isVips === undefined) {
		httpsRequest.getT('/app/common/type/248').then(res => { //会员开关
			if (res.code == 0 && res.data) {
				uni.setStorageSync('isVips', res.data.value)
				$cache_config.set('isVips', res.data.value)
			}
		});
	}

	//热搜词 249
	if ($cache_config.moreSearch === null || $cache_config.moreSearch === undefined) {
		httpsRequest.getT('/app/common/type/249').then(res => { //热搜词
			if (res.code == 0 && res.data) {
				uni.setStorageSync('moreSearch', res.data.value)
				$cache_config.set('moreSearch', res.data.value)
			}
		});
	}

	//安卓下载地址 49
	if ($cache_config.AppUrl === null || $cache_config.AppUrl === undefined) {
		httpsRequest.getT('/app/common/type/49').then(res => { //热搜词
			if (res.code == 0 && res.data) {
				uni.setStorageSync('AppUrl', res.data.value)
				$cache_config.set('AppUrl', res.data.value)
			}
		});
	}

	//微信虚拟支付OfferID 823
	if ($cache_config.OfferID === null || $cache_config.OfferID === undefined) {
		httpsRequest.getT('/app/common/type/823').then(res => { //热搜词
			if (res.code == 0 && res.data) {
				uni.setStorageSync('OfferID', res.data.value)
				$cache_config.set('OfferID', res.data.value)
			}
		});
	}

	//微信虚拟支付环境（0 米大师正式环境, 1 米大师沙箱环境） 824
	if ($cache_config.payEnv === null || $cache_config.payEnv === undefined) {
		httpsRequest.getT('/app/common/type/824').then(res => { //热搜词
			if (res.code == 0 && res.data) {
				uni.setStorageSync('payEnv', res.data.value)
				$cache_config.set('payEnv', res.data.value)
			}
		});
	}

	//充值提示 825
	if ($cache_config.moneyTips === null || $cache_config.moneyTips === undefined) {
		httpsRequest.getT('/app/common/type/825').then(res => { //热搜词
			if (res.code == 0 && res.data) {
				uni.setStorageSync('moneyTips', res.data.value)
				$cache_config.set('moneyTips', res.data.value)
			}
		});
	}

	//是否开启卡密充值	855
	if ($cache_config.kmPaySel === null || $cache_config.kmPaySel === undefined) {
		httpsRequest.getT('/app/common/type/855').then(res => {
			if (res.code == 0 && res.data) {
				uni.setStorageSync('kmPaySel', res.data.value)
				$cache_config.set('kmPaySel', res.data.value)
			}
		});
	}

	//是否开启分类	849
	if ($cache_config.homeTypeSel === null || $cache_config.homeTypeSel === undefined) {
		httpsRequest.getT('/app/common/type/849').then(res => {
			if (res.code == 0 && res.data) {
				uni.setStorageSync('homeTypeSel', res.data.value)
				$cache_config.set('homeTypeSel', res.data.value)
			}
		});
	}

	//是否开启收益充值余额	856
	if ($cache_config.syPaySel === null || $cache_config.syPaySel === undefined) {
		httpsRequest.getT('/app/common/type/856').then(res => {
			if (res.code == 0 && res.data) {
				uni.setStorageSync('syPaySel', res.data.value)
				$cache_config.set('syPaySel', res.data.value)
			}
		});
	}

	//抖音im客服	857
	if ($cache_config.imId === null || $cache_config.imId === undefined) {
		httpsRequest.getT('/app/common/type/857').then(res => {
			if (res.code == 0 && res.data) {
				uni.setStorageSync('imId', res.data.value)
				$cache_config.set('imId', res.data.value)
			}
		});
	}
	if ($cache_config.isAccountPay === null || $cache_config.isAccountPay === undefined) {
		httpsRequest.getT('/app/common/type/858').then(res => { //是否开启公众号充值
			if (res.code == 0 && res.data) {
				uni.setStorageSync('isAccountPay', res.data.value)
				$cache_config.set('isAccountPay', res.data.value)
			}
		});
	}

	//抖音广告id
	if ($cache_config.dyadUnitId === null || $cache_config.dyadUnitId === undefined) {
		httpsRequest.getT('/app/common/type/860').then(res => { //抖音广告
			if (res.code == 0 && res.data) {
				uni.setStorageSync('dyadUnitId', res.data.value)
				$cache_config.set('dyadUnitId', res.data.value)
			}
		});
	}

}