search-result.vue 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  1. <template>
  2. <view class="search-result-page">
  3. <page-meta :page-style="showPrompt ? 'overflow: hidden;' : ''"></page-meta>
  4. <image class="top-bg-image" src="/pages-sell/static/top-bg.png" mode="widthFix"></image>
  5. <Navbar title="书嗨" :titleSize="36" :title-color="navbarTitleColor" :back-icon-color="navbarIconColor"
  6. :background="navbarBackground"></Navbar>
  7. <!-- Custom Header -->
  8. <view class="search-bar-wrapper">
  9. <u-search style="width: 100%;" v-model="keyword" placeholder="书名/作者/ISBN" :show-action="true"
  10. action-text="反馈" :action-style="{ color: '#fff' }" bg-color="#fff" shape="round" :clearabled="true"
  11. search-icon="/pages-sell/static/search-icon.png" @custom="toFeedback" @search="onSearch"
  12. @focus="onSearchFocus" @blur="onSearchBlur"></u-search>
  13. <scroll-view scroll-y class="prompt-panel" v-if="showPrompt" style="height: 100vh;">
  14. <view class="prompt-item" v-for="(item, index) in promptList" :key="index" @click="selectPrompt(item)">
  15. <rich-text :nodes="highlightKeyword(item, keyword)" class="prompt-text"></rich-text>
  16. <view class="arrow-wrap" @click.stop="fillKeyword(item)">
  17. <u-icon name="arrow-up" color="#ccc" size="28" style="transform: rotate(45deg);"></u-icon>
  18. </view>
  19. </view>
  20. </scroll-view>
  21. </view>
  22. <!-- Banner -->
  23. <view class="banner-section">
  24. <image src="/pages-sell/static/top-banner.png" class="top-banner" mode="widthFix"></image>
  25. </view>
  26. <!-- Content Area -->
  27. <view class="content-area">
  28. <!-- Everyone is watching -->
  29. <view class="section-block" v-if="hotBook && hotBook.isbn">
  30. <text class="section-title">大家都在看</text>
  31. <HotRecommendItem :item="hotBook" @add-cart="addToCart" @update-item="updateHotBook"></HotRecommendItem>
  32. </view>
  33. <!-- Filter Bar -->
  34. <view class="filter-bar">
  35. <view class="filter-item" :class="{ active: sort === 1 }" @click="handleSort(1)">
  36. <text>综合</text>
  37. <view class="sort-icons">
  38. <u-icon name="arrow-down-fill" :color="sort === 1 ? '#38C148' : '#cccccc'" size="14"></u-icon>
  39. </view>
  40. </view>
  41. <view class="filter-item" :class="{ active: sort === 4 || sort === 5 }" @click="handleSort('sales')">
  42. <text>销量</text>
  43. <view class="sort-icons">
  44. <u-icon name="arrow-up-fill" :color="sort === 4 ? '#38C148' : '#cccccc'" size="14"></u-icon>
  45. <u-icon name="arrow-down-fill" :color="sort === 5 ? '#38C148' : '#cccccc'" size="14"></u-icon>
  46. </view>
  47. </view>
  48. <view class="filter-item" :class="{ active: sort === 2 || sort === 3 }" @click="handleSort('price')">
  49. <text>价格</text>
  50. <view class="sort-icons">
  51. <u-icon name="arrow-up-fill" :color="sort === 2 ? '#38C148' : '#cccccc'" size="14"></u-icon>
  52. <u-icon name="arrow-down-fill" :color="sort === 3 ? '#38C148' : '#cccccc'" size="14"></u-icon>
  53. </view>
  54. </view>
  55. <view class="filter-item" @click="showFilter = true">
  56. <text>筛选</text>
  57. <image src="/pages-sell/static/search-result/icon-filter.png" class="icon-filter"></image>
  58. </view>
  59. </view>
  60. <!-- Book List -->
  61. <view class="book-list">
  62. <RecommendItem v-for="(item, index) in bookList" :key="index" :item="item" @add-cart="addToCart"
  63. @update-item="updateBookItem($event, index)" :show-desc="false">
  64. </RecommendItem>
  65. <u-loadmore :status="loadStatus" margin-top="30" margin-bottom="30"></u-loadmore>
  66. </view>
  67. </view>
  68. <!-- Filter Popup -->
  69. <u-popup v-model="showFilter" mode="bottom">
  70. <view class="filter-popup">
  71. <view class="filter-content">
  72. <view class="filter-title">价格区间</view>
  73. <view class="price-inputs">
  74. <u-input v-model="tempMinPrice" type="number" placeholder="最低价" border="surround"></u-input>
  75. <view class="divider">-</view>
  76. <u-input v-model="tempMaxPrice" type="number" placeholder="最高价" border="surround"></u-input>
  77. </view>
  78. </view>
  79. <view class="filter-footer">
  80. <view class="btn reset" @click="resetFilter">重置</view>
  81. <view class="btn confirm" @click="confirmFilter">确定</view>
  82. </view>
  83. </view>
  84. </u-popup>
  85. <!-- Red Packet Float -->
  86. <!-- <view class="red-packet-float" @click="openPacket">
  87. <view class="time-badge">
  88. <text>剩</text>
  89. <u-count-down :timestamp="timestamp" separator="colon" color="#DF1407" bg-color="transparent"
  90. font-size="18" separator-size="18" separator-color="#DF1407"></u-count-down>
  91. </view>
  92. <image src="/pages-sell/static/search/icon-red-packet.png" class="packet-img" mode="aspectFit"></image>
  93. </view> -->
  94. <!-- Surprise Packet Dialog -->
  95. <PacketDialog ref="packetDialog"></PacketDialog>
  96. <select-good-popup-host />
  97. </view>
  98. </template>
  99. <script>
  100. import RecommendItem from '../components/recommend-item/index.vue';
  101. import HotRecommendItem from '../components/hot-recommend-item/index.vue';
  102. import Navbar from '@/components/navbar/navbar.vue';
  103. import PacketDialog from '@/pages-sell/components/packet-dialog/index.vue';
  104. import SelectGoodPopupHost from '@/components/select-good-popup-host.vue';
  105. export default {
  106. components: {
  107. RecommendItem,
  108. HotRecommendItem,
  109. Navbar,
  110. PacketDialog,
  111. SelectGoodPopupHost,
  112. },
  113. data() {
  114. return {
  115. keyword: '',
  116. pageNum: 1,
  117. pageSize: 10,
  118. hotBook: {},
  119. bookList: [],
  120. loadStatus: 'loadmore',
  121. navbarBackground: 'transparent',
  122. navbarTitleColor: '#ffffff',
  123. navbarIconColor: '#ffffff',
  124. sort: 1, // 1-综合 2-价格升序 3-价格降序 4-销量升序 5-销量降序
  125. showFilter: false,
  126. tempMinPrice: '',
  127. tempMaxPrice: '',
  128. minPrice: '',
  129. maxPrice: '',
  130. timestamp: 86400, // 24 hours countdown
  131. promptList: [],
  132. inputFocused: false,
  133. inputTimer: null,
  134. blurTimer: null,
  135. }
  136. },
  137. computed: {
  138. showPrompt() {
  139. return this.inputFocused && !!String(this.keyword || '').trim() && this.promptList.length > 0;
  140. }
  141. },
  142. watch: {
  143. keyword(val) {
  144. if (!this.inputFocused) return;
  145. const keyword = String(val || '').trim();
  146. if (!keyword) {
  147. this.promptList = [];
  148. return;
  149. }
  150. if (this.inputTimer) clearTimeout(this.inputTimer);
  151. this.inputTimer = setTimeout(() => {
  152. this.getPromptData(keyword);
  153. }, 300);
  154. }
  155. },
  156. onPullDownRefresh() {
  157. this.pageNum = 1;
  158. this.loadStatus = 'loading';
  159. this.loadData();
  160. },
  161. onReachBottom() {
  162. if (this.loadStatus === 'nomore') return;
  163. this.pageNum++;
  164. this.loadStatus = 'loading';
  165. this.loadData();
  166. },
  167. onPageScroll(e) {
  168. if (e.scrollTop > 50) {
  169. this.navbarBackground = '#ffffff';
  170. this.navbarTitleColor = '#000000';
  171. this.navbarIconColor = '#000000';
  172. } else {
  173. this.navbarBackground = 'transparent';
  174. this.navbarTitleColor = '#ffffff';
  175. this.navbarIconColor = '#ffffff';
  176. }
  177. },
  178. onLoad(options) {
  179. this.keyword = options.keyword ? decodeURIComponent(options.keyword) : '';
  180. this.loadData();
  181. },
  182. methods: {
  183. loadData() {
  184. const params = {
  185. keyword: this.keyword,
  186. pageNum: this.pageNum,
  187. pageSize: this.pageSize,
  188. sort: this.sort
  189. };
  190. if (this.minPrice) params.minPrice = this.minPrice;
  191. if (this.maxPrice) params.maxPrice = this.maxPrice;
  192. this.$u.api.getSearchKeywordAjax(params).then(res => {
  193. uni.stopPullDownRefresh();
  194. const rows = res.data.rows || [];
  195. if (this.pageNum === 1) {
  196. this.bookList = rows;
  197. } else {
  198. this.bookList = [...this.bookList, ...rows];
  199. }
  200. if (rows.length < this.pageSize) {
  201. this.loadStatus = 'nomore';
  202. } else {
  203. this.loadStatus = 'loadmore';
  204. }
  205. if (res.data.couponName) {
  206. this.openPacket({
  207. couponName: res.data.couponName,
  208. faceMoney: res.data.faceMoney,
  209. thresholdMoney: res.data.thresholdMoney,
  210. });
  211. }
  212. }).catch(() => {
  213. uni.stopPullDownRefresh();
  214. this.loadStatus = 'loadmore';
  215. });
  216. if (this.pageNum === 1) {
  217. this.$u.api.getSearchRecommendAjax(this.keyword).then(res => {
  218. if (res.code == 200) {
  219. res.data.discountDesc = res.data.productPrice ? ((res.data.productPrice / res.data.price) * 10).toFixed(2) + '折' : '';
  220. res.data.discountPrice = res.data.productPrice ? (res.data.price - res.data.productPrice).toFixed(2) : '';
  221. this.hotBook = res.data;
  222. }
  223. });
  224. }
  225. },
  226. handleSort(type) {
  227. if (type === 1) {
  228. if (this.sort === 1) return;
  229. this.sort = 1;
  230. } else if (type === 'price') {
  231. if (this.sort === 2) {
  232. this.sort = 3;
  233. } else {
  234. this.sort = 2;
  235. }
  236. } else if (type === 'sales') {
  237. if (this.sort === 5) {
  238. this.sort = 4;
  239. } else {
  240. this.sort = 5;
  241. }
  242. }
  243. this.pageNum = 1;
  244. this.loadStatus = 'loading';
  245. this.loadData();
  246. },
  247. resetFilter() {
  248. this.tempMinPrice = '';
  249. this.tempMaxPrice = '';
  250. },
  251. confirmFilter() {
  252. this.minPrice = this.tempMinPrice;
  253. this.maxPrice = this.tempMaxPrice;
  254. this.showFilter = false;
  255. this.pageNum = 1;
  256. this.loadStatus = 'loading';
  257. this.loadData();
  258. },
  259. goBack() {
  260. uni.navigateBack();
  261. },
  262. toFeedback() {
  263. uni.navigateTo({
  264. url: '/pages-sell/pages/feedback'
  265. });
  266. },
  267. onSearch(val) {
  268. const key = String(val || this.keyword || '').replace(/<[^>]+>/g, '').trim();
  269. if (!key) return;
  270. this.keyword = key;
  271. this.promptList = [];
  272. this.inputFocused = false;
  273. this.pageNum = 1;
  274. this.loadData();
  275. },
  276. onSearchFocus() {
  277. if (this.blurTimer) clearTimeout(this.blurTimer);
  278. this.inputFocused = true;
  279. const keyword = String(this.keyword || '').trim();
  280. if (keyword) this.getPromptData(keyword);
  281. },
  282. onSearchBlur() {
  283. // 延迟收起,避免点击联想词时先失焦导致点不到
  284. this.blurTimer = setTimeout(() => {
  285. this.inputFocused = false;
  286. }, 300);
  287. },
  288. getPromptData(keyword) {
  289. this.$u.api.getSearchPromptAjax({ keyword }).then(res => {
  290. if (res.code == 200 && this.inputFocused && String(this.keyword || '').trim() === keyword) {
  291. this.promptList = res.data || [];
  292. }
  293. })
  294. },
  295. highlightKeyword(text, keyword) {
  296. if (!text) return '';
  297. if (text.includes('<em>')) {
  298. return text
  299. .replace(/<em>/g, '<span style="color: #37C148; font-style: normal;">')
  300. .replace(/<\/em>/g, '</span>');
  301. }
  302. if (!keyword) return text;
  303. const escapedKeyword = String(keyword).replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
  304. const reg = new RegExp('(' + escapedKeyword + ')', 'gi');
  305. return text.replace(reg, '<span style="color: #37C148; font-style: normal;">$1</span>');
  306. },
  307. selectPrompt(item) {
  308. if (this.blurTimer) clearTimeout(this.blurTimer);
  309. this.onSearch(item);
  310. },
  311. fillKeyword(key) {
  312. if (!key) return;
  313. if (this.blurTimer) clearTimeout(this.blurTimer);
  314. this.inputFocused = true;
  315. this.keyword = String(key).replace(/<[^>]+>/g, '');
  316. },
  317. addToCart(data) {
  318. // data now contains { product, quality, quantity } from the popup
  319. console.log('Added to cart:', data);
  320. uni.showToast({
  321. title: '已加入购物车',
  322. icon: 'success',
  323. duration: 3000
  324. });
  325. },
  326. updateBookItem(updatedItem, index) {
  327. this.$set(this.bookList, index, { ...this.bookList[index], ...updatedItem });
  328. },
  329. updateHotBook(updatedItem) {
  330. this.hotBook = { ...this.hotBook, ...updatedItem };
  331. },
  332. openPacket(data) {
  333. this.$refs.packetDialog.open(data);
  334. },
  335. closePacket() {
  336. this.$refs.packetDialog.close();
  337. }
  338. },
  339. onShareAppMessage(res) {
  340. if (res.from === "button" && res.target && res.target.dataset && res.target.dataset.shareType === "reduce") {
  341. let reduceCode = uni.getStorageSync("reduceCodeShare");
  342. if (this.$u && this.$u.api && this.$u.api.goToReduceShareAjax) {
  343. this.$u.api.goToReduceShareAjax({ reduceCode: reduceCode });
  344. } else {
  345. uni.$u && uni.$u.http && uni.$u.http.get('/token/shop/order/goToShare', { reduceCode: reduceCode });
  346. }
  347. return {
  348. title: "快来帮我减钱买书吧!",
  349. path: "/pages/sell/index?reduceCode=" + reduceCode,
  350. imageUrl: "https://shuhi.oss-cn-qingdao.aliyuncs.com/mini/share.jpg",
  351. desc: "书嗨,专注于书籍交易的平台,提供新书和二手书的买卖服务",
  352. };
  353. }
  354. return {
  355. title: "书嗨",
  356. path: "/pages-sell/pages/search-result?keyword=" + (this.keyword || ""),
  357. };
  358. }
  359. }
  360. </script>
  361. <style lang="scss" scoped>
  362. .search-result-page {
  363. min-height: 100vh;
  364. background-color: #F6F6F6;
  365. font-family: 'Source Han Sans SC', sans-serif;
  366. position: relative;
  367. }
  368. /* 顶部大背景图 */
  369. .top-bg-image {
  370. position: fixed;
  371. top: 0;
  372. left: 0;
  373. width: 100%;
  374. z-index: 0;
  375. display: block;
  376. }
  377. .search-bar-wrapper {
  378. position: relative;
  379. z-index: 20;
  380. display: flex;
  381. align-items: center;
  382. padding: 0 30rpx;
  383. height: 88rpx;
  384. width: 100%;
  385. }
  386. .prompt-panel {
  387. position: absolute;
  388. left: 0;
  389. right: 0;
  390. top: 80rpx;
  391. background: #fff;
  392. z-index: 30;
  393. }
  394. .prompt-item {
  395. display: flex;
  396. align-items: center;
  397. justify-content: space-between;
  398. padding: 30rpx 24rpx;
  399. border-bottom: 1rpx solid #F5F5F5;
  400. .prompt-text {
  401. font-size: 28rpx;
  402. color: #333;
  403. flex: 1;
  404. }
  405. .arrow-wrap {
  406. padding: 10rpx 0 10rpx 20rpx;
  407. }
  408. }
  409. .banner-section {
  410. position: relative;
  411. width: 100%;
  412. z-index: 1;
  413. padding: 10rpx 30rpx;
  414. .top-banner {
  415. width: 100%;
  416. display: block;
  417. }
  418. }
  419. .content-area {
  420. position: relative;
  421. z-index: 2;
  422. padding: 30rpx 30rpx;
  423. border-radius: 30rpx 30rpx 0 0;
  424. min-height: 500rpx;
  425. .section-block {
  426. margin-bottom: 30rpx;
  427. background-color: #fff;
  428. padding: 30rpx 24rpx;
  429. border-radius: 20rpx;
  430. .section-title {
  431. font-size: 32rpx;
  432. font-weight: bold;
  433. color: #333;
  434. margin-bottom: 20rpx;
  435. display: block;
  436. }
  437. }
  438. }
  439. .filter-bar {
  440. display: flex;
  441. justify-content: space-between;
  442. align-items: center;
  443. margin-bottom: 20rpx;
  444. .filter-item {
  445. display: flex;
  446. align-items: center;
  447. font-size: 28rpx;
  448. color: #666;
  449. &.active {
  450. color: #333;
  451. font-weight: bold;
  452. .triangle-icon {
  453. border-top-color: #333;
  454. }
  455. }
  456. text {
  457. margin-right: 6rpx;
  458. }
  459. .triangle-icon {
  460. width: 0;
  461. height: 0;
  462. border-left: 8rpx solid transparent;
  463. border-right: 8rpx solid transparent;
  464. border-top: 10rpx solid #666;
  465. }
  466. .sort-icons {
  467. display: flex;
  468. flex-direction: column;
  469. margin-left: 4rpx;
  470. justify-content: center;
  471. gap: 4rpx;
  472. u-icon {
  473. margin: -2rpx 0;
  474. }
  475. }
  476. .icon-filter {
  477. width: 24rpx;
  478. height: 24rpx;
  479. margin-left: 4rpx;
  480. }
  481. }
  482. }
  483. .book-list {
  484. background: #fff;
  485. border-radius: 20rpx;
  486. padding: 0 24rpx;
  487. padding-bottom: 40rpx;
  488. }
  489. .filter-popup {
  490. background-color: #fff;
  491. height: 100%;
  492. display: flex;
  493. flex-direction: column;
  494. .filter-content {
  495. flex: 1;
  496. padding: 30rpx;
  497. .filter-title {
  498. font-size: 30rpx;
  499. font-weight: bold;
  500. color: #333;
  501. margin-bottom: 30rpx;
  502. }
  503. .price-inputs {
  504. display: flex;
  505. align-items: center;
  506. justify-content: space-between;
  507. u-input {
  508. flex: 1;
  509. }
  510. .divider {
  511. margin: 0 20rpx;
  512. color: #999;
  513. }
  514. }
  515. }
  516. .filter-footer {
  517. display: flex;
  518. height: 100rpx;
  519. .btn {
  520. flex: 1;
  521. display: flex;
  522. align-items: center;
  523. justify-content: center;
  524. font-size: 32rpx;
  525. &.reset {
  526. background-color: #fff;
  527. color: #666;
  528. border-top: 1rpx solid #eee;
  529. }
  530. &.confirm {
  531. background-color: #38C148;
  532. color: #fff;
  533. }
  534. }
  535. }
  536. }
  537. .red-packet-float {
  538. position: fixed;
  539. bottom: 200rpx;
  540. right: 40rpx;
  541. z-index: 100;
  542. display: flex;
  543. flex-direction: column;
  544. align-items: center;
  545. .time-badge {
  546. background: #FFE173;
  547. color: #DF1407;
  548. font-size: 18rpx;
  549. padding: 4rpx 10rpx;
  550. border-radius: 10rpx;
  551. position: absolute;
  552. top: -40rpx;
  553. right: -20rpx;
  554. white-space: nowrap;
  555. z-index: 2;
  556. display: flex;
  557. align-items: center;
  558. text {
  559. margin-right: 4rpx;
  560. }
  561. }
  562. .packet-img {
  563. width: 100rpx;
  564. height: 120rpx;
  565. }
  566. }
  567. </style>