Kaynağa Gözat

优化:订单结算页显示账户可用余额

Yahoo 4 yıl önce
ebeveyn
işleme
4b9a793295

+ 15 - 13
app/api/controller/Checkout.php

@@ -27,20 +27,10 @@ use think\response\Json;
  */
 class Checkout extends Controller
 {
+    // 结算台验证器
     /* @var CheckoutValidate $validate */
     private $validate;
 
-    /**
-     * 构造方法
-     * @throws BaseException
-     */
-    public function initialize()
-    {
-        parent::initialize();
-        // 验证类
-        $this->validate = new CheckoutValidate;
-    }
-
     /**
      * 结算台订单信息
      * @param string $mode
@@ -93,8 +83,8 @@ class Checkout extends Controller
             'goodsNum' => 0,
         ]));
         // 表单验证
-        if (!$this->validate->scene('buyNow')->check($params)) {
-            return $this->renderError($this->validate->getError(), ['is_created' => false]);
+        if (!$this->getValidate()->scene('buyNow')->check($params)) {
+            return $this->renderError($this->getValidate()->getError(), ['isCreated' => false]);
         }
         // 立即购买:获取订单商品列表
         $model = new OrderModel;
@@ -179,6 +169,18 @@ class Checkout extends Controller
         ]);
     }
 
+    /**
+     * 获取结算台验证器
+     * @return CheckoutValidate
+     */
+    private function getValidate(): CheckoutValidate
+    {
+        if (!$this->validate) {
+            $this->validate = new CheckoutValidate;
+        }
+        return $this->validate;
+    }
+
     /**
      * 获取购物车ID集
      * @return false|string[]

+ 2 - 5
app/api/service/order/Checkout.php

@@ -515,7 +515,7 @@ class Checkout extends BaseService
             helper::setDataAttribute($goods, [
                 'is_user_grade' => true,
                 'grade_ratio' => $discountRatio,
-                'grade_goods_price' =>  UserGradeService::getDiscountPrice($goods['goods_price'], $discountRatio),
+                'grade_goods_price' => UserGradeService::getDiscountPrice($goods['goods_price'], $discountRatio),
                 'grade_total_money' => helper::bcsub($goods['total_price'], $gradeTotalPrice),
                 'total_price' => $gradeTotalPrice,
             ], false);
@@ -549,10 +549,7 @@ class Checkout extends BaseService
         // 计算订单商品优惠券抵扣金额
         $goodsListTemp = helper::getArrayColumns($this->goodsList, ['goods_id', 'total_price']);
         $CouponMoney = new GoodsDeductService;
-        $rangeGoodsList = $CouponMoney->setGoodsList($goodsListTemp)
-            ->setCouponInfo($couponInfo)
-            ->setGoodsCouponMoney($goodsListTemp, $couponInfo)
-            ->getRangeGoodsList();
+        $rangeGoodsList = $CouponMoney->setGoodsList($goodsListTemp)->setCouponInfo($couponInfo)->getRangeGoodsList();
         // 分配订单商品优惠券抵扣金额
         foreach ($this->goodsList as &$goods) {
             if (isset($rangeGoodsList[$goods['goods_id']])) {

+ 3 - 1
app/api/service/user/Grade.php

@@ -53,7 +53,9 @@ class Grade extends BaseService
      */
     public static function getDiscountPrice($originalPrice, $discountRatio): string
     {
-        $discountPrice = helper::bcmul($originalPrice, $discountRatio / 10);
+        // 使用高精度方法计算等级折扣; 因bcmach不支持四舍五入, 所以精确计算到3位数, 使用round四舍五入
+        $discountPrice = helper::bcmul($originalPrice, $discountRatio / 10, 3);
+        $discountPrice = round((float)$discountPrice, 2);
         return helper::number2($discountPrice, true);
     }
 }