| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229 |
- <template>
- <view class="section">
- <view class="section-header">
- <text class="section-title">🔥 推荐阅读</text>
- <text class="section-more" @click="$emit('moreArticles')">更多 ›</text>
- </view>
- <view class="article-list">
- <!-- 无数据时显示占位卡片 -->
- <template v-if="!displayArticles || displayArticles.length === 0">
- <view class="article-card" v-for="i in 3" :key="i">
- <view class="article-category" :style="{ background: colors[(i-1) % colors.length] }">
- <text class="article-category-text">推荐文章</text>
- </view>
- <text class="article-title">暂无推荐文章</text>
- <text class="article-summary">精彩内容即将上线,敬请期待</text>
- <view class="article-meta">
- <view class="meta-item">
- <text class="meta-icon">📖</text>
- <text class="meta-text">0</text>
- </view>
- <text class="article-date">--</text>
- </view>
- </view>
- </template>
- <!-- 有数据时显示文章卡片 -->
- <template v-else><view class="article-card" v-for="article in displayArticles" :key="article.id" @click="$emit('articleClick', article)">
- <view class="article-category" :style="{ background: article.categoryColor || defaultColors[0] }">
- <text class="article-category-text">{{ article.category || '推荐文章' }}</text>
- </view>
- <text class="article-title">{{ article.title }}</text>
- <view class="weight-badges" v-if="article._badges && article._badges.length">
- <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>
- </view>
- <text class="article-summary" v-if="article.summary">{{ article.summary }}</text>
- <view class="article-meta">
- <view class="meta-item">
- <text class="meta-icon">📖</text>
- <text class="meta-text">{{ article.views || '0' }}</text>
- </view>
- <view class="meta-item">
- <text class="meta-icon">👍</text>
- <text class="meta-text">{{ article.likes || '0' }}</text>
- </view>
- <text class="article-date" v-if="article.date">{{ article.date }}</text>
- </view>
- </view>
- </template>
- </view>
- </view>
- </template>
- <script>
- import { getFeaturedArticles } from '@/utils/api.js'
- var DIMENSION_MAP = {
- body: { name: '身', color: '#FF8C42' },
- mind: { name: '心', color: '#FF6B9D' },
- wisdom: { name: '智', color: '#6366F1' },
- action: { name: '行', color: '#10B981' },
- wealth: { name: '富', color: '#F59E0B' }
- }
- var defaultGradientColors = [
- 'linear-gradient(135deg, #6366F1, #818CF8)',
- 'linear-gradient(135deg, #8B5CF6, #A78BFA)',
- 'linear-gradient(135deg, #5B9BD5, #8FC5E8)',
- 'linear-gradient(135deg, #10B981, #34D399)',
- 'linear-gradient(135deg, #F97316, #FB923C)'
- ]
- export default {
- props: {
- articles: { type: Array, default: function() { return [] } },
- isLoggedIn: { type: Boolean, default: false },
- colors: { type: Array, default: function() { return defaultGradientColors } }
- },
- data: function() {
- return {
- selfArticles: [],
- loading: false
- }
- },
- computed: {
- defaultColors: function() {
- return defaultGradientColors
- },
- displayArticles: function() {
- var self = this
- var list = (this.articles && this.articles.length > 0 ? this.articles : self.selfArticles)
- return (list || []).map(function(item) {
- item._badges = self.parseWeights(item.dimensionWeights)
- return item
- })
- }
- },
- mounted: function() {
- if (!this.articles || this.articles.length === 0) {
- this.loadArticles()
- }
- },
- methods: {
- loadArticles: function() {
- var self = this
- self.loading = true
- getFeaturedArticles({ size: 3 }).then(function(res) {
- self.loading = false
- if (res && res.code === 200 && res.data) {
- var list = Array.isArray(res.data) ? res.data : (res.data.records || [])
- self.selfArticles = list.slice(0, 3)
- }
- }).catch(function() {
- self.loading = false
- self.selfArticles = []
- })
- },
- parseWeights: function(str) {
- if (!str) return []
- try {
- var obj = JSON.parse(str)
- var result = []
- for (var key in obj) {
- if (obj[key] > 0 && DIMENSION_MAP[key]) {
- result.push({ name: DIMENSION_MAP[key].name, color: DIMENSION_MAP[key].color, weight: obj[key] })
- }
- }
- return result
- } catch (e) {
- return []
- }
- }
- }
- }
- </script>
- <style scoped>
- .section {
- margin: 20rpx 20rpx;
- }
- .section-header {
- display: flex;
- justify-content: space-between;
- align-items: center;
- margin-bottom: 20rpx;
- }
- .section-title {
- font-size: 30rpx;
- font-weight: bold;
- color: #333;
- }
- .section-more {
- font-size: 24rpx;
- color: #999;
- }
- .article-list {
- display: flex;
- flex-direction: column;
- }
- .article-card {
- background: #fff;
- border-radius: 20rpx;
- padding: 28rpx 24rpx;
- box-shadow: 0 2rpx 12rpx rgba(0,0,0,0.06);
- margin-bottom: 20rpx;
- }
- .article-card:active {
- opacity: 0.8;
- }
- .article-category {
- display: inline-block;
- padding: 4rpx 18rpx;
- border-radius: 20rpx;
- margin-bottom: 14rpx;
- }
- .article-category-text {
- font-size: 20rpx;
- color: #fff;
- font-weight: 500;
- }
- .article-title {
- font-size: 28rpx;
- font-weight: bold;
- color: #333;
- display: block;
- margin-bottom: 10rpx;
- }
- .weight-badges {
- display: flex;
- flex-direction: row;
- flex-wrap: wrap;
- gap: 6rpx;
- margin-bottom: 10rpx;
- }
- .weight-badge {
- font-size: 18rpx;
- padding: 2rpx 10rpx;
- border-radius: 8rpx;
- font-weight: 500;
- }
- .article-summary {
- font-size: 24rpx;
- color: #888;
- line-height: 1.5;
- display: block;
- margin-bottom: 20rpx;
- }
- .article-meta {
- display: flex;
- flex-direction: row;
- align-items: center;
- }
- .meta-item {
- display: flex;
- flex-direction: row;
- align-items: center;
- margin-right: 24rpx;
- }
- .meta-icon {
- font-size: 24rpx;
- margin-right: 6rpx;
- }
- .meta-text {
- font-size: 22rpx;
- color: #999;
- }
- .article-date {
- font-size: 22rpx;
- color: #bbb;
- margin-left: auto;
- }
- </style>
|