DimensionArticles.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 { getFeaturedArticles } 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. colors: { type: Array, default: function() { return defaultGradientColors } }
  72. },
  73. data: function() {
  74. return {
  75. selfArticles: [],
  76. loading: false
  77. }
  78. },
  79. computed: {
  80. defaultColors: function() {
  81. return defaultGradientColors
  82. },
  83. displayArticles: function() {
  84. var self = this
  85. var list = (this.articles && this.articles.length > 0 ? this.articles : self.selfArticles)
  86. return (list || []).map(function(item) {
  87. item._badges = self.parseWeights(item.dimensionWeights)
  88. return item
  89. })
  90. }
  91. },
  92. mounted: function() {
  93. if (!this.articles || this.articles.length === 0) {
  94. this.loadArticles()
  95. }
  96. },
  97. methods: {
  98. loadArticles: function() {
  99. var self = this
  100. self.loading = true
  101. getFeaturedArticles({ size: 3 }).then(function(res) {
  102. self.loading = false
  103. if (res && res.code === 200 && res.data) {
  104. var list = Array.isArray(res.data) ? res.data : (res.data.records || [])
  105. self.selfArticles = list.slice(0, 3)
  106. }
  107. }).catch(function() {
  108. self.loading = false
  109. self.selfArticles = []
  110. })
  111. },
  112. parseWeights: function(str) {
  113. if (!str) return []
  114. try {
  115. var obj = JSON.parse(str)
  116. var result = []
  117. for (var key in obj) {
  118. if (obj[key] > 0 && DIMENSION_MAP[key]) {
  119. result.push({ name: DIMENSION_MAP[key].name, color: DIMENSION_MAP[key].color, weight: obj[key] })
  120. }
  121. }
  122. return result
  123. } catch (e) {
  124. return []
  125. }
  126. }
  127. }
  128. }
  129. </script>
  130. <style scoped>
  131. .section {
  132. margin: 20rpx 20rpx;
  133. }
  134. .section-header {
  135. display: flex;
  136. justify-content: space-between;
  137. align-items: center;
  138. margin-bottom: 20rpx;
  139. }
  140. .section-title {
  141. font-size: 30rpx;
  142. font-weight: bold;
  143. color: #333;
  144. }
  145. .section-more {
  146. font-size: 24rpx;
  147. color: #999;
  148. }
  149. .article-list {
  150. display: flex;
  151. flex-direction: column;
  152. }
  153. .article-card {
  154. background: #fff;
  155. border-radius: 20rpx;
  156. padding: 28rpx 24rpx;
  157. box-shadow: 0 2rpx 12rpx rgba(0,0,0,0.06);
  158. margin-bottom: 20rpx;
  159. }
  160. .article-card:active {
  161. opacity: 0.8;
  162. }
  163. .article-category {
  164. display: inline-block;
  165. padding: 4rpx 18rpx;
  166. border-radius: 20rpx;
  167. margin-bottom: 14rpx;
  168. }
  169. .article-category-text {
  170. font-size: 20rpx;
  171. color: #fff;
  172. font-weight: 500;
  173. }
  174. .article-title {
  175. font-size: 28rpx;
  176. font-weight: bold;
  177. color: #333;
  178. display: block;
  179. margin-bottom: 10rpx;
  180. }
  181. .weight-badges {
  182. display: flex;
  183. flex-direction: row;
  184. flex-wrap: wrap;
  185. gap: 6rpx;
  186. margin-bottom: 10rpx;
  187. }
  188. .weight-badge {
  189. font-size: 18rpx;
  190. padding: 2rpx 10rpx;
  191. border-radius: 8rpx;
  192. font-weight: 500;
  193. }
  194. .article-summary {
  195. font-size: 24rpx;
  196. color: #888;
  197. line-height: 1.5;
  198. display: block;
  199. margin-bottom: 20rpx;
  200. }
  201. .article-meta {
  202. display: flex;
  203. flex-direction: row;
  204. align-items: center;
  205. }
  206. .meta-item {
  207. display: flex;
  208. flex-direction: row;
  209. align-items: center;
  210. margin-right: 24rpx;
  211. }
  212. .meta-icon {
  213. font-size: 24rpx;
  214. margin-right: 6rpx;
  215. }
  216. .meta-text {
  217. font-size: 22rpx;
  218. color: #999;
  219. }
  220. .article-date {
  221. font-size: 22rpx;
  222. color: #bbb;
  223. margin-left: auto;
  224. }
  225. </style>