Search.php 15 KB

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