diff --git a/package1/buyFood/buyFood.vue b/package1/buyFood/buyFood.vue index cb5e4d3..385bce1 100644 --- a/package1/buyFood/buyFood.vue +++ b/package1/buyFood/buyFood.vue @@ -282,10 +282,10 @@ - + + style="position: absolute;top: 0;right: 0;width: 240rpx;height: 180rpx;" @tap="$refs.pintuanPopup.close()"> @@ -318,7 +318,9 @@ 剩余23:59:23结束 - 分享至微信 + { this.assignedWorker = worker; this.selected = 'zhiding'; }); }, + onShareAppMessage(res) { + if (this.isPaymentSuccessGroupInitiate) { + let groupId = this.createdOrderInfo?.groupId || this.createdOrderInfo?.groupDeliveryOrderId || this.createdOrderInfo?.id || this.currentOrderId; + let targetMembers = this.groupItem?.groupRule?.groupCount || 2; + let shopItemStr = encodeURIComponent(JSON.stringify(this.shopItem)); + let isFTF = this.isFaceToFace ? 1 : 0; + let path = `/package1/group/groupBuySingle?type=shop&item=${shopItemStr}&groupId=${groupId}&targetMembers=${targetMembers}&isFaceToFace=${isFTF}`; + + return { + title: '我发起了一个拼团,快来一起拼单吧!', + path: path, + } + } + return { + title: '买饭', + path: '/pages/index/index' + } + }, onUnload() { uni.$off('updateDeliveryWorker'); }, @@ -587,9 +608,7 @@ icon: 'success' }); setTimeout(() => { - uni.navigateBack({ - delta: 2 - }); + that.handlePaymentSuccess(); }, 1500); }, fail: function(err) { @@ -608,9 +627,7 @@ icon: 'none' }); setTimeout(() => { - uni.navigateBack({ - delta: 2 - }); + that.handlePaymentSuccess(); }, 1500); }).catch(e => { that.tui.toast("请求失败"); @@ -621,6 +638,34 @@ } }) }, + handlePaymentSuccess() { + if (!this.isGroupBuy) { + uni.redirectTo({ + url: '/package1/order/orderDetail?id=' + this.currentOrderId + }); + } else { + let isInitiating = !this.groupItem.groupId; + let isJoining = !!this.groupItem.groupId; + + if (isInitiating) { + this.isPaymentSuccessGroupInitiate = true; + this.$refs.pintuanPopup.open('center'); + } else if (isJoining) { + uni.redirectTo({ + url: '/package1/order/pendGroup?groupId=' + this.groupItem.groupId + }); + } + } + }, + onPintuanPopupChange(e) { + if (!e.show && this.isPaymentSuccessGroupInitiate) { + // 跳转到 pendGroup + let groupId = this.createdOrderInfo?.groupId || this.createdOrderInfo?.groupDeliveryOrderId || this.createdOrderInfo?.id || this.currentOrderId; + uni.redirectTo({ + url: `/package1/order/pendGroup?groupId=${groupId}` + }); + } + }, getMustFinishTime() { if (!this.formData.deliveryTime || this.formData.deliveryTime === '自动送达' || this.formData.deliveryTime === '尽快送达') return null; @@ -723,6 +768,7 @@ let totalAmount = res.result.totalAmount; this.backendTotalAmount = totalAmount; this.currentOrderId = orderId; + this.createdOrderInfo = res.result; this.$refs.payPopup.open('bottom'); } else { uni.showToast({ diff --git a/package1/group/groupBuySingle.vue b/package1/group/groupBuySingle.vue index 0a15de2..6b014c3 100644 --- a/package1/group/groupBuySingle.vue +++ b/package1/group/groupBuySingle.vue @@ -130,12 +130,12 @@ - 直接购买¥{{item.attributeListPrice | sliceMsg}} - - 拼团购买¥{{item.productGroupBuyPrices | slicePrice}} + + 拼团购买¥{{ getGroupPrice(item) }} @@ -468,6 +468,71 @@ + + + + + + + + 支付剩余时间 59:09 + + + ¥{{backendTotalAmount ? backendTotalAmount.toFixed(2) : '0.00'}} + + + 若拼团失败,将会为您自动退款 + + + + + + + 微信支付 + + + 使用微信支付 + + + + + + + + 确认付款 + + + + + + + + + + + + + 参团成功! + + + 您已成功参与拼团 + + + + + + + + + + @@ -505,6 +570,9 @@ isPintuan: true, groupId: '', isFaceToFaceGroup: false, + targetMembers: 2, + backendTotalAmount: 0, + currentOrderId: '', menuList: [{ categoryName: '猜你喜欢', id: '', @@ -587,10 +655,23 @@ if (option.isFaceToFace) { this.isFaceToFaceGroup = option.isFaceToFace === '1' || option.isFaceToFace === 'true'; } + if (option.targetMembers) { + this.targetMembers = parseInt(option.targetMembers); + } else { + this.targetMembers = 2; // Default fallback + } if (this.type == 'shop') { - this.shopItem = JSON.parse(option.item); + try { + this.shopItem = JSON.parse(decodeURIComponent(option.item)); + } catch(e) { + this.shopItem = JSON.parse(option.item); + } } else { - this.productItem = JSON.parse(option.item); + try { + this.productItem = JSON.parse(decodeURIComponent(option.item)); + } catch(e) { + this.productItem = JSON.parse(option.item); + } } this.getCategory(this.shopItem.id); this.getProduct(''); @@ -721,12 +802,35 @@ if (index == 'pintuan') { this.isPintuan = true; - // Set default group rule - if (item.productGroupBuyPrices && item.productGroupBuyPrices.length > 0) { - this.selectedGroupRule = item.productGroupBuyPrices[0]; + + if (this.groupId) { + // Determine best price match given targetMembers + let prices = item.productGroupBuyPrices || []; + let validPrices = prices.filter(p => p.groupCount <= this.targetMembers); + if (validPrices.length > 0) { + this.selectedGroupRule = validPrices.reduce((max, obj) => (obj.groupCount > max.groupCount ? obj : max)); + } else if (prices.length > 0) { + this.selectedGroupRule = prices.reduce((prev, curr) => (prev.groupCount < curr.groupCount ? prev : curr)); + } else { + this.selectedGroupRule = {}; + } + + if (this.isFaceToFaceGroup) { + if (this.parsedSpecs.length === 0) { + // No specs needed, jump straight to direct pay + this.submitFTFJoinPay({}); + return; + } + } } else { - this.selectedGroupRule = {}; + // Normal init + if (item.productGroupBuyPrices && item.productGroupBuyPrices.length > 0) { + this.selectedGroupRule = item.productGroupBuyPrices[0]; + } else { + this.selectedGroupRule = {}; + } } + this.$refs.pintuanPopup.open('bottom'); } else { this.isPintuan = false; @@ -769,6 +873,13 @@ specChoices[spec.name] = spec.selected; } let packageFee = parseFloat(this.currentItem.lunchBox || 0); + + if (this.groupId && this.isFaceToFaceGroup) { + // Direct face-to-face pay path + this.submitFTFJoinPay(specChoices); + return; + } + // Pack data for checkout let goData = { item: this.currentItem, @@ -786,6 +897,47 @@ }); this.$refs.pintuanPopup.close(); }, + submitFTFJoinPay(specChoices) { + let items = [{ + productId: this.currentItem.id, + specs: JSON.stringify(specChoices || {}), + price: parseFloat(this.selectedGroupRule && this.selectedGroupRule.groupPrice ? this.selectedGroupRule.groupPrice : 0), + quantity: 1 + }]; + + let packageFee = parseFloat(this.currentItem.lunchBox || 0); + + let payload = { + userId: uni.getStorageSync('id') || 'test-user123', + shopId: this.shopItem.id, + deliveryType: 1, + packageFee: packageFee, + remark: '', + items: items, + receiverName: '', + receiverPhone: '', + receiverAddress: '', + shopName: this.shopItem.shopName, + shopPhone: this.shopItem.shopPhone || '', + shopAddress: this.shopItem.shopAddress || '', + groupId: this.groupId + }; + + uni.showLoading({ title: '创建订单中...' }); + this.tui.request("/mall/order/create", "POST", payload, false, false).then(res => { + uni.hideLoading(); + if (res.success && res.result) { + this.currentOrderId = res.result.id; + this.backendTotalAmount = res.result.totalAmount; + this.$refs.payPopup.open('bottom'); + if (this.$refs.pintuanPopup) this.$refs.pintuanPopup.close(); + } else { + uni.showToast({ title: res.message || '下单失败', icon: 'none' }); + } + }).catch(err => { + uni.hideLoading(); + }); + }, addToCart(item, specs) { let specStr = specs ? JSON.stringify(specs) : ''; let cartId = item.id + '_' + specStr; @@ -869,6 +1021,73 @@ }) } }, + getGroupPrice(item) { + if (!this.groupId) { + return this.$options.filters.slicePrice(item.productGroupBuyPrices); + } + let target = this.targetMembers; + let prices = item.productGroupBuyPrices || []; + if (prices.length === 0) return '0.00'; + let validPrices = prices.filter(p => p.groupCount <= target); + if (validPrices.length === 0) { + let minRule = prices.reduce((prev, curr) => (prev.groupCount < curr.groupCount ? prev : curr)); + return parseFloat(minRule.groupPrice).toFixed(2); + } + let bestRule = validPrices.reduce((max, obj) => (obj.groupCount > max.groupCount ? obj : max)); + return parseFloat(bestRule.groupPrice).toFixed(2); + }, + wxPayment() { + let that = this; + if (!this.currentOrderId || !this.backendTotalAmount) return; + + let amountInCents = Math.round(this.backendTotalAmount * 100); + + this.tui.request("/api/wechat/pay/unified-order", "POST", { + openid: uni.getStorageSync('miniProgramOpenid') || 'test-openid', + amount: amountInCents, + description: '拼团参与订单', + outTradeNo: this.currentOrderId + }, false, false).then((res) => { + if (res.code == 200) { + uni.requestPayment({ + provider: 'wxpay', + timeStamp: res.timeStamp, + nonceStr: res.nonceStr, + package: res.package, + signType: res.signType, + paySign: res.paySign, + success: function(res2) { + that.$refs.payPopup.close(); + uni.showToast({ title: '支付成功', icon: 'success' }); + setTimeout(() => { that.$refs.joinSuccessPopup.open('bottom'); }, 1500); + }, + fail: function(err) { + that.tui.toast("支付失败或取消"); + } + }); + } else { + // 模拟支付成功 + if (res.code == 404 || res.code == 500 || res.code == 400 || !res.code) { + that.tui.request( + `/hiver/order/payMallOrderSuccess?orderId=${that.currentOrderId}&workerId=`, + "POST", {}, false, false).then(res2 => { + that.$refs.payPopup.close(); + uni.showToast({ title: '支付成功(模拟)', icon: 'none' }); + setTimeout(() => { that.$refs.joinSuccessPopup.open('bottom'); }, 1500); + }); + } else { + that.tui.toast(res.message); + } + } + }) + }, + onJoinSuccessPopupChange(e) { + if (!e.show) { + uni.redirectTo({ + url: '/package1/order/pendGroup?groupId=' + this.groupId + }); + } + }, back() { uni.navigateBack() }