FController.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  1. <?php
  2. namespace app\common\controllers;
  3. use yii\helpers\Url;
  4. use app\common\components\SiteUrl;
  5. use app\common\helpers\Identify;
  6. use app\modules\ad\models\AdData;
  7. use app\modules\shopping\models\ShoppingStore;
  8. use app\modules\ucenter\models\UserFavorite;
  9. use app\common\components\CacheId;
  10. use app\models\SearchRecord;
  11. use app\common\components\MultiSearchUrl;
  12. use app\models\ContentModel;
  13. use app\models\ContentModelField;
  14. use yii\data\Pagination;
  15. use Yii;
  16. /**
  17. * 前台基类
  18. */
  19. class FController extends CController
  20. {
  21. public $homeUrl;//首页地址
  22. public $userInfo;
  23. public $navList;//头部导航
  24. public $footerLinks;//底部链接
  25. public $wapMenuList;//手机端菜单
  26. public $wapNavList;//手机端导航
  27. public $filterConfig;//多条件筛选初始配置
  28. public $keywords;//Meta keywords
  29. public $description;//Meta keywords
  30. public $searchtitle;//Meta searchtitle;
  31. public function init()
  32. {
  33. parent::init();
  34. $this->homeUrl = Url::to(['/web/default/index'],true);
  35. $this->navList = $this->getNavData(2);//默认取PC内页导航数据
  36. $this->footerLinks = $this->getNavData(4);
  37. $this->wapNavList = $this->getNavData(6);
  38. $this->userInfo = Identify::getUserInfo();
  39. $this->initFilterConfig();
  40. }
  41. public function behaviors()
  42. {
  43. parent::behaviors();
  44. return [
  45. [
  46. 'class' => 'app\common\filters\FrontEndTokenFilter',
  47. ],
  48. ];
  49. }
  50. //获取导航/链接数据
  51. public function getNavData($type)
  52. {
  53. $allNavList = json_decode(Yii::$app->cache->get(CacheId::navCacheId()),true);
  54. $navList = $allNavList[$type];
  55. $navData = [];
  56. if(is_array($navList))foreach($navList as $nav)
  57. {
  58. if($nav['parent_id']>0)continue;
  59. if($nav['is_dropdown']&&$nav['dropdown_level']>0)
  60. {
  61. if($nav['content_model_id'])
  62. {
  63. $modelList = json_decode(Yii::$app->cache->get(CacheId::modelListCacheId()),true);
  64. $functionName = $modelList[$nav['content_model_id']]['table_name'].'List';
  65. //SiteUrl::$functionName(8);
  66. $categoryList = json_decode(Yii::$app->cache->get(CacheId::categoryItemsCacheId($nav['content_model_id'])),true);
  67. $childs = [];
  68. if(is_array($categoryList))foreach($categoryList as $category)
  69. {
  70. if(empty($category['parent_id']))
  71. {
  72. //二级菜单
  73. $secChilds = [];
  74. if(is_array($categoryList))foreach($categoryList as $secCategory)
  75. {
  76. if($secCategory['parent_id']==$category['cat_id'])
  77. {
  78. //三级菜单
  79. $thirdChilds = [];
  80. if(is_array($categoryList))foreach($categoryList as $thirdCategory)
  81. {
  82. if($thirdCategory['parent_id']==$secCategory['cat_id'])
  83. {
  84. $thirdChilds[] = array('title'=>$thirdCategory['cat_name'],'url'=>SiteUrl::$functionName($thirdCategory['cat_id']),'blank'=>$nav['blank']);
  85. }
  86. }
  87. $secChilds[] = array('title'=>$secCategory['cat_name'],'url'=>SiteUrl::$functionName($secCategory['cat_id']),'blank'=>$nav['blank'],'childs'=>$thirdChilds);
  88. }
  89. }
  90. $childs[] = array('title'=>$category['cat_name'],'url'=>SiteUrl::$functionName($category['cat_id']),'blank'=>$nav['blank'],'childs'=>$secChilds);
  91. }
  92. }
  93. $navData[] = array('title'=>$nav['title'],'url'=>$nav['url'],'blank'=>$nav['blank'],'width'=>$nav['width'],'is_dropdown'=>$nav['is_dropdown'],'dropdown_level'=>$nav['dropdown_level'],'iconfont'=>$nav['iconfont'],'childs'=>$childs);
  94. }
  95. else
  96. {
  97. $childs = [];
  98. if(is_array($navList))foreach($navList as $childNav)
  99. {
  100. if($childNav['parent_id']==$nav['id'])
  101. {
  102. $secChilds = [];
  103. if(is_array($navList))foreach($navList as $secNav)
  104. {
  105. if($secNav['parent_id']==$childNav['id'])
  106. {
  107. //三级菜单
  108. $thirdChilds = [];
  109. if(is_array($navList))foreach($navList as $thirdNav)
  110. {
  111. if($thirdNav['parent_id']==$secNav['id'])
  112. {
  113. $thirdChilds[] = array('title'=>$thirdNav['title'],'url'=>$thirdNav['url'],'blank'=>$thirdNav['blank'],'is_new'=>$thirdNav['is_new'],'is_hot'=>$thirdNav['is_hot'],'iconfont'=>$thirdNav['iconfont']);
  114. }
  115. }
  116. $secChilds[] = array('title'=>$secNav['title'],'url'=>$secNav['url'],'blank'=>$secNav['blank'],'is_new'=>$secNav['is_new'],'is_hot'=>$secNav['is_hot'],'iconfont'=>$secNav['iconfont'],'childs'=>$thirdChilds);
  117. }
  118. }
  119. $childs[] = array('title'=>$childNav['title'],'url'=>$childNav['url'],'blank'=>$childNav['blank'],'is_new'=>$childNav['is_new'],'is_hot'=>$childNav['is_hot'],'iconfont'=>$childNav['iconfont'],'childs'=>$secChilds);
  120. }
  121. }
  122. $navData[] = array('title'=>$nav['title'],'url'=>$nav['url'],'blank'=>$nav['blank'],'width'=>$nav['width'],
  123. 'is_dropdown'=>$nav['is_dropdown'],'dropdown_level'=>$nav['dropdown_level'],'iconfont'=>$nav['iconfont'],'childs'=>$childs);
  124. }
  125. }
  126. else
  127. {
  128. $childs = [];
  129. if(is_array($navList))foreach($navList as $childNav)
  130. {
  131. if($childNav['parent_id']==$nav['id'])
  132. {
  133. $secChilds = [];
  134. if(is_array($navList))foreach($navList as $secNav)
  135. {
  136. if($secNav['parent_id']==$childNav['id'])
  137. {
  138. //三级菜单
  139. $thirdChilds = [];
  140. if(is_array($navList))foreach($navList as $thirdNav)
  141. {
  142. if($thirdNav['parent_id']==$secNav['id'])
  143. {
  144. $thirdChilds[] = array('title'=>$thirdNav['title'],'url'=>$thirdNav['url'],'blank'=>$thirdNav['blank'],'is_new'=>$thirdNav['is_new'],'is_hot'=>$thirdNav['is_hot'],'iconfont'=>$thirdNav['iconfont']);
  145. }
  146. }
  147. $secChilds[] = array('title'=>$secNav['title'],'url'=>$secNav['url'],'blank'=>$secNav['blank'],'is_new'=>$secNav['is_new'],'is_hot'=>$secNav['is_hot'],'iconfont'=>$secNav['iconfont'],'childs'=>$thirdChilds);
  148. }
  149. }
  150. $childs[] = array('title'=>$childNav['title'],'url'=>$childNav['url'],'blank'=>$childNav['blank'],'is_new'=>$childNav['is_new'],'is_hot'=>$childNav['is_hot'],'iconfont'=>$childNav['iconfont'],'childs'=>$secChilds);
  151. }
  152. }
  153. $navData[] = array('title'=>$nav['title'],'url'=>$nav['url'],'blank'=>$nav['blank'],'is_dropdown'=>$nav['is_dropdown'],'dropdown_level'=>$nav['dropdown_level'],'iconfont'=>$nav['iconfont'],'childs'=>$childs);
  154. }
  155. }
  156. return $navData;
  157. }
  158. //单条记录字段值修改
  159. public function actionSetfield()
  160. {
  161. $table = strtolower(safe_replace($_GET['table']));
  162. $fileldName = safe_replace($_GET['field']);
  163. $fieldValue = safe_replace($_GET['value']);
  164. $pk = safe_replace($_GET['pk']);
  165. $pkField = \app\common\models\EActiveRecord::getPrikey($table);
  166. if(!empty($pk)&&!empty($fileldName))
  167. {
  168. if(empty($this->userInfo))
  169. {
  170. $msgdata = ['error' => 1,'msg' => '操作失败'];
  171. }
  172. else
  173. {
  174. $result = Yii::$app->db->createCommand("UPDATE {{%$table}} SET $fileldName='$fieldValue' WHERE $pkField='$pk' and user_id=".$this->userInfo['user_id'])->execute();
  175. if($result)
  176. {
  177. $msgdata = ['error' => 0,'msg' => '操作成功'];
  178. }
  179. else
  180. {
  181. $msgdata = ['error' => 1,'msg' => '操作失败'];
  182. }
  183. }
  184. }
  185. else
  186. {
  187. $msgdata = ['error' => 1,'msg' => '操作失败'];
  188. }
  189. echo_json($msgdata);
  190. }
  191. //获取广告数据
  192. public function getAdData($space_id)
  193. {
  194. $dataList = AdData::find()->where("space_id='".$space_id."' and status=1 and end_time>".TIMESTAMP)->orderBy(['list_order'=>SORT_ASC])->all();
  195. return $dataList;
  196. }
  197. //判断当前用户是否已关注该记录
  198. public function checkFavor($table_name,$data_id)
  199. {
  200. if($this->hasLogined())
  201. {
  202. $user_id = Identify::getUserInfo(null,'user_id');
  203. $exist = UserFavorite::find()->where("user_id=$user_id and table_name='".$table_name."' and data_id='".$data_id."'")->exists();
  204. if($exist)
  205. {
  206. return true;
  207. }
  208. else
  209. {
  210. return false;
  211. }
  212. }
  213. else
  214. {
  215. return false;
  216. }
  217. }
  218. //返回个人主页链接
  219. function getUpage($user_id,$open_home=0)
  220. {
  221. if(defined('IN_WAP')&&IN_WAP==TRUE)
  222. {
  223. $str = 'href="javascript:;"';
  224. }
  225. else
  226. {
  227. if($open_home)
  228. {
  229. $str = 'href="'.\app\common\components\SiteUrl::uHome($user_id).'" target="_blank"';
  230. }
  231. else
  232. {
  233. $str = 'href="javascript:;"';
  234. }
  235. }
  236. return $str;
  237. }
  238. //返回前台创作者名称信息
  239. function getOwnername($user_id,$nick_name='')
  240. {
  241. if(empty($user_id))return '游客';
  242. $store = ShoppingStore::find()->where("user_id=$user_id")->one();
  243. if($store)
  244. {
  245. return $store->store_name;
  246. }
  247. else
  248. {
  249. if(!empty($nick_name))
  250. {
  251. return $nick_name;
  252. }
  253. else
  254. {
  255. $user = \app\modules\ucenter\models\User::findOne($user_id);
  256. return $user->nick_name;
  257. }
  258. }
  259. }
  260. //返回前台创作者名称信息
  261. function getOwneravatar($user_id,$avatar='')
  262. {
  263. if(empty($user_id))return;
  264. $store = ShoppingStore::find()->where("user_id=$user_id")->one();
  265. if($store)
  266. {
  267. return $store->logo;
  268. }
  269. else
  270. {
  271. if(!empty($avatar))
  272. {
  273. return $avatar;
  274. }
  275. else
  276. {
  277. $user = \app\modules\ucenter\models\User::findOne($user_id);
  278. return $user->avatar;
  279. }
  280. }
  281. }
  282. //返回前台主页简介
  283. function getOwnerdes($user_id,$des='')
  284. {
  285. if(empty($user_id))return '';
  286. $store = ShoppingStore::find()->where("user_id=$user_id")->one();
  287. if($store)
  288. {
  289. return $store->desc;
  290. }
  291. else
  292. {
  293. if(!empty($des))
  294. {
  295. return $des;
  296. }
  297. else
  298. {
  299. $user = \app\modules\ucenter\models\User::findOne($user_id);
  300. return $user->signature;
  301. }
  302. }
  303. }
  304. //返回前台主页关键词
  305. function getOwnerkeywords($user_id,$keywords='')
  306. {
  307. if(empty($user_id))return '';
  308. $store = ShoppingStore::find()->where("user_id=$user_id")->one();
  309. if($store)
  310. {
  311. return $store->keywords;
  312. }
  313. else
  314. {
  315. return $keywords;
  316. }
  317. }
  318. //初始化列表多条件筛选配置
  319. public function initFilterConfig()
  320. {
  321. //文档筛选条件
  322. $this->filterConfig['doc'] = array(
  323. 'fieldConfig' => array(
  324. 'c'=>array('type'=>'cat_ids','flag'=>'like','field'=>'cat_ids'),
  325. 'xa'=>array('type'=>'equal','flag'=>'=','field'=>'ext_type_1'),
  326. 'xb'=>array('type'=>'equal','flag'=>'=','field'=>'ext_type_2'),
  327. 'xc'=>array('type'=>'equal','flag'=>'=','field'=>'ext_type_3'),
  328. 'xd'=>array('type'=>'equal','flag'=>'=','field'=>'ext_type_4'),
  329. 'xe'=>array('type'=>'equal','flag'=>'=','field'=>'ext_type_5'),
  330. 'sp'=>array('type'=>'boollist','flag'=>'=','field'=>array('is_new','is_hot','is_original','is_recommend')),
  331. 'vp'=>array('type'=>'boollist','flag'=>'=','field'=>array('is_vip','vip_free','is_free')),
  332. ),
  333. 'sortConfig' => array(
  334. '1'=>array('value'=>1, 'order'=>'create_time desc','title'=>'最新上传','tips'=>'点击按上传时间排序'),
  335. '2'=>array('value'=>2, 'order'=>'downs desc','title'=>'热门下载','tips'=>'点击按下载量排序'),
  336. '3'=>array('value'=>3, 'order'=>'views desc','title'=>'浏览优先','tips'=>'点击按浏览量排序'),
  337. '4'=>array('value'=>4, 'order'=>'favors desc','title'=>'收藏优先','tips'=>'点击按收藏量排序'),
  338. ),
  339. 'sortConfigWap' => array(
  340. '1'=>array('value'=>1, 'order'=>'create_time desc','title'=>'综合','tips'=>'点击按上传时间排序'),
  341. '2'=>array('value'=>2, 'order'=>'downs desc','title'=>'下载量','tips'=>'点击按下载量排序'),
  342. '3'=>array('value'=>3, 'order'=>'views desc','title'=>'浏览量','tips'=>'点击按浏览量排序'),
  343. ),
  344. 'xConfig' => array('1'=>'xa','2'=>'xb','3'=>'xc','4'=>'xd','5'=>'xe'),
  345. 'spConfig' => array('1'=>'最新','2'=>'最热','3'=>'原创','4'=>'推荐'),
  346. 'vpConfig' => array('1'=>'VIP专享','2'=>'VIP免费','3'=>'免费')
  347. );
  348. //资讯筛选条件
  349. $this->filterConfig['news'] = array(
  350. 'fieldConfig' => array(
  351. 'c'=>array('type'=>'cat_ids','flag'=>'like','field'=>'cat_ids'),
  352. 'sp'=>array('type'=>'boollist','flag'=>'=','field'=>array('is_new','is_hot','is_recommend')),
  353. ),
  354. 'sortConfig' => array(
  355. '1'=>array('value'=>1, 'order'=>'create_time desc','title'=>'最新发布','tips'=>'点击按发布时间排序'),
  356. '2'=>array('value'=>2, 'order'=>'digs desc','title'=>'点赞优先','tips'=>'点击按点赞量排序'),
  357. '3'=>array('value'=>3, 'order'=>'views desc','title'=>'浏览优先','tips'=>'点击按浏览量排序'),
  358. '4'=>array('value'=>4, 'order'=>'favors desc','title'=>'收藏优先','tips'=>'点击按收藏量排序'),
  359. ),
  360. 'spConfig' => array('1'=>'最新','2'=>'最热','3'=>'推荐'),
  361. );
  362. }
  363. //搜索逻辑处理
  364. public function doSearch($kw,$model_id,$higherconfig=null)
  365. {
  366. if(!empty($higherconfig))$mapp = true;
  367. require(BASE_PATH.'common'.DIRECTORY_SEPARATOR.'components'.DIRECTORY_SEPARATOR.'sphinxapi.php');
  368. $higherconfig = $higherconfig?$higherconfig:Yii::$app->controller->module->higherconfig;
  369. //每页显示数量
  370. $pageSize = 10;
  371. //记录搜索热点
  372. $kwModel = SearchRecord::find()->where("keyword='".$kw."' and content_model_id=$model_id")->one();
  373. if(!empty($kwModel))
  374. {
  375. $kwModel->times++;
  376. $kwModel->save();
  377. }
  378. else
  379. {
  380. $kwModel = new SearchRecord();
  381. $kwModel->content_model_id = $model_id;
  382. $kwModel->keyword = $kw;
  383. $kwModel->times = 1;
  384. $kwModel->create_time = TIMESTAMP;
  385. $kwModel->save();
  386. }
  387. //获取排序配置
  388. $searchModelList = getSysconfigValue('search_models_flag');
  389. $contentFilterConfig = $this->filterConfig[$searchModelList[$model_id]];
  390. //筛选条件解析
  391. $multiSearch = MultiSearchUrl::getSelfInstance(array('fieldConfig'=> $contentFilterConfig['fieldConfig'],'sortConfig'=>$contentFilterConfig['sortConfig']));
  392. $searchConditions = $multiSearch->getSearchConditions();
  393. //序列化多维度筛选
  394. $sqlInfo = $multiSearch->sqllizeConditions();
  395. $st = $multiSearch->getConditionValueByType($searchConditions,'st');
  396. //没有排序条件,默认按最新上传时间
  397. if(empty($sqlInfo['order']))$sqlInfo['order']="order by create_time desc";
  398. $page = Yii::$app->request->get('page',1);
  399. if($higherconfig['open_sphinx'])
  400. {
  401. $cl = new \SphinxClient ();
  402. $q = $kw;
  403. $host = "localhost";
  404. $port = 9312;
  405. //此处变量名称与内容模型主表名一一对应
  406. $index = $higherconfig[$searchModelList[$model_id].'_index'];
  407. $cl->SetServer ( $host, $port );
  408. $connect = $cl->_Connect();
  409. if(!$connect)
  410. {
  411. $this->showMessage(array('class'=>'danger','message'=>'sphinx连接失败'),1);
  412. }
  413. $cl->SetConnectTimeout ( 1 );
  414. $cl->SetArrayResult ( true );
  415. $cl->SetMatchMode(SPH_MATCH_ALL);
  416. $cl->SetLimits(($page-1)*$pageSize,$pageSize);
  417. $cl->SetFieldWeights(array('title'=>10,'tags'=>5,'keywords'=>5,'description'=>3));
  418. if(!empty($st))
  419. {
  420. $cl->SetSortMode(SPH_SORT_EXTENDED, str_replace("order by ","",$sqlInfo['order']));
  421. }
  422. else
  423. {
  424. $cl->SetSortMode(SPH_SORT_EXTENDED, "@weight DESC,@id DESC");//按照相关度排序
  425. }
  426. $res = $cl->Query ( $q, $index );
  427. $count = $res['total_found'];
  428. $resultList = [];
  429. //定义关键字标注内容
  430. $opts = array('before_match'=>'<span style="color:red">','after_match'=>'</span>');
  431. if(is_array($res['matches']))foreach($res['matches'] as $k=>$match)
  432. {
  433. // "mysql" 我的索引名 “你好”要标红的关键字;
  434. $go = $cl->BuildExcerpts(array($match['attrs']['title']),$index,$kw,$opts);
  435. $res['matches'][$k]['attrs']['title'] = $go[0];
  436. $resultList[$k] = $res['matches'][$k]['attrs'];
  437. $resultList[$k]['id'] = $match['id'];
  438. $resultList[$k]['description'] = str_replace($kw,"<span style='color:red;'>$kw</span>",$resultList[$k]['description']);
  439. }
  440. $pages = new Pagination([
  441. 'totalCount' => $count,
  442. 'pageSize' => $pageSize,
  443. 'defaultPageSize' => $pageSize, //添加默认
  444. ]);
  445. }
  446. else
  447. {
  448. $contentModel = ContentModel::findOne($model_id);
  449. $contentModelFieldList = ContentModelField::find()->where("model_id=$model_id")->all();
  450. if($contentModel->table_name=='doc')
  451. {
  452. $tableName = Yii::$app->db->tablePrefix.$contentModel->table_name.'_real';
  453. }
  454. else
  455. {
  456. $tableName = Yii::$app->db->tablePrefix.$contentModel->table_name;
  457. }
  458. //增加标记判断
  459. if(is_array($contentModelFieldList))foreach($contentModelFieldList as $contentModelField)
  460. {
  461. if(in_array($contentModelField->field,['is_delete','is_del','disabled']))
  462. {
  463. $sqlother[] = " and ".$contentModelField->field.'=0';
  464. }
  465. }
  466. if(!empty($sqlother))$sqlother = join(" ",$sqlother);
  467. $query = new \yii\db\Query();
  468. $query->from($tableName);
  469. $sql = "(title like '%".$kw."%' or keywords like '%".$kw."%' or description like '%".$kw."%') and status=1 ".$sqlother;
  470. $query->where($sql);
  471. $query->orderBy(str_replace('order by ','',$sqlInfo['order']));
  472. $countQuery = clone $query;
  473. $count = $countQuery->count();
  474. $pages = new Pagination([
  475. 'totalCount' => $count,
  476. 'pageSize' => $pageSize,
  477. 'defaultPageSize' => $pageSize, //添加默认
  478. ]);
  479. //获取当前页内容
  480. $resultList = $query->offset($pages->offset)->limit($pages->limit)->all();
  481. if(is_array($resultList))foreach($resultList as $k=>$result)
  482. {
  483. $resultList[$k]['title'] = str_replace($kw,"<span style='color:red;'>$kw</span>",$resultList[$k]['title']);
  484. $resultList[$k]['description'] = str_replace($kw,"<span style='color:red;'>$kw</span>",$resultList[$k]['description']);
  485. }
  486. }
  487. if(!$mapp)
  488. {
  489. $badwords = getSearchBadWords();
  490. $hournow = get_date(TIMESTAMP,'H');
  491. $filter = true;
  492. if(Yii::$app->controller->module->badwordconfig['hours'])
  493. {
  494. $hours = explode(",",Yii::$app->controller->module->badwordconfig['hours']);
  495. if(!in_array($hournow,$hours))
  496. {
  497. $filter = false;
  498. }
  499. }
  500. if($filter)
  501. {
  502. //设定过滤条件(爬虫过滤)
  503. if(isSpider()&&Yii::$app->controller->module->badwordconfig['open_spider'])
  504. {
  505. if(is_array($badwords))foreach($badwords as $badword)
  506. {
  507. if(strpos($kw,$badword)!==false)
  508. {
  509. return ['resultList'=>[],'searchConditions'=>$searchConditions,'st'=>[],'count'=>0,'pages'=>[],'pageCount'=>0,'contentFilterConfig'=>[],'currentPage'=>0,'pageSize'=>0];
  510. }
  511. }
  512. }
  513. //设定过滤条件(手机站)
  514. if(!isSpider()&&defined('IN_WAP')&&IN_WAP==TRUE&&Yii::$app->controller->module->badwordconfig['open_wap'])
  515. {
  516. if(is_array($badwords))foreach($badwords as $badword)
  517. {
  518. if(strpos($kw,$badword)!==false)
  519. {
  520. return ['resultList'=>[],'searchConditions'=>$searchConditions,'st'=>[],'count'=>0,'pages'=>[],'pageCount'=>0,'contentFilterConfig'=>[],'currentPage'=>0,'pageSize'=>0];
  521. }
  522. }
  523. }
  524. //设定过滤条件(PC站)
  525. if(!isSpider()&&defined('REQUEST_FROM')&&REQUEST_FROM==1&&Yii::$app->controller->module->badwordconfig['open_pc'])
  526. {
  527. if(is_array($badwords))foreach($badwords as $badword)
  528. {
  529. if(strpos($kw,$badword)!==false)
  530. {
  531. return ['resultList'=>[],'searchConditions'=>$searchConditions,'st'=>[],'count'=>0,'pages'=>[],'pageCount'=>0,'contentFilterConfig'=>[],'currentPage'=>0,'pageSize'=>0];
  532. }
  533. }
  534. }
  535. }
  536. }
  537. return ['resultList'=>$resultList,'searchConditions'=>$searchConditions,'st'=>$st,'count'=>$count,'pages'=>$pages,'pageCount'=>ceil($count/$pageSize),'contentFilterConfig'=>$contentFilterConfig,'currentPage'=>$page,'pageSize'=>$pageSize];
  538. }
  539. //列表页Sphinx查询
  540. public function doList($query,$sql,$sqlInfo,$table,$pageSize,$higherconfig=null)
  541. {
  542. require(BASE_PATH.'common'.DIRECTORY_SEPARATOR.'components'.DIRECTORY_SEPARATOR.'sphinxapi.php');
  543. $page = Yii::$app->request->get('page',1);
  544. $higherconfig = $higherconfig?$higherconfig:Yii::$app->controller->module->higherconfig;
  545. if($higherconfig['open_sphinx'])
  546. {
  547. $contentModel = ContentModel::find()->where("table_name='".$table."'")->one();
  548. $cl = new \SphinxClient ();
  549. $host = "localhost";
  550. $port = 9312;
  551. //此处变量名称与内容模型主表名一一对应
  552. $searchModelList = getSysconfigValue('search_models_flag');
  553. $index = $higherconfig[$searchModelList[$contentModel->model_id].'_index'];
  554. $cl->SetServer ( $host, $port );
  555. $connect = $cl->_Connect();
  556. if(!$connect)
  557. {
  558. $this->showMessage(array('class'=>'danger','message'=>'sphinx连接失败'),1);
  559. }
  560. $cl->SetConnectTimeout ( 1 );
  561. $cl->SetArrayResult ( true );
  562. $cl->SetMatchMode(SPH_MATCH_EXTENDED2);
  563. $cl->SetLimits(($page-1)*$pageSize,$pageSize);
  564. if(empty($sqlInfo['order']))$sqlInfo['order']="order by create_time desc";
  565. $cl->SetSortMode(SPH_SORT_EXTENDED, str_replace("order by ","",$sqlInfo['order']));
  566. $fieldList = ContentModelField::find()->where("model_id=".$contentModel->model_id)->all();
  567. if(is_array($fieldList))foreach($fieldList as $field)
  568. {
  569. $sql = str_replace($field->field,'@'.$field->field,$sql);
  570. }
  571. $sql = str_replace('and','&',$sql);
  572. $sql = str_replace('or','|',$sql);
  573. $sql = str_replace('%','',$sql);
  574. $sql = str_replace('like','',$sql);
  575. $res = $cl -> query($sql,$index);
  576. $count = $res['total_found'];
  577. /* echo $sql;
  578. echo "Query failed: " . $cl->GetLastError() . ".\n";
  579. echo json_encode($res);*/
  580. $resultList = [];
  581. //定义关键字标注内容
  582. if(is_array($res['matches']))foreach($res['matches'] as $k=>$match)
  583. {
  584. $resultList[$k] = $res['matches'][$k]['attrs'];
  585. $resultList[$k]['id'] = $match['id'];
  586. }
  587. $pages = new Pagination([
  588. 'totalCount' => $count,
  589. 'pageSize' => $pageSize,
  590. 'defaultPageSize' => $pageSize, //添加默认
  591. ]);
  592. $return = [
  593. 'count'=>$count,
  594. 'pages'=>$pages,
  595. 'pageSize'=>$pageSize,
  596. 'resultList'=>$resultList,
  597. ];
  598. }
  599. else
  600. {
  601. $query->where($sql);
  602. //没有排序条件,默认按最新上传时间
  603. if(empty($sqlInfo['order']))$sqlInfo['order']="order by create_time desc";
  604. $query->orderBy(str_replace('order by ','',$sqlInfo['order']));
  605. //分页
  606. $countQuery = clone $query;
  607. $pages = new Pagination([
  608. 'totalCount' => $countQuery->count(),
  609. 'pageSize' => $pageSize,
  610. 'defaultPageSize' => $pageSize //添加默认
  611. ]);
  612. $resultList = $query->offset($pages->offset)->limit($pages->limit)->all();
  613. $return = [
  614. 'count'=>$countQuery->count(),
  615. 'pages'=>$pages,
  616. 'pageSize'=>$pageSize,
  617. 'resultList'=>$resultList,
  618. ];
  619. }
  620. return $return;
  621. }
  622. }