import pageUrl from './pageUrl'
import api from '../api/api.js'
import cookie from './cookie.js'

async function navTo(obj) {
	let token = '';
	if(process.env.UNI_PLATFORM =="h5"){
		if(window.location.href.indexOf('//prod-casedata.igandan.com')>-1){
			token = uni.getStorageSync('AUTH_TOKEN_CASEDATA');
		}else{
			token = uni.getStorageSync('DEV_AUTH_TOKEN_CASEDATA');
		}
	}else{
		  const { envVersion } = uni.getAccountInfoSync().miniProgram;
          if (envVersion == "release") {
			token = uni.getStorageSync('AUTH_TOKEN_CASEDATA');
		  }else{
			token = uni.getStorageSync('DEV_AUTH_TOKEN_CASEDATA');
		  }
	}
	if (!token) {
		if(process.env.UNI_PLATFORM =="h5"){
			let video_token =cookie.readCookie('video_token');
			console.log(video_token);
			if(video_token){
			 const res = await api.h5Login({
				token: video_token
			});
			
				let result = res.data.data;
				console.log(result);
				if (window.location.href.indexOf('//prod-casedata.igandan.com')>-1) {
					uni.setStorageSync("AUTH_TOKEN_CASEDATA",result.token);
				} else {
					uni.setStorageSync("DEV_AUTH_TOKEN_CASEDATA",result.token);
				};
					uni.setStorageSync("userInfo",{
					avatar:result.avatar,
					user_id:result.user_id,
					status:result.status,
					user_name:result.user_name,
					doctor_id:result.doctor_id,
				});
				window.location.reload();
			
			
			
			}else{
				let H5url = 'https://dev-wx.igandan.com';
				if(window.location.href.indexOf('//dev-casedata.igandan.com') == -1) {
					H5url = 'https://wx.igandan.com'
				}
				window.location.href = H5url + "/hcp/Signup2020online_tologin?back_url=" + encodeURIComponent(window.location.href);
			}
		}else{
			let page_url = pageUrl();
			uni.setStorageSync('redirectUrl', page_url);
			uni.navigateTo({
				url: '/pages/login/login?redirectUrl=has'
			});
		}
		
	} else {
		if(process.env.UNI_PLATFORM =="h5"){
				uni.navigateTo(obj)
		}else{
			const pages = getCurrentPages();
		let len = pages.length;
		console.log(len)
		if (len >=10) {
			uni.redirectTo(obj)
		} else {
			uni.navigateTo(obj)
			
		}
		}
	}
	

}

export default navTo