DimensionArticles.vue 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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 { BrandColors } from '../utils/brand-colors.js'
  53. import { getRecommendedArticles } from '@/utils/api.js'
  54. var DIMENSION_MAP = {
  55. body: { name: '身', color: BrandColors.body },
  56. mind: { name: '心', color: BrandColors.mind },
  57. wisdom: { name: '智', color: BrandColors.wisdom },
  58. action: { name: '行', color: BrandColors.success },
  59. wealth: { name: '富', color: BrandColors.wealth }
  60. }
  61. var defaultGradientColors = [
  62. 'linear-gradient(135deg, #6366F1, #818CF8)',
  63. 'linear-gradient(135deg, #8B5CF6, #A78BFA)',
  64. 'linear-gradient(135deg, #0EA5E9, #38BDF8)',
  65. 'linear-gradient(135deg, #10B981, #34D399)',
  66. 'linear-gradient(135deg, #F97316, #FB923C)'
  67. ]
  68. export default {
  69. props: {
  70. articles: { type: Array, default: function() { return [] } },
  71. isLoggedIn: { type: Boolean, default: false },
  72. dimensionCode: { type: String, default: null },
  73. colors: { type: Array, default: function() { return defaultGradientColors } }
  74. },
  75. data: function() {
  76. return {
  77. selfArticles: [],
  78. loading: false
  79. }
  80. },
  81. computed: {
  82. defaultColors: function() {
  83. return defaultGradientColors
  84. },
  85. displayArticles: function() {
  86. var self = this
  87. var list = (this.articles && this.articles.length > 0 ? this.articles : self.selfArticles)
  88. return (list || []).map(function(item) {
  89. item._badges = self.parseWeights(item.dimensionWeights)
  90. return item
  91. })
  92. }
  93. },
  94. watch: {
  95. dimensionCode: function(newVal, oldVal) {
  96. if (newVal !== oldVal && newVal && this.isLoggedIn) {
  97. this.loadArticles()
  98. }
  99. }
  100. },
  101. mounted: function() {
  102. if (this.isLoggedIn && (!this.articles || this.articles.length === 0) && this.dimensionCode) {
  103. this.loadArticles()
  104. }
  105. },
  106. methods: {
  107. loadArticles: function() {
  108. var self = this
  109. if (!self.isLoggedIn) return
  110. self.loading = true
  111. getRecommendedArticles({ type: 'article', dimensionCode: self.dimensionCode, size: 5 }).then(function(res) {
  112. self.loading = false
  113. if (res && res.code === 200 && res.data && res.data.recommendations) {
  114. self.selfArticles = res.data.recommendations.slice(0, 5)
  115. }
  116. }).catch(function() {
  117. self.loading = false
  118. self.selfArticles = []
  119. })
  120. },
  121. parseWeights: function(str) {
  122. if (!str) return []
  123. try {
  124. var obj = JSON.parse(str)
  125. var result = []
  126. for (var key in obj) {
  127. if (obj[key] > 0 && DIMENSION_MAP[key]) {
  128. result.push({ name: DIMENSION_MAP[key].name, color: DIMENSION_MAP[key].color, weight: obj[key] })
  129. }
  130. }
  131. return result
  132. } catch (e) {
  133. return []
  134. }
  135. }
  136. }
  137. }
  138. </script>
  139. <style scoped lang="scss">
  140. .section {
  141. margin: 20rpx 20rpx;
  142. }
  143. .section-header {
  144. display: flex;
  145. justify-content: space-between;
  146. align-items: center;
  147. margin-bottom: 20rpx;
  148. }
  149. .section-title {
  150. font-size: 30rpx;
  151. font-weight: bold;
  152. color: $facade-text-strong;
  153. }
  154. .section-more {
  155. font-size: 24rpx;
  156. color: $facade-text-muted;
  157. }
  158. .article-list {
  159. display: flex;
  160. flex-direction: column;
  161. }
  162. .article-card {
  163. background: $app-surface;
  164. border-radius: 20rpx;
  165. padding: 28rpx 24rpx;
  166. box-shadow: 0 2rpx 12rpx rgba(0,0,0,0.06);
  167. margin-bottom: 20rpx;
  168. }
  169. .article-card:active {
  170. opacity: 0.8;
  171. }
  172. .article-category {
  173. display: inline-block;
  174. padding: 4rpx 18rpx;
  175. border-radius: 20rpx;
  176. margin-bottom: 14rpx;
  177. }
  178. .article-category-text {
  179. font-size: 20rpx;
  180. color: $app-surface;
  181. font-weight: 500;
  182. }
  183. .article-title {
  184. font-size: 28rpx;
  185. font-weight: bold;
  186. color: $facade-text-strong;
  187. display: block;
  188. margin-bottom: 10rpx;
  189. }
  190. .weight-badges {
  191. display: flex;
  192. flex-direction: row;
  193. flex-wrap: wrap;
  194. gap: 6rpx;
  195. margin-bottom: 10rpx;
  196. }
  197. .weight-badge {
  198. font-size: 18rpx;
  199. padding: 2rpx 10rpx;
  200. border-radius: 8rpx;
  201. font-weight: 500;
  202. }
  203. .article-summary {
  204. font-size: 24rpx;
  205. color: $facade-text-faint;
  206. line-height: 1.5;
  207. display: block;
  208. margin-bottom: 20rpx;
  209. }
  210. .article-meta {
  211. display: flex;
  212. flex-direction: row;
  213. align-items: center;
  214. }
  215. .meta-item {
  216. display: flex;
  217. flex-direction: row;
  218. align-items: center;
  219. margin-right: 24rpx;
  220. }
  221. .meta-icon {
  222. font-size: 24rpx;
  223. margin-right: 6rpx;
  224. }
  225. .meta-text {
  226. font-size: 22rpx;
  227. color: $facade-text-muted;
  228. }
  229. .article-date {
  230. font-size: 22rpx;
  231. color: $facade-text-ghost-light;
  232. margin-left: auto;
  233. }
  234. </style>