index.vue 16 KB

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