Kaynağa Gözat

新增:后台可设置余额充值最低金额

Martin 5 yıl önce
ebeveyn
işleme
b2ce77abfd

+ 0 - 1
app/api/controller/Recharge.php

@@ -55,5 +55,4 @@ class Recharge extends Controller
         $message = ['success' => '充值成功', 'error' => '订单未支付'];
         return $this->renderSuccess(compact('payment', 'message'));
     }
-
 }

+ 36 - 7
app/api/model/recharge/Order.php

@@ -78,7 +78,7 @@ class Order extends OrderModel
      * @throws \think\db\exception\DbException
      * @throws \think\db\exception\ModelNotFoundException
      */
-    public function createOrder(int $planId = null, float $customMoney = 0.00)
+    public function createOrder(?int $planId = null, float $customMoney = 0.00)
     {
         // 确定充值方式
         $rechargeType = $planId > 0 ? RechargeTypeEnum::PLAN : RechargeTypeEnum::CUSTOM;
@@ -200,23 +200,52 @@ class Order extends OrderModel
 
     /**
      * 表单验证
-     * @param $rechargeType
-     * @param $planId
-     * @param $customMoney
+     * @param int $rechargeType
+     * @param int $planId
+     * @param float $customMoney
      * @return bool
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\DbException
+     * @throws \think\db\exception\ModelNotFoundException
      */
-    private function validateForm($rechargeType, $planId, $customMoney)
+    private function validateForm(int $rechargeType, int $planId, float $customMoney)
     {
         if (empty($planId) && $customMoney <= 0) {
             $this->error = '请选择充值套餐或输入充值金额';
             return false;
         }
         // 验证自定义的金额
-        if ($rechargeType == RechargeTypeEnum::CUSTOM && $customMoney <= 0) {
-            $this->error = '请选择充值套餐或输入充值金额';
+        if ($rechargeType == RechargeTypeEnum::CUSTOM && !$this->validateFormCustom($customMoney)) {
             return false;
         }
         return true;
     }
 
+    /**
+     * 表单验证 [自定义充值]
+     * @param float $customMoney 充值金额
+     * @return bool
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\DbException
+     * @throws \think\db\exception\ModelNotFoundException
+     */
+    private function validateFormCustom(float $customMoney)
+    {
+        // 充值设置
+        $setting = SettingModel::getItem('recharge');
+        if ($setting['is_custom'] == false) {
+            $this->error = '很抱歉,当前不允许充值自定义金额';
+            return false;
+        }
+        if ($customMoney <= 0) {
+            $this->error = '请输入正确的充值金额';
+            return false;
+        }
+        // 验证最低充值金额
+        if (helper::bccomp($customMoney, $setting['lowest_money']) === -1) {
+            $this->error = "很抱歉,当前最低充值金额不能低于{$setting['lowest_money']}元";
+            return false;
+        }
+        return true;
+    }
 }

+ 16 - 1
app/common/library/helper.php

@@ -42,7 +42,7 @@ class helper
     {
         $columnArr = [];
         foreach ($source as $item) {
-            isset($item[$column]) && $columnArr[] = $item[$column];
+            isset($item[$column]) && $columnArr[] = $item[$column];
         }
         return $columnArr;
     }
@@ -158,11 +158,26 @@ class helper
         return \bcdiv($leftOperand, $rightOperand, $scale);
     }
 
+    /**
+     * 浮点数比较
+     * 若二个字符串一样大则返回 0;若左边的数字字符串 (left operand) 比右边 (right operand) 的大则返回 +1;若左边的数字字符串比右边的小则返回 -1
+     * @param $leftOperand
+     * @param $rightOperand
+     * @param int $scale
+     * @return int
+     */
     public static function bccomp($leftOperand, $rightOperand, $scale = 2)
     {
         return \bccomp($leftOperand, $rightOperand, $scale);
     }
 
+    /**
+     * 比较两个数值是否相等
+     * @param $leftOperand
+     * @param $rightOperand
+     * @param int $scale
+     * @return bool
+     */
     public static function bcequal($leftOperand, $rightOperand, $scale = 2)
     {
         return self::bccomp($leftOperand, $rightOperand, $scale) === 0;

+ 8 - 7
app/common/model/Wxapp.php

@@ -8,11 +8,12 @@
 // +----------------------------------------------------------------------
 // | Author: 萤火科技 <admin@yiovo.com>
 // +----------------------------------------------------------------------
-declare (strict_types = 1);
+declare (strict_types=1);
 
 namespace app\common\model;
 
 use cores\BaseModel;
+
 use think\facade\Cache;
 use app\common\exception\BaseException;
 
@@ -31,27 +32,28 @@ class Wxapp extends BaseModel
 
     /**
      * 获取小程序信息
-     * @param null $storeId
+     * @param int|null $storeId
      * @return array|static|null
      * @throws \think\db\exception\DataNotFoundException
      * @throws \think\db\exception\DbException
      * @throws \think\db\exception\ModelNotFoundException
      */
-    public static function detail($storeId = null)
+    public static function detail(?int $storeId = null)
     {
-        return (new static)->find($storeId);
+        $storeId = $storeId ?: static::$storeId;
+        return (new static)->where(['store_id' => $storeId])->find();
     }
 
     /**
      * 从缓存中获取小程序信息
-     * @param null $storeId
+     * @param int|null $storeId
      * @return array
      * @throws BaseException
      * @throws \think\db\exception\DataNotFoundException
      * @throws \think\db\exception\DbException
      * @throws \think\db\exception\ModelNotFoundException
      */
-    public static function getWxappCache($storeId = null)
+    public static function getWxappCache(?int $storeId = null)
     {
         // 小程序id
         is_null($storeId) && $storeId = static::$storeId;
@@ -65,5 +67,4 @@ class Wxapp extends BaseModel
         }
         return $data;
     }
-
 }

+ 3 - 2
app/common/model/store/Setting.php

@@ -8,7 +8,7 @@
 // +----------------------------------------------------------------------
 // | Author: 萤火科技 <admin@yiovo.com>
 // +----------------------------------------------------------------------
-declare (strict_types = 1);
+declare (strict_types=1);
 
 namespace app\common\model\store;
 
@@ -250,7 +250,8 @@ class Setting extends BaseModel
                 'values' => [
                     'is_entrance' => 1,   // 是否允许用户充值
                     'is_custom' => 1,     // 是否允许自定义金额
-                    'is_match_plan' => 1, // 自定义金额是否自动匹配合适的套餐
+                    'lowest_money' => 10,   // 最低充值金额
+                    'is_match_plan' => 1,   // 自定义金额是否自动匹配合适的套餐
                     'describe' => "1. 账户充值仅限微信在线方式支付,充值金额实时到账;\n" .
                         "2. 账户充值套餐赠送的金额即时到账;\n" .
                         "3. 账户余额有效期:自充值日起至用完即止;\n" .