Search.php 15 KB

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