store_list.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. /**
  3. * 店铺列表
  4. *
  5. * by abcbc.com
  6. */
  7. defined('InShopNC') or exit('Access Invalid!');
  8. class store_listControl extends BaseHomeControl {
  9. /**
  10. * 店铺列表
  11. */
  12. public function indexOp(){
  13. //读取语言包
  14. Language::read('home_store_class_index');
  15. $lang = Language::getLangContent();
  16. //店铺类目快速搜索
  17. $class_list = ($h = F('store_class')) ? $h : rkcache('store_class',true,'file');
  18. if (!key_exists($_GET['cate_id'],$class_list)) $_GET['cate_id'] = 0;
  19. Tpl::output('class_list',$class_list);
  20. //店铺搜索
  21. $model = Model();
  22. $condition = array();
  23. $keyword = trim($_GET['keyword']);
  24. if(C('fullindexer.open') && !empty($keyword)){
  25. //全文搜索
  26. $condition = $this->full_search($keyword);
  27. }else{
  28. if ($keyword != ''){
  29. $condition['store_name|store_zy'] = array('like','%'.$keyword.'%');
  30. }
  31. if ($_GET['user_name'] != ''){
  32. $condition['member_name'] = trim($_GET['user_name']);
  33. }
  34. }
  35. if (!empty($_GET['area_info'])){
  36. $condition['area_info'] = array('like','%'.$_GET['area_info'].'%');
  37. }
  38. if ($_GET['cate_id'] > 0){
  39. $child = array_merge((array)$class_list[$_GET['cate_id']]['child'],array($_GET['cate_id']));
  40. $condition['sc_id'] = array('in',$child);
  41. }
  42. $condition['store_state'] = 1;
  43. if (!in_array($_GET['order'],array('desc','asc'))){
  44. unset($_GET['order']);
  45. }
  46. if (!in_array($_GET['key'],array('store_sales','store_credit'))){
  47. unset($_GET['key']);
  48. }
  49. $order = 'store_sort asc';
  50. if (isset($condition['store.store_id'])){
  51. $condition['store_id'] = $condition['store.store_id'];unset($condition['store.store_id']);
  52. }
  53. $model_store = Model('store');
  54. $store_list = $model_store->where($condition)->order($order)->page(10)->select();
  55. //获取店铺商品数,推荐商品列表等信息
  56. $store_list = $model_store->getStoreSearchList($store_list);
  57. //print_r($store_list);exit();
  58. //信用度排序
  59. if($_GET['key'] == 'store_credit') {
  60. if($_GET['order'] == 'desc') {
  61. $store_list = sortClass::sortArrayDesc($store_list, 'store_credit_average');
  62. }else {
  63. $store_list = sortClass::sortArrayAsc($store_list, 'store_credit_average');
  64. }
  65. }else if($_GET['key'] == 'store_sales') {//销量排行
  66. if($_GET['order'] == 'desc') {
  67. $store_list = sortClass::sortArrayDesc($store_list, 'num_sales_jq');
  68. }else {
  69. $store_list = sortClass::sortArrayAsc($store_list, 'num_sales_jq');
  70. }
  71. }
  72. Tpl::output('store_list',$store_list);
  73. Tpl::output('show_page',$model->showpage(2));
  74. //当前位置
  75. if (intval($_GET['cate_id']) > 0){
  76. $nav_link[1]['link'] = 'index.php?act=shop_search';
  77. $nav_link[1]['title'] = $lang['site_search_store'];
  78. $nav =$class_list[$_GET['cate_id']];
  79. //如果有父级
  80. if ($nav['sc_parent_id'] > 0){
  81. $tmp = $class_list[$nav['sc_parent_id']];
  82. //存入父级
  83. $nav_link[] = array(
  84. 'title'=>$tmp['sc_name'],
  85. 'link'=>"index.php?act=store_list&cate_id=".$nav['sc_parent_id']
  86. );
  87. }
  88. //存入当前级
  89. $nav_link[] = array(
  90. 'title'=>$nav['sc_name']
  91. );
  92. }else{
  93. $nav_link[1]['link'] = 'index.php';
  94. $nav_link[1]['title'] = $lang['homepage'];
  95. $nav_link[2]['title'] = $lang['site_search_store'];
  96. }
  97. $purl = $this->getParam();
  98. Tpl::output('nav_link_list',$nav_link);
  99. Tpl::output('purl', urlShop($purl['act'], $purl['op'], $purl['param']));
  100. //SEO
  101. Model('seo')->type('index')->show();
  102. Tpl::output('html_title',(empty($_GET['keyword']) ? '' : $_GET['keyword'].' - ').C('site_name').$lang['nc_common_search']);
  103. Tpl::showpage('store_list');
  104. }
  105. /**
  106. * 全文搜索
  107. *
  108. */
  109. private function full_search($search_txt){
  110. $conf = C('fullindexer');
  111. import('libraries.sphinx');
  112. $cl = new SphinxClient();
  113. $cl->SetServer($conf['host'], $conf['port']);
  114. $cl->SetConnectTimeout(1);
  115. $cl->SetArrayResult(true);
  116. $cl->SetRankingMode($conf['rankingmode']?$conf['rankingmode']:0);
  117. $cl->setLimits(0,$conf['querylimit']);
  118. $matchmode = $conf['matchmode'];
  119. $cl->setMatchMode($matchmode);
  120. //可以使用全文搜索进行状态筛选及排序,但需要经常重新生成索引,否则结果不太准,所以暂不使用。使用数据库,速度会慢些
  121. // $cl->SetFilter('store_state',array(1),false);
  122. // if ($_GET['key'] == 'store_credit'){
  123. // $order = $_GET['order'] == 'desc' ? SPH_SORT_ATTR_DESC : SPH_SORT_ATTR_ASC;
  124. // $cl->SetSortMode($order,'store_sort');
  125. // }
  126. $res = $cl->Query($search_txt, $conf['index_shop']);
  127. if ($res){
  128. if (is_array($res['matches'])){
  129. foreach ($res['matches'] as $value) {
  130. $matchs_id[] = $value['id'];
  131. }
  132. }
  133. }
  134. if ($search_txt != ''){
  135. $condition['store.store_id'] = array('in',$matchs_id);
  136. }
  137. return $condition;
  138. }
  139. function getParam() {
  140. $param = $_GET;
  141. $purl = array();
  142. $purl['act'] = $param['act'];
  143. unset($param['act']);
  144. $purl['op'] = $param['op'];
  145. unset($param['op']); unset($param['curpage']);
  146. $purl['param'] = $param;
  147. return $purl;
  148. }
  149. }
  150. class sortClass{
  151. //升序
  152. public static function sortArrayAsc($preData,$sortType='store_sort'){
  153. $sortData = array();
  154. foreach ($preData as $key_i => $value_i){
  155. $price_i = $value_i[$sortType];
  156. $min_key = '';
  157. $sort_total = count($sortData);
  158. foreach ($sortData as $key_j => $value_j){
  159. if($price_i<$value_j[$sortType]){
  160. $min_key = $key_j+1;
  161. break;
  162. }
  163. }
  164. if(empty($min_key)){
  165. array_push($sortData, $value_i);
  166. }else {
  167. $sortData1 = array_slice($sortData, 0,$min_key-1);
  168. array_push($sortData1, $value_i);
  169. if(($min_key-1)<$sort_total){
  170. $sortData2 = array_slice($sortData, $min_key-1);
  171. foreach ($sortData2 as $value){
  172. array_push($sortData1, $value);
  173. }
  174. }
  175. $sortData = $sortData1;
  176. }
  177. }
  178. return $sortData;
  179. }
  180. //降序
  181. public static function sortArrayDesc($preData,$sortType='store_sort'){
  182. $sortData = array();
  183. foreach ($preData as $key_i => $value_i){
  184. $price_i = $value_i[$sortType];
  185. $min_key = '';
  186. $sort_total = count($sortData);
  187. foreach ($sortData as $key_j => $value_j){
  188. if($price_i>$value_j[$sortType]){
  189. $min_key = $key_j+1;
  190. break;
  191. }
  192. }
  193. if(empty($min_key)){
  194. array_push($sortData, $value_i);
  195. }else {
  196. $sortData1 = array_slice($sortData, 0,$min_key-1);
  197. array_push($sortData1, $value_i);
  198. if(($min_key-1)<$sort_total){
  199. $sortData2 = array_slice($sortData, $min_key-1);
  200. foreach ($sortData2 as $value){
  201. array_push($sortData1, $value);
  202. }
  203. }
  204. $sortData = $sortData1;
  205. }
  206. }
  207. return $sortData;
  208. }
  209. }