Search.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | A3Mall
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2020 http://www.a3-mall.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Author: xzncit <158373108@qq.com>
  8. // +----------------------------------------------------------------------
  9. namespace app\api\service;
  10. use app\common\exception\BaseException;
  11. use app\common\models\goods\Goods as GoodsModel;
  12. use app\common\models\StatisticsSearch as StatisticsSearchModel;
  13. use app\admin\model\StatisticsSearchGoods as StatisticsSearchGoodsModel;
  14. use app\common\models\SearchKeywords as SearchKeywordsModel;
  15. use mall\utils\Tool;
  16. use think\facade\Config;
  17. class Search extends Service {
  18. /**
  19. * 获取关键字
  20. * @return array
  21. * @throws \think\db\exception\DataNotFoundException
  22. * @throws \think\db\exception\DbException
  23. * @throws \think\db\exception\ModelNotFoundException
  24. */
  25. public static function getSearchKeywordsList(){
  26. return array_map(function ($result){
  27. return $result["name"];
  28. },SearchKeywordsModel::where(["is_top"=>0])->select()->toArray());
  29. }
  30. /**
  31. * 商品列表排序
  32. * @param $data
  33. * @return array
  34. */
  35. protected static function getOrder($data){
  36. $type = $data["type"]??0;
  37. $sort = $data["sort"]??1;
  38. switch($type){
  39. case '2':
  40. $order = 'sell_price';
  41. $text = $sort == 1 ? "ASC" : "DESC";
  42. break;
  43. case '1':
  44. $order = 'sale';
  45. $text = 'DESC';
  46. break;
  47. case '0':
  48. default :
  49. $order = 'id';
  50. $text = 'DESC';
  51. break;
  52. }
  53. return [ "field"=>$order, "order"=>$text ];
  54. }
  55. /**
  56. * 获取列表数据
  57. * @param $data
  58. * @return array
  59. * @throws BaseException
  60. */
  61. public static function getList($data){
  62. $keywords = strip_tags($data["keywords"])??"";
  63. $condition = [
  64. ["status","=",0]
  65. ];
  66. $sort = self::getOrder($data);
  67. $size = Config::get("website.pageSize");
  68. $page = $data["page"]??1;
  69. $count = GoodsModel::where($condition)->where("title|content","like",'%'.$keywords.'%')->count();
  70. $result = GoodsModel::field("id,title,photo,sell_price as price,sale")->where($condition)->where("title|content","like",'%'.$keywords.'%')->order($sort["field"],$sort["order"])->page($page,$size)->select()->toArray();
  71. $array = [ "list"=>array_map(function ($res){
  72. $res["photo"] = Tool::thumb($res["photo"],"medium",true);
  73. return $res;
  74. },$result), "page"=>$page, "total"=>0, "size"=>$size ];
  75. $total = ceil($count / $size);
  76. $array["total"] = $total;
  77. if($total == $page -1){
  78. throw new BaseException("没有数据了哦!",-1,$array);
  79. }
  80. return $array;
  81. }
  82. /**
  83. * 保存搜索记录
  84. * @param $data
  85. * @return bool
  86. * @throws \think\db\exception\DataNotFoundException
  87. * @throws \think\db\exception\DbException
  88. * @throws \think\db\exception\ModelNotFoundException
  89. */
  90. public static function keywords($data){
  91. $keywords = strip_tags($data["keywords"])??"";
  92. $goods_id = intval($data["goods_id"])??0;
  93. $client_type = intval($data["client_type"])??0;
  94. $type = intval($data["type"])??0;
  95. if(empty($keywords)){
  96. throw new \Exception("ok",1);
  97. }
  98. if(StatisticsSearchModel::where("name",$keywords)->count()){
  99. StatisticsSearchModel::where("name",$keywords)->inc("num")->update();
  100. }else{
  101. StatisticsSearchModel::create(["name"=>$keywords,"num"=>1]);
  102. }
  103. if(!GoodsModel::where('id',$goods_id)->find()){
  104. return true;
  105. }
  106. StatisticsSearchGoodsModel::create([
  107. "goods_id"=>$goods_id,
  108. "name"=>$keywords,
  109. "referer"=>$client_type,
  110. "type"=>(in_array($client_type,[0,1,3]) ? $client_type : $type),
  111. "create_time"=>time()
  112. ]);
  113. return true;
  114. }
  115. }