Commit efbb5eb44a715bdd251c15105a5c9d88b2424de1

Authored by 陈威
1 parent e95f4538
Exists in dev

微信h5支付功能

Showing 1 changed file with 55 additions and 12 deletions   Show diff stats
frontend/app-user/pages/home/placeOrder.vue
... ... @@ -133,6 +133,8 @@ export default {
133 133 ddlx: "配送",
134 134 sfhy: "是",
135 135 xqList: [],
  136 + zflx: "",
  137 + openId: null
136 138 },
137 139 adsObj: {
138 140 name: "",
... ... @@ -255,13 +257,32 @@ export default {
255 257 // uni.navigateTo({
256 258 // url:"/pages/home/orderSuccessful"
257 259 // })
258   - const result = await this.$api.memberApi.savewDd({...this.orderBody, zffs: '微信', zflx: 'JSAPI'});
  260 + console.log('支付---------》')
  261 + let rData = {};
  262 + // 判断环境
  263 + // #ifdef H5
  264 + this.orderBody.zffs = '微信';
  265 + this.orderBody.zflx = 'JSAPI';
  266 + this.orderBody.openId = uni.getStorageSync('openId');
  267 + console.log('H5环境---------》')
  268 + console.log('H5环境rData---------》', this.orderBody)
  269 + // #endif
  270 +
  271 + // #ifdef APP-PLUS
  272 + this.orderBody.zffs = '微信';
  273 + this.orderBody.zflx = 'APP';
  274 + console.log('app环境rData---------》', this.orderBody)
  275 + uni.$u.toast('APP暂不支持在线下单,请前往微信公众号进行在线下单。');
  276 + return false
  277 + // #endif
  278 + console.log('orderBody---------》', this.orderBody)
  279 + const result = await this.$api.memberApi.savewDd(this.orderBody);
259 280 let {
260 281 code,
261 282 value
262 283 } = result;
263 284 if (code === 200) {
264   - console.log('value--->',value);
  285 + console.log('value--->', value);
265 286 //在这里唤起支付
266 287 this.handlePayment(value); // 处理支付逻辑
267 288 }
... ... @@ -279,34 +300,56 @@ export default {
279 300 signType: paymentData.payment.signType,
280 301 paySign: paymentData.payment.paySign
281 302 };
282   - // 调用微信支付
283   - this.initWechatPay(paymentParams,paymentData);
  303 +
  304 + // 判断环境
  305 + // #ifdef H5
  306 + this.handleH5Payment(paymentParams, paymentData);
  307 + // #endif
  308 +
  309 + // #ifdef APP-NVUE
  310 + this.handleAppPayment(paymentParams, paymentData);
  311 + // #endif
  312 + },
  313 + //app端支付
  314 + handleAppPayment(paymentParams, paymentData) {
  315 + // 假设APP提供了名为`invokeWechatPay`的方法来处理支付
  316 + uni.invoke('invokeWechatPay', paymentParams, (res) => {
  317 + if (res.err_msg === 'success') {
  318 + console.log('支付成功', paymentParams);
  319 + uni.navigateTo({
  320 + url: "/pages/home/orderSuccessful?datas=" + JSON.stringify({ddid: paymentData.ddid})
  321 + });
  322 + } else {
  323 + console.error('支付失败', res.err_msg);
  324 + }
  325 + });
284 326 },
285   - initWechatPay(paymentParams,paymentData) {
  327 + //h5端支付
  328 + handleH5Payment(paymentParams, paymentData) {
286 329 if (typeof WeixinJSBridge == "undefined") {
287 330 if (document.addEventListener) {
288 331 document.addEventListener('WeixinJSBridgeReady', () => {
289   - this.onBridgeReady(paymentParams,paymentData);
  332 + this.onBridgeReady(paymentParams, paymentData);
290 333 }, false);
291 334 } else if (document.attachEvent) {
292   - document.attachEvent('WeixinJSBridgeReady', this.onBridgeReady(paymentParams,paymentData));
293   - document.attachEvent('onWeixinJSBridgeReady', this.onBridgeReady(paymentParams,paymentData));
  335 + document.attachEvent('WeixinJSBridgeReady', this.onBridgeReady(paymentParams, paymentData));
  336 + document.attachEvent('onWeixinJSBridgeReady', this.onBridgeReady(paymentParams, paymentData));
294 337 }
295 338 } else {
296   - this.onBridgeReady(paymentParams,paymentData);
  339 + this.onBridgeReady(paymentParams, paymentData);
297 340 }
298 341 },
299   - onBridgeReady(paymentParams,paymentData) {
  342 + onBridgeReady(paymentParams, paymentData) {
300 343 WeixinJSBridge.invoke(
301 344 'getBrandWCPayRequest',
302 345 paymentParams,
303 346 res => {
304 347 if (res.err_msg == "get_brand_wcpay_request:ok") {
305 348 // 支付成功
306   - console.log('支付成功',paymentParams);
  349 + console.log('支付成功', paymentParams);
307 350 // 可以跳转到支付成功的页面
308 351 uni.navigateTo({
309   - url: "/pages/home/orderSuccessful?datas=" + JSON.stringify({ddid:paymentData.ddid})
  352 + url: "/pages/home/orderSuccessful?datas=" + JSON.stringify({ddid: paymentData.ddid})
310 353 });
311 354 } else {
312 355 // 支付失败
... ...