index.vue 5.9 KB

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