DimensionArticles.vue 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <template>
  2. <view class="section">
  3. <view class="section-header">
  4. <text class="section-title">🔥 推荐阅读</text>
  5. <text class="section-more" @click="$emit('moreArticles')">更多 ›</text>
  6. </view>
  7. <view class="article-list">
  8. <!-- 无数据时显示占位卡片 -->
  9. <template v-if="!displayArticles || displayArticles.length === 0">
  10. <view class="article-card" v-for="i in 3" :key="i">
  11. <view class="article-category" :style="{ background: colors[(i-1) % colors.length] }">
  12. <text class="article-category-text">推荐文章</text>
  13. </view>
  14. <text class="article-title">暂无推荐文章</text>
  15. <text class="article-summary">精彩内容即将上线,敬请期待</text>
  16. <view class="article-meta">
  17. <view class="meta-item">
  18. <text class="meta-icon">&#x1F4D6;</text>
  19. <text class="meta-text">0</text>
  20. </view>
  21. <text class="article-date">--</text>
  22. </view>
  23. </view>
  24. </template>
  25. <!-- 有数据时显示文章卡片 -->
  26. <template v-else><view class="article-card" v-for="article in displayArticles" :key="article.id" @click="$emit('articleClick', article)">
  27. <view class="article-category" :style="{ background: article.categoryColor || defaultColors[0] }">
  28. <text class="article-category-text">{{ article.category || '推荐文章' }}</text>
  29. </view>
  30. <text class="article-title">{{ article.title }}</text>
  31. <view class="weight-badges" v-if="article._badges && article._badges.length">
  32. <text class="weight-badge" v-for="badge in article._badges" :key="badge.name" :style="{ background: badge.color + '20', color: badge.color }">{{ badge.name }}{{ badge.weight }}%</text>
  33. </view>
  34. <text class="article-summary" v-if="article.summary">{{ article.summary }}</text>
  35. <view class="article-meta">
  36. <view class="meta-item">
  37. <text class="meta-icon">&#x1F4D6;</text>
  38. <text class="meta-text">{{ article.views || '0' }}</text>
  39. </view>
  40. <view class="meta-item">
  41. <text class="meta-icon">&#x1F44D;</text>
  42. <text class="meta-text">{{ article.likes || '0' }}</text>
  43. </view>
  44. <text class="article-date" v-if="article.date">{{ article.date }}</text>
  45. </view>
  46. </view>
  47. </template>
  48. </view>
  49. </view>
  50. </template>
  51. <script>
  52. import { getRecommendedArticles } from '@/utils/api.js'
  53. var DIMENSION_MAP = {
  54. body: { name: '身', color: '#FF8C42' },
  55. mind: { name: '心', color: '#FF6B9D' },
  56. wisdom: { name: '智', color: '#6366F1' },
  57. action: { name: '行', color: '#10B981' },
  58. wealth: { name: '富', color: '#F59E0B' }
  59. }
  60. var defaultGradientColors = [
  61. 'linear-gradient(135deg, #6366F1, #818CF8)',
  62. 'linear-gradient(135deg, #8B5CF6, #A78BFA)',
  63. 'linear-gradient(135deg, #5B9BD5, #8FC5E8)',
  64. 'linear-gradient(135deg, #10B981, #34D399)',
  65. 'linear-gradient(135deg, #F97316, #FB923C)'
  66. ]
  67. export default {
  68. props: {
  69. articles: { type: Array, default: function() { return [] } },
  70. isLoggedIn: { type: Boolean, default: false },
  71. dimensionCode: { type: String, default: null },
  72. colors: { type: Array, default: function() { return defaultGradientColors } }
  73. },
  74. data: function() {
  75. return {
  76. selfArticles: [],
  77. loading: false
  78. }
  79. },
  80. computed: {
  81. defaultColors: function() {
  82. return defaultGradientColors
  83. },
  84. displayArticles: function() {
  85. var self = this
  86. var list = (this.articles && this.articles.length > 0 ? this.articles : self.selfArticles)
  87. return (list || []).map(function(item) {
  88. item._badges = self.parseWeights(item.dimensionWeights)
  89. return item
  90. })
  91. }
  92. },
  93. watch: {
  94. dimensionCode: function(newVal, oldVal) {
  95. if (newVal !== oldVal && newVal && this.isLoggedIn) {
  96. this.loadArticles()
  97. }
  98. }
  99. },
  100. mounted: function() {
  101. if (this.isLoggedIn && (!this.articles || this.articles.length === 0) && this.dimensionCode) {
  102. this.loadArticles()
  103. }
  104. },
  105. methods: {
  106. loadArticles: function() {
  107. var self = this
  108. if (!self.isLoggedIn) return
  109. self.loading = true
  110. getRecommendedArticles({ type: 'article', dimensionCode: self.dimensionCode, size: 5 }).then(function(res) {
  111. self.loading = false
  112. if (res && res.code === 200 && res.data && res.data.recommendations) {
  113. self.selfArticles = res.data.recommendations.slice(0, 5)
  114. }
  115. }).catch(function() {
  116. self.loading = false
  117. self.selfArticles = []
  118. })
  119. },
  120. parseWeights: function(str) {
  121. if (!str) return []
  122. try {
  123. var obj = JSON.parse(str)
  124. var result = []
  125. for (var key in obj) {
  126. if (obj[key] > 0 && DIMENSION_MAP[key]) {
  127. result.push({ name: DIMENSION_MAP[key].name, color: DIMENSION_MAP[key].color, weight: obj[key] })
  128. }
  129. }
  130. return result
  131. } catch (e) {
  132. return []
  133. }
  134. }
  135. }
  136. }
  137. </script>
  138. <style scoped>
  139. .section {
  140. margin: 20rpx 20rpx;
  141. }
  142. .section-header {
  143. display: flex;
  144. justify-content: space-between;
  145. align-items: center;
  146. margin-bottom: 20rpx;
  147. }
  148. .section-title {
  149. font-size: 30rpx;
  150. font-weight: bold;
  151. color: #333;
  152. }
  153. .section-more {
  154. font-size: 24rpx;
  155. color: #999;
  156. }
  157. .article-list {
  158. display: flex;
  159. flex-direction: column;
  160. }
  161. .article-card {
  162. background: #fff;
  163. border-radius: 20rpx;
  164. padding: 28rpx 24rpx;
  165. box-shadow: 0 2rpx 12rpx rgba(0,0,0,0.06);
  166. margin-bottom: 20rpx;
  167. }
  168. .article-card:active {
  169. opacity: 0.8;
  170. }
  171. .article-category {
  172. display: inline-block;
  173. padding: 4rpx 18rpx;
  174. border-radius: 20rpx;
  175. margin-bottom: 14rpx;
  176. }
  177. .article-category-text {
  178. font-size: 20rpx;
  179. color: #fff;
  180. font-weight: 500;
  181. }
  182. .article-title {
  183. font-size: 28rpx;
  184. font-weight: bold;
  185. color: #333;
  186. display: block;
  187. margin-bottom: 10rpx;
  188. }
  189. .weight-badges {
  190. display: flex;
  191. flex-direction: row;
  192. flex-wrap: wrap;
  193. gap: 6rpx;
  194. margin-bottom: 10rpx;
  195. }
  196. .weight-badge {
  197. font-size: 18rpx;
  198. padding: 2rpx 10rpx;
  199. border-radius: 8rpx;
  200. font-weight: 500;
  201. }
  202. .article-summary {
  203. font-size: 24rpx;
  204. color: #888;
  205. line-height: 1.5;
  206. display: block;
  207. margin-bottom: 20rpx;
  208. }
  209. .article-meta {
  210. display: flex;
  211. flex-direction: row;
  212. align-items: center;
  213. }
  214. .meta-item {
  215. display: flex;
  216. flex-direction: row;
  217. align-items: center;
  218. margin-right: 24rpx;
  219. }
  220. .meta-icon {
  221. font-size: 24rpx;
  222. margin-right: 6rpx;
  223. }
  224. .meta-text {
  225. font-size: 22rpx;
  226. color: #999;
  227. }
  228. .article-date {
  229. font-size: 22rpx;
  230. color: #bbb;
  231. margin-left: auto;
  232. }
  233. </style>