行数以当前本地源码为准,不同版本可能有少量偏移。
1. 前端增加订单确认/算价请求状态
文件:crmeb_pro_uniapp/pages/goods/order_confirm/index.vue
方法:data()
行数:784-787
代码:
confirmLoading: false,
computedLoading: false,
confirmRequestId: 0,
computedRequestId: 0,2. 前端计算价格只接受最后一次请求结果
文件:crmeb_pro_uniapp/pages/goods/order_confirm/index.vue
方法:computedPrice()
行数:1106-1143
代码:
computedPrice: function () {
let requestId = ++this.computedRequestId;
this.computedLoading = true;
let shippingType = this.shippingType;
postOrderComputed(this.orderKey, {
addressId: this.addressId,
useIntegral: this.useIntegral ? 1 : 0,
useMoney: this.balanceCheck ? 1 : 0,
couponId: this.couponId,
shipping_type: parseInt(shippingType) + 1,
payType: this.payType
})
.then((res) => {
if (requestId !== this.computedRequestId) return;
let result = res.data.result;
if (result) {
this.totalPrice = result.pay_price;
this.totalIntegral = result.pay_integral;
this.integral_price = result.deduction_price;
this.usedMoney = result.usedMoney;
this.coupon_price = result.coupon_price;
this.promotions_detail = result.promotions_detail;
this.integral = this.useIntegral ? result.SurplusIntegral : this.userInfo.integral;
this.now_money = this.balanceCheck ? result.surplusMoney : this.userInfo.now_money;
this.$set(this.priceGroup, 'storePostage', shippingType == 1 ? 0 : result.pay_postage);
this.$set(this.priceGroup, 'storePostageDiscount', result.storePostageDiscount);
}
})
.catch((err) => {
return this.$util.Tips({
title: err
});
})
.finally(() => {
if (requestId === this.computedRequestId) {
this.computedLoading = false;
}
});
},3. 前端切换地址时去掉旧 orderKey 的重复算价
文件:crmeb_pro_uniapp/pages/goods/order_confirm/index.vue
方法:OnChangeAddress(id, item)
行数:1214-1223
代码:
if (this.shippingType == 0) {
this.addressId = id;
this.getaddressInfo();
} else {
this.system_store = item;
this.storeId = id;
this.range = item.range;
}
if (!this.send_gift_type) {
this.getConfirm();
}4. 前端确认单只接受最后一次请求并阻止计算中提交
文件:crmeb_pro_uniapp/pages/goods/order_confirm/index.vue
方法:getConfirm(numType)、formVerify()
行数:1241-1388、1566-1572
代码:
getConfirm: function (numType) {
if (this.send_gift_type == 2) return;
let that = this;
let requestId = ++this.confirmRequestId;
this.confirmLoading = true;
let shippingType = parseInt(this.shippingType) + 1;
let addressId = 0,
storeid;
if (shippingType == 1) {
addressId = that.addressId;
storeid = 0;
} else {
addressId = '';
storeid = that.storeId;
}
let senGiftType = that.send_gift_type != 0 ? 1 : 0;
orderConfirm(that.cartId, that.news, addressId, shippingType, storeid, that.couponId, that.luckRecordId, senGiftType)
.then((res) => {
if (requestId !== that.confirmRequestId) return;
// 中间原有赋值逻辑保持不变
that.$set(that, 'orderKey', res.data.orderKey);
that.$set(that, 'priceGroup', res.data.priceGroup);
that.computedPrice();
})
.catch((err) => {
return this.$util.Tips({ title: err });
})
.finally(() => {
if (requestId === that.confirmRequestId) {
that.confirmLoading = false;
}
});
},
formVerify() {
let that = this;
if (that.confirmLoading || that.computedLoading) {
return that.$util.Tips({
title: '订单金额计算中,请稍后提交'
});
}
// 后续原有校验逻辑保持不变
}
5. 后端计算订单金额时使用本次地址重新计算后的商品运费明细
文件:crmeb_pro/app/services/order/StoreOrderComputedServices.php
方法:computedOrder(...)
行数:157-179
代码:
// [$p, $couponPrice] = $results['coupon'];
[$p, $payPostage, $storePostageDiscount, $storeFreePostage, $isStoreFreePostage, $computedCartInfo] = $results['postage'];
if ($couponPrice < $payPrice) {//优惠券金额
$payPrice = bcsub((string)$payPrice, (string)$couponPrice, 2);
} else {
$payPrice = 0;
}
if ($type == 8) {
$firstOrderPrice = 0;
$payPrice = 0;
}
if ($firstOrderPrice < $payPrice) {//首单优惠金额
$payPrice = bcsub((string)$payPrice, (string)$firstOrderPrice, 2);
} else {
$payPrice = 0;
}
if ($uid && sys_config('integral_ratio_status') && in_array($type, [0, 6])) {
//使用积分
[$payPrice, $deductionPrice, $usedIntegral, $SurplusIntegral] = $this->useIntegral($useIntegral, $userInfo, $payPrice, $other);
}
// 使用本次地址重新计算后的商品运费明细,避免旧订单缓存中的 postage_price 被继续写入订单明细。
$cartInfo = $computedCartInfo ?: $cartInfo;
6. 后端计算邮费时返回最新 cartInfo,并在免邮/无邮费时清空商品邮费
文件:crmeb_pro/app/services/order/StoreOrderComputedServices.php
方法:computedPayPostage(...)
行数:526-577
代码:
public function computedPayPostage(int $uid, int $shipping_type, string $payType, array $cartInfo, array $addr, string $payPrice, array $postage = [], array $other = [], int $isSendGift = 0)
{
$storePostageDiscount = 0;
$storeFreePostage = $postage['storeFreePostage'] ?? 0;
$isStoreFreePostage = false;
if ($isSendGift == 1) {
$shipping_type = 0;
$addr = [];
}
if (!$storeFreePostage) {
$storeFreePostage = floatval(sys_config('store_free_postage')) ?: 0;//满额包邮金额
}
if (!$addr && !isset($addr['id']) || !$cartInfo) {
$payPostage = 0;
} else {
//$shipping_type = 1 快递发货 $shipping_type = 2 门店自提
if ($shipping_type == 2) {
if (!sys_config('store_func_status', 1) || !sys_config('store_self_mention', 1)) $shipping_type = 1;
}
//门店自提 || (线下支付 && 线下支付包邮) 没有邮费支付 || 节日任务赠送
$holidayGiftPushServices = app()->make(HolidayGiftPushServices::class);
$holidayGift = $holidayGiftPushServices->receiveHolidayGift($uid, 1);
if ($shipping_type === 2 || ($payType == 'offline' && ((isset($other['offlinePostage']) && $other['offlinePostage']) || sys_config('offline_postage')) == 1) || $holidayGift) {
$payPostage = 0;
} else {
if (!$postage || !isset($postage['storePostage']) || !isset($postage['storePostageDiscount'])) {
$postage = $this->getOrderPriceGroup($uid, $cartInfo, $addr, $storeFreePostage);
}
$cartInfo = $postage['cartInfo'] ?? $cartInfo;
$payPostage = $postage['storePostage'];
$storePostageDiscount = $postage['storePostageDiscount'];
/** @var UserServices $userService */
$userService = app()->make(UserServices::class);
//享受svip 运费折扣
if ($userService->checkUserIsSvip($uid)) {
$payPostage = bcsub((string)$payPostage, (string)$storePostageDiscount, 2);
} else {
$storePostageDiscount = 0;
}
$isStoreFreePostage = $postage['isStoreFreePostage'] ?? false;
$payPrice = (float)bcadd((string)$payPrice, (string)$payPostage, 2);
}
}
if ((float)$payPostage <= 0) {
foreach ($cartInfo as &$item) {
$item['postage_price'] = 0;
}
unset($item);
}
return [$payPrice, $payPostage, $storePostageDiscount, $storeFreePostage, $isStoreFreePostage, $cartInfo];
}
7. 后端创建订单时使用重算后的商品明细落库
文件:crmeb_pro/app/services/order/StoreOrderCreateServices.php
方法:createOrder(...)
行数:159-164
代码:
/** @var StoreOrderComputedServices $computedServices */
$computedServices = app()->make(StoreOrderComputedServices::class);
$priceData = $computedServices->computedOrder($uid, $userInfo, $cartGroup, $addressId, $payType, $useIntegral, $couponId, $shippingType, $isSendGift);
// 下单时以本次提交地址重新计算后的商品明细为准,避免旧 orderKey 缓存中的邮费明细落库。
$cartInfo = $priceData['cartInfo'] ?? $cartGroup['cartInfo'];
