index.vue 16 KB

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