| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348 |
- <template>
- <view class="container">
- <!-- 品牌头部 -->
- <PageBanner theme="mind-wisdom"
- tagline="探索内心世界,培养健康心智"
- quote="知己知彼,百战不殆" />
- <!-- 分类筛选 -->
- <view class="filter-section">
- <scroll-view scroll-x enable-flex show-scrollbar="false" class="category-scroll">
- <view class="filter-chips">
- <view
- v-for="cat in categoryList"
- :key="cat.value"
- :class="['filter-chip', currentCategory === cat.value ? 'active' : '']"
- @click="onCategoryChange(cat.value)"
- >
- {{ cat.label }}
- </view>
- </view>
- </scroll-view>
- </view>
- <!-- 文章列表 -->
- <scroll-view scroll-y class="article-list" @scrolltolower="onLoadMore">
- <view v-if="loading && articles.length === 0" class="loading-wrap">
- <text class="loading-text">加载中...</text>
- </view>
- <view v-else-if="articles.length === 0" class="empty-wrap">
- <text class="empty-icon">📝</text>
- <text class="empty-text">暂无相关文章</text>
- </view>
- <view v-else class="article-grid">
- <view
- v-for="item in articles"
- :key="item.id"
- class="article-card"
- @click="goDetail(item.id)"
- >
- <image
- class="article-cover"
- :src="item.coverImage || '/static/default-article.png'"
- mode="aspectFill"
- />
- <view class="article-body">
- <view class="article-meta">
- <text class="article-category">{{ item.categoryName || '心理健康' }}</text>
- <text class="article-date">{{ item.publishDate || '' }}</text>
- </view>
- <text class="article-title">{{ item.title }}</text>
- <text class="article-summary">{{ item.summary || item.content }}</text>
- <view class="article-footer">
- <text class="article-author">{{ item.author || '浠艾福' }}</text>
- <text class="article-read-count">阅读 {{ item.readCount || 0 }}</text>
- </view>
- </view>
- </view>
- </view>
- <view v-if="loadingMore" class="loading-more">
- <text class="loading-text">加载中...</text>
- </view>
- <view v-if="noMore && articles.length > 0" class="no-more">
- <text class="no-more-text">— 没有更多了 —</text>
- </view>
- <!-- 未登录提示 -->
- <view v-if="!isLoggedIn" class="login-hint-bar">
- <text class="login-hint-text">🔒 登录后解锁全部文章</text>
- </view>
- <!-- 底部占位 -->
- <view class="bottom-spacer"></view>
- </scroll-view>
- </view>
- </template>
- <script>
- import PageBanner from '../../components/PageBanner.vue'
- import { getArticleCategories, getArticleList } from '../../utils/api.js'
- export default {
- components: { PageBanner },
- data() {
- return {
- categoryList: [{ id: '', name: '全部' }],
- categoryMap: {},
- currentCategory: '',
- articles: [],
- page: 1,
- size: 10,
- total: 0,
- loading: false,
- noMore: false,
- isLoggedIn: false
- }
- },
- onLoad() {
- this.isLoggedIn = !!uni.getStorageSync('token')
- this.loadCategories()
- },
- onShow() {
- this.isLoggedIn = !!uni.getStorageSync('token')
- },
- methods: {
- async loadCategories() {
- try {
- const res = await getArticleCategories()
- if (res.code === 200 && res.data) {
- const cats = Array.isArray(res.data) ? res.data : []
- let map = {}
- cats.forEach(function(c) { map[c.id] = c.name })
- this.categoryMap = map
- this.categoryList = [{ id: '', name: '全部' }].concat(cats)
- }
- } catch (e) {
- // 默认分类列表
- this.categoryList = [
- { id: '', name: '全部' },
- { id: 1, name: '心理健康' },
- { id: 2, name: '情绪管理' }
- ]
- }
- // 分类加载完成后加载文章
- this.loadArticles()
- },
- onCategoryChange(id) {
- this.currentCategory = id
- this.page = 1
- this.articles = []
- this.noMore = false
- this.loadArticles()
- },
- async loadArticles() {
- if (this.loading) return
- this.loading = true
- try {
- let params = { page: this.page, size: this.size }
- if (this.currentCategory) {
- params.categoryId = this.currentCategory
- }
- const res = await getArticleList(params)
- if (res.code === 200 && res.data) {
- let pageData = res.data
- let list = pageData.records || []
- let mapped = list.map(function(item) {
- return {
- id: item.id,
- title: item.title || '',
- summary: item.summary || '',
- content: item.content || '',
- coverImage: item.coverImage || '',
- author: item.author || '浠艾福',
- categoryName: item.categoryName || item.category || '',
- publishDate: item.publishedAt ? item.publishedAt.slice(0, 10) : '',
- readCount: item.viewCount || 0
- }
- })
- if (this.page === 1) {
- this.articles = mapped
- } else {
- this.articles = this.articles.concat(mapped)
- }
- this.total = pageData.total || 0
- this.noMore = this.articles.length >= this.total
- } else {
- if (this.page === 1) {
- this.articles = []
- }
- }
- } catch (e) {
- uni.showToast({ title: '加载失败', icon: 'none' })
- } finally {
- this.loading = false
- }
- },
- onLoadMore() {
- if (this.noMore || this.loading) return
- this.page++
- this.loadArticles()
- },
- goDetail(id) {
- if (this.isLoggedIn) {
- uni.navigateTo({ url: '/pages/mind/article-detail?id=' + id })
- } else {
- uni.navigateTo({ url: '/pages/login/login?redirect=' + encodeURIComponent('/pages/mind/articles') })
- }
- }
- }
- }
- </script>
- <style scoped>
- .container {
- display: flex;
- flex-direction: column;
- min-height: 100vh;
- background: #f5f7fa;
- }
- /* 分类筛选 */
- .filter-section {
- background: #fff;
- padding: 16rpx 0 12rpx;
- border-bottom: 1rpx solid #eee;
- }
- .category-scroll {
- white-space: nowrap;
- }
- .filter-chips {
- display: flex;
- padding: 0 20rpx;
- gap: 16rpx;
- }
- .filter-chip {
- display: inline-block;
- padding: 8rpx 24rpx;
- font-size: 24rpx;
- color: #666;
- background: #f0f0f0;
- border-radius: 30rpx;
- flex-shrink: 0;
- }
- .filter-chip.active {
- background: #5B9BD5;
- color: #fff;
- }
- /* 文章列表 */
- .article-list {
- flex: 1;
- height: calc(100vh - 220rpx);
- }
- .loading-wrap,
- .empty-wrap {
- display: flex;
- flex-direction: column;
- align-items: center;
- padding-top: 120rpx;
- }
- .loading-text {
- font-size: 26rpx;
- color: #999;
- }
- .empty-icon {
- font-size: 100rpx;
- margin-bottom: 24rpx;
- }
- .empty-text {
- font-size: 28rpx;
- color: #999;
- }
- .article-grid {
- padding: 20rpx 24rpx;
- }
- .article-card {
- background: #fff;
- border-radius: 16rpx;
- overflow: hidden;
- margin-bottom: 20rpx;
- box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.06);
- }
- .article-cover {
- width: 100%;
- height: 280rpx;
- background: linear-gradient(135deg, #667eea, #764ba2);
- }
- .article-body {
- padding: 20rpx;
- }
- .article-meta {
- display: flex;
- align-items: center;
- margin-bottom: 10rpx;
- }
- .article-category {
- font-size: 20rpx;
- color: #5B9BD5;
- background: rgba(91,155,213,0.1);
- padding: 2rpx 12rpx;
- border-radius: 8rpx;
- margin-right: 12rpx;
- }
- .article-date {
- font-size: 20rpx;
- color: #999;
- }
- .article-title {
- display: block;
- font-size: 28rpx;
- font-weight: bold;
- color: #333;
- margin-bottom: 8rpx;
- line-height: 1.4;
- }
- .article-summary {
- display: block;
- font-size: 24rpx;
- color: #666;
- line-height: 1.5;
- margin-bottom: 12rpx;
- overflow: hidden;
- text-overflow: ellipsis;
- display: -webkit-box;
- -webkit-line-clamp: 2;
- -webkit-box-orient: vertical;
- }
- .article-footer {
- display: flex;
- justify-content: space-between;
- align-items: center;
- }
- .article-author {
- font-size: 20rpx;
- color: #999;
- }
- .article-read-count {
- font-size: 20rpx;
- color: #bbb;
- }
- .loading-more,
- .no-more {
- text-align: center;
- padding: 30rpx;
- }
- .no-more-text {
- font-size: 24rpx;
- color: #ccc;
- }
- /* 未登录提示 */
- .login-hint-bar {
- text-align: center;
- padding: 20rpx;
- background: #fef9e7;
- border-top: 1rpx solid #f0e6c0;
- margin: 20rpx 24rpx 0;
- border-radius: 12rpx;
- }
- .login-hint-text {
- font-size: 24rpx;
- color: #b8860b;
- }
- /* 底部占位 */
- .bottom-spacer {
- height: 120rpx;
- }
- </style>
|