<template>
	<view class="">
		<button @click="createOrder">创建订单 </button>
	</view>
</template>

<script>
	export default {
		name: '',
		data() {
			return {
				count:0
			};
		},
		mounted() {

		},
		methods: {
			createOrder() {
				//初始化js脚本
				this.initScript()
				//获取Fmopt
				this.fetchFmopt()
				//获取sessionId
				this.loadScriptFunc("", this.fetchSessionId())
				//打印数据
				this.cacheOrderData()
			},
			cacheOrderData() {
				let _this = this
				setTimeout(() => {
					_this.$nextTick(function() {
						try {
							_this.count++
							console.log("sessionId:" + window.sessionId);
							console.log("fingerprint:" + _fmOpt.getinfo());
							_this.cache(window.sessionId,_fmOpt.getinfo())
						} catch (e) {
							console.log(_this.count);
							if (_this.count < 10) {
								_this.cacheOrderData()
							}
						}
					})
				}, 20)
			},
			cache(sessionId,fingerprint){
				let orderResult = {
					fingerprint:encodeURIComponent(fingerprint),
					sessionId,
				}
				this.$cache.put('orderResult',orderResult)
			},
			initScript() {
				let monitorURL = "https://static.tongdun.net/monitor/monitor.js"
				this.loadScript(monitorURL)
				let tfdURL = "https://j.changyoyo.com/static/js/common/tfd.js"
				this.loadScript(tfdURL)
				let jqueryURL = "https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js"
				this.loadScript(jqueryURL)
			},

			fetchFmopt() {
				window._fmOpt = {
					partner: 'changyou',
					appName: 'changyou_web',
					token: 'changyou' + "-" + new Date().getTime() + "-" +
						Math.random().toString(16).substr(2),
					fmb: true,
					success: function(data) {
						console.log('blackbox:	', data)
					},
					fpHost: "https://fp.tongdun.net"
				};
				var cimg = new Image(1, 1);
				cimg.onload = function() {
					_fmOpt.imgLoaded = true;
				};
				cimg.src =
					"https://fp.tongdun.net/fp/clear.png?partnerCode=changyou&appName=changyou_web&tokenId=" +
					_fmOpt.token;
				var fm = document.createElement('script');
				fm.type = 'text/javascript';
				fm.async = true;
				fm.src = ('https:' == document.location.protocol ? 'https://' : 'http://') +
					'static.tongdun.net/v3/fm.js?ver=0.1&t=' + (new Date().getTime() / 3600000).toFixed(0);
				var s = document.getElementsByTagName('script')[0];
				s.parentNode.insertBefore(fm,
					s);
			},
			loadScript(src) {
				var script = document.createElement('script');
				script.src = src;
				document.body.appendChild(script);
			},
			loadScriptFunc(url, callback) {
				callback = typeof callback === 'function' ? callback : function() {};
				var head = document.getElementsByTagName('head')[0];
				var script = document.createElement('script');
				script.type = 'text/javascript';
				script.src = url;
				script.onreadystatechange = function() {
					if (this.readyState == "loaded" || this.readyState == "complete") {
						callback();
					}
				}
				script.onload = callback;
				head.appendChild(script);
			},
			fetchSessionId() {
				var url, ts, sessionId, appId, appkey;
				if (document.domain === 'm.changyoyo.com') {
					url = "tfd.changyoyo.com";
				} else {
					url = "101.132.237.10";
				}
				appId = "5308e20b";
				appkey = "3daf27ea2fe840ada6af8e1ec0d7b760";
				ts = new Date().getTime();
				sessionId = "changyo-pc-" + ts + "-" + Math.random().toString(16).substr(2);
				window.sessionId = sessionId;
				[document.getElementsByTagName('script')[0].parentNode.appendChild(document.createElement('script')).src =
					"http://" +
					url + "/did/js/dp.js?appId=" + appId + "&appkey=" + appkey + "&sessionId=" + sessionId +
					"&ts=" +
					ts
				][0];
			},
		}
	};
</script>

<style lang="scss" scoped>

</style>
