articles.vue 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. <template>
  2. <view class="container">
  3. <!-- 品牌头部 -->
  4. <PageBanner theme="mind-wisdom"
  5. tagline="探索内心世界,培养健康心智"
  6. quote="知己知彼,百战不殆" />
  7. <!-- 分类筛选 -->
  8. <view class="filter-section">
  9. <scroll-view scroll-x enable-flex show-scrollbar="false" class="category-scroll">
  10. <view class="filter-chips">
  11. <view
  12. v-for="cat in categoryList"
  13. :key="cat.value"
  14. :class="['filter-chip', currentCategory === cat.value ? 'active' : '']"
  15. @click="onCategoryChange(cat.value)"
  16. >
  17. {{ cat.label }}
  18. </view>
  19. </view>
  20. </scroll-view>
  21. </view>
  22. <!-- 文章列表 -->
  23. <scroll-view scroll-y class="article-list" @scrolltolower="onLoadMore">
  24. <view v-if="loading && articles.length === 0" class="loading-wrap">
  25. <text class="loading-text">加载中...</text>
  26. </view>
  27. <view v-else-if="articles.length === 0" class="empty-wrap">
  28. <text class="empty-icon">📝</text>
  29. <text class="empty-text">暂无相关文章</text>
  30. </view>
  31. <view v-else class="article-grid">
  32. <view
  33. v-for="item in articles"
  34. :key="item.id"
  35. class="article-card"
  36. @click="goDetail(item.id)"
  37. >
  38. <image
  39. class="article-cover"
  40. :src="item.coverImage || '/static/default-article.png'"
  41. mode="aspectFill"
  42. />
  43. <view class="article-body">
  44. <view class="article-meta">
  45. <text class="article-category">{{ item.categoryName || '心理健康' }}</text>
  46. <text class="article-date">{{ item.publishDate || '' }}</text>
  47. </view>
  48. <text class="article-title">{{ item.title }}</text>
  49. <text class="article-summary">{{ item.summary || item.content }}</text>
  50. <view class="article-footer">
  51. <text class="article-author">{{ item.author || '浠艾福' }}</text>
  52. <text class="article-read-count">阅读 {{ item.readCount || 0 }}</text>
  53. </view>
  54. </view>
  55. </view>
  56. </view>
  57. <view v-if="loadingMore" class="loading-more">
  58. <text class="loading-text">加载中...</text>
  59. </view>
  60. <view v-if="noMore && articles.length > 0" class="no-more">
  61. <text class="no-more-text">— 没有更多了 —</text>
  62. </view>
  63. <!-- 未登录提示 -->
  64. <view v-if="!isLoggedIn" class="login-hint-bar">
  65. <text class="login-hint-text">🔒 登录后解锁全部文章</text>
  66. </view>
  67. <!-- 底部占位 -->
  68. <view class="bottom-spacer"></view>
  69. </scroll-view>
  70. </view>
  71. </template>
  72. <script>
  73. import PageBanner from '../../components/PageBanner.vue'
  74. import { getArticleCategories, getArticleList } from '../../utils/api.js'
  75. export default {
  76. components: { PageBanner },
  77. data() {
  78. return {
  79. categoryList: [{ id: '', name: '全部' }],
  80. categoryMap: {},
  81. currentCategory: '',
  82. articles: [],
  83. page: 1,
  84. size: 10,
  85. total: 0,
  86. loading: false,
  87. noMore: false,
  88. isLoggedIn: false
  89. }
  90. },
  91. onLoad() {
  92. this.isLoggedIn = !!uni.getStorageSync('token')
  93. this.loadCategories()
  94. },
  95. onShow() {
  96. this.isLoggedIn = !!uni.getStorageSync('token')
  97. },
  98. methods: {
  99. async loadCategories() {
  100. try {
  101. const res = await getArticleCategories()
  102. if (res.code === 200 && res.data) {
  103. const cats = Array.isArray(res.data) ? res.data : []
  104. let map = {}
  105. cats.forEach(function(c) { map[c.id] = c.name })
  106. this.categoryMap = map
  107. this.categoryList = [{ id: '', name: '全部' }].concat(cats)
  108. }
  109. } catch (e) {
  110. // 默认分类列表
  111. this.categoryList = [
  112. { id: '', name: '全部' },
  113. { id: 1, name: '心理健康' },
  114. { id: 2, name: '情绪管理' }
  115. ]
  116. }
  117. // 分类加载完成后加载文章
  118. this.loadArticles()
  119. },
  120. onCategoryChange(id) {
  121. this.currentCategory = id
  122. this.page = 1
  123. this.articles = []
  124. this.noMore = false
  125. this.loadArticles()
  126. },
  127. async loadArticles() {
  128. if (this.loading) return
  129. this.loading = true
  130. try {
  131. let params = { page: this.page, size: this.size }
  132. if (this.currentCategory) {
  133. params.categoryId = this.currentCategory
  134. }
  135. const res = await getArticleList(params)
  136. if (res.code === 200 && res.data) {
  137. let pageData = res.data
  138. let list = pageData.records || []
  139. let mapped = list.map(function(item) {
  140. return {
  141. id: item.id,
  142. title: item.title || '',
  143. summary: item.summary || '',
  144. content: item.content || '',
  145. coverImage: item.coverImage || '',
  146. author: item.author || '浠艾福',
  147. categoryName: item.categoryName || item.category || '',
  148. publishDate: item.publishedAt ? item.publishedAt.slice(0, 10) : '',
  149. readCount: item.viewCount || 0
  150. }
  151. })
  152. if (this.page === 1) {
  153. this.articles = mapped
  154. } else {
  155. this.articles = this.articles.concat(mapped)
  156. }
  157. this.total = pageData.total || 0
  158. this.noMore = this.articles.length >= this.total
  159. } else {
  160. if (this.page === 1) {
  161. this.articles = []
  162. }
  163. }
  164. } catch (e) {
  165. uni.showToast({ title: '加载失败', icon: 'none' })
  166. } finally {
  167. this.loading = false
  168. }
  169. },
  170. onLoadMore() {
  171. if (this.noMore || this.loading) return
  172. this.page++
  173. this.loadArticles()
  174. },
  175. goDetail(id) {
  176. if (this.isLoggedIn) {
  177. uni.navigateTo({ url: '/pages/mind/article-detail?id=' + id })
  178. } else {
  179. uni.navigateTo({ url: '/pages/login/login?redirect=' + encodeURIComponent('/pages/mind/articles') })
  180. }
  181. }
  182. }
  183. }
  184. </script>
  185. <style scoped>
  186. .container {
  187. display: flex;
  188. flex-direction: column;
  189. min-height: 100vh;
  190. background: #f5f7fa;
  191. }
  192. /* 分类筛选 */
  193. .filter-section {
  194. background: #fff;
  195. padding: 16rpx 0 12rpx;
  196. border-bottom: 1rpx solid #eee;
  197. }
  198. .category-scroll {
  199. white-space: nowrap;
  200. }
  201. .filter-chips {
  202. display: flex;
  203. padding: 0 20rpx;
  204. gap: 16rpx;
  205. }
  206. .filter-chip {
  207. display: inline-block;
  208. padding: 8rpx 24rpx;
  209. font-size: 24rpx;
  210. color: #666;
  211. background: #f0f0f0;
  212. border-radius: 30rpx;
  213. flex-shrink: 0;
  214. }
  215. .filter-chip.active {
  216. background: #5B9BD5;
  217. color: #fff;
  218. }
  219. /* 文章列表 */
  220. .article-list {
  221. flex: 1;
  222. height: calc(100vh - 220rpx);
  223. }
  224. .loading-wrap,
  225. .empty-wrap {
  226. display: flex;
  227. flex-direction: column;
  228. align-items: center;
  229. padding-top: 120rpx;
  230. }
  231. .loading-text {
  232. font-size: 26rpx;
  233. color: #999;
  234. }
  235. .empty-icon {
  236. font-size: 100rpx;
  237. margin-bottom: 24rpx;
  238. }
  239. .empty-text {
  240. font-size: 28rpx;
  241. color: #999;
  242. }
  243. .article-grid {
  244. padding: 20rpx 24rpx;
  245. }
  246. .article-card {
  247. background: #fff;
  248. border-radius: 16rpx;
  249. overflow: hidden;
  250. margin-bottom: 20rpx;
  251. box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.06);
  252. }
  253. .article-cover {
  254. width: 100%;
  255. height: 280rpx;
  256. background: linear-gradient(135deg, #667eea, #764ba2);
  257. }
  258. .article-body {
  259. padding: 20rpx;
  260. }
  261. .article-meta {
  262. display: flex;
  263. align-items: center;
  264. margin-bottom: 10rpx;
  265. }
  266. .article-category {
  267. font-size: 20rpx;
  268. color: #5B9BD5;
  269. background: rgba(91,155,213,0.1);
  270. padding: 2rpx 12rpx;
  271. border-radius: 8rpx;
  272. margin-right: 12rpx;
  273. }
  274. .article-date {
  275. font-size: 20rpx;
  276. color: #999;
  277. }
  278. .article-title {
  279. display: block;
  280. font-size: 28rpx;
  281. font-weight: bold;
  282. color: #333;
  283. margin-bottom: 8rpx;
  284. line-height: 1.4;
  285. }
  286. .article-summary {
  287. display: block;
  288. font-size: 24rpx;
  289. color: #666;
  290. line-height: 1.5;
  291. margin-bottom: 12rpx;
  292. overflow: hidden;
  293. text-overflow: ellipsis;
  294. display: -webkit-box;
  295. -webkit-line-clamp: 2;
  296. -webkit-box-orient: vertical;
  297. }
  298. .article-footer {
  299. display: flex;
  300. justify-content: space-between;
  301. align-items: center;
  302. }
  303. .article-author {
  304. font-size: 20rpx;
  305. color: #999;
  306. }
  307. .article-read-count {
  308. font-size: 20rpx;
  309. color: #bbb;
  310. }
  311. .loading-more,
  312. .no-more {
  313. text-align: center;
  314. padding: 30rpx;
  315. }
  316. .no-more-text {
  317. font-size: 24rpx;
  318. color: #ccc;
  319. }
  320. /* 未登录提示 */
  321. .login-hint-bar {
  322. text-align: center;
  323. padding: 20rpx;
  324. background: #fef9e7;
  325. border-top: 1rpx solid #f0e6c0;
  326. margin: 20rpx 24rpx 0;
  327. border-radius: 12rpx;
  328. }
  329. .login-hint-text {
  330. font-size: 24rpx;
  331. color: #b8860b;
  332. }
  333. /* 底部占位 */
  334. .bottom-spacer {
  335. height: 120rpx;
  336. }
  337. </style>