search.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. <template>
  2. <view class="search-page">
  3. <!-- Navbar -->
  4. <Navbar title="搜索"></Navbar>
  5. <!-- Search Bar Area -->
  6. <view class="search-area">
  7. <view class="search-box">
  8. <image src="/pages-sell/static/search/icon-scan.png" class="search-icon-left" mode="aspectFit"></image>
  9. <input class="search-input" v-model="keyword" placeholder="书名 / 作者 / ISBN" confirm-type="search"
  10. @confirm="onSearch" :focus="true" />
  11. <u-icon name="close-circle-fill" color="#c0c4cc" size="32" v-if="keyword" @click="keyword = ''"
  12. class="clear-icon"></u-icon>
  13. <view class="search-btn" @click="onSearch">
  14. <text>搜索</text>
  15. </view>
  16. </view>
  17. </view>
  18. <!-- Search Prompt -->
  19. <view class="prompt-list" v-if="keyword.trim()">
  20. <view class="prompt-item" v-for="(item, index) in promptList" :key="index" @click="doSearch(item)">
  21. <rich-text :nodes="highlightKeyword(item, keyword)" class="prompt-text"></rich-text>
  22. <view class="arrow-wrap" @click.stop="fillKeyword(item)">
  23. <u-icon name="arrow-up" color="#ccc" size="28" style="transform: rotate(45deg);"></u-icon>
  24. </view>
  25. </view>
  26. </view>
  27. <view v-else>
  28. <!-- History -->
  29. <view class="section history-section" v-if="historyList.length">
  30. <view class="section-header">
  31. <text class="title">搜索历史</text>
  32. <image src="/pages-sell/static/search/icon-delete.png" class="icon-delete" mode="aspectFit"
  33. @click="clearHistory"></image>
  34. </view>
  35. <view class="tags-list">
  36. <view class="tag-item history-tag" v-for="(item, index) in historyList" :key="index"
  37. @click="fillKeyword(item)">
  38. {{ item }}
  39. </view>
  40. </view>
  41. </view>
  42. <!-- Hot Search -->
  43. <view class="section hot-section">
  44. <view class="section-header">
  45. <view class="left">
  46. <image src="/pages-sell/static/search/icon-fire.png" class="icon-fire" mode="aspectFit"></image>
  47. <text class="title">热门搜索</text>
  48. </view>
  49. <image
  50. :src="hotVisible ? '/pages-sell/static/search/icon-view.png' : '/pages-sell/static/search/icon-view.png'"
  51. class="icon-view" mode="aspectFit" @click="toggleHot" :style="{ opacity: hotVisible ? 1 : 0.5 }">
  52. </image>
  53. </view>
  54. <view class="tags-list" v-if="hotVisible">
  55. <view class="tag-item" v-for="(item, index) in hotList" :key="index" @click="fillKeyword(item.name)"
  56. :class="item.className">
  57. <image v-if="item.tag === 'NEW'" src="/pages-sell/static/search/icon-new.png" class="tag-icon-new"
  58. mode="heightFix"></image>
  59. <image v-if="item.tag === 'HOT'" src="/pages-sell/static/search/icon-fire.png" class="tag-icon-fire"
  60. mode="heightFix"></image>
  61. <text>{{ item.name }}</text>
  62. </view>
  63. </view>
  64. </view>
  65. </view>
  66. </view>
  67. </template>
  68. <script>
  69. import Navbar from '@/components/navbar/navbar.vue';
  70. export default {
  71. components: {
  72. Navbar,
  73. },
  74. data() {
  75. return {
  76. keyword: '',
  77. historyList: [],
  78. hotVisible: true,
  79. hotList: [],
  80. promptList: [],
  81. inputTimer: null
  82. }
  83. },
  84. watch: {
  85. keyword(val) {
  86. if (!val.trim()) {
  87. this.promptList = [];
  88. return;
  89. }
  90. if (this.inputTimer) clearTimeout(this.inputTimer);
  91. this.inputTimer = setTimeout(() => {
  92. this.getPromptData(val.trim());
  93. }, 300);
  94. }
  95. },
  96. onShow() {
  97. this.getHistoryData();
  98. this.getHotData();
  99. },
  100. methods: {
  101. getPromptData(keyword) {
  102. this.$u.api.getSearchPromptAjax({ keyword }).then(res => {
  103. if (res.code == 200) {
  104. this.promptList = res.data || [];
  105. }
  106. })
  107. },
  108. highlightKeyword(text, keyword) {
  109. if (!text) return '';
  110. // 如果后端返回了 <em> 标签,直接替换样式
  111. if (text.includes('<em>')) {
  112. return text
  113. .replace(/<em>/g, '<span style="color: #37C148; font-style: normal;">')
  114. .replace(/<\/em>/g, '</span>');
  115. }
  116. if (!keyword) return text;
  117. // escape keyword for regex
  118. const escapedKeyword = keyword.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
  119. const reg = new RegExp(`(${escapedKeyword})`, 'gi');
  120. return text.replace(reg, '<span style="color: #37C148; font-style: normal;">$1</span>');
  121. },
  122. getHistoryData() {
  123. this.$u.api.getSearchHistoryAjax().then(res => {
  124. this.historyList = res.data || [];
  125. })
  126. },
  127. getHotData() {
  128. this.$u.api.getHotSearchListAjax().then(res => {
  129. this.hotList = res.data.map((v, index) => ({
  130. name: v,
  131. className: index % 2 == 0 ? 'hot-tag' : 'history-tag'
  132. })) || [];
  133. })
  134. },
  135. onSearch() {
  136. if (!this.keyword.trim()) return;
  137. this.doSearch(this.keyword);
  138. },
  139. doSearch(key) {
  140. if (!key) return;
  141. // 去除可能包含的 HTML 标签 (如后端返回的 <em>)
  142. const cleanKey = key.replace(/<[^>]+>/g, '');
  143. console.log('Search:', cleanKey);
  144. this.keyword = cleanKey;
  145. // Navigate to result page or show results
  146. uni.showToast({
  147. title: '搜索: ' + cleanKey,
  148. icon: 'none'
  149. });
  150. uni.navigateTo({
  151. url: '/pages-sell/pages/search-result?keyword=' + cleanKey
  152. });
  153. },
  154. fillKeyword(key) {
  155. if (!key) return;
  156. const cleanKey = key.replace(/<[^>]+>/g, '');
  157. this.keyword = cleanKey;
  158. },
  159. clearHistory() {
  160. uni.showModal({
  161. title: '提示',
  162. content: '确定清空搜索历史吗?',
  163. success: (res) => {
  164. if (res.confirm) {
  165. this.$u.api.clearSearchHistoryAjax().then(res => {
  166. if (res.code == 200) {
  167. this.historyList = [];
  168. uni.showToast({
  169. title: '已清空',
  170. icon: 'none'
  171. });
  172. }
  173. });
  174. }
  175. }
  176. });
  177. },
  178. toggleHot() {
  179. this.hotVisible = !this.hotVisible;
  180. }
  181. }
  182. }
  183. </script>
  184. <style lang="scss" scoped>
  185. .search-page {
  186. min-height: 100vh;
  187. background-color: #fff;
  188. font-family: 'Source Han Sans SC', sans-serif;
  189. }
  190. .search-area {
  191. padding: 30rpx 30rpx;
  192. }
  193. .search-box {
  194. display: flex;
  195. align-items: center;
  196. background-color: #F6F6F6;
  197. border-radius: 40rpx;
  198. height: 80rpx;
  199. padding: 0 10rpx 0 30rpx;
  200. .search-icon-left {
  201. width: 36rpx;
  202. height: 36rpx;
  203. margin-right: 20rpx;
  204. }
  205. .search-input {
  206. flex: 1;
  207. height: 100%;
  208. font-size: 28rpx;
  209. color: #333;
  210. }
  211. .clear-icon {
  212. margin: 0 10rpx;
  213. }
  214. .search-btn {
  215. width: 120rpx;
  216. height: 60rpx;
  217. background: linear-gradient(0deg, #37C148 0%, #6ADD83 100%);
  218. border-radius: 48rpx; // 24px
  219. display: flex;
  220. align-items: center;
  221. justify-content: center;
  222. margin-left: 20rpx;
  223. text {
  224. color: #fff;
  225. font-size: 28rpx;
  226. font-weight: 500;
  227. }
  228. }
  229. }
  230. .section {
  231. padding: 0 30rpx 30rpx;
  232. .section-header {
  233. display: flex;
  234. justify-content: space-between;
  235. align-items: center;
  236. margin-bottom: 30rpx;
  237. .left {
  238. display: flex;
  239. align-items: center;
  240. }
  241. .title {
  242. font-size: 32rpx;
  243. font-weight: bold;
  244. color: #333;
  245. }
  246. .icon-delete,
  247. .icon-view {
  248. width: 32rpx;
  249. height: 32rpx;
  250. }
  251. .icon-fire {
  252. width: 36rpx;
  253. height: 36rpx;
  254. margin-right: 10rpx;
  255. }
  256. }
  257. .tags-list {
  258. display: flex;
  259. flex-wrap: wrap;
  260. .tag-item {
  261. background: #F8F7F0;
  262. border-radius: 30rpx;
  263. padding: 8rpx 30rpx;
  264. font-size: 26rpx;
  265. color: #666;
  266. margin-right: 20rpx;
  267. margin-bottom: 20rpx;
  268. display: flex;
  269. align-items: center;
  270. &.history-tag {
  271. background-color: #EBF8EE;
  272. }
  273. &.hot-item {
  274. background: #F5F3F9;
  275. }
  276. .tag-icon-new {
  277. height: 16rpx;
  278. margin-right: 8rpx;
  279. }
  280. .tag-icon-fire {
  281. width: 28rpx;
  282. height: 28rpx;
  283. margin-right: 8rpx;
  284. }
  285. }
  286. }
  287. }
  288. .prompt-list {
  289. padding: 0 30rpx;
  290. .prompt-item {
  291. display: flex;
  292. align-items: center;
  293. justify-content: space-between;
  294. padding: 30rpx 0;
  295. border-bottom: 1rpx solid #F5F5F5;
  296. .prompt-text {
  297. font-size: 28rpx;
  298. color: #333;
  299. flex: 1;
  300. }
  301. .arrow-wrap {
  302. padding: 10rpx;
  303. }
  304. }
  305. }
  306. </style>