articles.vue 18 KB

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