search-result.vue 20 KB

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