index.vue 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. </view>
  42. </template>
  43. <script>
  44. import { eventBus } from '@/utils/event-bus'
  45. export default {
  46. name: 'RecommendItem',
  47. props: {
  48. item: {
  49. type: Object,
  50. required: true
  51. },
  52. showDesc: {
  53. type: Boolean,
  54. default: true
  55. },
  56. // 1-搜索页 2-书单 3-商品详情,由使用方显式传入
  57. sourceFrom: {
  58. type: Number,
  59. default: 0
  60. },
  61. searchWord: {
  62. type: String,
  63. default: ''
  64. }
  65. },
  66. computed: {
  67. highlightedName() {
  68. return this.parseEmTag(this.item.name || this.item.title || '');
  69. },
  70. highlightedAuthor() {
  71. return this.parseEmTag(this.item.author || '');
  72. },
  73. highlightedDesc() {
  74. return this.parseEmTag(this.item.description || this.item.desc || '暂无简介');
  75. }
  76. },
  77. methods: {
  78. parseEmTag(text) {
  79. if (!text) return '';
  80. return text.replace(/<em>/g, '<span style="color: #38C148">').replace(/<\/em>/g, '</span>');
  81. },
  82. handleAction() {
  83. if (this.item.availableStock === 0) {
  84. this.handleNotify();
  85. } else {
  86. this.handleAddToCart();
  87. }
  88. },
  89. handleNotify() {
  90. const isCancel = this.item.hasArrivalNotice === 1;
  91. const apiUrl = isCancel ? '/token/shop/user/noticeArrivalCancel' : '/token/shop/user/noticeArrival';
  92. uni.$u.http.post(apiUrl, {
  93. isbn: this.item.isbn,
  94. }).then(res => {
  95. if (res.code === 200) {
  96. const newValue = isCancel ? 0 : 1;
  97. // Notify parent component to update the item
  98. this.$emit('update-item', { ...this.item, hasArrivalNotice: newValue });
  99. // Try updating local object properties directly (Vue reactivity)
  100. this.$set(this.item, 'hasArrivalNotice', newValue);
  101. this.item.hasArrivalNotice = newValue;
  102. this.$u.toast(isCancel ? '已取消到货通知' : '到货通知设置成功');
  103. }
  104. });
  105. },
  106. handleAddToCart() {
  107. const payload = {
  108. product: this.item,
  109. sourceFrom: this.sourceFrom,
  110. };
  111. if (this.searchWord) payload.searchWord = this.searchWord;
  112. eventBus.emit('openSelectGoodPopup', payload);
  113. },
  114. // 仅搜索来源把来源和搜索词带到详情,详情页加购/收藏才能记成搜索页
  115. handleClick() {
  116. let url = '/pages-sell/pages/detail?isbn=' + this.item.isbn;
  117. if (Number(this.sourceFrom) === 1) {
  118. url += '&sourceFrom=1';
  119. if (this.searchWord) {
  120. url += '&searchWord=' + encodeURIComponent(this.searchWord);
  121. }
  122. }
  123. uni.navigateTo({ url });
  124. }
  125. }
  126. }
  127. </script>
  128. <style lang="scss" scoped>
  129. .recommend-item {
  130. padding: 30rpx 0;
  131. border-bottom: 1rpx dashed #EEEEEE;
  132. .main-info {
  133. display: flex;
  134. margin-bottom: 30rpx;
  135. .image-wrapper {
  136. position: relative;
  137. width: 172rpx;
  138. height: 220rpx;
  139. border-radius: 8rpx;
  140. margin-right: 24rpx;
  141. flex-shrink: 0;
  142. overflow: hidden;
  143. .book-cover {
  144. width: 100%;
  145. height: 100%;
  146. }
  147. .status-mask {
  148. position: absolute;
  149. bottom: 0;
  150. left: 0;
  151. width: 100%;
  152. height: 40rpx;
  153. background-color: rgba(0, 0, 0, 0.6);
  154. display: flex;
  155. justify-content: center;
  156. align-items: center;
  157. text {
  158. color: #fff;
  159. font-size: 20rpx;
  160. }
  161. }
  162. }
  163. .info-right {
  164. flex: 1;
  165. display: flex;
  166. flex-direction: column;
  167. justify-content: space-between;
  168. padding: 4rpx 0;
  169. .title-author {
  170. .title {
  171. font-size: 32rpx;
  172. font-weight: bold;
  173. color: #333;
  174. margin-bottom: 12rpx;
  175. display: block;
  176. line-height: 1.4;
  177. }
  178. .author {
  179. font-size: 24rpx;
  180. color: #999;
  181. }
  182. }
  183. .price-action {
  184. display: flex;
  185. justify-content: space-between;
  186. align-items: flex-end;
  187. .price-box {
  188. .currency {
  189. font-size: 24rpx;
  190. color: #FF4B4B;
  191. font-weight: bold;
  192. }
  193. .price {
  194. font-size: 36rpx;
  195. color: #FF4B4B;
  196. font-weight: bold;
  197. margin-right: 12rpx;
  198. }
  199. .original {
  200. font-size: 24rpx;
  201. color: #999;
  202. text-decoration: line-through;
  203. }
  204. }
  205. .cart-btn {
  206. background: #38C248;
  207. border-radius: 30rpx;
  208. height: 60rpx;
  209. line-height: 60rpx;
  210. padding: 0 24rpx;
  211. &.gray-btn {
  212. background: #cccccc;
  213. }
  214. text {
  215. font-size: 26rpx;
  216. color: #fff;
  217. }
  218. }
  219. }
  220. }
  221. }
  222. .desc-section {
  223. .desc-header {
  224. position: relative;
  225. display: inline-block;
  226. margin-bottom: 16rpx;
  227. .label {
  228. font-size: 30rpx;
  229. font-weight: bold;
  230. color: #333;
  231. position: relative;
  232. z-index: 1;
  233. }
  234. .indicator {
  235. position: absolute;
  236. bottom: 4rpx;
  237. right: 4rpx;
  238. width: 30rpx;
  239. height: 8rpx;
  240. background: #4ED964;
  241. border-radius: 4rpx;
  242. z-index: 0;
  243. }
  244. }
  245. .desc-content {
  246. font-family: Source Han Sans SC;
  247. font-size: 26rpx;
  248. color: #8D8D8D;
  249. line-height: 1.6;
  250. display: block;
  251. text-align: justify;
  252. }
  253. }
  254. }
  255. </style>