detail.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. <template>
  2. <view class="detail-page">
  3. <!-- Top Background -->
  4. <view class="header-bg"></view>
  5. <!-- Custom Navbar -->
  6. <Navbar :title="product.bookName || '详情'" :titleSize="32" :title-color="navbarTitleColor"
  7. :back-icon-color="navbarIconColor" :background="navbarBackground">
  8. </Navbar>
  9. <!-- Notification Bar -->
  10. <!-- <view class="notification-bar">
  11. <u-avatar size="40" src="https://img.yzcdn.cn/vant/cat.jpeg"></u-avatar>
  12. <text class="notif-text">微 ** 用户 购买了 《苏菲的世界》</text>
  13. </view> -->
  14. <view class="content-scroll">
  15. <!-- Book Cover Area -->
  16. <view class="cover-area">
  17. <image class="book-cover" :src="product.cover" mode="heightFix"></image>
  18. <view class="share-btn" @click="openSharePopup">
  19. <image src="/pages-sell/static/goods/icon-share.png" class="share-icon"></image>
  20. <text>分享</text>
  21. </view>
  22. </view>
  23. <!-- Product Info Card -->
  24. <InfoCard :product="product"></InfoCard>
  25. <!-- Banner -->
  26. <view class="banner-area">
  27. <image src="/pages-sell/static/top-banner.png" class="banner-img" mode="widthFix"></image>
  28. </view>
  29. <!-- Service Info -->
  30. <ServiceCard @click="showReduceRulePopup" :bookInfo="product"></ServiceCard>
  31. <!-- Customer Service Float -->
  32. <FloatingDrag :width="126" :height="140" :initial-position="servicePosition"
  33. @position-change="handlePositionChange"
  34. @drag-start="handleDragStart"
  35. @drag-end="handleDragEnd">
  36. <!-- #ifdef MP-ALIPAY -->
  37. <button class="service-btn" @click="navigateToCustomerService">
  38. <image src="/static/img/kf.png" class="cs-icon" mode="aspectFit"></image>
  39. </button>
  40. <!-- #endif -->
  41. <!-- #ifndef MP-ALIPAY -->
  42. <button class="service-btn" open-type="contact">
  43. <image src="/static/img/kf.png" class="cs-icon" mode="aspectFit"></image>
  44. </button>
  45. <!-- #endif -->
  46. </FloatingDrag>
  47. <!-- Tabs -->
  48. <view class="tabs-header">
  49. <view class="tab-item" :class="{ active: currentTab === 0 }" @click="switchTab(0)">
  50. <text>商品详情</text>
  51. <view class="indicator" v-if="currentTab === 0"></view>
  52. </view>
  53. <view class="tab-item" :class="{ active: currentTab === 1 }" @click="switchTab(1)">
  54. <text>相关推荐</text>
  55. <view class="indicator" v-if="currentTab === 1"></view>
  56. </view>
  57. </view>
  58. <!-- Product Detail Content -->
  59. <ProductContent :currentTab="currentTab" :product="product" :tipsContent="tipsContent"
  60. :relatedBooksList="relatedBooksList" @bookClick="onBookClick">
  61. </ProductContent>
  62. <u-gap height="220"></u-gap>
  63. </view>
  64. <!-- Footer -->
  65. <FooterBar :hasStock="hasStock" :isCollect="product.isCollect" :hasArrivalNotice="product.hasArrivalNotice"
  66. @addCart="openSelectPopup" @notify="handleNotify" @collect="handleCollect"></FooterBar>
  67. <!-- Select Popup -->
  68. <SelectGoodPopup ref="selectPopup" @confirm="onPopupConfirm" @notice-change="onNoticeChange"></SelectGoodPopup>
  69. <!-- Share Popup -->
  70. <SharePopup ref="sharePopup" :product="product"></SharePopup>
  71. <!-- Reduce Rule Popup -->
  72. <ReduceRulePopup ref="reduceRulePopup"></ReduceRulePopup>
  73. </view>
  74. </template>
  75. <script>
  76. import Navbar from '@/components/navbar/navbar.vue'
  77. import SelectGoodPopup from '../components/select-good-popup/index.vue'
  78. import InfoCard from '../components/detail/info-card.vue'
  79. import ServiceCard from '../components/detail/service-card.vue'
  80. import ProductContent from '../components/detail/product-content.vue'
  81. import FooterBar from '../components/detail/footer-bar.vue'
  82. import FloatingDrag from "@/components/floating-drag.vue";
  83. import SharePopup from '../components/detail/share-popup.vue';
  84. import ReduceRulePopup from '../components/reduce-rule-popup/index.vue';
  85. export default {
  86. components: {
  87. Navbar,
  88. SelectGoodPopup,
  89. InfoCard,
  90. ServiceCard,
  91. ProductContent,
  92. FooterBar,
  93. FloatingDrag,
  94. SharePopup,
  95. ReduceRulePopup
  96. },
  97. data() {
  98. return {
  99. currentTab: 0,
  100. hasStock: false, // Toggle this to test stock status
  101. navbarBackground: 'transparent',
  102. navbarTitleColor: '#ffffff',
  103. navbarIconColor: '#ffffff',
  104. isDragging: false, // 是否正在拖动悬浮按钮
  105. tipsContent: [
  106. '印刷版次较多,二手图书封面、版次、原价等信息可能与商品介绍有差异,具体以收到实物为准;',
  107. '二手图书性质特殊,不保证随新书赠送的光盘、海报、卡片等内容,仅支持图书质量问题退款,否则将扣除运费成本;',
  108. '收到的图书如有质量问题,请于七天内联系客服处理,超出售后时效,不予处理。'
  109. ],
  110. relatedBooksList: [],
  111. product: {},
  112. servicePosition: {
  113. left: "auto",
  114. right: 0,
  115. bottom: "300rpx",
  116. },
  117. }
  118. },
  119. onLoad(options) {
  120. let isbn = options.isbn || options.id; // Support both just in case
  121. // 兼容扫码进入场景,App.vue 可能会把 isbn 存入全局,或者当前页的 scene 里带了 isbn
  122. if (!isbn && options.scene) {
  123. const decodeScene = decodeURIComponent(options.scene);
  124. const paramPairs = decodeScene.split('&');
  125. paramPairs.forEach((pair) => {
  126. const [key, value] = pair.split('=');
  127. if (key === 'isbn') {
  128. isbn = value;
  129. }
  130. });
  131. }
  132. // 兼容从 App.vue 缓存中获取 (因为如果是扫小程序码直接进入这个页面,App.vue的onLaunch会先执行并存入缓存,然后执行页面的onLoad)
  133. if (!isbn) {
  134. isbn = uni.getStorageSync('scene_isbn');
  135. }
  136. if (isbn) {
  137. // 使用完后清除缓存,防止影响后续正常跳转
  138. uni.removeStorageSync('scene_isbn');
  139. this.getBookDetail(isbn);
  140. } else {
  141. uni.showToast({
  142. title: '参数错误',
  143. icon: 'none'
  144. });
  145. setTimeout(() => {
  146. uni.navigateBack();
  147. }, 1500);
  148. }
  149. },
  150. onPageScroll(e) {
  151. if (this.isDragging) return; // 拖动悬浮按钮时禁止滚动
  152. if (e.scrollTop > 0) {
  153. this.navbarBackground = '#ffffff';
  154. this.navbarTitleColor = '#000000';
  155. this.navbarIconColor = '#000000';
  156. } else {
  157. this.navbarBackground = 'transparent';
  158. this.navbarTitleColor = '#ffffff';
  159. this.navbarIconColor = '#ffffff';
  160. }
  161. },
  162. methods: {
  163. getBookDetail(isbn) {
  164. uni.showLoading({ title: '加载中' });
  165. this.$u.api.getBookDetailAjax({ isbn }).then(res => {
  166. uni.hideLoading();
  167. if (res.code === 200) {
  168. this.product = res.data;
  169. this.hasStock = res.data.skuList.some(sku => sku.stockNum > 0);
  170. if (res.data.recommendList) {
  171. this.relatedBooksList = res.data.recommendList;
  172. }
  173. } else {
  174. uni.showToast({
  175. title: res.msg || '获取详情失败',
  176. icon: 'none'
  177. });
  178. }
  179. }).catch(() => {
  180. uni.hideLoading();
  181. });
  182. },
  183. switchTab(index) {
  184. this.currentTab = index;
  185. },
  186. openSelectPopup(sourceFrom) {
  187. this.$refs.selectPopup.open(this.product, sourceFrom);
  188. },
  189. onNoticeChange(val) {
  190. this.$set(this.product, 'hasArrivalNotice', val);
  191. },
  192. handleNotify() {
  193. const isCancel = this.product.hasArrivalNotice === 1;
  194. const apiUrl = isCancel ? '/token/shop/user/noticeArrivalCancel' : '/token/shop/user/noticeArrival';
  195. uni.showLoading({ mask: true });
  196. uni.$u.http.post(apiUrl, { isbn: this.product.isbn }).then(res => {
  197. uni.hideLoading();
  198. if (res.code === 200) {
  199. this.$set(this.product, 'hasArrivalNotice', isCancel ? 0 : 1);
  200. uni.showToast({
  201. title: isCancel ? '已取消到货通知' : '已订阅到货通知',
  202. icon: 'success'
  203. });
  204. } else {
  205. uni.showToast({
  206. title: res.msg || '操作失败',
  207. icon: 'none'
  208. });
  209. }
  210. }).catch(() => {
  211. uni.hideLoading();
  212. });
  213. },
  214. async onPopupConfirm(data) {
  215. console.log('Added to cart:', data);
  216. await this.$updateCartBadge();
  217. uni.showToast({
  218. title: '已加入购物车',
  219. icon: 'success',
  220. duration: 3000
  221. });
  222. },
  223. handleCollect() {
  224. if (!this.product || !this.product.isbn) {
  225. return this.$u.toast('商品信息不完整');
  226. }
  227. const isCollect = this.product.isCollect;
  228. const api = isCollect ? this.$u.api.removeCollectAjax : this.$u.api.addCollectAjax;
  229. const params = isCollect ? [this.product.isbn] : this.product.isbn;
  230. uni.showLoading({ mask: true });
  231. api(params).then(res => {
  232. uni.hideLoading();
  233. if (res.code === 200) {
  234. this.$set(this.product, 'isCollect', !isCollect);
  235. this.$u.toast(isCollect ? '已取消收藏' : '收藏成功');
  236. } else {
  237. this.$u.toast(res.msg);
  238. }
  239. }).catch(() => {
  240. uni.hideLoading();
  241. });
  242. },
  243. showReduceRulePopup() {
  244. this.$refs.reduceRulePopup.open();
  245. },
  246. onBookClick(book) {
  247. console.log('Book clicked:', book);
  248. uni.navigateTo({
  249. url: '/pages-sell/pages/detail?id=' + encodeURIComponent(book.title)
  250. });
  251. },
  252. openSharePopup() {
  253. this.$refs.sharePopup.open();
  254. },
  255. // 处理位置变更
  256. handlePositionChange(position) {
  257. this.servicePosition = position;
  258. },
  259. // 拖动开始
  260. handleDragStart() {
  261. this.isDragging = true;
  262. },
  263. // 拖动结束
  264. handleDragEnd() {
  265. this.isDragging = false;
  266. },
  267. //支付宝小程序的客服
  268. navigateToCustomerService() {
  269. uni.navigateTo({
  270. url: "/pages-mine/pages/customer-service",
  271. });
  272. },
  273. },
  274. onShareAppMessage(res) {
  275. if (res.from === "button" && res.target && res.target.dataset && res.target.dataset.shareType === "reduce") {
  276. let reduceCode = uni.getStorageSync("reduceCodeShare");
  277. // 调用分享接口
  278. if (this.$u && this.$u.api && this.$u.api.goToReduceShareAjax) {
  279. this.$u.api.goToReduceShareAjax({
  280. reduceCode: reduceCode
  281. });
  282. } else {
  283. uni.$u && uni.$u.http && uni.$u.http.get('/token/shop/order/goToShare', { reduceCode: reduceCode });
  284. }
  285. return {
  286. title: "快来帮我减钱买书吧!",
  287. path: "/pages/home/index?reduceCode=" + reduceCode,
  288. imageUrl: "https://shuhi.oss-cn-qingdao.aliyuncs.com/mini/share.jpg",
  289. desc: "书嗨,专注于书籍交易的平台,提供新书和二手书的买卖服务",
  290. };
  291. }
  292. return {
  293. title: `${this.product.bookName}`,
  294. path: `/pages-sell/pages/detail?isbn=${this.product.isbn}`,
  295. imageUrl: this.product.cover
  296. };
  297. }
  298. }
  299. </script>
  300. <style lang="scss" scoped>
  301. .detail-page {
  302. min-height: 100vh;
  303. background-color: #f5f5f5;
  304. position: relative;
  305. padding-bottom: 100rpx;
  306. }
  307. .header-bg {
  308. position: absolute;
  309. top: 0;
  310. left: 0;
  311. width: 100%;
  312. height: 664rpx;
  313. background: linear-gradient(0deg, #4ED868 0%, #D1FFE5 100%);
  314. z-index: 0;
  315. }
  316. .notification-bar {
  317. position: fixed;
  318. top: 180rpx;
  319. /* Adjust based on navbar height */
  320. left: 30rpx;
  321. z-index: 10;
  322. background: rgba(0, 0, 0, 0.3);
  323. border-radius: 30rpx;
  324. padding: 6rpx 20rpx 6rpx 6rpx;
  325. display: flex;
  326. align-items: center;
  327. .notif-text {
  328. font-size: 24rpx;
  329. color: #fff;
  330. margin-left: 10rpx;
  331. }
  332. }
  333. .content-scroll {
  334. position: relative;
  335. z-index: 1;
  336. height: 100vh;
  337. }
  338. .cover-area {
  339. position: relative;
  340. display: flex;
  341. justify-content: center;
  342. padding-top: 60rpx;
  343. padding-bottom: 40rpx;
  344. .book-cover {
  345. width: 360rpx;
  346. height: 360rpx;
  347. box-shadow: 0 10rpx 20rpx rgba(0, 0, 0, 0.1);
  348. }
  349. .share-btn {
  350. position: absolute;
  351. right: 50rpx;
  352. top: 50rpx;
  353. display: flex;
  354. flex-direction: column;
  355. align-items: center;
  356. z-index: 10; // Ensure it's clickable
  357. .share-icon {
  358. width: 40rpx;
  359. height: 40rpx;
  360. margin-bottom: 4rpx;
  361. }
  362. text {
  363. font-size: 20rpx;
  364. color: #333;
  365. }
  366. }
  367. }
  368. .banner-area {
  369. background: #f5f5f5;
  370. padding: 20rpx;
  371. .banner-img {
  372. width: 100%;
  373. border-radius: 20rpx;
  374. }
  375. }
  376. .service-btn {
  377. width: 100%;
  378. height: 100%;
  379. display: flex;
  380. justify-content: center;
  381. align-items: center;
  382. padding: 0;
  383. margin: 0;
  384. background-color: transparent;
  385. line-height: 1;
  386. border-radius: 0;
  387. &::after {
  388. border: none;
  389. }
  390. .cs-icon {
  391. width: 100rpx;
  392. height: 100rpx;
  393. }
  394. }
  395. .tabs-header {
  396. display: flex;
  397. justify-content: center;
  398. /* Center the tabs */
  399. background: #F8F8F8;
  400. padding: 20rpx 0;
  401. /* Remove horizontal padding as we center items */
  402. .tab-item {
  403. padding: 0 30rpx;
  404. /* Add internal padding for hit area */
  405. position: relative;
  406. text {
  407. font-family: 'Source Han Sans SC';
  408. font-size: 30rpx;
  409. /* Assuming 30px from design tool = 30rpx */
  410. color: #666666;
  411. transition: all 0.3s;
  412. }
  413. &.active {
  414. text {
  415. font-size: 30rpx;
  416. /* Keep consistent size or adjust if 'big' effect needed, user said 30px */
  417. font-weight: bold;
  418. color: #333333;
  419. }
  420. .indicator {
  421. position: absolute;
  422. bottom: 2rpx;
  423. /* Adjust vertical position */
  424. left: 50%;
  425. transform: translateX(-50%);
  426. width: 120rpx;
  427. /* Make it wider or relative to text? Usually fixed or text width. Let's try matching text width visually or a fixed width */
  428. height: 8rpx;
  429. /* Slightly thicker for gradient visibility */
  430. background: linear-gradient(90deg, rgba(78, 217, 100, 0.1) 0%, #4ED964 100%);
  431. border-radius: 4rpx;
  432. }
  433. }
  434. }
  435. }
  436. </style>