index.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  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. self.recommendedArticles = res.data.list || res.data || []
  222. }
  223. } catch (e) {
  224. // silent fail
  225. } finally {
  226. self.recommendLoading = false
  227. }
  228. },
  229. onLoadMore() {
  230. if (this.noMore || this.loading || this.loadingMore) return
  231. this.page++
  232. this.loadArticles(true)
  233. },
  234. goDetail(idOrArticle) {
  235. var id = idOrArticle && idOrArticle.id ? idOrArticle.id : idOrArticle
  236. uni.navigateTo({ url: '/pages/article-center/article-detail?id=' + id })
  237. },
  238. goEdit() {
  239. var isLoggedIn = !!uni.getStorageSync('token')
  240. if (isLoggedIn) {
  241. uni.navigateTo({ url: '/pages/article-center/article-edit' })
  242. } else {
  243. uni.navigateTo({ url: '/pages/login/login?redirect=' + encodeURIComponent('/pages/article-center/index') })
  244. }
  245. },
  246. goExplore() {
  247. uni.showToast({ title: '书库即将上线', icon: 'none' })
  248. },
  249. formatDate(dateStr) {
  250. if (!dateStr) return ''
  251. return dateStr.slice(0, 10)
  252. },
  253. parseDimensions(str) {
  254. if (!str) return []
  255. var dimMap = {
  256. body: { code: 'body', color: '#FF8C42', name: '身' },
  257. mind: { code: 'mind', color: '#FF6B9D', name: '心' },
  258. wisdom: { code: 'wisdom', color: '#6366F1', name: '智' },
  259. action: { code: 'action', color: '#10B981', name: '行' },
  260. wealth: { code: 'wealth', color: '#F59E0B', name: '富' }
  261. }
  262. var codes = str.split(',').map(function(s) { return s.trim().toLowerCase() })
  263. return codes.filter(function(c) { return dimMap[c] }).map(function(c) { return dimMap[c] })
  264. },
  265. focusSearch() {
  266. this.searchFocused = true
  267. },
  268. onSearch() {
  269. this.page = 1
  270. this.articles = []
  271. this.noMore = false
  272. this.loadArticles()
  273. },
  274. onSearchBlur() {
  275. if (!this.searchKeyword) {
  276. this.searchFocused = false
  277. }
  278. },
  279. onBack() {
  280. uni.navigateBack()
  281. },
  282. getImageUrl: function(path) {
  283. return config.API_BASE_URL + path
  284. }
  285. }
  286. }
  287. </script>
  288. <style scoped>
  289. /* ====================================
  290. 知识中心 — 重设计
  291. 白色导航 + 药丸Tab + 精装卡片
  292. ==================================== */
  293. .ac-container {
  294. min-height: 100vh;
  295. background: #f5f7fa;
  296. position: relative;
  297. }
  298. /* ---- 导航栏(白色简约) ---- */
  299. .ac-nav {
  300. display: flex;
  301. align-items: center;
  302. justify-content: space-between;
  303. padding: 84rpx 28rpx 20rpx;
  304. background: #fff;
  305. position: sticky;
  306. top: 0;
  307. z-index: 100;
  308. }
  309. .ac-nav-left {
  310. width: 60rpx;
  311. height: 48rpx;
  312. display: flex;
  313. align-items: center;
  314. justify-content: flex-start;
  315. flex-shrink: 0;
  316. }
  317. .ac-nav-back {
  318. font-size: 36rpx;
  319. color: #333;
  320. font-weight: bold;
  321. }
  322. .ac-nav-title {
  323. font-size: 32rpx;
  324. font-weight: bold;
  325. color: #1a1a1a;
  326. flex: 1;
  327. text-align: center;
  328. }
  329. .ac-nav-right {
  330. display: flex;
  331. align-items: center;
  332. gap: 20rpx;
  333. flex-shrink: 0;
  334. }
  335. .ac-nav-search-icon {
  336. font-size: 32rpx;
  337. color: #666;
  338. }
  339. .ac-nav-add {
  340. font-size: 40rpx;
  341. color: #F97316;
  342. font-weight: 300;
  343. line-height: 1;
  344. }
  345. /* ---- 搜索展开条 ---- */
  346. .ac-search-bar {
  347. display: flex;
  348. align-items: center;
  349. padding: 12rpx 28rpx 16rpx;
  350. background: #fff;
  351. border-bottom: 1rpx solid #f0f0f0;
  352. }
  353. .ac-search-input {
  354. flex: 1;
  355. height: 60rpx;
  356. background: #f5f5f5;
  357. border-radius: 30rpx;
  358. padding: 0 28rpx;
  359. font-size: 26rpx;
  360. color: #333;
  361. }
  362. .ac-search-cancel {
  363. font-size: 26rpx;
  364. color: #999;
  365. margin-left: 16rpx;
  366. flex-shrink: 0;
  367. }
  368. /* ---- Tab 药丸导航 ---- */
  369. .ac-tabs {
  370. padding: 16rpx 28rpx;
  371. background: #fff;
  372. border-bottom: 1rpx solid #f0f0f0;
  373. position: sticky;
  374. z-index: 10;
  375. }
  376. .ac-tab-scroll {
  377. white-space: nowrap;
  378. display: flex;
  379. }
  380. .ac-tab {
  381. display: inline-flex;
  382. align-items: center;
  383. justify-content: center;
  384. height: 52rpx;
  385. padding: 0 28rpx;
  386. margin-right: 16rpx;
  387. font-size: 24rpx;
  388. color: #666;
  389. background: #f0f0f0;
  390. border-radius: 26rpx;
  391. border: 2rpx solid #f0f0f0;
  392. flex-shrink: 0;
  393. transition: all 0.2s;
  394. }
  395. .ac-tab.active {
  396. font-weight: 600;
  397. }
  398. /* ---- 滚动列表区域 ---- */
  399. .ac-scroll {
  400. height: calc(100vh - 200rpx);
  401. }
  402. /* ---- 骨架屏 ---- */
  403. .ac-skeleton-wrap { padding: 24rpx 28rpx; }
  404. .sk-card {
  405. background: #fff;
  406. border-radius: 24rpx;
  407. overflow: hidden;
  408. margin-bottom: 24rpx;
  409. box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.04);
  410. }
  411. .sk-cover {
  412. height: 340rpx;
  413. background: linear-gradient(90deg, #f0f0f0 25%, #e8e8e8 50%, #f0f0f0 75%);
  414. background-size: 200% 100%;
  415. animation: sk-shimmer 1.5s infinite;
  416. }
  417. .sk-body { padding: 24rpx; }
  418. .sk-row {
  419. height: 22rpx;
  420. background: linear-gradient(90deg, #f0f0f0 25%, #e8e8e8 50%, #f0f0f0 75%);
  421. background-size: 200% 100%;
  422. border-radius: 6rpx;
  423. margin-bottom: 14rpx;
  424. animation: sk-shimmer 1.5s infinite;
  425. }
  426. .sk-tag { width: 20%; }
  427. .sk-title { width: 65%; }
  428. .sk-desc { width: 90%; }
  429. .sk-footer { width: 40%; height: 18rpx; }
  430. @keyframes sk-shimmer {
  431. 0% { background-position: -200% 0; }
  432. 100% { background-position: 200% 0; }
  433. }
  434. /* ---- 空状态 ---- */
  435. .ac-empty {
  436. display: flex;
  437. flex-direction: column;
  438. align-items: center;
  439. padding-top: 240rpx;
  440. }
  441. .ac-empty-img {
  442. width: 200rpx;
  443. height: 200rpx;
  444. margin-bottom: 28rpx;
  445. }
  446. .ac-empty-text {
  447. font-size: 28rpx;
  448. color: #bbb;
  449. }
  450. /* ---- 文章卡片(新版) ---- */
  451. .ac-feed {
  452. padding: 24rpx 28rpx;
  453. }
  454. .ac-card {
  455. background: #fff;
  456. border-radius: 24rpx;
  457. overflow: hidden;
  458. margin-bottom: 24rpx;
  459. box-shadow: 0 2rpx 12rpx rgba(0,0,0,0.06);
  460. transition: transform 0.15s;
  461. }
  462. .ac-card-pressed {
  463. transform: scale(0.97);
  464. opacity: 0.9;
  465. }
  466. /* 封面 + 维度浮标 */
  467. .ac-card-media {
  468. position: relative;
  469. width: 100%;
  470. overflow: hidden;
  471. }
  472. .ac-card-cover {
  473. display: block;
  474. width: 100%;
  475. height: 340rpx;
  476. background: #f0f0f0;
  477. }
  478. .ac-card-dimension-tags {
  479. position: absolute;
  480. bottom: 12rpx;
  481. left: 16rpx;
  482. display: flex;
  483. gap: 8rpx;
  484. }
  485. .ac-card-dim-tag {
  486. padding: 2rpx 14rpx;
  487. border-radius: 12rpx;
  488. font-size: 20rpx;
  489. color: #fff;
  490. font-weight: 500;
  491. line-height: 1.4;
  492. }
  493. /* 正文区 */
  494. .ac-card-body {
  495. padding: 24rpx 24rpx 28rpx;
  496. }
  497. .ac-card-meta-top {
  498. display: flex;
  499. align-items: center;
  500. margin-bottom: 12rpx;
  501. }
  502. .ac-card-category {
  503. font-size: 20rpx;
  504. color: #F97316;
  505. background: rgba(249,115,22,0.08);
  506. padding: 2rpx 14rpx;
  507. border-radius: 8rpx;
  508. margin-right: 16rpx;
  509. }
  510. .ac-card-readtime {
  511. font-size: 20rpx;
  512. color: #aaa;
  513. }
  514. .ac-card-title {
  515. display: block;
  516. font-size: 30rpx;
  517. font-weight: 600;
  518. color: #1a1a1a;
  519. line-height: 1.45;
  520. margin-bottom: 10rpx;
  521. overflow: hidden;
  522. text-overflow: ellipsis;
  523. display: -webkit-box;
  524. -webkit-line-clamp: 2;
  525. -webkit-box-orient: vertical;
  526. }
  527. .ac-card-summary {
  528. display: block;
  529. font-size: 24rpx;
  530. color: #999;
  531. line-height: 1.5;
  532. margin-bottom: 16rpx;
  533. overflow: hidden;
  534. text-overflow: ellipsis;
  535. display: -webkit-box;
  536. -webkit-line-clamp: 2;
  537. -webkit-box-orient: vertical;
  538. }
  539. .ac-card-footer {
  540. display: flex;
  541. align-items: center;
  542. font-size: 20rpx;
  543. color: #bbb;
  544. }
  545. .ac-card-author {
  546. color: #888;
  547. }
  548. .ac-card-sep {
  549. margin: 0 10rpx;
  550. color: #ddd;
  551. }
  552. .ac-card-date {
  553. flex: 1;
  554. }
  555. .ac-card-fav {
  556. color: #ccc;
  557. }
  558. /* ---- 加载更多 / 没有更多 ---- */
  559. .ac-loading-more,
  560. .ac-no-more {
  561. text-align: center;
  562. padding: 30rpx;
  563. }
  564. .ac-loading-text {
  565. font-size: 24rpx;
  566. color: #bbb;
  567. }
  568. .ac-no-more-text {
  569. font-size: 24rpx;
  570. color: #ddd;
  571. }
  572. .ac-bottom-gap {
  573. height: 140rpx;
  574. }
  575. /* ---- 浮动发布按钮 ---- */
  576. .ac-fab {
  577. position: fixed;
  578. right: 36rpx;
  579. bottom: 100rpx;
  580. width: 96rpx;
  581. height: 96rpx;
  582. background: linear-gradient(135deg, #F97316, #EA580C);
  583. color: #fff;
  584. border-radius: 50%;
  585. display: flex;
  586. align-items: center;
  587. justify-content: center;
  588. box-shadow: 0 6rpx 20rpx rgba(249,115,22,0.35);
  589. z-index: 100;
  590. }
  591. .ac-fab:active {
  592. transform: scale(0.92);
  593. opacity: 0.85;
  594. }
  595. .ac-fab-icon {
  596. font-size: 48rpx;
  597. font-weight: 300;
  598. line-height: 1;
  599. }
  600. </style>