gq 8 năm trước cách đây
mục cha
commit
8908b5b7cd
2 tập tin đã thay đổi với 23 bổ sung34 xóa
  1. 22 33
      server/app/Http/Controllers/Api/V1/IndexController.php
  2. 1 1
      server/routes/api.php

+ 22 - 33
server/app/Http/Controllers/Api/V1/IndexController.php

xqd xqd
@@ -139,12 +139,12 @@ class IndexController extends Controller
     }
 
     /**
-     * @api {get}  /api/index/search 搜索(search) get post
+     * @api {get}  /api/index/search 搜索(search)
      * @apiDescription 搜索(search)
      * @apiGroup Index
      * @apiPermission none
      * @apiVersion 0.1.0
-     * @apiParam {string}  keyword   关键字
+     * @apiParam {string}  keyword   关键字可选
      * @apiSuccessExample {json} Success-Response:
      * HTTP/1.1 200 OK
      *     get
@@ -186,42 +186,31 @@ class IndexController extends Controller
     public function search(Request $request)
     {
         $user = $this->getUser();
-
-        if ($request->method() == 'GET') {
-//            历史搜索
-            $data1  = $user->search()->orderBy('id','desc')->limit(10)->get();
-//            热门搜索
-            $data2 = SearchInfoModel::get();
-            $arr = [];
-            foreach ($data2 as $k => $v) {
-                if (count($arr) == 8) {
-                    break;
-                }
-                if (!array_key_exists($v->search,$arr)) {
-                    $arr[$v->search] = $v->times;
-                }else{
-                    $arr[$v->search] += $v->times;
-                }
-            }
-            arsort($arr);
-            return $this->api(compact('arr','data1'));
-        }
-
-        $validator = \Validator::make($request->all(),
-            [
-                'keyword'  => 'required',
-            ],
-            [
-                'keyword.required'  => '关键字必填',
-            ]
-        );
-        if($validator->fails()) return $this->validatorError($validator->messages()->all(),ErrorCode::CLIENT_WRONG_PARAMS);
-
         $keyword ='%'.$request->keyword.'%';
         $data1 = UserInfoModel::where('nickname','like',$keyword)->get();
         $data2 = DreamInfoModel::where('dream','like',$keyword)->
             orWhere('dream','like',$keyword)->get();
         $data3  = BaseSettingsModel::where('category','sign')->get();
+        if (empty($request->keyword)) {
+//            历史搜索
+                $data1  = $user->search()->orderBy('id','desc')->limit(10)->get();
+//            热门搜索
+                $data2 = SearchInfoModel::get();
+                $arr = [];
+                foreach ($data2 as $k => $v) {
+                    if (count($arr) == 8) {
+                        break;
+                    }
+                    if (!array_key_exists($v->search,$arr)) {
+                        $arr[$v->search] = $v->times;
+                    }else{
+                        $arr[$v->search] += $v->times;
+                    }
+                }
+                arsort($arr);
+                return $this->api(compact('arr','data1'));
+        }
+
         return $this->api(compact('data1','data2','data3'));
     }
 

+ 1 - 1
server/routes/api.php

xqd
@@ -96,7 +96,7 @@ $api->version('v1', ['namespace' => 'App\Http\Controllers\Api\V1'], function ($a
         'as' => 'index.new',
         'uses' => 'IndexController@news',
     ]);
-    $api->any('index/search', [
+    $api->get('index/search', [
         'as' => 'index.search',
         'uses' => 'IndexController@search',
     ]);