xuqiaoying 2 years ago
parent
commit
2648e650a5
4 changed files with 152 additions and 31 deletions
  1. 9 0
      common/httpList/order.js
  2. 3 0
      common/request.js
  3. 80 25
      pages2/order/confirm_pay.vue
  4. 60 6
      pages2/order/index.vue

+ 9 - 0
common/httpList/order.js

@@ -39,6 +39,7 @@ export default {
 			data: data
 		})
 	},
+	// 新增小程序订单
 	placeSmallOrder(data) {
 		return myRequest({
 			url: '/order/placeSmallOrder',
@@ -46,6 +47,14 @@ export default {
 			data: data
 		})
 	},
+	// 新增公众号订单
+	placeGzhOrder(data) {
+		return myRequest({
+			url: '/order/placeGzhOrder',
+			method: 'post',
+			data: data
+		})
+	},
 	resumeSmallOrder(data) {
 		return myRequest({
 			url: '/order/resumeSmallOrder',

+ 3 - 0
common/request.js

@@ -80,6 +80,7 @@ export const myRequest = (options) => {
 								uni.navigateTo({
 									url: '/pages4/login/login'
 								});
+								uni.removeStorageSync('h5_code')
 							}
 						} else {
 							num++
@@ -95,6 +96,7 @@ export const myRequest = (options) => {
 							uni.navigateTo({
 								url: '/pages4/login/login'
 							});
+							uni.removeStorageSync('h5_code')
 						}
 					}
 				}
@@ -150,6 +152,7 @@ export const myRequest = (options) => {
 				uni.navigateTo({
 					url: '/pages4/login/login'
 				});
+				uni.removeStorageSync('h5_code')
 			}
 		}
 	}

+ 80 - 25
pages2/order/confirm_pay.vue

@@ -122,8 +122,7 @@ export default {
 	},
 	methods: {
 		postOrder(){
-			let self = this
-			let list = self.shoppingCartList
+			let list = this.shoppingCartList
 			for(let i=0;i<list.length;i++){
 				let item = list[i];
 					if(item.goodsType==1){
@@ -147,15 +146,28 @@ export default {
 					}
 			}
 			let data = {goodsList:list}
-			
+			// #ifdef MP-WEIXIN
 			this.$api.placeSmallOrder(data).then(res => {
 				console.log(res,'res')
-				if(res.data.code==200){
+				this.orderResult(res)
+			});
+			// #endif
+
+			// #ifdef H5
+			data['url'] = location.href
+			this.$api.placeGzhOrder(data).then(res => {
+				console.log(res,'res')
+				this.orderResult(res)
+			});
+			// #endif
+		},
+		orderResult(res) {
+			if(res.data.code==200){
 					
 					uni.setStorageSync('updateCart',1) //提醒刷新购物车
 					if(this.totalPrice == 0) { //免费商品
 						uni.redirectTo({
-						    url: `/pages2/order/confirm_success?sn=${res.data.data.orderSn}&isBk=${self.isBK}`
+						    url: `/pages2/order/confirm_success?sn=${res.data.data.orderSn}&isBk=${this.isBK}`
 						});
 					} else {
 						let data = res.data.data
@@ -163,6 +175,7 @@ export default {
 							title:'支付中',
 							mask:true,
 						})
+						// #ifdef MP-WEIXIN
 						uni.requestPayment({
 						    provider: data.provider,
 						    nonceStr: data.nonceStr,
@@ -170,33 +183,38 @@ export default {
 						    signType: data.signType,
 						    paySign: data.sign,
 							timeStamp: String(data.timeStamp),
-						    success: function (res) {
+						    success: (res) => {
 								
-									uni.hideLoading()
-								self.btnNo = false
+								uni.hideLoading()
+								this.btnNo = false
 								uni.redirectTo({
-								    url: `/pages2/order/confirm_success?sn=${data.orderSn}&isBk=${self.isBK}`
+								    url: `/pages2/order/confirm_success?sn=${data.orderSn}&isBk=${this.isBK}`
 								});
 						        console.log('success:' + JSON.stringify(res));
 						    },
-						    fail: function (err) {
+						    fail: (err) => {
 								uni.hideLoading()
-								self.btnNo = false
+								this.btnNo = false
 						        console.log('fail:' + JSON.stringify(err));
 						    }
 						});
+						// #endif
+
+						// #ifdef H5
+						this.h5_wxpay(data)
+						// #endif
 					}
 					
 				}else if(res.data.code == 510){ //有未支付订单
-					self.hasPaying = true;
-					self.btnNo = false
+					this.hasPaying = true;
+					this.btnNo = false
 					this.modalMsg = res.data.msg;
 					this.showConfirmButton = true;
 					this.confirmText = "跳转到【我的订单】\n查看未支付订单";
 					this.showModal = true;
 				} else if(res.data.code == 511) { //511 重复购买
-					self.hasPaying = false;
-					self.btnNo = false
+					this.hasPaying = false;
+					this.btnNo = false
 					this.modalMsg = res.data.msg;
 					this.showConfirmButton = true;
 					this.showModal = true;
@@ -219,13 +237,55 @@ export default {
 					}
 					
 				} else {
-					self.hasPaying = false;
-					self.btnNo = false
+					this.hasPaying = false;
+					this.btnNo = false
 					this.showConfirmButton = false;
 					this.modalMsg = res.data.msg;
 					this.showModal = true;
 				}
-			});
+		},
+		h5_wxpay(data) {
+			function onBridgeReady() {
+				// 加载框
+				WeixinJSBridge.invoke(
+					'getBrandWCPayRequest', {
+						'appId': data.appId, // 公众号名称,由商户传入
+						'timeStamp': data.timeStamp, // 时间戳,自1970年以来的秒数
+						'nonceStr': data.nonceStr, // 随机串
+						'package': data.package,
+						'signType': data.signType, // 微信签名方式:
+						'paySign': data.paySign // 微信签名
+					},
+					function(res) {
+						// 判断支付状态
+						console.log('支付状态',res)
+						if (res.err_msg === 'get_brand_wcpay_request:ok') {
+							this.$u.toast('支付成功')
+							uni.hideLoading()
+							this.btnNo = false
+							uni.redirectTo({
+								url: `/pages2/order/confirm_success?sn=${data.orderSn}&isBk=${this.isBK}`
+							});
+						} else if (res.err_msg === 'get_brand_wcpay_request:cancel') {
+							this.$u.toast('取消支付')
+						} else {
+							uni.hideLoading()
+							this.btnNo = false
+							this.$u.toast('支付失败')
+						}
+					}
+				)
+			}
+			if (typeof WeixinJSBridge === 'undefined') {
+				if (document.addEventListener) {
+					document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false)
+				} else if (document.attachEvent) {
+					document.attachEvent('WeixinJSBridgeReady', onBridgeReady)
+					document.attachEvent('onWeixinJSBridgeReady', onBridgeReady)
+				}
+			} else {
+				onBridgeReady()
+			}
 		},
 		modalCancel() {
 			this.showModal = false;
@@ -273,13 +333,8 @@ export default {
 			// #endif
 			
 			// #ifdef H5
-			// https://www.xyyxt.net/?ask_type=h.xyyxt.net
-			// location.replace(
-			// 	"https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx6f48f721d18244eb" +
-			// 	"&redirect_uri=" +
-			// 	encodeURIComponent('https://www.xyyxt.net/?ask_type=h.xyyxt.net') +
-			// 	"&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect"
-			// )
+			// 在首页已经请求过接口/gzh_login 提交code了
+			this.postOrder()
 			// #endif
 		},
 		radioChange(e) {

+ 60 - 6
pages2/order/index.vue

@@ -250,13 +250,10 @@ export default {
 				}
 			}); 
 			// #endif
+
 			// #ifdef H5
-			// location.replace(
-			// 	"https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx6f48f721d18244eb" +
-			// 	"&redirect_uri=" +
-			// 	encodeURIComponent('https://www.xyyxt.net') +
-			// 	"&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect"
-			// )
+			// 在首页已经请求过接口/gzh_login 提交code了
+			this.resumeOrderPay(item)
 			// #endif
 		},
 		getOpenid(code,item){
@@ -269,6 +266,7 @@ export default {
 		},
 		resumeOrderPay(item){
 			let data = {orderSn:item.orderSn}
+			// #ifdef MP-WEIXIN
 			this.$api.resumeSmallOrder(data).then(res => {
 				if(res.data.code==200){
 					let data = res.data.data
@@ -297,6 +295,62 @@ export default {
 					})
 				}
 			});
+			// #endif
+
+			// #ifdef H5
+			data['url'] = location.href
+			this.$api.placeGzhOrder(data).then(res => {
+				console.log(res,'res')
+				if(res.data.code==200){
+					this.h5_wxpay(res.data.data)
+				}else{
+					uni.showModal({
+						title: "提示",
+						content: res.data.msg,
+						showCancel: false
+					})
+				}
+			});
+			// #endif
+		},
+		h5_wxpay(data) {
+			function onBridgeReady() {
+				// 加载框
+				WeixinJSBridge.invoke(
+					'getBrandWCPayRequest', {
+						'appId': data.appId, // 公众号名称,由商户传入
+						'timeStamp': data.timeStamp, // 时间戳,自1970年以来的秒数
+						'nonceStr': data.nonceStr, // 随机串
+						'package': data.package,
+						'signType': data.signType, // 微信签名方式:
+						'paySign': data.paySign // 微信签名
+					},
+					function(res) {
+						// 判断支付状态
+						console.log('支付状态',res)
+						if (res.err_msg === 'get_brand_wcpay_request:ok') {
+							this.$u.toast('支付成功')
+							uni.redirectTo({
+							    url: '/pages2/order/confirm_success?sn='+data.orderSn
+							});
+						} else if (res.err_msg === 'get_brand_wcpay_request:cancel') {
+							this.$u.toast('取消支付')
+						} else {
+							this.$u.toast('支付失败')
+						}
+					}
+				)
+			}
+			if (typeof WeixinJSBridge === 'undefined') {
+				if (document.addEventListener) {
+					document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false)
+				} else if (document.attachEvent) {
+					document.attachEvent('WeixinJSBridgeReady', onBridgeReady)
+					document.attachEvent('onWeixinJSBridgeReady', onBridgeReady)
+				}
+			} else {
+				onBridgeReady()
+			}
 		},
 		openPopup() {},
 		//删除订单