index.vue 16 KB

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