<template>
	<view @click="showLog">
	</view>
</template>
<script>
	import { choseType } from '@/api/index.js';
	import {
		mapMutations,
		mapState
	} from 'vuex';
	import {
		getUserInfo
	} from '@/api/login.js';
	// #ifdef H5
	import {
		wechatAuth
	} from '@/api/wx';
	// #endif
	// #ifdef MP-WEIXIN
	import {
		wechatMpAuth
	} from '@/api/wx';
	// #endif
	export default {
		computed: {
			...mapState(['urlFile'])
		},
		onLoad(option) {
			let obj = this;
			// 判断是否需要保存定向地址
			// #ifdef H5
			this.loadH5()
			// #endif
			// #ifdef MP-WEIXIN
			this.loadMp(option)
			// #endif
		},
		methods: {
			...mapMutations(['setUserInfo', 'login']),
			// #ifdef H5
			loadH5() {
				let obj = this;
				let url = window.location.href;
				let code = url.match(/code=([0-9]|[a-z]|[A-Z])*/g)[0].replace('code=', '');
				// 设置成功后修改当前页面
				history.replaceState({
					name: '首页'
				}, '首页', obj.urlFile + '/#/pages/user/user');
				let spread = uni.getStorageSync('spread') || '';
				wechatAuth({
					code: code,
					spread: spread,
				}).then(({
					data
				}) => {
					obj.wchatAuth(data);
				}).catch((e) => {
					console.log(e)
				});;
			},
			// #endif
			// #ifdef MP-WEIXIN
			loadMp(option) {
				let obj = this;
				// 获取登录授权页数据
				let user = obj.$api.prePage().userInfo;
				// #ifndef MP
				// 获取推广人id
				let spread_spid = uni.getStorageSync('spread') || '';
				// #endif
				// #ifdef MP
				// 小程序推广人
				let spread_code = uni.getStorageSync('spread_code') || '';
				// #endif
				wechatMpAuth({
					code: option.code,
					iv: user.target.iv,
					encryptedData: user.target.encryptedData,
					// #ifndef MP
					spread_spid: spread_spid,
					// #endif
					// #ifdef MP
					spread_code: spread_code
					// #endif
				}).then(({
					data
				}) => {
					obj.wchatAuth(data);
				}).catch((e) => {
					uni.showModal({
						title: '错误',
						content: JSON.stringify(e),
						showCancel: false,
					});
				});
			},
			// #endif
			wchatAuth(data) {
				let obj = this;
				// 保存token
				uni.setStorageSync('token', data.token);
				let path = uni.getStorageSync('present');
				// 获取用户基础信息
				getUserInfo({}).then(e => {
					obj.$log.addLog(e.data)
					obj.login();
					// 保存返回用户数据
					obj.setUserInfo(e.data);
					let chooseType = uni.getStorageSync('chooseType') || ''
					// 没有选择身份或没有填写身份信息
					if (e.data.ext_info == null) {
						if (!chooseType) {
							uni.navigateTo({
								url: '/pages/index/identity',
								fail() {
									uni.switchTab({
										url: '/pages/user/user',
										fail() {

										}
									})
								}
							});
						} else {
							// if (chooseType == 1) {
								
							// 	uni.navigateTo({
							// 		url: `/pages/public/PatRegister`
							// 	});
							// }
							// if (chooseType == 2) {
							// 	uni.navigateTo({
							// 		url: `/pages/public/DocRegister`
							// 	});
							// }
							// if (chooseType == 3) {
							// 	uni.navigateTo({
							// 		url: `/pages/public/MecRegister`
							// 	});
							// }
							choseType({
								type: chooseType*1
							})
								.then(function(e) {
									if (e.msg == '修改成功') {
										if (chooseType == 1) {
											uni.navigateTo({
												url: `/pages/public/PatRegister`
											});
										}
										if (chooseType == 2) {
											uni.navigateTo({
												url: `/pages/public/DocRegister`
											});
										}
										if (chooseType == 3) {
											uni.navigateTo({
												url: `/pages/public/MecRegister`
											});
										}
									}
								})
								.catch(e => {
									console.log(e);
								});
						}

					} else if (e.data.ext_info != null) {
						// 1是病人 2是医生 3是机构
						// 校验用户类型和入口是否一致
						if (!chooseType) {
							if(path.indexOf('expert') != -1) {
								uni.navigateTo({
									url: path
								})
							}
							uni.switchTab({
								url: '/pages/index/home',
								fail(e) {
									obj.$log.addLog('t4')
									uni.switchTab({
										url: '/pages/user/user',
										fail() {

										}
									})
								}
							})
						} else {
							
								if (e.data.type == 2) {
									uni.setTabBarItem({
										index: 0,
										text: '首页',
										iconPath: 'static/tabBar/tab-home.png',
										selectedIconPath: 'static/tabBar/tab-home-current.png'
									});
									uni.setTabBarItem({
										index: 1,
										text: '我的病人',
										iconPath: 'static/tabBar/tab-cate.png',
										selectedIconPath: 'static/tabBar/tab-cate-current.png',
										visible: true
									});
									uni.setTabBarItem({
										index: 2,
										text: '科普学习',
										iconPath: 'static/tabBar/tab-study.png',
										selectedIconPath: 'static/tabBar/tab-study-current.png',
										visible: false
									});
									uni.setTabBarItem({
										index: 3,
										text: '接单',
										iconPath: 'static/tabBar/jd.png',
										selectedIconPath: 'static/tabBar/jd-act.png',
										visible: true
									})
									uni.setTabBarItem({
										index: 4,
										text: '我的',
										iconPath: 'static/tabBar/tab-my.png',
										selectedIconPath: 'static/tabBar/tab-my-current.png'
									});
								}
								if (e.data.type == 3) {
									uni.setTabBarItem({
										index: 0,
										text: '首页',
										iconPath: 'static/tabBar/tab-home.png',
										selectedIconPath: 'static/tabBar/tab-home-current.png'
									});
									uni.setTabBarItem({
										index: 1,
										text: '咨询记录',
										iconPath: 'static/tabBar/tab-cate.png',
										selectedIconPath: 'static/tabBar/tab-cate-current.png',
										visible: true
									});
									uni.setTabBarItem({
										index: 2,
										text: '科普学习',
										iconPath: 'static/tabBar/tab-study.png',
										selectedIconPath: 'static/tabBar/tab-study-current.png',
										visible: false
									});
									uni.setTabBarItem({
										index: 3,
										text: '发布订单',
										iconPath: 'static/tabBar/jd.png',
										selectedIconPath: 'static/tabBar/jd-act.png',
										pagePath: '/pages/dd/dd',
										visible: true
									})

									uni.setTabBarItem({
										index: 4,
										text: '我的',
										iconPath: 'static/tabBar/tab-my.png',
										selectedIconPath: 'static/tabBar/tab-my-current.png'
									});
								}
								if (e.data.type == 1) {
									uni.setTabBarItem({
										index: 0,
										text: '首页',
										iconPath: 'static/tabBar/tab-home.png',
										selectedIconPath: 'static/tabBar/tab-home-current.png'
									});
									uni.setTabBarItem({
										index: 1,
										text: '咨询记录',
										iconPath: 'static/tabBar/tab-cate.png',
										selectedIconPath: 'static/tabBar/tab-cate-current.png',
										visible: true
									});
									uni.setTabBarItem({
										index: 2,
										text: '科普学习',
										iconPath: 'static/tabBar/tab-study.png',
										selectedIconPath: 'static/tabBar/tab-study-current.png',
										visible: false
									});
									uni.setTabBarItem({
										index: 3,
										text: '接单',
										iconPath: 'static/tabBar/jd.png',
										selectedIconPath: 'static/tabBar/jd-act.png',
										visible: false
									})
									uni.setTabBarItem({
										index: 4,
										text: '我的',
										iconPath: 'static/tabBar/tab-my.png',
										selectedIconPath: 'static/tabBar/tab-my-current.png.png'
									});
								}
								uni.switchTab({
									url: '/pages/index/home',
									fail(e) {
										obj.$log.addLog('t4')
										uni.switchTab({
											url: '/pages/user/user',
											fail() {

											}
										})
									}
								})
							
						}


					} else {
						obj.$log.addLog('t3')
						uni.switchTab({
							url: '/pages/index/home',
							fail() {
								uni.switchTab({
									url: '/pages/user/user',
									fail() {

									}
								})
							}
						})
					}
				}).catch((e) => {
					uni.showModal({
						title: '错误',
						content: JSON.stringify(e),
						showCancel: false,
					});
				});;
			},
			showLog() {
				let log = this.$log.getLog();
				uni.showModal({
					title: '日志',
					content: JSON.stringify(log),
					showCancel: false,

				});
			}
		}
	};
</script>
<style></style>
