index.vue 16 KB

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