Commit b1a4cf1d by 连敬荣

代码重构

parent 0616d4f2
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use App\Model\LieBrandModel;
use Illuminate\Http\Request; use Illuminate\Http\Request;
class SearchController extends Controller class SearchController extends Controller
...@@ -11,14 +12,14 @@ class SearchController extends Controller ...@@ -11,14 +12,14 @@ class SearchController extends Controller
//新增询价,sku查询接口 //新增询价,sku查询接口
public function getSkuList(Request $request) public function getSkuList(Request $request)
{ {
$k = $request->input('k'); $sku = $request->input('k');
$types = $request->input('types'); $types = $request->input('types');
$url = Config('website.search_url') . "/search/spu/think?spu_name=" . $k; $url = Config('website.search_url') . "/search/spu/think?spu_name=" . $sku;
$res = post_curl($url, []); $res = post_curl($url, []);
$resArr = \GuzzleHttp\json_decode($res, true); $resArr = \GuzzleHttp\json_decode($res, true);
$temp = []; $temp = [];
if (count($resArr["data"]) == 0 && $types == 1) { #返回没查到的数据 if (count($resArr["data"]) == 0 && $types == 1) { #返回没查到的数据
array_push($temp, ["value" => "", "name" => $k]); array_push($temp, ["value" => "", "name" => $sku]);
} else { } else {
foreach ($resArr["data"] as $k => $v) { foreach ($resArr["data"] as $k => $v) {
array_push($temp, ["value" => json_encode($v), "name" => $v["spu_name"] . " 品牌:" . $v["brand_name"]]); array_push($temp, ["value" => json_encode($v), "name" => $v["spu_name"] . " 品牌:" . $v["brand_name"]]);
...@@ -29,10 +30,11 @@ class SearchController extends Controller ...@@ -29,10 +30,11 @@ class SearchController extends Controller
} }
//新增询价,品牌查询接口 //新增询价,品牌查询接口
public function ApiBrandList(Request $request) public function getBrandList(Request $request)
{ {
$k = $request->input('k'); $k = $request->input('k');
$types = $request->input('types'); $types = @$request->input('types')?@$request->input('types'):1;
$lieBrandModel = new LieBrandModel();
$lieBrandModel->getBrandList($k,$types);
} }
} }
\ No newline at end of file
<?php <?php
Route::Get('/ApiSkuList','SearchController@getSkuList');//新增询价查询sku接口; Route::Get('/ApiSkuList','SearchController@getSkuList');//新增询价查询sku接口;
\ No newline at end of file Route::Get('/ApiBrandList','SearchController@getBrandList');//新增询价查询品牌接口;
\ No newline at end of file
<?php
namespace App\Model;
use Illuminate\Database\Eloquent\Model;
class LieBrandModel extends Model
{
protected $connection = 'spu';
protected $table = 'brand';
protected $primaryKey = 'brand_id';
public function getBrandList($k,$types=1){
if ($types == 1) { #模糊搜索
$brandInfoList = $this->select('brand_name','brand_id')->where('brand_name','like','%'.$k.'%')->get()->toArray();
$brandInfo = [];
foreach ($brandInfoList as $k=>$v){
array_push($brandInfo,["value"=>$v["brand_id"],"name"=>$v["brand_name"]]);
}
if (count($brandInfo) == 0){
$brandInfo = [["value"=>"0","name"=>$k]];
}
echo json_encode($brandInfo,JSON_UNESCAPED_UNICODE) ;
exit();
}
if ($types == 2) { #模糊搜索
$brandInfo = $this->select("select brand_id as value,brand_name as name from lie_brand where brand_name like '%".$k."%' limit 10");
echo json_encode($brandInfo,JSON_UNESCAPED_UNICODE) ;
exit();
}
if ($types == 3){ #精确搜索
$brandInfo = $this->select("select brand_id as brand_id,brand_name as name from lie_brand where brand_name = '".$k."' limit 1");
if (!$brandInfo){
return false;
}else{
return $brandInfo[0]["brand_id"];
}
}
}
}
<?php
namespace App\Model;
use Illuminate\Database\Eloquent\Model;
class LieBrandModell extends Model
{
//
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment