search.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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" @click="onScan"></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. onScan() {
  140. uni.scanCode({
  141. success: (res) => {
  142. if (res.result) {
  143. this.keyword = res.result;
  144. }
  145. },
  146. fail: (err) => {
  147. console.log('扫码失败', err);
  148. }
  149. });
  150. },
  151. doSearch(key) {
  152. if (!key) return;
  153. // 去除可能包含的 HTML 标签 (如后端返回的 <em>)
  154. const cleanKey = key.replace(/<[^>]+>/g, '');
  155. console.log('Search:', cleanKey);
  156. this.keyword = cleanKey;
  157. // Navigate to result page or show results
  158. uni.showToast({
  159. title: '搜索: ' + cleanKey,
  160. icon: 'none'
  161. });
  162. uni.navigateTo({
  163. url: '/pages-sell/pages/search-result?keyword=' + cleanKey
  164. });
  165. },
  166. fillKeyword(key) {
  167. if (!key) return;
  168. const cleanKey = key.replace(/<[^>]+>/g, '');
  169. this.keyword = cleanKey;
  170. },
  171. clearHistory() {
  172. uni.showModal({
  173. title: '提示',
  174. content: '确定清空搜索历史吗?',
  175. success: (res) => {
  176. if (res.confirm) {
  177. this.$u.api.clearSearchHistoryAjax().then(res => {
  178. if (res.code == 200) {
  179. this.historyList = [];
  180. uni.showToast({
  181. title: '已清空',
  182. icon: 'none'
  183. });
  184. }
  185. });
  186. }
  187. }
  188. });
  189. },
  190. toggleHot() {
  191. this.hotVisible = !this.hotVisible;
  192. }
  193. }
  194. }
  195. </script>
  196. <style lang="scss" scoped>
  197. .search-page {
  198. min-height: 100vh;
  199. background-color: #fff;
  200. font-family: 'Source Han Sans SC', sans-serif;
  201. }
  202. .search-area {
  203. padding: 30rpx 30rpx;
  204. }
  205. .search-box {
  206. display: flex;
  207. align-items: center;
  208. background-color: #F6F6F6;
  209. border-radius: 40rpx;
  210. height: 80rpx;
  211. padding: 0 10rpx 0 30rpx;
  212. .search-icon-left {
  213. width: 36rpx;
  214. height: 36rpx;
  215. margin-right: 20rpx;
  216. }
  217. .search-input {
  218. flex: 1;
  219. height: 100%;
  220. font-size: 28rpx;
  221. color: #333;
  222. }
  223. .clear-icon {
  224. margin: 0 10rpx;
  225. }
  226. .search-btn {
  227. width: 120rpx;
  228. height: 60rpx;
  229. background: linear-gradient(0deg, #37C148 0%, #6ADD83 100%);
  230. border-radius: 48rpx; // 24px
  231. display: flex;
  232. align-items: center;
  233. justify-content: center;
  234. margin-left: 20rpx;
  235. text {
  236. color: #fff;
  237. font-size: 28rpx;
  238. font-weight: 500;
  239. }
  240. }
  241. }
  242. .section {
  243. padding: 0 30rpx 30rpx;
  244. .section-header {
  245. display: flex;
  246. justify-content: space-between;
  247. align-items: center;
  248. margin-bottom: 30rpx;
  249. .left {
  250. display: flex;
  251. align-items: center;
  252. }
  253. .title {
  254. font-size: 32rpx;
  255. font-weight: bold;
  256. color: #333;
  257. }
  258. .icon-delete,
  259. .icon-view {
  260. width: 32rpx;
  261. height: 32rpx;
  262. }
  263. .icon-fire {
  264. width: 36rpx;
  265. height: 36rpx;
  266. margin-right: 10rpx;
  267. }
  268. }
  269. .tags-list {
  270. display: flex;
  271. flex-wrap: wrap;
  272. .tag-item {
  273. background: #F8F7F0;
  274. border-radius: 30rpx;
  275. padding: 8rpx 30rpx;
  276. font-size: 26rpx;
  277. color: #666;
  278. margin-right: 20rpx;
  279. margin-bottom: 20rpx;
  280. display: flex;
  281. align-items: center;
  282. &.history-tag {
  283. background-color: #EBF8EE;
  284. }
  285. &.hot-item {
  286. background: #F5F3F9;
  287. }
  288. .tag-icon-new {
  289. height: 16rpx;
  290. margin-right: 8rpx;
  291. }
  292. .tag-icon-fire {
  293. width: 28rpx;
  294. height: 28rpx;
  295. margin-right: 8rpx;
  296. }
  297. }
  298. }
  299. }
  300. .prompt-list {
  301. padding: 0 30rpx;
  302. .prompt-item {
  303. display: flex;
  304. align-items: center;
  305. justify-content: space-between;
  306. padding: 30rpx 0;
  307. border-bottom: 1rpx solid #F5F5F5;
  308. .prompt-text {
  309. font-size: 28rpx;
  310. color: #333;
  311. flex: 1;
  312. }
  313. .arrow-wrap {
  314. padding: 10rpx;
  315. }
  316. }
  317. }
  318. </style>