<template>
	<view class="body">
		<image src="/static/czd/loading.gif" mode=""></image>
		<view>支付中</view>
	</view>
</template>

<script>
	const module = uni.requireNativePlugin('SandPayTypeModule');
	const dcHMPLib = uni.requireNativePlugin("HMPTLib-HMPLibModule")
	export default {
		data() {
			return {
				param: {},
				orderNo: null,
			}
		},
		onLoad(options) {
			const uaAppend = 'sandsdkpayios'
			plus.navigator.setUserAgent(plus.navigator.getUserAgent() + ' ' + uaAppend)
			if (options.orderNo) {
				this.param.orderNo = options.orderNo;
				this.payCreate()
			}
		},
		onShow() {
			// if(this.orderNo&&this.$u.os()=='ios'){
			this.getUrlType(this.orderNo)
			// }
		},
		methods: {
			getUrlType(orderNo) {
				this.$yghttp.post('/sand/order/getRedirectMode', {
					orderNo: orderNo
				}).then(res => {
					if (res.data.jumpType == 1) {
						uni.redirectTo({
							url: res.data.path+'?pagTag=1'
						});
					} else if (res.data.jumpType == 2) {
						uni.redirectTo({
							url: "/pages/web-view/Apps?path=" + res.data.path
						})
					}
				})
			},
			payCreate(type) {
				let platform = uni.getSystemInfoSync().platform;
				this.$yghttp.post("/sand/order/pay/create", this.param).then(res => {
					this.orderNo = this.param.orderNo;
					if (platform == 'ios') {
						this.allpayMethod(res.data)
					} else if (platform == 'android') {
						this.myClick(res.data)
					}

				}).catch(e => {
					console.log(e)
				})
			},
			// 安卓
			myClick(url) {
				// this.startLoading("请求中..")
				module.cashierPay(url, ret => {
					let data = JSON.parse(ret.tokenId)
					if (ret.funcCode === '01010005') {
						this.wxPay(data.tokenId)
					} else {
						this.endLoading()
						if (ret.funcCode) {
							console.log("功能编码:" + ret.funcCode)
						}

						if (ret.resultCode) {
							console.log("支付结果:" + ret.resultCode)
						} else {
							console.log("需调用服务端接口查询支付结果")
						}

						if (ret.failReason) {
							console.log("错误信息:" + ret.failReason)
							this.getUrlType(this.orderNo)
						}

						if (ret.ex) {
							console.log("异常信息:" + ret.ex);

						}
					}
				})
			},
			wxPay(tokenId) {
				plus.share.getServices(shareList => {
					let sweixin = shareList.find(val => val.id == 'weixin')
					if (sweixin) {
						sweixin.launchMiniProgram({
							id: 'gh_914ccc860aa5', //小程序原始id
							path: 'pages/zf/index?token_id=' + tokenId + '&funcCode=01010005',
							type: 0 // 正式版:0、开发版:1、体验版:2					
						})
						this.endLoading()
					} else {
						uni.showToast({
							icon: 'none',
							title: "未安装微信,无法打开对应小程序"
						})
					}
				}, e => {
					uni.showToast({
						icon: 'none',
						title: "获取微信服务列表失败:" + JSON.stringify(e)
					})
				})

			},
			// ios
			allpayMethod(url) {
				// console.log("打印SDK方法", dcHMPLib.sandHMPCashierPayMethod)
				dcHMPLib.sandHMPCashierPayMethod(
					url,
					(ret) => {
						console.log("打印SDK返回的json数据" + JSON.stringify(ret))
						// 微信sdk吊起
						if (ret.status == "success" && ret.funcCode == "01010005") {
							// {"status":"success","subAppId":"wx899fd5","miniProgramType":"2","pathUrl":"pages/zf/index?","tokenId":"202308027092435608865476608","funcCode":"02010005","ghOriId":"gh_8f69bbed2867"}
							this.pullupWxSdkWith(ret.tokenId, ret.funcCode, ret.ghOriId, ret.miniProgramType);
						} else {
							this.getUrlType(this.orderNo)
						}
					})
			},
			pullupWxSdkWith(tokenId, funcCode, originalId, miniProgramType) {

				// 参考微信使用吊起部分代码 /*  使用微信的uniapp提供的SDK吊起 微信小程序支付 使用杉德sdk传出的tokenid 和小程序的原始id */
				if (plus.runtime.isApplicationExist({
						pname: 'com.tencent.mm',
						action: 'weixin://'
					})) {
					console.log("微信应用已安装");
					// 去配置完整的通用链接 然后在吊起 TO DO: - - - - = = = - - = - = -
					plus.share.getServices(function(shareList) {
						let sweixin = shareList.find(val => val.id == 'weixin')

						if (sweixin) {
							console.log("weixin吊起小程序");

							sweixin.launchMiniProgram({
								id: originalId, //小程序原始id
								path: 'pages/zf/index?token_id=' + tokenId + "&funcCode=" + funcCode,
								type: miniProgramType // 0表示正式版本的小程序 2表示体验版
							})
						}
					}, function(res) {
						console.log(JSON.stringify(res));
						uni.showToast({
							icon: 'none',
							title: "获取微信服务列表失败:" + JSON.stringify(e)
						})
					});
				} else {
					console.log("微信应用未安装");
					uni.showToast({
						icon: 'none',
						title: "未安装微信,无法打开对应小程序"
					})
				}

			},
			startLoading(message) {
				uni.showLoading({
					title: message
				})
			},
			endLoading() {
				uni.hideLoading()
			}
		}
	}
</script>

<style lang="scss" scoped>
	.body {
		text-align: center;
		padding-top: 300rpx;
		color: #303133;

		image {
			width: 200rpx;
			height: 200rpx;
		}
	}
</style>