<template>
  <web-view :src="url"></web-view>
</template>
 
<script>
export default {
  data() {
    return {
      url: '',
	  saveUrl: '',
    }
  },
  onLoad(option) {
	//   if (option.url) {
	//       try {
	//           this.url = decodeURIComponent(option.url);
	//       } catch (error) {
	//           console.error('URL解码出错:', error);
	//           this.url = uni.getStorageSync('webUrl');
	//       }
	//   } else {
	//       this.url = uni.getStorageSync('webUrl');
	//   }
	//   return;
	  
	  let url = ''
	  let that = this
	  if (option.url) {
	  	url = decodeURIComponent(option.url)
	  }else{
		url = decodeURIComponent(uni.getStorageSync('webUrl'))
	  }
	  
	  if (url.toLowerCase().indexOf('m.cloud.sz-trip.com')>=0) {
		  this.handleJDSZ(url)
		  setTimeout(()=>{
			  that.saveUrl = url
		  },500)
	  } else {
		  this.url = url
		  this.saveUrl = url
	  }
	  
    console.log(this.url, 'onload')
    // 传入需要跳转的链接 使用web-view标签进行跳转
  },
  onShow() {
	  console.log(this.url, this.saveUrl, 'onshow')
  	if (!this.url && this.saveUrl) {
		let param = this.getUrlParam(this.saveUrl)
		console.log(param)
		this.checkIsLoginJdsz().then(res => {
			if(res) {
				param.token = res
				param.isKunshan = 1
				let baseUrl = param.tempUrl
				let queryString = Object.entries(param).filter(v=>v[0]!='tempUrl')
				  .map(([key, value]) => `${(key)}=${(value)}`)
				  .join('&');
				
				const fullUrl = `${baseUrl}?${queryString}`;
				console.log(fullUrl);
				
				this.url = fullUrl
			}
		})
	}
  },
  methods: {
	  handleJDSZ (url) {
		  let param = this.getUrlParam(url)
		  console.log(param)
		  this.checkIsLoginJdsz().then(res => {
			
		  	if(res) {
		  		param.token = res
				param.isKunshan = 1
				let baseUrl = param.tempUrl
				let queryString = Object.entries(param).filter(v=>v[0]!='tempUrl')
				  .map(([key, value]) => `${(key)}=${(value)}`)
				  .join('&');
				
				const fullUrl = `${baseUrl}?${queryString}`;
				console.log(fullUrl);
				this.url = fullUrl
		  	}
		  }).finally(()=>{
			this.saveUrl = url
		  })
		  
	  },
	  getUrlParam(url) { 
	      var theParam = {tempUrl: url}; 
		  let searchStartIndex = url.indexOf("?")
	      if (searchStartIndex>=0) {
			  theParam.tempUrl = url.substring(0,searchStartIndex);
	          var strs = url.substring(searchStartIndex+1);
	          strs = strs.split("&"); //将获取到的字符串从&分割，输出参数数组，即输出[参数1=xx,参数2=xx,参数3=xx,...]的数组形式
	          for(var i = 0; i < strs.length; i ++) { //遍历参数数组
	              theParam[strs[i].split("=")[0]]=decodeURIComponent(strs[i].split("=")[1]);
	          } 
	      } 
	      return theParam; //返回参数值
	  }
  },
}
</script>
 
<style lang="scss" scoped>
	
</style>
 