index.vue 17 KB

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