Browse Source

fix:按照前端修改接口

super-yimizi 1 month ago
parent
commit
dce3821599
2 changed files with 25 additions and 6 deletions
  1. 9 5
      application/api/controller/Goods.php
  2. 16 1
      application/api/validate/Goods.php

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

@@ -152,11 +152,11 @@ class Goods extends Base
         if (!$validate->scene('lists')->check($param)) {
         if (!$validate->scene('lists')->check($param)) {
             $this->error($validate->getError());
             $this->error($validate->getError());
         }
         }
-        $pageSize = (int)$this->request->param('pageSize', 10);
-        $orderby = $this->request->param('orderby', 'weigh');
-        $orderway = $this->request->param('orderway', 'desc');
+        $pageSize  = $param['pageSize'] ?? 10;
+        $orderby   = $param['orderby'] ?? 'weigh';
+        $orderway  = $param['orderway'] ?? 'desc';
 
 
-        $list = GoodsModel::where(function ($query) use ($param) {
+        $query = GoodsModel::where(function ($query) use ($param) {
             $query->where('status',GoodsEnum::STATUS_ON_SALE);
             $query->where('status',GoodsEnum::STATUS_ON_SALE);
             //关键词
             //关键词
             if (isset($param['keywords']) && !empty($param['keywords'])) {
             if (isset($param['keywords']) && !empty($param['keywords'])) {
@@ -203,7 +203,11 @@ class Goods extends Base
                     $query->where('price', 'BETWEEN', $priceArr);
                     $query->where('price', 'BETWEEN', $priceArr);
                 }
                 }
             }
             }
-        })->order("{$orderby} {$orderway}")->paginate($pageSize);
+        });
+        if (!empty($orderby) && !empty($orderway)) {
+            $query = $query->order("{$orderby} {$orderway}");
+        }
+        $list = $query->paginate($pageSize);
 
 
         foreach ($list as $item) {
         foreach ($list as $item) {
             $item->visible(explode(',', 'id,title,image,price,sales,views,description,marketprice,createtime'));
             $item->visible(explode(',', 'id,title,image,price,sales,views,description,marketprice,createtime'));

+ 16 - 1
application/api/validate/Goods.php

@@ -11,6 +11,12 @@ class Goods extends Validate
      */
      */
     protected $rule = [
     protected $rule = [
         'orderby' => 'in:weigh,sales,price,views,comments',
         'orderby' => 'in:weigh,sales,price,views,comments',
+        'orderway' => 'in:asc,desc',
+        'pageSize' => 'integer|between:1,100',
+        'page' => 'integer|between:1,100',
+        'keywords' => 'string|max:255',
+        'category_id' => 'integer',
+        'brand_id' => 'integer',
     ];
     ];
 
 
     /**
     /**
@@ -18,12 +24,21 @@ class Goods extends Validate
      */
      */
     protected $message = [
     protected $message = [
         'orderby.in' => '排序字段只能是权重、销量、价格、浏览数或评论数',
         'orderby.in' => '排序字段只能是权重、销量、价格、浏览数或评论数',
+        'orderway.in' => '排序方式只能是升序或降序',
+        'pageSize.integer' => '页码必须是整数',
+        'pageSize.between' => '页码必须在1到100之间',
+        'page.integer' => '页码必须是整数',
+        'page.between' => '页码必须在1到100之间',
+        'keywords.string' => '关键词必须是字符串',
+        'keywords.max' => '关键词长度不能超过255个字符',
+        'category_id.integer' => '分类ID必须是整数',
+        'brand_id.integer' => '品牌ID必须是整数',
     ];
     ];
 
 
     /**
     /**
      * 验证场景
      * 验证场景
      */
      */
     protected $scene = [
     protected $scene = [
-        'lists' => ['orderby'],
+        'lists' => ['orderby', 'orderway', 'pageSize', 'page', 'keywords', 'category_id', 'brand_id'],
     ];
     ];
 } 
 }