Browse Source

fix:循环绑定问题

super-yimizi 7 hours ago
parent
commit
5785dbe726

+ 4 - 1
application/api/controller/AfterSale.php

@@ -133,7 +133,10 @@ class OrderGoods extends Base
         if (empty($id)) {
             $this->error('参数缺失');
         }
-        $row = OrderAftersales::with(['OrderGoods'])->where('order_goods_id', $id)->order('id desc')->where('user_id', $this->auth->id)->find();
+        $row = OrderAftersales::with(['OrderGoods'])->where([
+            'order_goods_id' => $id,
+            'user_id' => $this->auth->id
+        ])->order('id desc')->find();
         if (empty($row)) {
             $this->error('未找到记录');
         }

+ 4 - 1
application/api/controller/Cart.php

@@ -108,7 +108,10 @@ class Cart extends Base
             'nums' => $nums,
         ]);
 
-        $row = Carts::with(['Goods', 'Sku'])->where('id', $id)->where('user_id', $this->auth->id)->find();
+        $row = Carts::with(['Goods', 'Sku'])->where([
+            'id' => $id,
+            'user_id' => $this->auth->id
+        ])->find();
         if (!$row) {
             $this->error('未找到记录');
         }

+ 9 - 2
application/api/controller/Goods.php

@@ -45,7 +45,10 @@ class Goods extends Base
                             }
                         ]);
                     }
-                ])->where('status', 'normal')->where('pid', 0)->field('id,goods_id,content,star,user_id,images,comments,createtime')->with([
+                ])->where([
+                    'status' => 'normal',
+                    'pid' => 0
+                ])->field('id,goods_id,content,star,user_id,images,comments,createtime')->with([
                     'User' => function ($u) {
                         $u->field('id,nickname,avatar');
                     }
@@ -60,7 +63,11 @@ class Goods extends Base
         $row->setInc('views');
         //收藏
         if ($this->auth->isLogin()) {
-            $row->is_collect = !!(Collect::where('user_id', $this->auth->id)->where('goods_id', $id)->where('status', 1)->find());
+            $row->is_collect = !!(Collect::where([
+                'user_id' => $this->auth->id,
+                'goods_id' => $id,
+                'status' => 1
+            ])->find());
         } else {
             $row->is_collect = false;
         }

+ 16 - 4
application/api/controller/Order.php

@@ -128,7 +128,10 @@ class Order extends Base
         try {
             if (!empty($cart_ids)) {
                 // 购物车模式 - 校验购物车id合法性
-                $row = (new Carts)->where('id', 'IN', $cart_ids)->where('user_id', '<>', $this->auth->id)->find();
+                $row = (new Carts)->where([
+                    'id' => ['IN', $cart_ids],
+                    'user_id' => ['<>', $this->auth->id]
+                ])->find();
                 if ($row) {
                     $this->error('存在不合法购物车数据');
                 }
@@ -177,7 +180,10 @@ class Order extends Base
         try {
             if (!empty($cart_ids)) {
                 // 购物车模式 - 校验购物车id合法性
-                $row = (new Carts)->where('id', 'IN', $cart_ids)->where('user_id', '<>', $this->auth->id)->find();
+                $row = (new Carts)->where([
+                    'id' => ['IN', $cart_ids],
+                    'user_id' => ['<>', $this->auth->id]
+                ])->find();
                 if ($row) {
                     $this->error('存在不合法购物车数据');
                 }
@@ -235,7 +241,10 @@ class Order extends Base
         try {
             if (!empty($cart_ids)) {
                 // 购物车模式 - 校验购物车id合法性
-                $row = (new Carts)->where('id', 'IN', $cart_ids)->where('user_id', '<>', $this->auth->id)->find();
+                $row = (new Carts)->where([
+                    'id' => ['IN', $cart_ids],
+                    'user_id' => ['<>', $this->auth->id]
+                ])->find();
                 if ($row) {
                     $this->error('存在不合法购物车数据');
                 }
@@ -460,7 +469,10 @@ class Order extends Base
         if (!$order->shippingstate) {
             $this->error('订单未发货');
         }
-        $electronics = Db::name('shop_order_electronics')->where('order_sn', $order_sn)->where('status', 0)->find();
+        $electronics = Db::name('shop_order_electronics')->where([
+            'order_sn' => $order_sn,
+            'status' => 0
+        ])->find();
         if (!$electronics) {
             $this->error('订单未发货');
         }

+ 4 - 2
application/api/controller/Pay.php

@@ -107,8 +107,10 @@ class Pay extends Base
         $platform = $this->request->header('platform');
         $userId = $this->auth->getUser()->id;
         // 获取订单实例
-        $order = new Order();
-        $order = $order->where('user_id', $userId)->where('id', $orderId)->find();
+        $order = Order::where([
+            'user_id' => $userId,
+            'id' => $orderId
+        ])->find();
          
         if (!$order) {
             $this->error(__('No Results were found'));

+ 9 - 8
application/api/controller/commission/Agent.php

@@ -223,22 +223,23 @@ class Agent extends Commission
         $startDate = isset($params['start_date']) ? $params['start_date'] : '';
         $endDate = isset($params['end_date']) ? $params['end_date'] : '';
 
-        // 构建查询条件
-        $query = UserModel::where('parent_user_id', $agentId)
-            ->where('status', 1);
-
+        // 构建查询条件数组
+        $whereConditions = [
+            'parent_user_id' => $agentId,
+            'status' => 1
+        ];
         // 根据时间筛选参数添加时间条件
         if ($timeFilter !== 'all') {
             $timeConditions = $this->getTimeFilterConditions($timeFilter, $startDate, $endDate);
             if ($timeConditions) {
-                $query->where('bind_time', '>=', $timeConditions['start_time']);
+                $whereConditions['bind_time'] = ['>=', $timeConditions['start_time']];
                 if (isset($timeConditions['end_time'])) {
-                    $query->where('bind_time', '<=', $timeConditions['end_time']);
+                    // 使用between条件更简洁
+                    $whereConditions['bind_time'] = ['between', [$timeConditions['start_time'], $timeConditions['end_time']]];
                 }
             }
         }
-
-        $data = $query->field('id,username,nickname,avatar,mobile,bind_time,commission,order_count,total_consume,status,parent_user_id')
+        $data = UserModel::where($whereConditions)->field('id,username,nickname,avatar,mobile,bind_time,commission,order_count,total_consume,status,parent_user_id')
             ->with(['agent' => function ($query) {
                 return $query->with('level_info')->field('user_id,agent_type,child_user_count_all,child_order_money_all');
             }])

+ 4 - 3
application/api/controller/commission/AgentApply.php

@@ -118,9 +118,10 @@ class AgentApply extends Base
             $inviteCode = $data['invite_code'];
             
             // 查询邀请码对应的代理商
-            $parentAgent = AgentModel::where('invite_code', $inviteCode)
-                                    ->where('status', AgentModel::AGENT_STATUS_NORMAL)
-                                    ->find();
+            $parentAgent = AgentModel::where([
+                'invite_code' => $inviteCode,
+                'status' => AgentModel::AGENT_STATUS_NORMAL
+            ])->find();
             
             if (!$parentAgent) {
                 $this->error('邀请码无效或代理商状态异常');

+ 11 - 6
application/api/controller/commission/Reward.php

@@ -37,20 +37,25 @@ class Reward extends Commission
         $pageSize = isset($params['page_size']) ? (int)$params['page_size'] : 8;
         $yearMonth = isset($params['year_month']) ? trim($params['year_month']) : '';
 
-        // 构建查询条件
-        $query = RewardModel::where('agent_id', $agent->user_id)
-            ->where('status', RewardModel::COMMISSION_REWARD_STATUS_ACCOUNTED) // 只查询已结算的佣金
-            ->with(['commissionOrder']) // 关联佣金订单信息
-            ->order('commission_time desc'); // 按佣金时间倒序
+        // 构建查询条件数组
+        $whereConditions = [
+            'agent_id' => $agent->user_id,
+            'status' => RewardModel::COMMISSION_REWARD_STATUS_ACCOUNTED // 只查询已结算的佣金
+        ];
 
         // 年月筛选 (支持YYYY-MM格式)
         if (!empty($yearMonth)) {
             $timeWhere = $this->getYearMonthConditions($yearMonth);
             if ($timeWhere) {
-                $query->where($timeWhere);
+                $whereConditions = array_merge($whereConditions, $timeWhere);
             }
         }
 
+        // 使用数组构造查询
+        $query = RewardModel::where($whereConditions)
+            ->with(['commissionOrder']) // 关联佣金订单信息
+            ->order('commission_time desc'); // 按佣金时间倒序
+
         // 分页查询
         $data = $query->paginate($pageSize);
         $paginateData = $data->toArray();

+ 12 - 3
application/api/validate/AgentApply.php

@@ -199,7 +199,10 @@ class AgentApply extends Validate
         
         // 验证省份
         if (in_array('province_id', $requiredFields) && !empty($data['province_id'])) {
-            $province = $areaModel->where('id', $data['province_id'])->where('level', 1)->find();
+            $province = $areaModel->where([
+                'id' => $data['province_id'],
+                'level' => 1
+            ])->find();
             if (!$province) {
                 return '选择的省份不存在';
             }
@@ -207,7 +210,10 @@ class AgentApply extends Validate
 
         // 验证城市
         if (in_array('city_id', $requiredFields) && !empty($data['city_id'])) {
-            $city = $areaModel->where('id', $data['city_id'])->where('level', 2)->find();
+            $city = $areaModel->where([
+                'id' => $data['city_id'],
+                'level' => 2
+            ])->find();
             if (!$city) {
                 return '选择的城市不存在';
             }
@@ -220,7 +226,10 @@ class AgentApply extends Validate
 
         // 验证区域
         if (in_array('district_id', $requiredFields) && !empty($data['district_id'])) {
-            $district = $areaModel->where('id', $data['district_id'])->where('level', 3)->find();
+            $district = $areaModel->where([
+                'id' => $data['district_id'],
+                'level' => 3
+            ])->find();
             if (!$district) {
                 return '选择的区域不存在';
             }

+ 5 - 1
application/common/Service/Withdraw.php

@@ -572,7 +572,11 @@ class Withdraw
                 } elseif (in_array($platform, ['WechatOfficialAccount', 'WechatMiniProgram'])) {
                     $platform = lcfirst(str_replace('Wechat', '', $platform));
                 }
-                $thirdOauth = ThirdOauth::where('provider', 'wechat')->where('platform', $platform)->where('user_id', $this->user->id)->find();
+                $thirdOauth = ThirdOauth::where([
+                    'provider' => 'wechat',
+                    'platform' => $platform,
+                    'user_id' => $this->user->id
+                ])->find();
                 if (!$thirdOauth) {
                     throw new BusinessException('请先绑定微信账号', -1);
                 }