Search.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. <?php
  2. namespace app\home\controller;
  3. use think\facade\View;
  4. use think\facade\Lang;
  5. /**
  6. * ============================================================================
  7. *
  8. * ============================================================================
  9. * 版权所有 2014-2028 浙江惠利玛产业互联网有限公司,并保留所有权利。
  10. * 网站地址: https://www.valimart.net/
  11. * ----------------------------------------------------------------------------
  12. *
  13. * ============================================================================
  14. * 控制器
  15. */
  16. class Search extends BaseMall {
  17. //每页显示商品数
  18. const PAGESIZE = 12;
  19. //模型对象
  20. private $_model_search;
  21. public function initialize() {
  22. parent::initialize();
  23. Lang::load(base_path() . 'home/lang/'.config('lang.default_lang').'/search.lang.php');
  24. }
  25. public function index() {
  26. $this->_model_search = model('search');
  27. //显示左侧分类
  28. //默认分类,从而显示相应的属性和品牌
  29. $cate_id = $default_classid = intval(input('param.cate_id'));
  30. $keyword = input('param.keyword');
  31. $goods_class_array = array();
  32. if ($default_classid > 0) {
  33. $goods_class_array = $this->_model_search->getLeftCategory(array($default_classid));
  34. } elseif ($keyword != '') {
  35. //从TAG中查找分类
  36. $goods_class_array = $this->_model_search->getTagCategory($keyword);
  37. //取出第一个分类作为默认分类,从而显示相应的属性和品牌
  38. $default_classid = isset($goods_class_array[0]) ? $goods_class_array[0] : "";
  39. $goods_class_array = $this->_model_search->getLeftCategory($goods_class_array, 1);
  40. }
  41. View::assign('goods_class_array', $goods_class_array);
  42. View::assign('default_classid', $default_classid);
  43. //获得经过属性过滤的商品信息
  44. list($goods_param, $brand_array, $initial_array, $attr_array, $checked_brand, $checked_attr) = $this->_model_search->getAttribute(input('param.'), $default_classid);
  45. View::assign('brand_array', $brand_array);
  46. View::assign('initial_array', $initial_array);
  47. View::assign('attr_array', $attr_array);
  48. View::assign('checked_brand', $checked_brand);
  49. View::assign('checked_attr', $checked_attr);
  50. //处理排序
  51. $order = 'goodscommon.mall_goods_commend desc,goodscommon.mall_goods_sort asc';
  52. $key = input('param.key');
  53. $sequence= input('param.order');
  54. if (in_array($key, array('1', '2', '3'))) {
  55. $sequence = $sequence == '1' ? 'asc' : 'desc';
  56. $order = str_replace(array('1', '2', '3'), array('goods.goods_salenum', 'goods.goods_click', 'goods.goods_promotion_price'), $key);
  57. $order .= ' ' . $sequence;
  58. }
  59. $goods_model = model('goods');
  60. // 字段
  61. $fields = "goods.goods_id,goodscommon.goods_commonid,goodscommon.goods_name,goodscommon.goods_advword,goodscommon.gc_id,goodscommon.store_id,goodscommon.store_name,goodscommon.goods_price,goods.goods_promotion_price,goods.goods_promotion_type,goodscommon.goods_marketprice,goods.goods_storage,goodscommon.goods_image,goodscommon.goods_freight,goods.goods_salenum,goods.color_id,goods.evaluation_good_star,goods.evaluation_count,goodscommon.is_virtual,goodscommon.virtual_type,goodscommon.is_goodsfcode,goodscommon.is_appoint,goods.is_have_gift";
  62. $condition = array();
  63. //执行正常搜索
  64. if (isset($goods_param['class']['depth'])) {
  65. $condition[] = array('goodscommon.gc_id_' . $goods_param['class']['depth'],'=',$goods_param['class']['gc_id']);
  66. }
  67. $b_id = intval(input('param.b_id'));
  68. if ($b_id > 0) {
  69. $condition[]=array('goodscommon.brand_id','=',$b_id);
  70. }
  71. if ($keyword != '') {
  72. $condition[]=array('goodscommon.goods_name|goodscommon.goods_advword','like', '%' . $keyword . '%');
  73. }
  74. $area_id = intval(input('param.area_id'));
  75. if ($area_id > 0) {
  76. $condition[]=array('goodscommon.areaid_1','=',$area_id);
  77. }
  78. $type = intval(input('param.type'));
  79. if ($type == 1) {
  80. $condition[]=array('goodscommon.is_platform_store','=',1);
  81. }
  82. $gift = intval(input('param.gift'));
  83. if ($gift == 1) {
  84. $condition[]=array('goods.is_have_gift','=',1);
  85. }
  86. if (isset($goods_param['goodsid_array'])) {
  87. $condition[]=array('goods.goods_id','in', $goods_param['goodsid_array']);
  88. }
  89. $priceMin = intval(input('param.priceMin'));
  90. if ($priceMin > 0) {
  91. $condition[]=array('goodscommon.goods_price','>=', $priceMin);
  92. }
  93. $priceMax = intval(input('param.priceMax'));
  94. if ($priceMax > 0) {
  95. $condition[]=array('goodscommon.goods_price','<=', $priceMax);
  96. }
  97. if ($priceMin > 0 && $priceMax > 0) {
  98. $condition[] = array('goodscommon.goods_price','between', array($priceMin, $priceMax));
  99. }
  100. $goods_list = $goods_model->getGoodsUnionList($condition, $fields, $order,'goodscommon.goods_commonid', self::PAGESIZE);
  101. // }
  102. View::assign('show_page', is_object($goods_model->page_info)?$goods_model->page_info->render():"");
  103. // 商品多图
  104. if (!empty($goods_list)) {
  105. $commonid_array = array(); // 商品公共id数组
  106. $storeid_array = array(); // 店铺id数组
  107. foreach ($goods_list as $value) {
  108. $commonid_array[] = $value['goods_commonid'];
  109. $storeid_array[] = $value['store_id'];
  110. }
  111. $commonid_array = array_unique($commonid_array);
  112. $storeid_array = array_unique($storeid_array);
  113. // 商品多图
  114. $goodsimage_more = model('goods')->getGoodsImageList(array(array('goods_commonid','in', $commonid_array)));
  115. // 店铺
  116. $store_list = model('store')->getStoreMemberIDList($storeid_array);
  117. //搜索的关键字
  118. $search_keyword = $keyword;
  119. foreach ($goods_list as $key => $value) {
  120. if(!$value['goods_storage']){
  121. $goods_info=$goods_model->getGoodsStorageByCommonId($value['goods_commonid']);
  122. if($goods_info){
  123. $value['goods_id']=$goods_info['goods_id'];
  124. }
  125. }
  126. $goods_list[$key]=$goods_model->getGoodsInfoAndPromotionById($value['goods_id']);
  127. $goods_list[$key]['virtual_type']=$value['virtual_type'];
  128. // 商品多图
  129. //商品列表主图限制不越过5个
  130. $n = 0;
  131. foreach ($goodsimage_more as $v) {
  132. if ($value['goods_commonid'] == $v['goods_commonid'] && $value['store_id'] == $v['store_id'] && $value['color_id'] == $v['color_id']) {
  133. $n++;
  134. $goods_list[$key]['image'][] = $v;
  135. if ($n >= 5)
  136. break;
  137. }
  138. }
  139. // 店铺的开店会员编号
  140. $store_id = $value['store_id'];
  141. $goods_list[$key]['member_id'] = $store_list[$store_id]['member_id'];
  142. //将关键字置红
  143. if ($search_keyword) {
  144. $goods_list[$key]['goods_name_highlight'] = str_replace($search_keyword, '<font style="color:#f00;">' . $search_keyword . '</font>', $value['goods_name']);
  145. } else {
  146. $goods_list[$key]['goods_name_highlight'] = $value['goods_name'];
  147. }
  148. }
  149. }
  150. View::assign('goods_list', $goods_list);
  151. if ($keyword != '') {
  152. View::assign('show_keyword', $keyword);
  153. } else {
  154. View::assign('show_keyword', isset($goods_param['class']['gc_name']) ? $goods_param['class']['gc_name'] : '');
  155. }
  156. $goodsclass_model = model('goodsclass');
  157. // SEO
  158. if ($keyword == '') {
  159. $seo_class_name = isset($goods_param['class']['gc_name'])?$goods_param['class']['gc_name']:'';
  160. if (is_numeric($cate_id) && empty($keyword)) {
  161. $seo_info = $goodsclass_model->getKeyWords($cate_id);
  162. if (empty($seo_info[1])) {
  163. $seo_info[1] = config('ds_config.site_name') . ' - ' . $seo_class_name;
  164. }
  165. $seo = model('seo')->type($seo_info)->param(array('name' => $seo_class_name))->show();
  166. $this->_assign_seo($seo);
  167. }
  168. } elseif ($keyword != '') {
  169. $keyword=urldecode($keyword);
  170. View::assign('html_title', (empty($keyword) ? '' : $keyword . ' - ') . config('ds_config.site_name') . lang('ds_common_search'));
  171. }
  172. // 当前位置导航
  173. $nav_link_list = $goodsclass_model->getGoodsclassnav($cate_id);
  174. View::assign('nav_link_list', $nav_link_list);
  175. // 得到自定义导航信息
  176. $nav_id = intval(input('param.nav_id'));
  177. View::assign('index_sign', $nav_id);
  178. // 地区
  179. $province_array = model('area')->getTopLevelAreas();
  180. View::assign('province_array', $province_array);
  181. /* 引用搜索相关函数 */
  182. require_once(base_path() . '/home/common_search.php');
  183. // 浏览过的商品
  184. $viewed_goods = model('goodsbrowse')->getViewedGoodsList(session('member_id'), 20);
  185. View::assign('viewed_goods', $viewed_goods);
  186. return View::fetch($this->template_dir . 'search');
  187. }
  188. /**
  189. * 获得推荐商品
  190. */
  191. public function get_hot_goods() {
  192. $gc_id = input('param.cate_id');
  193. if ($gc_id <= 0) {
  194. exit;
  195. }
  196. // 获取分类id及其所有子集分类id
  197. $goods_class = model('goodsclass')->getGoodsclassForCacheModel();
  198. if (empty($goods_class[$gc_id])) {
  199. exit;
  200. }
  201. $child = (!empty($goods_class[$gc_id]['child'])) ? explode(',', $goods_class[$gc_id]['child']) : array();
  202. $childchild = (!empty($goods_class[$gc_id]['childchild'])) ? explode(',', $goods_class[$gc_id]['childchild']) : array();
  203. $gcid_array = array_merge(array($gc_id), $child, $childchild);
  204. // 查询添加到推荐展位中的商品id
  205. $boothgoods_list = model('goods')->getGoodsOnlineList(array(array('gc_id','in', $gcid_array)), 'goods_id', 4, '');
  206. if (empty($boothgoods_list)) {
  207. exit;
  208. }
  209. $goodsid_array = array();
  210. foreach ($boothgoods_list as $val) {
  211. $goodsid_array[] = $val['goods_id'];
  212. }
  213. $fieldstr = "goods_id,goods_commonid,goods_name,goods_advword,store_id,store_name,goods_price,goods_promotion_price,goods_promotion_type,goods_marketprice,goods_storage,goods_image,goods_freight,goods_salenum,color_id,evaluation_count";
  214. $goods_list = model('goods')->getGoodsOnlineList(array(array('goods_id','in', $goodsid_array)), $fieldstr);
  215. if (empty($goods_list)) {
  216. exit;
  217. }
  218. View::assign('goods_list', $goods_list);
  219. echo View::fetch($this->template_dir.'goods_hot');
  220. }
  221. /**
  222. * 获得同类商品排行
  223. */
  224. public function get_listhot_goods() {
  225. $gc_id = input('param.cate_id');
  226. if ($gc_id <= 0) {
  227. return false;
  228. }
  229. // 获取分类id及其所有子集分类id
  230. $goods_class = model('goodsclass')->getGoodsclassForCacheModel();
  231. if (empty($goods_class[$gc_id])) {
  232. return false;
  233. }
  234. $child = (!empty($goods_class[$gc_id]['child'])) ? explode(',', $goods_class[$gc_id]['child']) : array();
  235. $childchild = (!empty($goods_class[$gc_id]['childchild'])) ? explode(',', $goods_class[$gc_id]['childchild']) : array();
  236. $gcid_array = array_merge(array($gc_id), $child, $childchild);
  237. // 查询添加到推荐展位中的商品id
  238. $boothgoods_list = model('goods')->getGoodsOnlineList(array( array('gc_id','in', $gcid_array)));
  239. if (empty($boothgoods_list)) {
  240. return false;
  241. }
  242. $goodsid_array = array();
  243. foreach ($boothgoods_list as $val) {
  244. $goodsid_array[] = $val['goods_id'];
  245. }
  246. $fieldstr = "goods_id,goods_commonid,goods_name,goods_advword,store_id,store_name,goods_price,goods_promotion_price,goods_promotion_type,goods_marketprice,goods_storage,goods_image,goods_freight,goods_salenum,color_id,evaluation_count";
  247. $goods_list = model('goods')->getGoodsOnlineList(array(array('goods_id','in', $goodsid_array)), $fieldstr, 5, 'goods_salenum desc');
  248. if (empty($goods_list)) {
  249. return false;
  250. }
  251. View::assign('goods_list', $goods_list);
  252. }
  253. /**
  254. * 获得推荐商品
  255. */
  256. public function get_booth_goods() {
  257. $gc_id = input('param.cate_id');
  258. if ($gc_id <= 0) {
  259. exit;
  260. }
  261. // 获取分类id及其所有子集分类id
  262. $goods_class = model('goodsclass')->getGoodsclassForCacheModel();
  263. if (empty($goods_class[$gc_id])) {
  264. exit;
  265. }
  266. $child = (!empty($goods_class[$gc_id]['child'])) ? explode(',', $goods_class[$gc_id]['child']) : array();
  267. $childchild = (!empty($goods_class[$gc_id]['childchild'])) ? explode(',', $goods_class[$gc_id]['childchild']) : array();
  268. $gcid_array = array_merge(array($gc_id), $child, $childchild);
  269. // 查询添加到推荐展位中的商品id
  270. $boothgoods_list = model('pbooth')->getBoothgoodsList(array(array('gc_id','in', $gcid_array)), 'goods_id', 0, 4, '');
  271. if (empty($boothgoods_list)) {
  272. exit;
  273. }
  274. $goodsid_array = array();
  275. foreach ($boothgoods_list as $val) {
  276. $goodsid_array[] = $val['goods_id'];
  277. }
  278. $fieldstr = "goods_id,goods_commonid,goods_name,goods_advword,store_id,store_name,goods_price,goods_promotion_price,goods_promotion_type,goods_marketprice,goods_storage,goods_image,goods_freight,goods_salenum,color_id,evaluation_count";
  279. $goods_list = model('goods')->getGoodsOnlineList(array(array('goods_id','in', $goodsid_array)), $fieldstr);
  280. if (empty($goods_list)) {
  281. exit;
  282. }
  283. View::assign('goods_list', $goods_list);
  284. echo View::fetch($this->template_dir.'goods_booth');
  285. }
  286. /**
  287. * 获得猜你喜欢
  288. */
  289. public function get_guesslike() {
  290. $goodslist = model('goodsbrowse')->getGuessLikeGoods(session('member_id'), 20);
  291. if (!empty($goodslist)) {
  292. View::assign('goodslist', $goodslist);
  293. echo View::fetch($this->template_dir.'goods_guesslike');
  294. }
  295. }
  296. }
  297. ?>