<template>
	<view>

	</view>
</template>

<script>
	import {
		qzOrderToPay
	} from '../../Utils/Api.js'
	let app = getApp();
	// #ifdef H5
	var jweixin = require('jweixin-module');
	// #endif
	export default {
		data() {
			return {
				orderNo: '',
				source: '',
				orderInfoUrl: ''
			}
		},
		onLoad(options) {
			this.orderNo = options.orderNo;
			this.source = options.source;
			this.orderInfoUrl = options.orderInfoUrl;
			this.orderToPay();
		},
		methods: {
			//获取订单数据
			orderToPay() {
				let that = this;
				// #ifdef H5
				let params = {
					"orderId": that.orderNo,
					"openId": app.globalData.openId,
					"orderScene": that.source,
					"openIdType": 2
				}
				// #endif
				// #ifdef MP
				let params = {
					"orderNo": that.orderNo,
					"openId": app.globalData.openId,
					"source": that.source
				}
				// #endif
				qzOrderToPay(params).then(res => {
					if (res.return_code == '000000') {
						// #ifdef MP
						uni.showLoading({
							title: '支付中...'
						})
						uni.requestPayment({
							"appId": res.return_data.appId,
							"nonceStr": res.return_data.nonceStr,
							"package": res.return_data.package,
							"paySign": res.return_data.sign,
							"signType": "MD5",
							"timeStamp": res.return_data.timeStamp,
							success: function(res) {
								uni.hideLoading();
								uni.showToast({
									title: '支付成功'
								})
								uni.redirectTo({
									url: '../tabBar/order/webUrl/webUrl?url=' + that
										.orderInfoUrl+'&orderid='+that.orderNo+'&sour='+that.source
								})
							},
							fail: function(err) {
								uni.hideLoading();
								uni.redirectTo({
									url: '../tabBar/order/webUrl/webUrl?url=' + that
										.orderInfoUrl+'&orderid='+that.orderNo+'&sour='+that.source
								})
							},
						});
						// #endif
						//判断是否是公众号
						// #ifdef H5
						//判断微信浏览器
						that.payRequest(res);
						// #endif

					} else {
						uni.showToast({
							title: res.return_msg,
							icon: 'none'
						})
						uni.redirectTo({
							url: '../tabBar/order/webUrl/webUrl?url=' + that.orderInfoUrl + '&orderid='+that.orderNo
						})
					}
				})
			},
			payRequest: function(self) {
				uni.showLoading({
					title: '支付中...'
				})
				jweixin.config({
					// debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
					appId: self.return_data.appId, // 必填,公众号的唯一标识
					timestamp: self.return_data.timeStamp, // 必填,生成签名的时间戳
					nonceStr: self.return_data.nonceStr, // 必填,生成签名的随机串
					signature: self.return_data.sign, // 必填,签名,见附录1
					jsApiList: ['chooseWXPay'] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
				});
				uni.hideLoading();
				jweixin.ready(function() {
					jweixin.checkJsApi({
						jsApiList: ['chooseWXPay'], // 需要检测的JS接口列表,所有JS接口列表见附录2,
						success: function(res) {},
						fail: function(res) {}
					});
					jweixin.chooseWXPay({
						appId: self.return_data.appId,
						timestamp: self.return_data
							.timeStamp, // 支付签名时间戳,注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符
						nonceStr: self.return_data.nonceStr, // 支付签名随机串,不长于 32 位
						package: self.return_data
							.package, // 统一支付接口返回的prepay_id参数值,提交格式如:prepay_id=***)
						signType: 'MD5', // 签名方式,默认为'SHA1',使用新版支付需传入'MD5'
						paySign: self.return_data.sign, // 支付签名
						success: function(res) {
							// 支付成功后的回调函数
							uni.showToast({
								title: '支付成功'
							})
							uni.reLaunch({
								url: '../tabBar/user/user'
							});
						},
						cancel: function(r) {},
						fail: function(res) {}
					});
				});
				jweixin.error(function(res) {
					uni.showToast({
						icon: 'none',
						title: '支付失败了',
						duration: 4000
					});
					// config信息验证失败会执行error函数,如签名过期导致验证失败,具体错误信息可以打开config的debug模式查看,也可以在返回的res参数中查看,对于SPA可以在这里更新签名。
					/*alert("config信息验证失败");*/
				});
			},
		}
	}
</script>

<style>

</style>