<template>
	<view class="">
		<web-view :src="$store.state.webViewUrl"  @onPostMessage="postMessage" @message="postMessage" :webview-styles="webviewStyles"></web-view>
		<!-- <web-view src="http://192.168.1.44:92/vue/wxStudentCertificate3"  @onPostMessage="postMessage" @message="postMessage"></web-view> -->
	</view>
</template>

<script>
	let polling = null
	let timerNum = 0
	import { order_info } from '@/config/api.js'
	export default {
		data() {
			return {
				iswxPay: false,
				webviewStyles: {
					progress: {
						color: '#3877ff'
					},
				},
			}
		},
		onLoad(options) {
			this.outTradeNo = options.outTradeNo
			this.initWx()
			this.order_infoFn()
		},
		onShow() {
			if(this.iswxPay) {
				timerNum = 0
				this.pollFn()
			}
		},
		onHide() {
			clearInterval(polling)
			polling = null
		},
		methods: {
			// 监听h5事件
			postMessage(res) {
				console.log('监听到了')
				let item = res.detail.data[0]
				console.log(res)
				console.log('监听到的发送事件')
				console.log(item)
				if(item.action=='ysb-wxPay') {
					// this.$u.toast('用户点击了微信支付')
					this.iswxPay = true
					this.wxPay(item)
				}else if(item.action=='ysb-paySuccess') {
					this.$u.toast('支付成功')
					this.iswxPay = false
					this.$store.dispatch('getUserInfo')
					setTimeout(()=>{
						uni.switchTab({
							url: '/pages/tabbar/learnCar/index'
						})
					},800)
				}
			},
			// 轮询定时
			pollFn() {
				polling = setInterval(async ()=>{
					timerNum ++
					if(timerNum>10) {
						clearInterval(polling)
						polling = null
					}
					await this.order_infoFn()
				},1000)
				
			},
			// 查询订单状态
			async order_infoFn() {
				const {data: res} = await order_info({outTradeNo:this.outTradeNo})
				if(res.status==10) {
					this.$u.toast('支付成功')
					clearInterval(polling)
					polling = null
					setTimeout(()=>{
						uni.switchTab({
							url: '/pages/tabbar/learnCar/index'
						})
					},1500)
				}
				console.log('订单状态')
				console.log(res)
			},
			// "action": "lyxc-wxPay",
			// "miniProgramType": 2,
			// "miniAppId": "gh_89b377cbc340",
			// "path": "/pages/wxPayOnly/wxPayOnly?channelNo=QD202404083509&orderNo=20240423170705024771&inpartNo=820198016000045&merchantName=中原智慧新校园&amount=1&redMsg=&payType=04"
			// 跳转到微信支付
			wxPay(item) {
				console.log(item)
				// #ifdef APP-PLUS
				this.sweixin ? this.sweixin.launchMiniProgram({
					path: item.path, //跳转小程序页面路径 pages/index/index
					type: item.miniProgramType, //可取值： 0-正式版； 1-测试版； 2-体验版。 默认值为0。
					id: item.miniAppId //小程序的原始id
				}) : plus.nativeUI.alert('当前环境不支持微信操作!');
				// #endif
			},
			// 初始化微信支付方法
			initWx() {
				// #ifdef APP-PLUS
				plus.share.getServices((s) => {
					var shares = {};
					for (var i = 0; i < s.length; i++) {
						var t = s[i];
						console.log("...........", t);
						shares[t.id] = t;
					}
					var sweixin = shares['weixin'];
					this.sweixin = sweixin
				}, function(e) {
					console.log("获取分享服务列表失败：" + e.message);
				});
				// #endif
			},
		}
	}
</script>

<style>
</style>