articles.vue 18 KB

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