xunsearch.html 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. {layout name="common/layout" /}
  2. <style>
  3. .form-search label {
  4. font-weight: normal;
  5. margin-right: 5px;
  6. }
  7. .result-list {
  8. margin: 0;
  9. }
  10. .result-list dd {
  11. margin-bottom: 20px;
  12. /*border-bottom:1px solid #f3f3f3;*/
  13. }
  14. .result-list dd:last-child {
  15. margin-bottom: 0;
  16. }
  17. .result-list dd .description {
  18. font-size: 14px;
  19. color: #666;
  20. }
  21. .result-list dd .extra, .result-list dd a {
  22. font-size: 13px;
  23. color: green;
  24. margin-top: 5px;
  25. }
  26. .result-list dd .extra span {
  27. margin-right: 5px;
  28. }
  29. .result-list dd .extra strong {
  30. font-weight: 400;
  31. margin-right: 1px;
  32. }
  33. .result-list h4 {
  34. font-weight: 400;
  35. font-size: 16px;
  36. margin-bottom: 3px;
  37. line-height: 1.54;
  38. }
  39. .result-list h4 a {
  40. color: #007bff;
  41. }
  42. .result-list em {
  43. color: #c00;
  44. font-style: normal;
  45. }
  46. .result-list . field-info {
  47. }
  48. .search-suggestion {
  49. background-color: #F5F5F5;
  50. padding: 10px;
  51. margin-bottom: 15px;
  52. }
  53. .search-links {
  54. margin-top: 25px;
  55. }
  56. .search-links a {
  57. color: #007bff;
  58. margin-right: 5px;
  59. }
  60. .autocomplete-searchmenu .autocomplete-suggestion {
  61. padding: 5px 12px;
  62. }
  63. ul.orderlist li {
  64. padding: 3px 10px;
  65. }
  66. </style>
  67. <div class="p-3 mb-3 bg-white">
  68. <div class="p-3" style="border-bottom: 1px solid #eee;margin-bottom: 15px;">
  69. <div class="">
  70. <form class="form-search pt-2" id="search-form" method="get">
  71. <input type="hidden" name="order" value="{$order}">
  72. <div class="row">
  73. <div class="col-xs-12 col-sm-6" id="q-input">
  74. <div class="input-group">
  75. <input type="search" class="search-query form-control" name="q" title="输入任意关键词皆可搜索" value="<?php echo htmlspecialchars($q); ?>">
  76. <span class="input-group-btn">
  77. <button class="btn btn-primary ml-2" type="submit" style="border-radius:2px;padding:6px 30px;">搜索</button>
  78. </span>
  79. </div>
  80. </div>
  81. <div class="col-xs-12 col-sm-6 mt-2">
  82. <label><input type="radio" name="fulltext" value="0" {:$fulltext?'':'checked'} /> 标题</label>
  83. <label>
  84. <input type="radio" name="fulltext" value="1" {:$fulltext?'checked':''} /> 全文
  85. </label>
  86. <label>
  87. <input type="checkbox" name="fuzzy" value="1" {:$fuzzy?'checked':''} /> 模糊搜索
  88. </label>
  89. {if false}
  90. <label>
  91. <input type="checkbox" name="synonyms" value="1" {:$synonyms?'checked':''} /> 同义词
  92. </label>
  93. {/if}
  94. <span class="dropdown">
  95. <a class="dropdown-toggle" data-toggle="dropdown" data-target="#" href="javascript:">
  96. {:isset($orderList[$order])?$orderList[$order]:$orderList['relevance']} <span class="caret"></span>
  97. </a>
  98. <ul class="dropdown-menu orderlist">
  99. {foreach name="orderList" id="item"}
  100. <li role="presentation"><a role="menuitem" tabindex="-1" data-value="{$key}" href="javascript:">{$item}</a></li>
  101. {/foreach}
  102. </ul>
  103. </span>
  104. </div>
  105. </div>
  106. </form>
  107. </div>
  108. </div>
  109. <div class="container">
  110. <!-- 开始搜索 -->
  111. <?php if (!empty($q)): ?>
  112. <div class="">
  113. <div class="span12">
  114. <!-- 搜索状态 -->
  115. <p class="result text-muted">为您找到约 <?php echo number_format($count); ?> 条结果,搜索耗时:<?php printf('%.4f', $search_cost); ?>秒</p>
  116. <!-- 搜索建议 -->
  117. <?php if (count($corrected) > 0): ?>
  118. <div class="search-suggestion">
  119. 您是不是要找:
  120. <?php foreach ($corrected as $word): ?>
  121. <span><a href="<?php echo '?q=' . urlencode($word); ?>" class="text-danger"><?php echo $word; ?></a></span>
  122. <?php endforeach; ?>
  123. </div>
  124. <?php endif; ?>
  125. <!-- 未找到结果 -->
  126. <?php if ($count === 0 && empty($error)): ?>
  127. <div class="search-not-found">
  128. <p>找不到和 {$q|htmlspecialchars} 相符的内容或信息。</p>
  129. <p>建议您:</p>
  130. <ul class="list-unstyled">
  131. <li>1.请检查输入字词有无错误。</li>
  132. <li>2.请换用另外的查询字词。</li>
  133. <li>3.请改用较短、较为常见的字词。</li>
  134. </ul>
  135. </div>
  136. <?php endif; ?>
  137. <!-- 查询结果 -->
  138. <dl class="result-list">
  139. <?php foreach ($docs as $doc): ?>
  140. <dt>
  141. <h4>
  142. <a href="{$doc['url']}" target="_blank">
  143. <?php echo $highlight(htmlspecialchars($doc['title'])); ?>
  144. </a>
  145. </h4>
  146. </dt>
  147. <dd>
  148. <div class="description"><?php echo $fulltext ? $highlight(htmlspecialchars($doc['content'])) : htmlspecialchars($doc['content']); ?></div>
  149. <div class="extra">
  150. <span class="tag tag-xs">日志</span>
  151. <span><strong>发布于</strong>{$doc.createtime|human_date}</span>
  152. <span>{$doc['views']} 次浏览</span>
  153. <span>{$doc['comments']} 次评论</span>
  154. </div>
  155. </dd>
  156. <?php endforeach; ?>
  157. </dl>
  158. <!-- 分页 -->
  159. <?php if (!empty($pager)): ?>
  160. <div class="pager mb-0 mt-4" style="text-align:left;">
  161. <ul class="pagination">
  162. <?php echo $pager; ?>
  163. </ul>
  164. </div>
  165. <?php endif; ?>
  166. </div>
  167. </div>
  168. <?php endif; ?>
  169. <!-- 结束搜索 -->
  170. </div>
  171. <!-- 热门搜索 -->
  172. <?php if (count($hot) > 0 && false): ?>
  173. <div class="container">
  174. <div class="search-links">
  175. <h4>热门搜索</h4>
  176. <p>
  177. <?php foreach($hot as $word => $freq): ?>
  178. <span><a href="<?php echo '?q=' . urlencode($word); ?>"><?php echo $word; ?></a></span>
  179. <?php endforeach; ?>
  180. </p>
  181. </div>
  182. </div>
  183. <?php endif; ?>
  184. <!-- 相关搜索 -->
  185. <?php if (count($related) > 0): ?>
  186. <div class="container">
  187. <div class="search-links">
  188. <h4>相关搜索</h4>
  189. <p>
  190. <?php foreach ($related as $word): ?>
  191. <span><a href="<?php echo '?q=' . urlencode($word); ?>"><?php echo $word; ?></a></span>
  192. <?php endforeach; ?>
  193. </p>
  194. </div>
  195. </div>
  196. <?php endif; ?>
  197. <div class="container">
  198. <div class="search-links">
  199. <p>本搜索基于 <a href="https://www.fastadmin.net/?ref=xunsearch" target="_blank">FastAdmin</a> 和 <a href="http://www.fastadmin.net/store/xunsearch.html?ref=xunsearch" target="_blank" title="开源免费的中文全文检索">Xunsearch全文搜索插件</a> 实现</p>
  200. </div>
  201. </div>
  202. </div>
  203. <script data-render="script" src="__ADDON__/default/js/jquery.autocomplete.js"></script>
  204. <script data-render="script">
  205. $(function () {
  206. var form = $('#search-form');
  207. var search = $("input[name='q']", form);
  208. search.autoComplete({
  209. minChars: 1,
  210. menuClass: 'autocomplete-searchmenu',
  211. header: '',
  212. footer: '',
  213. source: function (term, response) {
  214. try {
  215. xhr.abort();
  216. } catch (e) {
  217. }
  218. xhr = $.getJSON('{:addon_url("blog/search/suggestion")}', {q: term}, function (data) {
  219. response(data);
  220. });
  221. },
  222. onSelect: function (e, term, item) {
  223. if (typeof callback === 'function') {
  224. callback.call(elem, term, item);
  225. } else {
  226. form.trigger("submit");
  227. }
  228. }
  229. });
  230. form.submit(function () {
  231. if (search.val() == '') {
  232. layer.msg('请先输入关键词');
  233. search.focus();
  234. return false;
  235. }
  236. });
  237. $(document).on("click", "ul.orderlist li a", function () {
  238. $("input[name=order]", form).val($(this).data("value"));
  239. form.trigger("submit");
  240. });
  241. });
  242. </script>