articles.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. <template>
  2. <view class="container">
  3. <scroll-view scroll-y class="page-scroll" @scrolltolower="onLoadMore">
  4. <!-- 品牌头部 -->
  5. <PageBanner :theme="bannerTheme"
  6. :tagline="bannerTagline"
  7. :quote="bannerQuote" />
  8. <!-- 推荐阅读 -->
  9. <view class="featured-section" v-if="featuredArticles.length > 0">
  10. <view class="section-header">
  11. <text class="section-title">🔥 推荐阅读</text>
  12. <text class="section-subtitle">精选成长好文</text>
  13. </view>
  14. <view class="featured-grid">
  15. <view
  16. v-for="article in featuredArticles"
  17. :key="article.id"
  18. class="featured-card"
  19. @click="goDetail(article.id)"
  20. >
  21. <view class="featured-category" :style="{ background: article.categoryColor }">
  22. <text class="featured-category-text">{{ article.category }}</text>
  23. </view>
  24. <text class="featured-title">{{ article.title }}</text>
  25. <text class="featured-summary">{{ article.summary }}</text>
  26. <view class="featured-meta">
  27. <text class="featured-views">👁 {{ article.views }}</text>
  28. <text class="featured-date">{{ article.date }}</text>
  29. </view>
  30. </view>
  31. </view>
  32. </view>
  33. <!-- 维度筛选 -->
  34. <view class="filter-section">
  35. <scroll-view scroll-x enable-flex show-scrollbar="false" class="dimension-scroll">
  36. <view class="filter-chips">
  37. <view
  38. v-for="dim in dimensionList"
  39. :key="dim.code"
  40. :class="['filter-chip', currentDimension === dim.code ? 'active' : '']"
  41. :style="dim.activeStyle"
  42. @click="onDimensionChange(dim.code)"
  43. >
  44. {{ dim.label }}
  45. </view>
  46. </view>
  47. </scroll-view>
  48. </view>
  49. <!-- 分类筛选 -->
  50. <view class="filter-section" v-if="categoryList.length > 1">
  51. <scroll-view scroll-x enable-flex show-scrollbar="false" class="category-scroll">
  52. <view class="filter-chips">
  53. <view
  54. v-for="cat in categoryList"
  55. :key="cat.value"
  56. :class="['filter-chip', currentCategory === cat.value ? 'active' : '']"
  57. @click="onCategoryChange(cat.value)"
  58. >
  59. {{ cat.label }}
  60. </view>
  61. </view>
  62. </scroll-view>
  63. </view>
  64. <!-- 全部文章 -->
  65. <view class="all-articles-section">
  66. <view class="section-header">
  67. <text class="section-title">全部文章</text>
  68. <text class="section-subtitle" v-if="total > 0">共 {{ total }} 篇</text>
  69. </view>
  70. <view v-if="loading && articles.length === 0" class="loading-wrap">
  71. <text class="loading-text">加载中...</text>
  72. </view>
  73. <view v-else-if="articles.length === 0" class="empty-wrap">
  74. <text class="empty-icon">📚</text>
  75. <text class="empty-text">暂无相关知识</text>
  76. </view>
  77. <view v-else class="article-grid">
  78. <view
  79. v-for="item in articles"
  80. :key="item.id"
  81. class="article-card"
  82. @click="goDetail(item.id)"
  83. >
  84. <image
  85. class="article-cover"
  86. :src="item.coverImage || '/static/default-article.png'"
  87. mode="aspectFill"
  88. />
  89. <view class="article-body">
  90. <view class="article-meta">
  91. <text class="article-category">{{ item.categoryName || '知识' }}</text>
  92. <view class="article-type-tags">
  93. <text v-if="item.contentType" class="type-tag" :class="'type-' + item.contentType">{{ contentTypeLabel(item.contentType) }}</text>
  94. <text v-if="item.difficultyLevel > 0" class="difficulty-tag">{{ '★'.repeat(item.difficultyLevel) }}</text>
  95. </view>
  96. </view>
  97. <text class="article-title">{{ item.title }}</text>
  98. <text class="article-summary">{{ item.summary || item.content }}</text>
  99. <view class="article-footer">
  100. <text class="article-author">{{ item.author || '浠艾福' }}</text>
  101. <text class="article-read-count">阅读 {{ item.readCount || 0 }}</text>
  102. </view>
  103. </view>
  104. </view>
  105. </view>
  106. <view v-if="loadingMore" class="loading-more">
  107. <text class="loading-text">加载中...</text>
  108. </view>
  109. <view v-if="noMore && articles.length > 0" class="no-more">
  110. <text class="no-more-text">— 没有更多了 —</text>
  111. </view>
  112. <view v-if="!isLoggedIn" class="login-hint-bar">
  113. <text class="login-hint-text">🔒 登录后解锁全部内容</text>
  114. </view>
  115. </view>
  116. <view class="bottom-spacer"></view>
  117. </scroll-view>
  118. </view>
  119. </template>
  120. <script>
  121. import PageBanner from '../../components/PageBanner.vue'
  122. import { getArticleCategories, getArticleList, getFeaturedArticles } from '../../utils/api.js'
  123. var dimensionConfig = {
  124. all: { label: '全部', color: '#64748B', theme: 'mind-wisdom', tagline: '探索知识,滋养成长', quote: '学而时习之,不亦说乎' },
  125. body: { label: '身', color: '#FF8C42', theme: 'health', tagline: '主动健康,从知识开始', quote: '健康是人生的第一财富' },
  126. mind: { label: '心', color: '#FF6B9D', theme: 'mind-wisdom', tagline: '探索内心世界,培养健康心智', quote: '知己知彼,百战不殆' },
  127. wisdom: { label: '智', color: '#6366F1', theme: 'wisdom', tagline: '聪明学习,快乐成长', quote: '学而不思则罔,思而不学则殆' },
  128. action:{ label: '行', color: '#10B981', theme: 'action', tagline: '知行合一,方得始终', quote: '纸上得来终觉浅,绝知此事要躬行' },
  129. wealth:{ label: '富', color: '#F59E0B', theme: 'wealth', tagline: '理财智慧,从小培养', quote: '君子爱财,取之有道' }
  130. }
  131. export default {
  132. components: { PageBanner },
  133. data() {
  134. return {
  135. currentDimension: 'all',
  136. categoryList: [{ id: '', name: '全部' }],
  137. categoryMap: {},
  138. currentCategory: '',
  139. articles: [],
  140. featuredArticles: [],
  141. page: 1,
  142. size: 10,
  143. total: 0,
  144. loading: false,
  145. loadingMore: false,
  146. noMore: false,
  147. isLoggedIn: false
  148. }
  149. },
  150. computed: {
  151. dimensionList: function() {
  152. var self = this
  153. return Object.keys(dimensionConfig).map(function(k) {
  154. var cfg = dimensionConfig[k]
  155. var activeStyle = ''
  156. if (k === self.currentDimension) {
  157. activeStyle = 'background:' + cfg.color + ';color:#fff;'
  158. }
  159. return { code: k, label: cfg.label, color: cfg.color, activeStyle: activeStyle }
  160. })
  161. },
  162. bannerTheme: function() {
  163. return dimensionConfig[this.currentDimension] ? dimensionConfig[this.currentDimension].theme : 'mind-wisdom'
  164. },
  165. bannerTagline: function() {
  166. return dimensionConfig[this.currentDimension] ? dimensionConfig[this.currentDimension].tagline : ''
  167. },
  168. bannerQuote: function() {
  169. return dimensionConfig[this.currentDimension] ? dimensionConfig[this.currentDimension].quote : ''
  170. }
  171. },
  172. onLoad: function(options) {
  173. this.isLoggedIn = !!uni.getStorageSync('token')
  174. this.loadFeaturedArticles()
  175. if (options && options.dimension && dimensionConfig[options.dimension]) {
  176. this.currentDimension = options.dimension
  177. }
  178. this.loadCategories()
  179. },
  180. onShow: function() {
  181. this.isLoggedIn = !!uni.getStorageSync('token')
  182. },
  183. methods: {
  184. loadFeaturedArticles: function() {
  185. var self = this
  186. getFeaturedArticles({ size: 5 }).then(function(res) {
  187. if (res.code === 200 && res.data) {
  188. var gradientColors = [
  189. 'linear-gradient(135deg, #6366F1, #818CF8)',
  190. 'linear-gradient(135deg, #8B5CF6, #A78BFA)',
  191. 'linear-gradient(135deg, #5B9BD5, #8FC5E8)',
  192. 'linear-gradient(135deg, #10B981, #34D399)',
  193. 'linear-gradient(135deg, #F97316, #FB923C)'
  194. ]
  195. var list = Array.isArray(res.data) ? res.data : (res.data.records || [])
  196. self.featuredArticles = list.map(function(item, index) {
  197. return {
  198. id: item.id,
  199. category: item.categoryName || '成长文章',
  200. categoryColor: gradientColors[index % gradientColors.length],
  201. title: item.title || '',
  202. summary: item.summary || '',
  203. views: item.viewCount ? String(item.viewCount) : '0',
  204. date: item.publishedAt ? item.publishedAt.slice(0, 10) : ''
  205. }
  206. })
  207. }
  208. }).catch(function() {})
  209. },
  210. contentTypeLabel: function(type) {
  211. var labels = { article: '文章', knowledge: '知识点', course: '课程', tip: '贴士' }
  212. return labels[type] || type
  213. },
  214. async loadCategories() {
  215. try {
  216. const res = await getArticleCategories()
  217. if (res.code === 200 && res.data) {
  218. const cats = Array.isArray(res.data) ? res.data : []
  219. var map = {}
  220. cats.forEach(function(c) { map[c.id] = c.name })
  221. this.categoryMap = map
  222. this.categoryList = [{ id: '', name: '全部' }].concat(cats)
  223. }
  224. } catch (e) {
  225. this.categoryList = [
  226. { id: '', name: '全部' },
  227. { id: 1, name: '心理健康' },
  228. { id: 2, name: '情绪管理' }
  229. ]
  230. }
  231. this.loadArticles()
  232. },
  233. onDimensionChange: function(code) {
  234. this.currentDimension = code
  235. this.page = 1
  236. this.articles = []
  237. this.noMore = false
  238. this.loadArticles()
  239. },
  240. onCategoryChange: function(id) {
  241. this.currentCategory = id
  242. this.page = 1
  243. this.articles = []
  244. this.noMore = false
  245. this.loadArticles()
  246. },
  247. async loadArticles() {
  248. if (this.loading) return
  249. this.loading = true
  250. try {
  251. var params = { page: this.page, size: this.size }
  252. if (this.currentCategory) {
  253. params.categoryId = this.currentCategory
  254. }
  255. if (this.currentDimension !== 'all') {
  256. params.dimensionCode = this.currentDimension
  257. }
  258. const res = await getArticleList(params)
  259. if (res.code === 200 && res.data) {
  260. var pageData = res.data
  261. var list = pageData.records || []
  262. var mapped = list.map(function(item) {
  263. return {
  264. id: item.id,
  265. title: item.title || '',
  266. summary: item.summary || '',
  267. content: item.content || '',
  268. coverImage: item.coverImage || '',
  269. author: item.author || '浠艾福',
  270. categoryName: item.categoryName || item.category || '',
  271. publishDate: item.publishedAt ? item.publishedAt.slice(0, 10) : '',
  272. readCount: item.viewCount || 0,
  273. contentType: item.contentType || '',
  274. difficultyLevel: item.difficultyLevel || 0
  275. }
  276. })
  277. if (this.page === 1) {
  278. this.articles = mapped
  279. } else {
  280. this.articles = this.articles.concat(mapped)
  281. }
  282. this.total = pageData.total || 0
  283. this.noMore = this.articles.length >= this.total
  284. } else {
  285. if (this.page === 1) {
  286. this.articles = []
  287. }
  288. }
  289. } catch (e) {
  290. uni.showToast({ title: '加载失败', icon: 'none' })
  291. } finally {
  292. this.loading = false
  293. }
  294. },
  295. onLoadMore() {
  296. if (this.noMore || this.loading) return
  297. this.page++
  298. this.loadArticles()
  299. },
  300. goDetail(id) {
  301. if (this.isLoggedIn) {
  302. uni.navigateTo({ url: '/pages/mind-detail/article-detail?id=' + id })
  303. } else {
  304. uni.navigateTo({ url: '/pages/login/login?redirect=' + encodeURIComponent('/pages/mind/articles') })
  305. }
  306. }
  307. }
  308. }
  309. </script>
  310. <style scoped>
  311. .container {
  312. display: flex;
  313. flex-direction: column;
  314. height: 100vh;
  315. background: #f5f7fa;
  316. }
  317. /* 页面滚动容器 */
  318. .page-scroll {
  319. flex: 1;
  320. height: 100vh;
  321. }
  322. /* 区块通用标题 */
  323. .section-header {
  324. display: flex;
  325. align-items: baseline;
  326. padding: 0 4rpx;
  327. margin-bottom: 20rpx;
  328. }
  329. .section-title {
  330. font-size: 30rpx;
  331. font-weight: bold;
  332. color: #333;
  333. }
  334. .section-subtitle {
  335. font-size: 24rpx;
  336. color: #999;
  337. margin-left: 12rpx;
  338. }
  339. /* ===== 推荐阅读 ===== */
  340. .featured-section {
  341. margin: 24rpx 24rpx 16rpx;
  342. padding: 0 4rpx;
  343. }
  344. .featured-grid {
  345. display: flex;
  346. flex-direction: column;
  347. }
  348. .featured-card {
  349. background: #fff;
  350. border-radius: 20rpx;
  351. padding: 28rpx 24rpx;
  352. box-shadow: 0 2rpx 12rpx rgba(0,0,0,0.06);
  353. margin-bottom: 20rpx;
  354. }
  355. .featured-card:active {
  356. opacity: 0.8;
  357. }
  358. .featured-category {
  359. display: inline-block;
  360. padding: 4rpx 18rpx;
  361. border-radius: 20rpx;
  362. margin-bottom: 14rpx;
  363. }
  364. .featured-category-text {
  365. font-size: 20rpx;
  366. color: #fff;
  367. font-weight: 500;
  368. }
  369. .featured-title {
  370. font-size: 28rpx;
  371. font-weight: bold;
  372. color: #333;
  373. display: block;
  374. margin-bottom: 10rpx;
  375. line-height: 1.4;
  376. }
  377. .featured-summary {
  378. font-size: 24rpx;
  379. color: #888;
  380. line-height: 1.5;
  381. display: block;
  382. margin-bottom: 16rpx;
  383. overflow: hidden;
  384. text-overflow: ellipsis;
  385. display: -webkit-box;
  386. -webkit-line-clamp: 2;
  387. -webkit-box-orient: vertical;
  388. }
  389. .featured-meta {
  390. display: flex;
  391. flex-direction: row;
  392. align-items: center;
  393. }
  394. .featured-views {
  395. font-size: 22rpx;
  396. color: #bbb;
  397. margin-right: 20rpx;
  398. }
  399. .featured-date {
  400. font-size: 22rpx;
  401. color: #bbb;
  402. margin-left: auto;
  403. }
  404. /* ===== 全部文章 ===== */
  405. .all-articles-section {
  406. margin: 8rpx 24rpx 0;
  407. padding: 0 4rpx;
  408. }
  409. /* 筛选条 */
  410. .filter-section {
  411. background: #fff;
  412. padding: 16rpx 0 12rpx;
  413. border-bottom: 1rpx solid #eee;
  414. }
  415. .dimension-scroll,
  416. .category-scroll {
  417. white-space: nowrap;
  418. }
  419. .dimension-scroll {
  420. border-bottom: 1rpx solid #f0f0f0;
  421. padding-bottom: 12rpx;
  422. }
  423. .filter-chips {
  424. display: flex;
  425. padding: 0 20rpx;
  426. gap: 16rpx;
  427. }
  428. .filter-chip {
  429. display: inline-block;
  430. padding: 8rpx 24rpx;
  431. font-size: 24rpx;
  432. color: #666;
  433. background: #f0f0f0;
  434. border-radius: 30rpx;
  435. flex-shrink: 0;
  436. }
  437. .filter-chip.active {
  438. background: #5B9BD5;
  439. color: #fff;
  440. }
  441. .filter-chip:first-child {
  442. margin-left: 4rpx;
  443. }
  444. /* 文章元信息标签 */
  445. .article-meta {
  446. display: flex;
  447. align-items: center;
  448. flex-wrap: wrap;
  449. margin-bottom: 10rpx;
  450. }
  451. .article-type-tags {
  452. display: flex;
  453. align-items: center;
  454. gap: 8rpx;
  455. margin-left: auto;
  456. }
  457. .type-tag {
  458. font-size: 18rpx;
  459. padding: 2rpx 10rpx;
  460. border-radius: 6rpx;
  461. background: #f0f0f0;
  462. color: #666;
  463. }
  464. .type-tag.type-article {
  465. background: #e8f4fd;
  466. color: #2b7fc4;
  467. }
  468. .type-tag.type-knowledge {
  469. background: #e8f8ee;
  470. color: #2a9055;
  471. }
  472. .type-tag.type-course {
  473. background: #fef3e2;
  474. color: #c47a2b;
  475. }
  476. .type-tag.type-tip {
  477. background: #f3e8fd;
  478. color: #7b2bc4;
  479. }
  480. .difficulty-tag {
  481. font-size: 18rpx;
  482. color: #f59e0b;
  483. }
  484. .loading-wrap,
  485. .empty-wrap {
  486. display: flex;
  487. flex-direction: column;
  488. align-items: center;
  489. padding-top: 120rpx;
  490. }
  491. .loading-text {
  492. font-size: 26rpx;
  493. color: #999;
  494. }
  495. .empty-icon {
  496. font-size: 100rpx;
  497. margin-bottom: 24rpx;
  498. }
  499. .empty-text {
  500. font-size: 28rpx;
  501. color: #999;
  502. }
  503. .article-grid {
  504. padding: 20rpx 24rpx;
  505. }
  506. .article-card {
  507. background: #fff;
  508. border-radius: 16rpx;
  509. overflow: hidden;
  510. margin-bottom: 20rpx;
  511. box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.06);
  512. }
  513. .article-cover {
  514. width: 100%;
  515. height: 280rpx;
  516. background: linear-gradient(135deg, #667eea, #764ba2);
  517. }
  518. .article-body {
  519. padding: 20rpx;
  520. }
  521. .article-category {
  522. font-size: 20rpx;
  523. color: #5B9BD5;
  524. background: rgba(91,155,213,0.1);
  525. padding: 2rpx 12rpx;
  526. border-radius: 8rpx;
  527. margin-right: 12rpx;
  528. }
  529. .article-date {
  530. font-size: 20rpx;
  531. color: #999;
  532. }
  533. .article-title {
  534. display: block;
  535. font-size: 28rpx;
  536. font-weight: bold;
  537. color: #333;
  538. margin-bottom: 8rpx;
  539. line-height: 1.4;
  540. }
  541. .article-summary {
  542. display: block;
  543. font-size: 24rpx;
  544. color: #666;
  545. line-height: 1.5;
  546. margin-bottom: 12rpx;
  547. overflow: hidden;
  548. text-overflow: ellipsis;
  549. display: -webkit-box;
  550. -webkit-line-clamp: 2;
  551. -webkit-box-orient: vertical;
  552. }
  553. .article-footer {
  554. display: flex;
  555. justify-content: space-between;
  556. align-items: center;
  557. }
  558. .article-author {
  559. font-size: 20rpx;
  560. color: #999;
  561. }
  562. .article-read-count {
  563. font-size: 20rpx;
  564. color: #bbb;
  565. }
  566. .loading-more,
  567. .no-more {
  568. text-align: center;
  569. padding: 30rpx;
  570. }
  571. .no-more-text {
  572. font-size: 24rpx;
  573. color: #ccc;
  574. }
  575. /* 未登录提示 */
  576. .login-hint-bar {
  577. text-align: center;
  578. padding: 20rpx;
  579. background: #fef9e7;
  580. border-top: 1rpx solid #f0e6c0;
  581. margin: 20rpx 24rpx 0;
  582. border-radius: 12rpx;
  583. }
  584. .login-hint-text {
  585. font-size: 24rpx;
  586. color: #b8860b;
  587. }
  588. /* 底部占位 */
  589. .bottom-spacer {
  590. height: 120rpx;
  591. }
  592. </style>