articles.vue 9.3 KB

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