vhis.js 6.69 KB
/**
 * 页面描述:vhis
 * 嵌套平安提供的iFrame
 */

import {
	mapState
} from "vuex";
import api from '@/api/api'
import {
	httpPost
} from '@/api/fetch-api.js'

import Auth from '@components/auth/auth.vue';
import JsEncrypt from "jsencrypt";

let key = process.env.VUE_APP_CONTENT_ENCRYPT_KEY;
let rsa = new JsEncrypt();
rsa.setPublicKey(key);

function encryptContent(obj) {
	let str = JSON.stringify(obj);
	let securityContent = rsa.encrypt(str);
	return securityContent;
};

/**
 * 路由上,p代表产品,d代表参数。
 * d 是一个json结构,对应的数据结构是:
 * 					name: 名字
					gender: 性别,M:男;F:女
					birthday: yyyy-MM-dd
					certiType: 参考用户体系码表
					certiNo: 
 */
export default {
	data() {
		return {
			key: 'value',
			outsideUrl: "",
			information: null,
			iframeSrc: "",

			isInit: false,
			topSpaceHeight: 90,
		}
	},
	components: {
		Auth
	},
	computed: {
		...mapState({
			userInfo: state => state.userInfo
		}),
	},
	methods: {
		init() {
			if (this.isLogin() && !this.information) {
				httpPost({
					url: api.profile,
					sid: true
				}).then(content => {
					if (content) {
						this.information = content;
						this.isInit = true;
						this.gotoVhis();
					}
				})
			} else {
				this.isInit = true;
				this.gotoVhis();
			}
		},
		userLogout() {
			if (this.isInit) {
				this.removePath();
				this.gotoVhis();
			}
		},
		isLogin() {
			return this.userInfo && this.userInfo.sid;
		},
		gotoVhis() {
			// 网销平台productCode : VHIS001,
			// let url = window.global.vhis;
			// 2020-06-16 改变策略,根据iframe中传入的内容决定调整路径
			let url = this.currentPath();
			let productCode = this.$route.query.p;
			productCode = productCode ? productCode : "VHIS001";
			let lan = this.$i18n.locale;
			switch (lan) {
				case 'zh':
					url += "?language=zh-cn";
					break;
				case 'en':
					url += "?language=en";
					break;
				default:
					url += "?language=zh-hk";
					break;
			}
			url += "&partnerId=PA001&productCode=" + productCode;

			let data = null;
			// console.log(JSON.stringify(this.information));

			if (this.$route.query.d) {
				try {
					let queryData = JSON.parse(this.$route.query.d);
					data = queryData;
				} catch (e) {
					console.error("paramster 'd' is not a json ", this.$route.query.d);
				}
			} else if (this.information && this.information.idType && this.information.idNo) {
				data = {
					name: this.information.fullName,
					gender: this.information.sex,
					birthday: this.information.birthDate,
					certiType: this.information.policyIdType ? this.information.policyIdType : this.information.idType,
					certiNo: this.information.idNo
				};
			}

			if (data) {
				url += "&data=" + encryptContent(data);
			}
			// url = "http://www.baidu.com"
			// console.log("vhis:", url);
			this.$set(this, "outsideUrl", url);
			// var iframe = document.getElementById("myiframe");
			// try {
			// 	iframe.height = 0;
			// 	iframe.style.height = "0px";
			// } catch (ex) { }

			// let _this = this;
			// setTimeout(function () {
			// 	console.log("vhis:", url);
			// 	_this.outsideUrl = url;
			// }, 100);
		},
		// 获取当前的路径
		currentPath() {
			// 	var url = document.location.toString();
			//   var arrUrl = url.split("//");
			//   var start = arrUrl[1].indexOf("/");
			//   var relUrl = arrUrl[1].substring(start);//stop省略,截取从start开始到结尾的所有字符
			let src = this.getPath();
			src = src ? src : window.global.vhis;
			return src;
		},
		cachePath(path) {
			let key = "VHIS_PATH_CACHE";
			// console.log("cache path : ", path);
			sessionStorage.setItem(key, path);
		},
		getPath() {
			let key = "VHIS_PATH_CACHE";
			let val = sessionStorage.getItem(key);
			if (val == undefined || !val) {
				return "";
			}
			return val;
		},
		removePath() {
			let key = "VHIS_PATH_CACHE";
			sessionStorage.removeItem(key);
		}
	},
	watch: {
		"$route.query.p": function () {
			this.init();
		}
	},
	mounted() {
		this.init();

		this.$root.eventBus.$on("langChange", () => {
			try {
				this.gotoVhis();
			} catch (e) { }
		});
		// let url = this.$route.query.p;
		// if (url) {
		//     this.outsideUrl = url;
		// setTimeout(() => {
		//     var iframe = document.getElementById("myiframe");
		//     try {
		//         var bHeight = iframe.contentWindow.document.body.scrollHeight;
		//         var dHeight = iframe.contentWindow.document.documentElement.scrollHeight;
		//         var height = Math.min(bHeight, dHeight);
		//         iframe.height = height + 50;
		//     } catch (ex) {
		//     }
		// }, 500);
		// }

		// 隐藏滚动条
		// document.getElementsByTagName("body")[0].className = "hide-scrollbar";

		window.removeEventListener("message", function () { });
		window.addEventListener('message', (e) => {
			// console.log(this.$refs.myiframe);
			// console.log("vhis change : ", e.data);
			let iframeSrc = e.data && e.data.iframeSrc ? e.data.iframeSrc : null;
			if (iframeSrc) {
				this.iframeSrc = iframeSrc;
				this.cachePath(iframeSrc);
			}

			// 配置高度
			let height = e.data && e.data.height ? e.data.height : null;
			if (height) {
				var iframe = document.getElementById("myiframe");
				try {
					iframe.height = height;
					iframe.style.height = height + "px";
				} catch (ex) { }
			}

			// 页面滚动
			let scrollTop = e.data && e.data.scrollTop ? e.data.scrollTop : null;
			if (scrollTop) {
				setTimeout(() => {
					document.body.scrollTop = scrollTop + this.topSpaceHeight;
					document.documentElement.scrollTop = scrollTop + this.topSpaceHeight;
				}, 100);
			}


			// 提示滚动高度
			let getScrollTop = e.data && e.data.getScrollTop ? e.data.getScrollTop : null;
			if (getScrollTop && getScrollTop == 1) {
				let scrollHeight = document.body.scrollTop;
				scrollHeight = scrollHeight > 0 ? scrollHeight : document.documentElement.scrollTop;
				scrollHeight = scrollHeight - this.topSpaceHeight;
				scrollHeight = scrollHeight > 0 ? scrollHeight : 0;
				let vhisUrl = this.currentPath();
				let isHttps = vhisUrl.indexOf("https") == 0 ? true : false;
				let host = vhisUrl.replace("http://", "");
				host = host.replace("https://", "");
				host = host.indexOf("/") > 0 ? host.substr(0, host.indexOf("/")) : host;
				host = isHttps ? "https://" + host : "http://" + host;
				try {
					document.getElementById('myiframe').contentWindow.postMessage({ scrollHeight: scrollHeight }, host);
				} catch (e) {
					console.log(e);
				}
			}

		});

		// window.onscroll = () => {
		// 	let s = this.$refs.myiframe.getBoundingClientRect().top
		// 	console.log(s);
		// }
	},
	beforeDestroy() {
		// 移除隐藏滚动条样式
		// document.body.removeAttribute("class", "hide-scrollbar");
	},
	created() {

	}
}