index.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. <template>
  2. <view class="recommend-item" @click.stop="handleClick">
  3. <!-- Main Info Row -->
  4. <view class="main-info" :style="{ 'margin-bottom': showDesc ? '30rpx' : '0' }">
  5. <view class="image-wrapper">
  6. <image :src="item.cover" class="book-cover" mode="aspectFill"></image>
  7. <!-- 状态遮罩 -->
  8. <view class="status-mask" v-if="item.availableStock === 0">
  9. <text>暂无库存</text>
  10. </view>
  11. </view>
  12. <view class="info-right">
  13. <view class="title-author">
  14. <view class="title-row">
  15. <rich-text class="title" :nodes="highlightedName"></rich-text>
  16. </view>
  17. <rich-text class="author" :nodes="highlightedAuthor"></rich-text>
  18. </view>
  19. <view class="price-action">
  20. <view class="price-box">
  21. <text class="currency">¥</text>
  22. <text class="price">{{ item.productPrice || item.price }}</text>
  23. <text class="original">¥{{ item.originalPrice || item.price }}</text>
  24. </view>
  25. <view class="cart-btn" :class="{ 'gray-btn': item.availableStock === 0 && item.hasArrivalNotice === 1 }" @click.stop="handleAction">
  26. <text v-if="item.availableStock === 0 && item.hasArrivalNotice === 1">取消到货通知</text>
  27. <text v-else-if="item.availableStock === 0">到货通知</text>
  28. <text v-else>加入购物车</text>
  29. </view>
  30. </view>
  31. </view>
  32. </view>
  33. <!-- Description Row -->
  34. <view class="desc-section" v-if="showDesc">
  35. <view class="desc-header">
  36. <text class="label">简介</text>
  37. <view class="indicator"></view>
  38. </view>
  39. <rich-text class="desc-content" :nodes="highlightedDesc"></rich-text>
  40. </view>
  41. <!-- Product Selection Popup -->
  42. <SelectGoodPopup ref="popup" @confirm="onPopupConfirm"></SelectGoodPopup>
  43. </view>
  44. </template>
  45. <script>
  46. import SelectGoodPopup from '../select-good-popup/index.vue';
  47. export default {
  48. name: 'RecommendItem',
  49. components: {
  50. SelectGoodPopup
  51. },
  52. props: {
  53. item: {
  54. type: Object,
  55. required: true
  56. },
  57. showDesc: {
  58. type: Boolean,
  59. default: true
  60. }
  61. },
  62. computed: {
  63. highlightedName() {
  64. return this.parseEmTag(this.item.name || this.item.title || '');
  65. },
  66. highlightedAuthor() {
  67. return this.parseEmTag(this.item.author || '');
  68. },
  69. highlightedDesc() {
  70. return this.parseEmTag(this.item.description || this.item.desc || '暂无简介');
  71. }
  72. },
  73. methods: {
  74. parseEmTag(text) {
  75. if (!text) return '';
  76. return text.replace(/<em>/g, '<span style="color: #38C148">').replace(/<\/em>/g, '</span>');
  77. },
  78. handleAction() {
  79. if (this.item.availableStock === 0) {
  80. this.handleNotify();
  81. } else {
  82. this.handleAddToCart();
  83. }
  84. },
  85. handleNotify() {
  86. const isCancel = this.item.hasArrivalNotice === 1;
  87. const apiUrl = isCancel ? '/token/shop/user/noticeArrivalCancel' : '/token/shop/user/noticeArrival';
  88. uni.$u.http.post(apiUrl, {
  89. isbn: this.item.isbn,
  90. }).then(res => {
  91. if (res.code === 200) {
  92. const newValue = isCancel ? 0 : 1;
  93. // Notify parent component to update the item
  94. this.$emit('update-item', { ...this.item, hasArrivalNotice: newValue });
  95. // Try updating local object properties directly (Vue reactivity)
  96. this.$set(this.item, 'hasArrivalNotice', newValue);
  97. this.item.hasArrivalNotice = newValue;
  98. this.$u.toast(isCancel ? '已取消到货通知' : '到货通知设置成功');
  99. }
  100. });
  101. },
  102. handleAddToCart() {
  103. // Open the popup using ref
  104. // 加入购物车时,传递 sourceFrom 参数为 2
  105. this.$refs.popup.open(this.item, this.showDesc ? 2 : 1);
  106. },
  107. onPopupConfirm(data) {
  108. // 不再触发事件,避免重复调用接口
  109. // this.$emit('add-cart', data);
  110. },
  111. //图书详情页
  112. handleClick() {
  113. uni.navigateTo({
  114. url: '/pages-sell/pages/detail?isbn=' + this.item.isbn
  115. });
  116. }
  117. }
  118. }
  119. </script>
  120. <style lang="scss" scoped>
  121. .recommend-item {
  122. padding: 30rpx 0;
  123. border-bottom: 1rpx dashed #EEEEEE;
  124. .main-info {
  125. display: flex;
  126. margin-bottom: 30rpx;
  127. .image-wrapper {
  128. position: relative;
  129. width: 172rpx;
  130. height: 220rpx;
  131. border-radius: 8rpx;
  132. margin-right: 24rpx;
  133. flex-shrink: 0;
  134. overflow: hidden;
  135. .book-cover {
  136. width: 100%;
  137. height: 100%;
  138. }
  139. .status-mask {
  140. position: absolute;
  141. bottom: 0;
  142. left: 0;
  143. width: 100%;
  144. height: 40rpx;
  145. background-color: rgba(0, 0, 0, 0.6);
  146. display: flex;
  147. justify-content: center;
  148. align-items: center;
  149. text {
  150. color: #fff;
  151. font-size: 20rpx;
  152. }
  153. }
  154. }
  155. .info-right {
  156. flex: 1;
  157. display: flex;
  158. flex-direction: column;
  159. justify-content: space-between;
  160. padding: 4rpx 0;
  161. .title-author {
  162. .title {
  163. font-size: 32rpx;
  164. font-weight: bold;
  165. color: #333;
  166. margin-bottom: 12rpx;
  167. display: block;
  168. line-height: 1.4;
  169. }
  170. .author {
  171. font-size: 24rpx;
  172. color: #999;
  173. }
  174. }
  175. .price-action {
  176. display: flex;
  177. justify-content: space-between;
  178. align-items: flex-end;
  179. .price-box {
  180. .currency {
  181. font-size: 24rpx;
  182. color: #FF4B4B;
  183. font-weight: bold;
  184. }
  185. .price {
  186. font-size: 36rpx;
  187. color: #FF4B4B;
  188. font-weight: bold;
  189. margin-right: 12rpx;
  190. }
  191. .original {
  192. font-size: 24rpx;
  193. color: #999;
  194. text-decoration: line-through;
  195. }
  196. }
  197. .cart-btn {
  198. background: #38C248;
  199. border-radius: 30rpx;
  200. height: 60rpx;
  201. line-height: 60rpx;
  202. padding: 0 24rpx;
  203. &.gray-btn {
  204. background: #cccccc;
  205. }
  206. text {
  207. font-size: 26rpx;
  208. color: #fff;
  209. }
  210. }
  211. }
  212. }
  213. }
  214. .desc-section {
  215. .desc-header {
  216. position: relative;
  217. display: inline-block;
  218. margin-bottom: 16rpx;
  219. .label {
  220. font-size: 30rpx;
  221. font-weight: bold;
  222. color: #333;
  223. position: relative;
  224. z-index: 1;
  225. }
  226. .indicator {
  227. position: absolute;
  228. bottom: 4rpx;
  229. right: 4rpx;
  230. width: 30rpx;
  231. height: 8rpx;
  232. background: #4ED964;
  233. border-radius: 4rpx;
  234. z-index: 0;
  235. }
  236. }
  237. .desc-content {
  238. font-family: Source Han Sans SC;
  239. font-size: 26rpx;
  240. color: #8D8D8D;
  241. line-height: 1.6;
  242. display: block;
  243. text-align: justify;
  244. }
  245. }
  246. }
  247. </style>