RelatedContent.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. <template>
  2. <view class="related-content">
  3. <!-- 首屏加载提示 -->
  4. <view v-if="!loaded && loading" class="related-loading-wrap">
  5. <text class="related-loading-text">加载中...</text>
  6. </view>
  7. <!-- 三源关联内容(任一分类无匹配整块隐藏) -->
  8. <block v-if="loaded">
  9. <!-- 相关文章 -->
  10. <view v-if="articles.length > 0" class="related-section">
  11. <view class="related-header">
  12. <text class="related-title">相关文章</text>
  13. <text class="related-subtitle">根据标签推荐</text>
  14. </view>
  15. <view class="ra-list">
  16. <view
  17. v-for="item in articles"
  18. :key="item.id"
  19. class="ra-item"
  20. hover-class="ra-item-pressed"
  21. @click="goArticle(item)"
  22. >
  23. <image class="ra-cover" :src="articleCover(item)" mode="aspectFill" lazy-load />
  24. <view class="ra-body">
  25. <text class="ra-title">{{ item.title }}</text>
  26. <text class="ra-summary" v-if="item.summary">{{ item.summary }}</text>
  27. </view>
  28. </view>
  29. </view>
  30. <view v-if="articlesLoading" class="related-loading-hint">加载中...</view>
  31. <view v-else-if="!articlesHasMore" class="related-loading-hint dim">没有更多了</view>
  32. </view>
  33. <!-- 相关活动 -->
  34. <view v-if="activities.length > 0" class="related-section">
  35. <view class="related-header">
  36. <text class="related-title">相关活动</text>
  37. <text class="related-subtitle">根据标签推荐</text>
  38. </view>
  39. <view class="ra-list">
  40. <view
  41. v-for="item in activities"
  42. :key="item.id"
  43. class="ra-item"
  44. hover-class="ra-item-pressed"
  45. @click="goActivity(item)"
  46. >
  47. <image class="ra-cover" :src="activityCover(item)" mode="aspectFill" lazy-load />
  48. <view class="ra-body">
  49. <text class="ra-title">{{ item.title }}</text>
  50. <text class="ra-summary" v-if="item.startTime">{{ formatActivityTime(item.startTime) }}</text>
  51. <view class="ra-bottom">
  52. <text class="ra-status" :class="'status-' + (item._cls || 'upcoming')">{{ item._txt || '待开始' }}</text>
  53. <text class="ra-price" v-if="item.priceLabel">{{ item.priceLabel }}</text>
  54. <text class="ra-price" v-else-if="item.price && item.price > 0">¥{{ formatPrice(item.price) }}</text>
  55. <text class="ra-price-free" v-else>免费</text>
  56. </view>
  57. </view>
  58. </view>
  59. </view>
  60. <view v-if="activitiesLoading" class="related-loading-hint">加载中...</view>
  61. <view v-else-if="!activitiesHasMore" class="related-loading-hint dim">没有更多了</view>
  62. </view>
  63. <!-- 相关商品(复用商城两列大卡片样式) -->
  64. <view v-if="products.length > 0" class="related-section">
  65. <view class="related-header">
  66. <text class="related-title">相关商品</text>
  67. <text class="related-subtitle">根据标签推荐</text>
  68. </view>
  69. <dimension-products :products="products" layout="mall" @productClick="goProduct" />
  70. <view v-if="productsLoading" class="related-loading-hint">加载中...</view>
  71. <view v-else-if="!productsHasMore" class="related-loading-hint dim">没有更多了</view>
  72. </view>
  73. </block>
  74. </view>
  75. </template>
  76. <script>
  77. import { getContentRelated } from '@/utils/api.js'
  78. import config from '@/config.js'
  79. import { parseDate } from '@/utils/format.js'
  80. import DimensionProducts from '@/components/DimensionProducts.vue'
  81. /**
  82. * 内容关联区块:以活动/文章/商品任意一类为源,按共享标签展示「相关文章 / 相关活动 / 相关商品」。
  83. * 数据由后端 /api/content/related-items 一次返回,本组件自加载、自渲染、自翻页。
  84. * 父页面在 scroll-view 触底时调用 ref.loadMore() 实现分段加载。
  85. *
  86. * Props:
  87. * - sourceType: 'article' | 'activity' | 'product'
  88. * - sourceId: 源内容ID
  89. */
  90. export default {
  91. name: 'RelatedContent',
  92. components: { DimensionProducts },
  93. props: {
  94. sourceType: { type: String, default: '' },
  95. sourceId: { type: [String, Number], default: '' }
  96. },
  97. data: function() {
  98. return {
  99. size: 4,
  100. loaded: false,
  101. loading: false,
  102. // 相关文章
  103. articles: [],
  104. articlesPage: 1,
  105. articlesLoading: false,
  106. articlesHasMore: true,
  107. // 相关活动
  108. activities: [],
  109. activitiesPage: 1,
  110. activitiesLoading: false,
  111. activitiesHasMore: true,
  112. // 相关商品
  113. products: [],
  114. productsPage: 1,
  115. productsLoading: false,
  116. productsHasMore: true
  117. }
  118. },
  119. created: function() {
  120. this.reload()
  121. },
  122. watch: {
  123. sourceId: function() {
  124. this.reload()
  125. }
  126. },
  127. methods: {
  128. reload: function() {
  129. if (!this.sourceType || this.sourceId === '' || this.sourceId === null || this.sourceId === undefined) {
  130. return
  131. }
  132. var self = this
  133. this.loaded = false
  134. this.loading = true
  135. getContentRelated({ sourceType: this.sourceType, sourceId: this.sourceId, size: this.size, page: 1 }).then(function(res) {
  136. self.loading = false
  137. self.loaded = true
  138. if (res && res.code === 200 && res.data) {
  139. var d = res.data
  140. self.articles = Array.isArray(d.articles) ? d.articles : []
  141. self.activities = self.decorateActivities(Array.isArray(d.activities) ? d.activities : [])
  142. self.products = Array.isArray(d.products) ? d.products : []
  143. self.articlesPage = 1
  144. self.activitiesPage = 1
  145. self.productsPage = 1
  146. self.articlesHasMore = self.articles.length >= self.size
  147. self.activitiesHasMore = self.activities.length >= self.size
  148. self.productsHasMore = self.products.length >= self.size
  149. } else {
  150. self.articles = []
  151. self.activities = []
  152. self.products = []
  153. self.articlesHasMore = false
  154. self.activitiesHasMore = false
  155. self.productsHasMore = false
  156. }
  157. }).catch(function() {
  158. self.loading = false
  159. self.loaded = true
  160. self.articles = []
  161. self.activities = []
  162. self.products = []
  163. self.articlesHasMore = false
  164. self.activitiesHasMore = false
  165. self.productsHasMore = false
  166. })
  167. },
  168. // 父页面 scroll-view 触底调用:按 DOM 顺序(文章→活动→商品)翻页尚未加载完的分类
  169. loadMore: function() {
  170. var self = this
  171. if (!this.loaded || this.loading) return
  172. if (this.articlesHasMore && !this.articlesLoading) {
  173. this.articlesLoading = true
  174. var nextArticlePage = this.articlesPage + 1
  175. getContentRelated({ sourceType: this.sourceType, sourceId: this.sourceId, size: this.size, page: nextArticlePage }).then(function(res) {
  176. self.articlesLoading = false
  177. if (res && res.code === 200 && res.data && Array.isArray(res.data.articles)) {
  178. var more = res.data.articles
  179. self.articles = self.articles.concat(more)
  180. self.articlesPage = nextArticlePage
  181. if (more.length < self.size) self.articlesHasMore = false
  182. } else {
  183. self.articlesHasMore = false
  184. }
  185. }).catch(function() {
  186. self.articlesLoading = false
  187. self.articlesHasMore = false
  188. })
  189. return
  190. }
  191. if (this.activitiesHasMore && !this.activitiesLoading) {
  192. this.activitiesLoading = true
  193. var nextActivityPage = this.activitiesPage + 1
  194. getContentRelated({ sourceType: this.sourceType, sourceId: this.sourceId, size: this.size, page: nextActivityPage }).then(function(res) {
  195. self.activitiesLoading = false
  196. if (res && res.code === 200 && res.data && Array.isArray(res.data.activities)) {
  197. var moreActs = self.decorateActivities(res.data.activities)
  198. self.activities = self.activities.concat(moreActs)
  199. self.activitiesPage = nextActivityPage
  200. if (moreActs.length < self.size) self.activitiesHasMore = false
  201. } else {
  202. self.activitiesHasMore = false
  203. }
  204. }).catch(function() {
  205. self.activitiesLoading = false
  206. self.activitiesHasMore = false
  207. })
  208. return
  209. }
  210. if (this.productsHasMore && !this.productsLoading) {
  211. this.productsLoading = true
  212. var nextProductPage = this.productsPage + 1
  213. getContentRelated({ sourceType: this.sourceType, sourceId: this.sourceId, size: this.size, page: nextProductPage }).then(function(res) {
  214. self.productsLoading = false
  215. if (res && res.code === 200 && res.data && Array.isArray(res.data.products)) {
  216. var moreProds = res.data.products
  217. self.products = self.products.concat(moreProds)
  218. self.productsPage = nextProductPage
  219. if (moreProds.length < self.size) self.productsHasMore = false
  220. } else {
  221. self.productsHasMore = false
  222. }
  223. }).catch(function() {
  224. self.productsLoading = false
  225. self.productsHasMore = false
  226. })
  227. }
  228. },
  229. // 活动卡片展示态:已结束 / 进行中 / 待开始(活动列表页同口径)
  230. decorateActivities: function(list) {
  231. var self = this
  232. return list.map(function(act) {
  233. var item = Object.assign({}, act)
  234. var now = new Date()
  235. var endTime = parseDate(item.endTime)
  236. if (item.status === 'ended' || (endTime && endTime < now)) {
  237. item._cls = 'ended'
  238. item._txt = '已结束'
  239. return item
  240. }
  241. var startTime = parseDate(item.startTime)
  242. if (startTime && startTime > now) {
  243. item._cls = 'upcoming'
  244. item._txt = '待开始'
  245. } else {
  246. item._cls = 'ongoing'
  247. item._txt = '进行中'
  248. }
  249. return item
  250. })
  251. },
  252. formatActivityTime: function(timeStr) {
  253. var d = parseDate(timeStr)
  254. if (!d) return ''
  255. var month = d.getMonth() + 1
  256. var day = d.getDate()
  257. var hour = d.getHours()
  258. var minute = d.getMinutes()
  259. var hourStr = hour < 10 ? '0' + hour : hour
  260. var minStr = minute < 10 ? '0' + minute : minute
  261. return month + '月' + day + '日 ' + hourStr + ':' + minStr + ' 开始'
  262. },
  263. formatPrice: function(cents) {
  264. if (cents === null || cents === undefined) return ''
  265. return (cents / 100).toFixed(2)
  266. },
  267. articleCover: function(item) {
  268. if (!item || !item.coverImage) return '/static/default-article.png'
  269. return this.resolveUrl(item.coverImage)
  270. },
  271. activityCover: function(item) {
  272. if (!item || !item.coverImage) return '/static/default-activity.png'
  273. return this.resolveUrl(item.coverImage)
  274. },
  275. resolveUrl: function(path) {
  276. var s = String(path || '').trim()
  277. if (!s || s === 'null' || s === 'undefined') return ''
  278. if (s.indexOf('http://') === 0 || s.indexOf('https://') === 0 || s.indexOf('/static/') === 0) return s
  279. return config.API_BASE_URL + (s.charAt(0) === '/' ? s : '/' + s)
  280. },
  281. // 跳转相关文章详情
  282. goArticle: function(item) {
  283. if (!item || !item.id) return
  284. uni.navigateTo({ url: '/pages/article-center/article-detail?id=' + item.id })
  285. },
  286. // 跳转相关活动详情
  287. goActivity: function(item) {
  288. if (!item || !item.id) return
  289. uni.navigateTo({ url: '/pages/activity/activity-detail/activity-detail?id=' + item.id })
  290. },
  291. // 跳转商品详情(优先使用后端返回的 url)
  292. goProduct: function(prod) {
  293. if (!prod) return
  294. var pid = prod.productId || prod.id
  295. var url = prod.url || ('/pages/discover-detail/product-detail/product-detail?id=' + (pid || ''))
  296. if (url.indexOf('/pages/') !== 0) return
  297. uni.navigateTo({ url: url })
  298. }
  299. }
  300. }
  301. </script>
  302. <style scoped lang="scss">
  303. .related-content {
  304. margin-top: 30rpx;
  305. }
  306. .related-loading-wrap {
  307. padding: 40rpx 30rpx;
  308. display: flex;
  309. justify-content: center;
  310. }
  311. .related-loading-text {
  312. font-size: 26rpx;
  313. color: $facade-text-gray-400;
  314. }
  315. .related-section {
  316. background: $app-surface;
  317. border-radius: 16rpx;
  318. padding: 24rpx;
  319. margin-bottom: 30rpx;
  320. }
  321. .related-header {
  322. display: flex;
  323. align-items: baseline;
  324. margin-bottom: 20rpx;
  325. }
  326. .related-title {
  327. font-size: 30rpx;
  328. font-weight: 600;
  329. color: $facade-text-gray-800;
  330. margin-right: 12rpx;
  331. }
  332. .related-subtitle {
  333. font-size: 22rpx;
  334. color: $facade-text-gray-400;
  335. }
  336. .ra-list {
  337. display: flex;
  338. flex-direction: column;
  339. }
  340. .ra-item {
  341. display: flex;
  342. flex-direction: row;
  343. align-items: flex-start;
  344. padding: 14rpx 0;
  345. border-radius: 12rpx;
  346. }
  347. .ra-item-pressed {
  348. background: $facade-fill-gray-100;
  349. }
  350. .ra-cover {
  351. width: 160rpx;
  352. height: 120rpx;
  353. border-radius: 12rpx;
  354. background: $facade-fill-gray-100;
  355. flex-shrink: 0;
  356. }
  357. .ra-body {
  358. flex: 1;
  359. min-width: 0;
  360. margin-left: 16rpx;
  361. display: flex;
  362. flex-direction: column;
  363. }
  364. .ra-title {
  365. font-size: 27rpx;
  366. font-weight: 500;
  367. color: $facade-text-gray-800;
  368. line-height: 1.4;
  369. display: -webkit-box;
  370. -webkit-box-orient: vertical;
  371. -webkit-line-clamp: 2;
  372. overflow: hidden;
  373. }
  374. .ra-summary {
  375. font-size: 23rpx;
  376. color: $facade-text-gray-400;
  377. line-height: 1.4;
  378. margin-top: 6rpx;
  379. display: -webkit-box;
  380. -webkit-box-orient: vertical;
  381. -webkit-line-clamp: 2;
  382. overflow: hidden;
  383. }
  384. .ra-bottom {
  385. display: flex;
  386. align-items: center;
  387. margin-top: 8rpx;
  388. }
  389. .ra-status {
  390. font-size: 20rpx;
  391. padding: 2rpx 12rpx;
  392. border-radius: 20rpx;
  393. margin-right: 12rpx;
  394. }
  395. .ra-status.upcoming {
  396. background: $uni-primary-pale;
  397. color: $brand-primary;
  398. }
  399. .ra-status.ongoing {
  400. background: $status-success-soft-2;
  401. color: $mat-green-800;
  402. }
  403. .ra-status.ended {
  404. background: $facade-fill-2;
  405. color: $facade-text-muted;
  406. }
  407. .ra-price {
  408. font-size: 23rpx;
  409. color: $status-danger;
  410. font-weight: 600;
  411. }
  412. .ra-price-free {
  413. font-size: 23rpx;
  414. color: $facade-text-gray-400;
  415. }
  416. .related-loading-hint {
  417. text-align: center;
  418. font-size: 24rpx;
  419. color: $facade-text-gray-400;
  420. padding: 16rpx 0 8rpx;
  421. }
  422. .related-loading-hint.dim {
  423. color: $facade-text-gray-300;
  424. }
  425. </style>