index.vue 17 KB

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