控制台应用,yzncms本身基于tp5.1框架,里面的队列用不了,bug,坑
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Search.php 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Yzncms [ 御宅男工作室 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2018 http://yzncms.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: 御宅男 <530765310@qq.com>
  10. // +----------------------------------------------------------------------
  11. // +----------------------------------------------------------------------
  12. // | 搜索管理
  13. // +----------------------------------------------------------------------
  14. namespace app\cms\controller;
  15. use addons\cms\library\FulltextSearch;
  16. use app\cms\model\Cms as Cms_Model;
  17. use think\Db;
  18. class Search extends Cmsbase
  19. {
  20. protected $noNeedLogin = ['*'];
  21. protected $noNeedRight = [];
  22. protected function initialize()
  23. {
  24. parent::initialize();
  25. $this->Cms_Model = new Cms_Model;
  26. }
  27. // 搜索
  28. public function index()
  29. {
  30. if ($this->cmsConfig['web_site_searchtype'] == 'xunsearch') {
  31. $info = get_addon_info('xunsearch');
  32. if (!$info || $info['status'] != 1) {
  33. return $this->error('请在后台插件管理中安装《迅搜搜索》并启用后再尝试');
  34. }
  35. return $this->xunsearch();
  36. }
  37. $seo = seo('', '搜索结果');
  38. //模型
  39. $modelid = $this->request->param('modelid/d', 0);
  40. //关键词
  41. $keyword = $this->request->param('keyword/s', '', 'trim,safe_replace,strip_tags,htmlspecialchars');
  42. $keyword = str_replace('%', '', $keyword); //过滤'%',用户全文搜索
  43. //时间范围
  44. $time = $this->request->param('time/s', '');
  45. $result = $this->validate([
  46. 'keyword' => $keyword,
  47. ], [
  48. 'keyword|标题关键词' => 'chsDash|max:25',
  49. ]);
  50. if (true !== $result) {
  51. $this->error($result);
  52. }
  53. debug('begin');
  54. //按时间搜索
  55. if ($time == 'day') {
  56. $search_time = time() - 86400;
  57. $sql_time = ' AND create_time > ' . $search_time;
  58. } elseif ($time == 'week') {
  59. $search_time = time() - 604800;
  60. $sql_time = ' AND create_time > ' . $search_time;
  61. } elseif ($time == 'month') {
  62. $search_time = time() - 2592000;
  63. $sql_time = ' AND create_time > ' . $search_time;
  64. } elseif ($time == 'year') {
  65. $search_time = time() - 31536000;
  66. $sql_time = ' AND create_time > ' . $search_time;
  67. } else {
  68. $search_time = 0;
  69. $sql_time = '';
  70. }
  71. //搜索历史记录
  72. $shistory = cookie("shistory");
  73. if (!$shistory) {
  74. $shistory = [];
  75. }
  76. array_unshift($shistory, $keyword);
  77. $shistory = array_slice(array_unique($shistory), 0, 10);
  78. //加入搜索历史
  79. cookie("shistory", $shistory);
  80. $modellist = cache('Model');
  81. if (!$modellist) {
  82. return $this->error('没有可搜索模型~');
  83. }
  84. if ($modelid) {
  85. if (!array_key_exists($modelid, $modellist)) {
  86. $this->error('模型错误~');
  87. }
  88. $searchField = Db::name('model_field')->where('modelid', $modelid)->where('ifsystem', 1)->where('ifsearch', 1)->column('name');
  89. if (empty($searchField)) {
  90. $this->error('没有设置搜索字段~');
  91. }
  92. $where = '';
  93. foreach ($searchField as $vo) {
  94. $where .= "$vo like '%$keyword%' or ";
  95. }
  96. $where = '(' . substr($where, 0, -4) . ') ';
  97. $where .= " AND status='1' $sql_time";
  98. $list = $this->Cms_Model->getList($modelid, $where, false, '*', "listorder DESC,id DESC", 10, 1, false);
  99. } else {
  100. foreach ($modellist as $key => $vo) {
  101. $searchField = Db::name('model_field')->where('modelid', $key)->where('ifsystem', 1)->where('ifsearch', 1)->column('name');
  102. if (empty($searchField)) {
  103. continue;
  104. }
  105. $where = '';
  106. foreach ($searchField as $v) {
  107. $where .= "$v like '%$keyword%' or ";
  108. }
  109. $where = '(' . substr($where, 0, -4) . ') ';
  110. $where .= " AND status='1' $sql_time";
  111. $list = $this->Cms_Model->getList($key, $where, false, '*', 'listorder DESC,id DESC', 10, 1, false);
  112. if ($list->isEmpty()) {
  113. continue;
  114. } else {
  115. break;
  116. }
  117. }
  118. }
  119. $count = $list->total();
  120. debug('end');
  121. $this->assign([
  122. 'time' => $time,
  123. 'modelid' => $modelid,
  124. 'keyword' => $keyword,
  125. 'shistory' => $shistory,
  126. 'SEO' => $seo,
  127. 'list' => $list,
  128. 'count' => $count,
  129. 'modellist' => $modellist,
  130. 'search_time' => debug('begin', 'end', 6), //运行时间
  131. 'pages' => $list->render(),
  132. ]);
  133. if (!empty($keyword)) {
  134. return $this->fetch('/search_result');
  135. } else {
  136. return $this->fetch('/search');
  137. }
  138. }
  139. //迅搜简单搜索示例 复杂搜索重写此方法
  140. public function xunsearch()
  141. {
  142. $seo = seo('', '搜索结果');
  143. //模型
  144. $modelid = $this->request->param('modelid/d', 0);
  145. //关键词
  146. $keyword = $this->request->param('keyword/s', '', 'trim,safe_replace,strip_tags,htmlspecialchars');
  147. $keyword = str_replace('%', '', $keyword); //过滤'%',用户全文搜索
  148. //时间范围
  149. $time = $this->request->param('time/s', '');
  150. $page = $this->request->get('page/d', '1');
  151. $pagesize = 5;
  152. $order = $this->request->get('order', '');
  153. $fulltext = $this->request->get('fulltext/d', '1');
  154. $fuzzy = $this->request->get('fuzzy/d', '0');
  155. $synonyms = $this->request->get('synonyms/d', '0');
  156. $result = $this->validate([
  157. 'keyword' => $keyword,
  158. ], [
  159. 'keyword|标题关键词' => 'chsDash|max:25',
  160. ]);
  161. if (true !== $result) {
  162. $this->error($result);
  163. }
  164. $search = FulltextSearch::setQuery($keyword, $fulltext, $fuzzy, $synonyms);
  165. if ($modelid > 0) {
  166. $search->addQueryString("modelid:({$modelid})");
  167. }
  168. //按时间搜索
  169. if ($time == 'day') {
  170. //一天
  171. $search_time = time() - 86400;
  172. $search->addRange('create_time', $search_time, time());
  173. } elseif ($time == 'week') {
  174. //一周
  175. $search_time = time() - 604800;
  176. $search->addRange('create_time', $search_time, time());
  177. } elseif ($time == 'month') {
  178. //一月
  179. $search_time = time() - 2592000;
  180. $search->addRange('create_time', $search_time, time());
  181. } elseif ($time == 'year') {
  182. //一年
  183. $search_time = time() - 31536000;
  184. $search->addRange('create_time', $search_time, time());
  185. }
  186. $modellist = cache('Model');
  187. if (!$modellist) {
  188. return $this->error('没有可搜索模型~');
  189. }
  190. $query = ['keyword' => $keyword, 'modelid' => $modelid];
  191. $result = FulltextSearch::search($page, $pagesize, $order, $query);
  192. //获取热门搜索
  193. $hot = FulltextSearch::hot();
  194. $this->assign([
  195. 'time' => $time,
  196. 'modelid' => $modelid,
  197. 'keyword' => $keyword,
  198. 'SEO' => $seo,
  199. 'list' => $result['list'],
  200. 'count' => $result['count'],
  201. 'total' => $result['total'],
  202. 'search_time' => $result['search_time'], //运行时间
  203. 'pages' => $result['list']->render(),
  204. 'search' => $result['search'],
  205. 'corrected' => $result['corrected'],
  206. 'related' => $result['related'],
  207. 'hot' => $hot,
  208. 'modellist' => $modellist,
  209. ]);
  210. if (!empty($keyword)) {
  211. return $this->fetch('/xunsearch_result');
  212. } else {
  213. return $this->fetch('/search');
  214. }
  215. }
  216. }