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="getImageUrl(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. import config from '@/config.js'
  121. var dimensionConfig = {
  122. all: { label: '全部', color: '#64748B', theme: 'mind-wisdom', tagline: '探索知识,滋养成长', quote: '学而时习之,不亦说乎' },
  123. body: { label: '身', color: '#FF8C42', theme: 'health', tagline: '主动健康,从知识开始', quote: '健康是人生的第一财富' },
  124. mind: { label: '心', color: '#FF6B9D', theme: 'mind-wisdom', tagline: '探索内心世界,培养健康心智', quote: '知己知彼,百战不殆' },
  125. wisdom: { label: '智', color: '#6366F1', theme: 'wisdom', tagline: '聪明学习,快乐成长', quote: '学而不思则罔,思而不学则殆' },
  126. action:{ label: '行', color: '#10B981', theme: 'action', tagline: '知行合一,方得始终', quote: '纸上得来终觉浅,绝知此事要躬行' },
  127. wealth:{ label: '富', color: '#F59E0B', theme: 'wealth', tagline: '理财智慧,从小培养', quote: '君子爱财,取之有道' }
  128. }
  129. export default {
  130. components: { PageBanner },
  131. data() {
  132. return {
  133. currentDimension: 'all',
  134. categoryList: [{ id: '', name: '全部' }],
  135. categoryMap: {},
  136. currentCategory: '',
  137. articles: [],
  138. featuredArticles: [],
  139. page: 1,
  140. size: 10,
  141. total: 0,
  142. loading: false,
  143. loadingMore: false,
  144. noMore: false,
  145. isLoggedIn: false,
  146. searchKeyword: '',
  147. searchFocused: 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. if (this.searchKeyword && this.searchKeyword.trim()) {
  259. params.keyword = this.searchKeyword.trim()
  260. }
  261. const res = await getArticleList(params)
  262. if (res.code === 200 && res.data) {
  263. var pageData = res.data
  264. var list = pageData.records || []
  265. var mapped = list.map(function(item) {
  266. return {
  267. id: item.id,
  268. title: item.title || '',
  269. summary: item.summary || '',
  270. content: item.content || '',
  271. coverImage: item.coverImage || '',
  272. author: item.author || '浠艾福',
  273. categoryName: item.categoryName || item.category || '',
  274. publishDate: item.publishedAt ? item.publishedAt.slice(0, 10) : '',
  275. readCount: item.viewCount || 0,
  276. contentType: item.contentType || '',
  277. difficultyLevel: item.difficultyLevel || 0
  278. }
  279. })
  280. if (this.page === 1) {
  281. this.articles = mapped
  282. } else {
  283. this.articles = this.articles.concat(mapped)
  284. }
  285. this.total = pageData.total || 0
  286. this.noMore = this.articles.length >= this.total
  287. } else {
  288. if (this.page === 1) {
  289. this.articles = []
  290. }
  291. }
  292. } catch (e) {
  293. uni.showToast({ title: '加载失败', icon: 'none' })
  294. } finally {
  295. this.loading = false
  296. }
  297. },
  298. onLoadMore() {
  299. if (this.noMore || this.loading) return
  300. this.page++
  301. this.loadArticles()
  302. },
  303. goDetail(id) {
  304. uni.navigateTo({ url: '/pages/article-center/article-detail?id=' + id })
  305. },
  306. goCreate() {
  307. if (this.isLoggedIn) {
  308. uni.navigateTo({ url: '/pages/article-center/article-edit' })
  309. } else {
  310. uni.navigateTo({ url: '/pages/login/login?redirect=' + encodeURIComponent('/pages/mind-extra/articles') })
  311. }
  312. },
  313. onBack() {
  314. uni.navigateBack()
  315. },
  316. focusSearch: function() {
  317. this.searchFocused = true
  318. },
  319. onSearch: function() {
  320. this.page = 1
  321. this.articles = []
  322. this.noMore = false
  323. this.loadArticles()
  324. },
  325. onSearchBlur: function() {
  326. if (!this.searchKeyword) {
  327. this.searchFocused = false
  328. }
  329. },
  330. getImageUrl: function(path) {
  331. return config.API_BASE_URL + path
  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>