搜索优化

This commit is contained in:
gongfuxiang 2019-06-02 02:02:00 +08:00
parent 7955273cdc
commit c8c4880074
4 changed files with 114 additions and 25 deletions

View File

@ -11,6 +11,53 @@
// 应用公共文件
/**
* 字符串转ascii
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @datetime 2019-06-02T01:13:47+0800
* @param [string] $str [字符串]
* @return [string] [转换后的ascii]
*/
function StrToAscii($str)
{
$change_after = '';
if(!empty($str))
{
$str = mb_convert_encoding($str, 'GB2312');
for($i=0;$i<strlen($str);$i++){
$temp_str = dechex(ord($str[$i]));
$change_after .= $temp_str[1].$temp_str[0];
}
}
return strtoupper($change_after);
}
/**
* ascii转字符串
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @datetime 2019-06-02T01:14:04+0800
* @param [string] $ascii [ascii]
* @return [string] [转换后的字符串]
*/
function AsciiToStr($ascii)
{
$str = '';
if(!empty($ascii))
{
$asc_arr = str_split(strtolower($ascii), 2);
for($i=0; $i<count($asc_arr); $i++)
{
$str .= chr(hexdec($asc_arr[$i][1].$asc_arr[$i][0]));
}
}
return mb_convert_encoding($str, 'UTF-8', 'GB2312');
}
/**
* 获取当前系统所在根路径
* @author Devil

View File

@ -13,6 +13,7 @@ namespace app\index\controller;
use app\service\SearchService;
use app\service\BrandService;
use app\service\SeoService;
use app\service\GoodsService;
/**
* 搜索
@ -36,27 +37,8 @@ class Search extends Common
public function __construct()
{
parent::__construct();
// 品牌id
$this->params['brand_id'] = intval(input('brand_id', 0));
// 分类id
$this->params['category_id'] = intval(input('category_id', 0));
// 筛选价格id
$this->params['screening_price_id'] = intval(input('screening_price_id', 0));
// 搜索关键字
$this->params['wd'] = str_replace(['?', ' ', '+', '-'], '', trim(input('wd')));
// 排序方式
$this->params['order_by_field'] = input('order_by_field', 'default');
$this->params['order_by_type'] = input('order_by_type', 'desc');
// 用户信息
$this->params['user_id'] = isset($this->user['id']) ? $this->user['id'] : 0;
}
/**
* 首页
* @author Devil
@ -66,11 +48,14 @@ class Search extends Common
*/
public function Index()
{
if(input('post.'))
$keywords = input('post.wd');
if(!empty($keywords))
{
$p = empty($this->params['wd']) ? [] : ['wd'=>$this->params['wd']];
return redirect(MyUrl('index/search/index', $p));
return redirect(MyUrl('index/search/index', ['wd'=>StrToAscii($keywords)]));
} else {
// 参数初始化
$this->ParamsInit();
// 品牌列表
$this->assign('brand_list', BrandService::CategoryBrandList(['category_id'=>$this->params['category_id'], 'keywords'=>$this->params['wd']]));
@ -84,12 +69,50 @@ class Search extends Common
$this->assign('params', $this->params);
// 浏览器名称
$this->assign('home_seo_site_title', SeoService::BrowserSeoTitle('商品搜索', 1));
if(!empty($this->params['category_id']))
{
$seo_name = GoodsService::GoodsCategoryValue($this->params['category_id'], 'name', '商品搜索');
} else {
$seo_name = empty($this->params['wd']) ? '商品搜索' : $this->params['wd'];
}
$this->assign('home_seo_site_title', SeoService::BrowserSeoTitle($seo_name, 1));
return $this->fetch();
}
}
/**
* 参数初始化
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @datetime 2019-06-02T01:38:27+0800
*/
private function ParamsInit()
{
// 参数
$params = input();
// 品牌id
$this->params['brand_id'] = isset($params['brand_id']) ? intval($params['brand_id']) : 0;
// 分类id
$this->params['category_id'] = isset($params['category_id']) ? intval($params['category_id']) : 0;
// 筛选价格id
$this->params['screening_price_id'] = isset($params['screening_price_id']) ? intval($params['screening_price_id']) : 0;
// 搜索关键字
$this->params['wd'] = empty($params['wd']) ? '' : (IS_AJAX ? trim($params['wd']) : AsciiToStr($params['wd']));
// 排序方式
$this->params['order_by_field'] = empty($params['order_by_field']) ? 'default' : $params['order_by_field'];
$this->params['order_by_type'] = empty($params['order_by_type']) ? 'desc' : $params['order_by_type'];
// 用户信息
$this->params['user_id'] = isset($this->user['id']) ? $this->user['id'] : 0;
}
/**
* 获取商品列表
* @author Devil
@ -100,6 +123,9 @@ class Search extends Common
*/
public function GoodsList()
{
// 参数初始化
$this->ParamsInit();
// 获取商品列表
$this->params['keywords'] = $this->params['wd'];
$ret = SearchService::GoodsList($this->params);

View File

@ -20,7 +20,7 @@
{{if !empty($home_search_keywords)}}
<ul class="search-hot-keywords">
{{foreach $home_search_keywords as $v}}
<li><a href="{{:MyUrl('index/search/index', ['wd'=>$v])}}" target="_blank">{{$v}}</a></li>
<li><a href="{{:MyUrl('index/search/index', ['wd'=>StrToAscii($v)])}}" target="_blank">{{$v}}</a></li>
{{/foreach}}
</ul>
{{/if}}

View File

@ -2163,5 +2163,21 @@ class GoodsService
}
return DataReturn('删除失败', -100);
}
/**
* 获取商品分类字段字段数据
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @datetime 2019-06-02T01:51:31+0800
* @param [int] $category_id [商品分类id]
* @param [string] $field [指定字段值]
* @param [string] $default [默认值]
*/
public static function GoodsCategoryValue($category_id, $field, $default = null)
{
$value = Db::name('GoodsCategory')->where(['id'=>intval($category_id)])->value($field);
return ($value === null) ? $default : $value;
}
}
?>