articles.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  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. uni.navigateTo({ url: '/pages/article-center/article-detail?id=' + id })
  304. },
  305. goCreate() {
  306. if (this.isLoggedIn) {
  307. uni.navigateTo({ url: '/pages/article-center/article-edit' })
  308. } else {
  309. uni.navigateTo({ url: '/pages/login/login?redirect=' + encodeURIComponent('/pages/mind-extra/articles') })
  310. }
  311. },
  312. onBack() {
  313. uni.navigateBack()
  314. },
  315. focusSearch: function() {
  316. this.searchFocused = true
  317. },
  318. onSearch: function() {
  319. this.page = 1
  320. this.articles = []
  321. this.noMore = false
  322. this.loadArticles()
  323. },
  324. onSearchBlur: function() {
  325. if (!this.searchKeyword) {
  326. this.searchFocused = false
  327. }
  328. }
  329. }
  330. }
  331. </script>
  332. <style scoped>
  333. .container {
  334. display: flex;
  335. flex-direction: column;
  336. height: 100vh;
  337. background: #f5f7fa;
  338. }
  339. /* 自定义导航栏 */
  340. .nav-header {
  341. display: flex;
  342. align-items: center;
  343. justify-content: space-between;
  344. padding: 80rpx 24rpx 16rpx;
  345. background: linear-gradient(135deg, #667eea, #764ba2);
  346. position: relative;
  347. z-index: 20;
  348. }
  349. .nav-left {
  350. width: 60rpx;
  351. height: 60rpx;
  352. display: flex;
  353. align-items: center;
  354. justify-content: center;
  355. }
  356. .nav-back-icon {
  357. font-size: 36rpx;
  358. color: #fff;
  359. font-weight: bold;
  360. }
  361. .nav-search {
  362. flex: 1;
  363. display: flex;
  364. align-items: center;
  365. background: rgba(255,255,255,0.25);
  366. border-radius: 32rpx;
  367. padding: 10rpx 20rpx;
  368. margin: 0 16rpx;
  369. }
  370. .nav-search-icon {
  371. font-size: 24rpx;
  372. margin-right: 10rpx;
  373. flex-shrink: 0;
  374. }
  375. .nav-search-placeholder {
  376. font-size: 24rpx;
  377. color: rgba(255,255,255,0.7);
  378. }
  379. .nav-search-input {
  380. flex: 1;
  381. font-size: 24rpx;
  382. color: #fff;
  383. background: transparent;
  384. }
  385. .nav-search-input::placeholder {
  386. color: rgba(255,255,255,0.7);
  387. }
  388. .nav-right {
  389. width: 60rpx;
  390. height: 60rpx;
  391. display: flex;
  392. align-items: center;
  393. justify-content: center;
  394. }
  395. .nav-add-btn {
  396. font-size: 48rpx;
  397. color: #fff;
  398. font-weight: 300;
  399. line-height: 1;
  400. }
  401. /* 页面滚动容器 */
  402. .page-scroll {
  403. flex: 1;
  404. }
  405. /* 区块通用标题 */
  406. .section-header {
  407. display: flex;
  408. align-items: baseline;
  409. padding: 0 4rpx;
  410. margin-bottom: 20rpx;
  411. }
  412. .section-title {
  413. font-size: 30rpx;
  414. font-weight: bold;
  415. color: #333;
  416. }
  417. .section-subtitle {
  418. font-size: 24rpx;
  419. color: #999;
  420. margin-left: 12rpx;
  421. }
  422. /* ===== 推荐阅读 ===== */
  423. .featured-section {
  424. margin: 24rpx 24rpx 16rpx;
  425. padding: 0 4rpx;
  426. }
  427. .featured-grid {
  428. display: flex;
  429. flex-direction: column;
  430. }
  431. .featured-card {
  432. background: #fff;
  433. border-radius: 20rpx;
  434. padding: 28rpx 24rpx;
  435. box-shadow: 0 2rpx 12rpx rgba(0,0,0,0.06);
  436. margin-bottom: 20rpx;
  437. }
  438. .featured-card:active {
  439. opacity: 0.8;
  440. }
  441. .featured-category {
  442. display: inline-block;
  443. padding: 4rpx 18rpx;
  444. border-radius: 20rpx;
  445. margin-bottom: 14rpx;
  446. }
  447. .featured-category-text {
  448. font-size: 20rpx;
  449. color: #fff;
  450. font-weight: 500;
  451. }
  452. .featured-title {
  453. font-size: 28rpx;
  454. font-weight: bold;
  455. color: #333;
  456. display: block;
  457. margin-bottom: 10rpx;
  458. line-height: 1.4;
  459. }
  460. .featured-summary {
  461. font-size: 24rpx;
  462. color: #888;
  463. line-height: 1.5;
  464. display: block;
  465. margin-bottom: 16rpx;
  466. overflow: hidden;
  467. text-overflow: ellipsis;
  468. display: -webkit-box;
  469. -webkit-line-clamp: 2;
  470. -webkit-box-orient: vertical;
  471. }
  472. .featured-meta {
  473. display: flex;
  474. flex-direction: row;
  475. align-items: center;
  476. }
  477. .featured-views {
  478. font-size: 22rpx;
  479. color: #bbb;
  480. margin-right: 20rpx;
  481. }
  482. .featured-date {
  483. font-size: 22rpx;
  484. color: #bbb;
  485. margin-left: auto;
  486. }
  487. /* ===== 全部文章 ===== */
  488. .all-articles-section {
  489. margin: 8rpx 24rpx 0;
  490. padding: 0 4rpx;
  491. }
  492. /* 筛选条 */
  493. .filter-section {
  494. background: #fff;
  495. padding: 16rpx 0 12rpx;
  496. border-bottom: 1rpx solid #eee;
  497. }
  498. .dimension-scroll,
  499. .category-scroll {
  500. white-space: nowrap;
  501. }
  502. .dimension-scroll {
  503. border-bottom: 1rpx solid #f0f0f0;
  504. padding-bottom: 12rpx;
  505. }
  506. .filter-chips {
  507. display: flex;
  508. padding: 0 20rpx;
  509. gap: 16rpx;
  510. }
  511. .filter-chip {
  512. display: inline-block;
  513. padding: 8rpx 24rpx;
  514. font-size: 24rpx;
  515. color: #666;
  516. background: #f0f0f0;
  517. border-radius: 30rpx;
  518. flex-shrink: 0;
  519. }
  520. .filter-chip.active {
  521. background: #5B9BD5;
  522. color: #fff;
  523. }
  524. .filter-chip:first-child {
  525. margin-left: 4rpx;
  526. }
  527. /* 文章元信息标签 */
  528. .article-meta {
  529. display: flex;
  530. align-items: center;
  531. flex-wrap: wrap;
  532. margin-bottom: 10rpx;
  533. }
  534. .article-type-tags {
  535. display: flex;
  536. align-items: center;
  537. gap: 8rpx;
  538. margin-left: auto;
  539. }
  540. .type-tag {
  541. font-size: 18rpx;
  542. padding: 2rpx 10rpx;
  543. border-radius: 6rpx;
  544. background: #f0f0f0;
  545. color: #666;
  546. }
  547. .type-tag.type-article {
  548. background: #e8f4fd;
  549. color: #2b7fc4;
  550. }
  551. .type-tag.type-knowledge {
  552. background: #e8f8ee;
  553. color: #2a9055;
  554. }
  555. .type-tag.type-course {
  556. background: #fef3e2;
  557. color: #c47a2b;
  558. }
  559. .type-tag.type-tip {
  560. background: #f3e8fd;
  561. color: #7b2bc4;
  562. }
  563. .difficulty-tag {
  564. font-size: 18rpx;
  565. color: #f59e0b;
  566. }
  567. .loading-wrap,
  568. .empty-wrap {
  569. display: flex;
  570. flex-direction: column;
  571. align-items: center;
  572. padding-top: 120rpx;
  573. }
  574. .loading-text {
  575. font-size: 26rpx;
  576. color: #999;
  577. }
  578. .empty-icon {
  579. font-size: 100rpx;
  580. margin-bottom: 24rpx;
  581. }
  582. .empty-text {
  583. font-size: 28rpx;
  584. color: #999;
  585. }
  586. .article-grid {
  587. padding: 20rpx 24rpx;
  588. }
  589. .article-card {
  590. background: #fff;
  591. border-radius: 16rpx;
  592. overflow: hidden;
  593. margin-bottom: 20rpx;
  594. box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.06);
  595. }
  596. .article-cover {
  597. width: 100%;
  598. height: 280rpx;
  599. background: linear-gradient(135deg, #667eea, #764ba2);
  600. }
  601. .article-body {
  602. padding: 20rpx;
  603. }
  604. .article-category {
  605. font-size: 20rpx;
  606. color: #5B9BD5;
  607. background: rgba(91,155,213,0.1);
  608. padding: 2rpx 12rpx;
  609. border-radius: 8rpx;
  610. margin-right: 12rpx;
  611. }
  612. .article-date {
  613. font-size: 20rpx;
  614. color: #999;
  615. }
  616. .article-title {
  617. display: block;
  618. font-size: 28rpx;
  619. font-weight: bold;
  620. color: #333;
  621. margin-bottom: 8rpx;
  622. line-height: 1.4;
  623. }
  624. .article-summary {
  625. display: block;
  626. font-size: 24rpx;
  627. color: #666;
  628. line-height: 1.5;
  629. margin-bottom: 12rpx;
  630. overflow: hidden;
  631. text-overflow: ellipsis;
  632. display: -webkit-box;
  633. -webkit-line-clamp: 2;
  634. -webkit-box-orient: vertical;
  635. }
  636. .article-footer {
  637. display: flex;
  638. justify-content: space-between;
  639. align-items: center;
  640. }
  641. .article-author {
  642. font-size: 20rpx;
  643. color: #999;
  644. }
  645. .article-read-count {
  646. font-size: 20rpx;
  647. color: #bbb;
  648. }
  649. .loading-more,
  650. .no-more {
  651. text-align: center;
  652. padding: 30rpx;
  653. }
  654. .no-more-text {
  655. font-size: 24rpx;
  656. color: #ccc;
  657. }
  658. /* 未登录提示 */
  659. .login-hint-bar {
  660. text-align: center;
  661. padding: 20rpx;
  662. background: #fef9e7;
  663. border-top: 1rpx solid #f0e6c0;
  664. margin: 20rpx 24rpx 0;
  665. border-radius: 12rpx;
  666. }
  667. .login-hint-text {
  668. font-size: 24rpx;
  669. color: #b8860b;
  670. }
  671. /* 底部占位 */
  672. .bottom-spacer {
  673. height: 120rpx;
  674. }
  675. </style>