index.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  1. <template>
  2. <view class="ac-container">
  3. <!-- 自定义导航栏 -->
  4. <view class="ac-nav">
  5. <view class="ac-nav-left" @click="onBack">
  6. <text class="ac-nav-back">&#x2190;</text>
  7. </view>
  8. <text class="ac-nav-title">知识中心</text>
  9. <view class="ac-nav-right">
  10. <text class="ac-nav-search-icon" @click="focusSearch">&#x1F50D;</text>
  11. <text class="ac-nav-add" @click="goEdit">+</text>
  12. </view>
  13. </view>
  14. <!-- 搜索栏(聚焦时展开) -->
  15. <view v-if="searchFocused || searchKeyword" class="ac-search-bar">
  16. <input
  17. class="ac-search-input"
  18. v-model="searchKeyword"
  19. placeholder="搜索文章..."
  20. confirm-type="search"
  21. @confirm="onSearch"
  22. @blur="onSearchBlur"
  23. />
  24. <text class="ac-search-cancel" @click="searchKeyword = ''; searchFocused = false; onSearch()">取消</text>
  25. </view>
  26. <!-- Tab 导航(药丸式) -->
  27. <view class="ac-tabs">
  28. <scroll-view scroll-x show-scrollbar="false" class="ac-tab-scroll">
  29. <view
  30. v-for="tab in tabList"
  31. :key="tab.code"
  32. :class="['ac-tab', currentTab === tab.code ? 'active' : '']"
  33. :style="currentTab === tab.code ? 'background:' + tab.color + ';color:#fff;border-color:' + tab.color : ''"
  34. @click="onTabChange(tab.code)"
  35. >
  36. {{ tab.name }}
  37. </view>
  38. </scroll-view>
  39. </view>
  40. <!-- 书册式推荐阅读 -->
  41. <ArticleBookshelf
  42. v-if="recommendedArticles.length > 0 || !recommendLoading"
  43. :articles="recommendedArticles"
  44. :loading="recommendLoading"
  45. :isLoggedIn="isLoggedIn"
  46. @articleClick="goDetail"
  47. @moreArticles="goExplore"
  48. />
  49. <!-- 文章列表区域 -->
  50. <scroll-view scroll-y class="ac-scroll" @scrolltolower="onLoadMore">
  51. <!-- 骨架屏 -->
  52. <view v-if="loading && articles.length === 0" class="ac-skeleton-wrap">
  53. <view v-for="n in 3" :key="n" class="sk-card">
  54. <view class="sk-cover"></view>
  55. <view class="sk-body">
  56. <view class="sk-row sk-tag"></view>
  57. <view class="sk-row sk-title"></view>
  58. <view class="sk-row sk-desc"></view>
  59. <view class="sk-row sk-footer"></view>
  60. </view>
  61. </view>
  62. </view>
  63. <!-- 空状态 -->
  64. <view v-else-if="articles.length === 0" class="ac-empty">
  65. <image class="ac-empty-img" src="/static/empty-article.png" mode="aspectFit"></image>
  66. <text class="ac-empty-text">暂无文章</text>
  67. </view>
  68. <!-- 文章列表 -->
  69. <view v-else class="ac-feed">
  70. <view
  71. v-for="item in articles"
  72. :key="item.id"
  73. class="ac-card"
  74. hover-class="ac-card-pressed"
  75. @click="goDetail(item.id)"
  76. >
  77. <view class="ac-card-media">
  78. <image
  79. class="ac-card-cover"
  80. :src="getImageUrl(item.coverImage) || '/static/default-article.png'"
  81. mode="aspectFill"
  82. />
  83. <view class="ac-card-dimension-tags" v-if="item.relatedDimensions">
  84. <view
  85. v-for="dim in parseDimensions(item.relatedDimensions)"
  86. :key="dim.code"
  87. class="ac-card-dim-tag"
  88. :style="'background:' + dim.color"
  89. >{{ dim.name }}</view>
  90. </view>
  91. </view>
  92. <view class="ac-card-body">
  93. <view class="ac-card-meta-top">
  94. <text class="ac-card-category" v-if="item.categoryName">{{ item.categoryName }}</text>
  95. <text class="ac-card-readtime">&#x1F4D6; {{ item.readTime || 3 }}分钟</text>
  96. </view>
  97. <text class="ac-card-title">{{ item.title }}</text>
  98. <text class="ac-card-summary" v-if="item.summary">{{ item.summary }}</text>
  99. <view class="ac-card-footer">
  100. <text class="ac-card-author">{{ item.author || '浠艾福' }}</text>
  101. <text class="ac-card-sep">|</text>
  102. <text class="ac-card-date">{{ formatDate(item.publishedAt) }}</text>
  103. <text class="ac-card-fav">&#x2B50; {{ item.favCount || 0 }}</text>
  104. </view>
  105. </view>
  106. </view>
  107. </view>
  108. <!-- 加载更多 / 没有更多 -->
  109. <view v-if="loadingMore" class="ac-loading-more">
  110. <text class="ac-loading-text">加载中...</text>
  111. </view>
  112. <view v-if="noMore && articles.length > 0" class="ac-no-more">
  113. <text class="ac-no-more-text">— 没有更多了 —</text>
  114. </view>
  115. <view class="ac-bottom-gap"></view>
  116. </scroll-view>
  117. <!-- 浮动发布按钮 -->
  118. <view class="ac-fab" @click="goEdit">
  119. <text class="ac-fab-icon">+</text>
  120. </view>
  121. </view>
  122. </template>
  123. <script>
  124. import { getArticleList, getFeaturedArticles } from '@/utils/api.js'
  125. import config from '@/config.js'
  126. export default {
  127. data() {
  128. return {
  129. tabList: [
  130. { code: '', name: '全部', color: '#F97316' },
  131. { code: 'body', name: '身', color: '#FF8C42' },
  132. { code: 'mind', name: '心', color: '#FF6B9D' },
  133. { code: 'wisdom', name: '智', color: '#6366F1' },
  134. { code: 'action', name: '行', color: '#10B981' },
  135. { code: 'wealth', name: '富', color: '#F59E0B' }
  136. ],
  137. currentTab: '',
  138. articles: [],
  139. page: 1,
  140. size: 10,
  141. total: 0,
  142. loading: false,
  143. loadingMore: false,
  144. noMore: false,
  145. searchKeyword: '',
  146. searchFocused: false,
  147. isLoggedIn: false,
  148. recommendedArticles: [],
  149. recommendLoading: false
  150. }
  151. },
  152. components: {
  153. ArticleBookshelf: () => require('@/components/ArticleBookshelf.vue')
  154. },
  155. onLoad(options) {
  156. // 支持从外部传入 dimension 参数,默认选中对应 tab
  157. if (options && options.dimension) {
  158. this.currentTab = options.dimension
  159. }
  160. this.isLoggedIn = !!uni.getStorageSync('token')
  161. this.loadArticles()
  162. this.loadRecommendedArticles()
  163. },
  164. onShow() {
  165. this.isLoggedIn = !!uni.getStorageSync('token')
  166. },
  167. methods: {
  168. async loadArticles(isLoadMore) {
  169. if (!isLoadMore) {
  170. if (this.loading) return
  171. this.loading = true
  172. this.page = 1
  173. this.articles = []
  174. this.noMore = false
  175. } else {
  176. if (this.loadingMore || this.noMore) return
  177. this.loadingMore = true
  178. }
  179. try {
  180. var params = { page: this.page, size: this.size }
  181. if (this.currentTab) {
  182. params.dimensionCode = this.currentTab
  183. }
  184. if (this.searchKeyword && this.searchKeyword.trim()) {
  185. params.keyword = this.searchKeyword.trim()
  186. }
  187. var res = await getArticleList(params)
  188. if (res.code === 200 && res.data) {
  189. var list = res.data.records || []
  190. if (this.page === 1) {
  191. this.articles = list
  192. } else {
  193. this.articles = this.articles.concat(list)
  194. }
  195. this.total = res.data.total || 0
  196. this.noMore = this.articles.length >= this.total
  197. }
  198. } catch (e) {
  199. uni.showToast({ title: '加载失败', icon: 'none' })
  200. } finally {
  201. this.loading = false
  202. this.loadingMore = false
  203. }
  204. },
  205. onTabChange(code) {
  206. if (this.currentTab === code) return
  207. this.currentTab = code
  208. this.searchKeyword = ''
  209. this.searchFocused = false
  210. this.page = 1
  211. this.articles = []
  212. this.noMore = false
  213. this.loadArticles()
  214. },
  215. async loadRecommendedArticles() {
  216. var self = this
  217. self.recommendLoading = true
  218. try {
  219. var res = await getFeaturedArticles({ size: 8 })
  220. if (res && res.code === 200 && res.data) {
  221. // 兼容数组 / { list } / { records } 三种响应结构,保证永远赋值数组
  222. var raw = Array.isArray(res.data) ? res.data : (res.data.list || res.data.records || [])
  223. self.recommendedArticles = Array.isArray(raw) ? raw : []
  224. }
  225. } catch (e) {
  226. // silent fail
  227. } finally {
  228. self.recommendLoading = false
  229. }
  230. },
  231. onLoadMore() {
  232. if (this.noMore || this.loading || this.loadingMore) return
  233. this.page++
  234. this.loadArticles(true)
  235. },
  236. goDetail(idOrArticle) {
  237. var id = idOrArticle && idOrArticle.id ? idOrArticle.id : idOrArticle
  238. uni.navigateTo({ url: '/pages/article-center/article-detail?id=' + id })
  239. },
  240. goEdit() {
  241. var isLoggedIn = !!uni.getStorageSync('token')
  242. if (isLoggedIn) {
  243. uni.navigateTo({ url: '/pages/article-center/article-edit' })
  244. } else {
  245. uni.navigateTo({ url: '/pages/login/login?redirect=' + encodeURIComponent('/pages/article-center/index') })
  246. }
  247. },
  248. goExplore() {
  249. uni.showToast({ title: '书库即将上线', icon: 'none' })
  250. },
  251. formatDate(dateStr) {
  252. if (!dateStr) return ''
  253. return dateStr.slice(0, 10)
  254. },
  255. parseDimensions(str) {
  256. if (!str) return []
  257. var dimMap = {
  258. body: { code: 'body', color: '#FF8C42', name: '身' },
  259. mind: { code: 'mind', color: '#FF6B9D', name: '心' },
  260. wisdom: { code: 'wisdom', color: '#6366F1', name: '智' },
  261. action: { code: 'action', color: '#10B981', name: '行' },
  262. wealth: { code: 'wealth', color: '#F59E0B', name: '富' }
  263. }
  264. var codes = str.split(',').map(function(s) { return s.trim().toLowerCase() })
  265. return codes.filter(function(c) { return dimMap[c] }).map(function(c) { return dimMap[c] })
  266. },
  267. focusSearch() {
  268. this.searchFocused = true
  269. },
  270. onSearch() {
  271. this.page = 1
  272. this.articles = []
  273. this.noMore = false
  274. this.loadArticles()
  275. },
  276. onSearchBlur() {
  277. if (!this.searchKeyword) {
  278. this.searchFocused = false
  279. }
  280. },
  281. onBack() {
  282. uni.navigateBack()
  283. },
  284. getImageUrl: function(path) {
  285. return config.API_BASE_URL + path
  286. }
  287. }
  288. }
  289. </script>
  290. <style scoped>
  291. /* ====================================
  292. 知识中心 — 重设计
  293. 白色导航 + 药丸Tab + 精装卡片
  294. ==================================== */
  295. .ac-container {
  296. min-height: 100vh;
  297. background: #f5f7fa;
  298. position: relative;
  299. }
  300. /* ---- 导航栏(白色简约) ---- */
  301. .ac-nav {
  302. display: flex;
  303. align-items: center;
  304. justify-content: space-between;
  305. padding: 84rpx 28rpx 20rpx;
  306. background: #fff;
  307. position: sticky;
  308. top: 0;
  309. z-index: 100;
  310. }
  311. .ac-nav-left {
  312. width: 60rpx;
  313. height: 48rpx;
  314. display: flex;
  315. align-items: center;
  316. justify-content: flex-start;
  317. flex-shrink: 0;
  318. }
  319. .ac-nav-back {
  320. font-size: 36rpx;
  321. color: #333;
  322. font-weight: bold;
  323. }
  324. .ac-nav-title {
  325. font-size: 32rpx;
  326. font-weight: bold;
  327. color: #1a1a1a;
  328. flex: 1;
  329. text-align: center;
  330. }
  331. .ac-nav-right {
  332. display: flex;
  333. align-items: center;
  334. gap: 20rpx;
  335. flex-shrink: 0;
  336. }
  337. .ac-nav-search-icon {
  338. font-size: 32rpx;
  339. color: #666;
  340. }
  341. .ac-nav-add {
  342. font-size: 40rpx;
  343. color: #F97316;
  344. font-weight: 300;
  345. line-height: 1;
  346. }
  347. /* ---- 搜索展开条 ---- */
  348. .ac-search-bar {
  349. display: flex;
  350. align-items: center;
  351. padding: 12rpx 28rpx 16rpx;
  352. background: #fff;
  353. border-bottom: 1rpx solid #f0f0f0;
  354. }
  355. .ac-search-input {
  356. flex: 1;
  357. height: 60rpx;
  358. background: #f5f5f5;
  359. border-radius: 30rpx;
  360. padding: 0 28rpx;
  361. font-size: 26rpx;
  362. color: #333;
  363. }
  364. .ac-search-cancel {
  365. font-size: 26rpx;
  366. color: #999;
  367. margin-left: 16rpx;
  368. flex-shrink: 0;
  369. }
  370. /* ---- Tab 药丸导航 ---- */
  371. .ac-tabs {
  372. padding: 16rpx 28rpx;
  373. background: #fff;
  374. border-bottom: 1rpx solid #f0f0f0;
  375. position: sticky;
  376. z-index: 10;
  377. }
  378. .ac-tab-scroll {
  379. white-space: nowrap;
  380. display: flex;
  381. }
  382. .ac-tab {
  383. display: inline-flex;
  384. align-items: center;
  385. justify-content: center;
  386. height: 52rpx;
  387. padding: 0 28rpx;
  388. margin-right: 16rpx;
  389. font-size: 24rpx;
  390. color: #666;
  391. background: #f0f0f0;
  392. border-radius: 26rpx;
  393. border: 2rpx solid #f0f0f0;
  394. flex-shrink: 0;
  395. transition: all 0.2s;
  396. }
  397. .ac-tab.active {
  398. font-weight: 600;
  399. }
  400. /* ---- 滚动列表区域 ---- */
  401. .ac-scroll {
  402. height: calc(100vh - 200rpx);
  403. }
  404. /* ---- 骨架屏 ---- */
  405. .ac-skeleton-wrap { padding: 24rpx 28rpx; }
  406. .sk-card {
  407. background: #fff;
  408. border-radius: 24rpx;
  409. overflow: hidden;
  410. margin-bottom: 24rpx;
  411. box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.04);
  412. }
  413. .sk-cover {
  414. height: 340rpx;
  415. background: linear-gradient(90deg, #f0f0f0 25%, #e8e8e8 50%, #f0f0f0 75%);
  416. background-size: 200% 100%;
  417. animation: sk-shimmer 1.5s infinite;
  418. }
  419. .sk-body { padding: 24rpx; }
  420. .sk-row {
  421. height: 22rpx;
  422. background: linear-gradient(90deg, #f0f0f0 25%, #e8e8e8 50%, #f0f0f0 75%);
  423. background-size: 200% 100%;
  424. border-radius: 6rpx;
  425. margin-bottom: 14rpx;
  426. animation: sk-shimmer 1.5s infinite;
  427. }
  428. .sk-tag { width: 20%; }
  429. .sk-title { width: 65%; }
  430. .sk-desc { width: 90%; }
  431. .sk-footer { width: 40%; height: 18rpx; }
  432. @keyframes sk-shimmer {
  433. 0% { background-position: -200% 0; }
  434. 100% { background-position: 200% 0; }
  435. }
  436. /* ---- 空状态 ---- */
  437. .ac-empty {
  438. display: flex;
  439. flex-direction: column;
  440. align-items: center;
  441. padding-top: 240rpx;
  442. }
  443. .ac-empty-img {
  444. width: 200rpx;
  445. height: 200rpx;
  446. margin-bottom: 28rpx;
  447. }
  448. .ac-empty-text {
  449. font-size: 28rpx;
  450. color: #bbb;
  451. }
  452. /* ---- 文章卡片(新版) ---- */
  453. .ac-feed {
  454. padding: 24rpx 28rpx;
  455. }
  456. .ac-card {
  457. background: #fff;
  458. border-radius: 24rpx;
  459. overflow: hidden;
  460. margin-bottom: 24rpx;
  461. box-shadow: 0 2rpx 12rpx rgba(0,0,0,0.06);
  462. transition: transform 0.15s;
  463. }
  464. .ac-card-pressed {
  465. transform: scale(0.97);
  466. opacity: 0.9;
  467. }
  468. /* 封面 + 维度浮标 */
  469. .ac-card-media {
  470. position: relative;
  471. width: 100%;
  472. overflow: hidden;
  473. }
  474. .ac-card-cover {
  475. display: block;
  476. width: 100%;
  477. height: 340rpx;
  478. background: #f0f0f0;
  479. }
  480. .ac-card-dimension-tags {
  481. position: absolute;
  482. bottom: 12rpx;
  483. left: 16rpx;
  484. display: flex;
  485. gap: 8rpx;
  486. }
  487. .ac-card-dim-tag {
  488. padding: 2rpx 14rpx;
  489. border-radius: 12rpx;
  490. font-size: 20rpx;
  491. color: #fff;
  492. font-weight: 500;
  493. line-height: 1.4;
  494. }
  495. /* 正文区 */
  496. .ac-card-body {
  497. padding: 24rpx 24rpx 28rpx;
  498. }
  499. .ac-card-meta-top {
  500. display: flex;
  501. align-items: center;
  502. margin-bottom: 12rpx;
  503. }
  504. .ac-card-category {
  505. font-size: 20rpx;
  506. color: #F97316;
  507. background: rgba(249,115,22,0.08);
  508. padding: 2rpx 14rpx;
  509. border-radius: 8rpx;
  510. margin-right: 16rpx;
  511. }
  512. .ac-card-readtime {
  513. font-size: 20rpx;
  514. color: #aaa;
  515. }
  516. .ac-card-title {
  517. display: block;
  518. font-size: 30rpx;
  519. font-weight: 600;
  520. color: #1a1a1a;
  521. line-height: 1.45;
  522. margin-bottom: 10rpx;
  523. overflow: hidden;
  524. text-overflow: ellipsis;
  525. display: -webkit-box;
  526. -webkit-line-clamp: 2;
  527. -webkit-box-orient: vertical;
  528. }
  529. .ac-card-summary {
  530. display: block;
  531. font-size: 24rpx;
  532. color: #999;
  533. line-height: 1.5;
  534. margin-bottom: 16rpx;
  535. overflow: hidden;
  536. text-overflow: ellipsis;
  537. display: -webkit-box;
  538. -webkit-line-clamp: 2;
  539. -webkit-box-orient: vertical;
  540. }
  541. .ac-card-footer {
  542. display: flex;
  543. align-items: center;
  544. font-size: 20rpx;
  545. color: #bbb;
  546. }
  547. .ac-card-author {
  548. color: #888;
  549. }
  550. .ac-card-sep {
  551. margin: 0 10rpx;
  552. color: #ddd;
  553. }
  554. .ac-card-date {
  555. flex: 1;
  556. }
  557. .ac-card-fav {
  558. color: #ccc;
  559. }
  560. /* ---- 加载更多 / 没有更多 ---- */
  561. .ac-loading-more,
  562. .ac-no-more {
  563. text-align: center;
  564. padding: 30rpx;
  565. }
  566. .ac-loading-text {
  567. font-size: 24rpx;
  568. color: #bbb;
  569. }
  570. .ac-no-more-text {
  571. font-size: 24rpx;
  572. color: #ddd;
  573. }
  574. .ac-bottom-gap {
  575. height: 140rpx;
  576. }
  577. /* ---- 浮动发布按钮 ---- */
  578. .ac-fab {
  579. position: fixed;
  580. right: 36rpx;
  581. bottom: 100rpx;
  582. width: 96rpx;
  583. height: 96rpx;
  584. background: linear-gradient(135deg, #F97316, #EA580C);
  585. color: #fff;
  586. border-radius: 50%;
  587. display: flex;
  588. align-items: center;
  589. justify-content: center;
  590. box-shadow: 0 6rpx 20rpx rgba(249,115,22,0.35);
  591. z-index: 100;
  592. }
  593. .ac-fab:active {
  594. transform: scale(0.92);
  595. opacity: 0.85;
  596. }
  597. .ac-fab-icon {
  598. font-size: 48rpx;
  599. font-weight: 300;
  600. line-height: 1;
  601. }
  602. </style>