index.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  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. if (!path || path === 'null' || path === 'undefined' || path === '/null') return ''
  312. var s = String(path).trim()
  313. if (!s || s === 'null' || s === '/null') return ''
  314. if (s.indexOf('http://') === 0 || s.indexOf('https://') === 0) return s
  315. return config.API_BASE_URL + (s.charAt(0) === '/' ? s : '/' + s)
  316. }
  317. }
  318. }
  319. </script>
  320. <style scoped>
  321. /* ====================================
  322. 知识分享 — 重设计
  323. 白色导航 + 药丸Tab + 精装卡片
  324. ==================================== */
  325. .ac-container {
  326. min-height: 100vh;
  327. background: #f5f7fa;
  328. position: relative;
  329. }
  330. /* ---- 导航栏(白色简约) ---- */
  331. .ac-nav {
  332. display: flex;
  333. align-items: center;
  334. justify-content: space-between;
  335. padding: 84rpx 28rpx 20rpx;
  336. background: #fff;
  337. position: sticky;
  338. top: 0;
  339. z-index: 100;
  340. }
  341. .ac-nav-left {
  342. width: 60rpx;
  343. height: 48rpx;
  344. display: flex;
  345. align-items: center;
  346. justify-content: flex-start;
  347. flex-shrink: 0;
  348. }
  349. .ac-nav-back {
  350. font-size: 36rpx;
  351. color: #333;
  352. font-weight: bold;
  353. }
  354. .ac-nav-title {
  355. font-size: 32rpx;
  356. font-weight: bold;
  357. color: #1a1a1a;
  358. flex: 1;
  359. text-align: center;
  360. }
  361. .ac-nav-right {
  362. display: flex;
  363. align-items: center;
  364. gap: 20rpx;
  365. flex-shrink: 0;
  366. }
  367. .ac-nav-search-icon {
  368. font-size: 32rpx;
  369. color: #666;
  370. }
  371. .ac-nav-add {
  372. font-size: 40rpx;
  373. color: #F97316;
  374. font-weight: 300;
  375. line-height: 1;
  376. }
  377. /* ---- 搜索展开条 ---- */
  378. .ac-search-bar {
  379. display: flex;
  380. align-items: center;
  381. padding: 12rpx 28rpx 16rpx;
  382. background: #fff;
  383. border-bottom: 1rpx solid #f0f0f0;
  384. }
  385. .ac-search-input {
  386. flex: 1;
  387. height: 60rpx;
  388. background: #f5f5f5;
  389. border-radius: 30rpx;
  390. padding: 0 28rpx;
  391. font-size: 26rpx;
  392. color: #333;
  393. }
  394. .ac-search-cancel {
  395. font-size: 26rpx;
  396. color: #999;
  397. margin-left: 16rpx;
  398. flex-shrink: 0;
  399. }
  400. /* ---- Tab 药丸导航 ---- */
  401. .ac-tabs {
  402. padding: 16rpx 28rpx;
  403. background: #fff;
  404. border-bottom: 1rpx solid #f0f0f0;
  405. position: sticky;
  406. z-index: 10;
  407. }
  408. .ac-tab-scroll {
  409. white-space: nowrap;
  410. display: flex;
  411. }
  412. .ac-tab {
  413. display: inline-flex;
  414. align-items: center;
  415. justify-content: center;
  416. height: 52rpx;
  417. padding: 0 28rpx;
  418. margin-right: 16rpx;
  419. font-size: 24rpx;
  420. color: #666;
  421. background: #f0f0f0;
  422. border-radius: 26rpx;
  423. border: 2rpx solid #f0f0f0;
  424. flex-shrink: 0;
  425. transition: all 0.2s;
  426. }
  427. .ac-tab.active {
  428. font-weight: 600;
  429. }
  430. /* ---- 滚动列表区域 ---- */
  431. .ac-scroll {
  432. height: calc(100vh - 200rpx);
  433. }
  434. /* ---- 骨架屏 ---- */
  435. .ac-skeleton-wrap { padding: 24rpx 28rpx; }
  436. .sk-card {
  437. background: #fff;
  438. border-radius: 24rpx;
  439. overflow: hidden;
  440. margin-bottom: 24rpx;
  441. box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.04);
  442. }
  443. .sk-cover {
  444. height: 340rpx;
  445. background: linear-gradient(90deg, #f0f0f0 25%, #e8e8e8 50%, #f0f0f0 75%);
  446. background-size: 200% 100%;
  447. animation: sk-shimmer 1.5s infinite;
  448. }
  449. .sk-body { padding: 24rpx; }
  450. .sk-row {
  451. height: 22rpx;
  452. background: linear-gradient(90deg, #f0f0f0 25%, #e8e8e8 50%, #f0f0f0 75%);
  453. background-size: 200% 100%;
  454. border-radius: 6rpx;
  455. margin-bottom: 14rpx;
  456. animation: sk-shimmer 1.5s infinite;
  457. }
  458. .sk-tag { width: 20%; }
  459. .sk-title { width: 65%; }
  460. .sk-desc { width: 90%; }
  461. .sk-footer { width: 40%; height: 18rpx; }
  462. @keyframes sk-shimmer {
  463. 0% { background-position: -200% 0; }
  464. 100% { background-position: 200% 0; }
  465. }
  466. /* ---- 空状态 ---- */
  467. .ac-empty {
  468. display: flex;
  469. flex-direction: column;
  470. align-items: center;
  471. padding-top: 240rpx;
  472. }
  473. .ac-empty-img {
  474. width: 200rpx;
  475. height: 200rpx;
  476. margin-bottom: 28rpx;
  477. }
  478. .ac-empty-text {
  479. font-size: 28rpx;
  480. color: #bbb;
  481. }
  482. /* ---- 文章卡片(新版) ---- */
  483. .ac-feed {
  484. padding: 24rpx 28rpx;
  485. }
  486. .ac-card {
  487. background: #fff;
  488. border-radius: 24rpx;
  489. overflow: hidden;
  490. margin-bottom: 24rpx;
  491. box-shadow: 0 2rpx 12rpx rgba(0,0,0,0.06);
  492. transition: transform 0.15s;
  493. }
  494. .ac-card-pressed {
  495. transform: scale(0.97);
  496. opacity: 0.9;
  497. }
  498. /* 封面 + 维度浮标 */
  499. .ac-card-media {
  500. position: relative;
  501. width: 100%;
  502. overflow: hidden;
  503. }
  504. .ac-card-cover {
  505. display: block;
  506. width: 100%;
  507. height: 340rpx;
  508. background: #f0f0f0;
  509. }
  510. .ac-card-dimension-tags {
  511. position: absolute;
  512. bottom: 12rpx;
  513. left: 16rpx;
  514. display: flex;
  515. gap: 8rpx;
  516. }
  517. .ac-card-dim-tag {
  518. padding: 2rpx 14rpx;
  519. border-radius: 12rpx;
  520. font-size: 20rpx;
  521. color: #fff;
  522. font-weight: 500;
  523. line-height: 1.4;
  524. }
  525. /* 正文区 */
  526. .ac-card-body {
  527. padding: 24rpx 24rpx 28rpx;
  528. }
  529. .ac-card-meta-top {
  530. display: flex;
  531. align-items: center;
  532. margin-bottom: 12rpx;
  533. }
  534. .ac-card-category {
  535. font-size: 20rpx;
  536. color: #F97316;
  537. background: rgba(249,115,22,0.08);
  538. padding: 2rpx 14rpx;
  539. border-radius: 8rpx;
  540. margin-right: 16rpx;
  541. }
  542. .ac-card-readtime {
  543. font-size: 20rpx;
  544. color: #aaa;
  545. }
  546. .ac-card-title {
  547. display: block;
  548. font-size: 30rpx;
  549. font-weight: 600;
  550. color: #1a1a1a;
  551. line-height: 1.45;
  552. margin-bottom: 10rpx;
  553. overflow: hidden;
  554. text-overflow: ellipsis;
  555. display: -webkit-box;
  556. -webkit-line-clamp: 2;
  557. -webkit-box-orient: vertical;
  558. }
  559. .ac-card-summary {
  560. display: block;
  561. font-size: 24rpx;
  562. color: #999;
  563. line-height: 1.5;
  564. margin-bottom: 16rpx;
  565. overflow: hidden;
  566. text-overflow: ellipsis;
  567. display: -webkit-box;
  568. -webkit-line-clamp: 2;
  569. -webkit-box-orient: vertical;
  570. }
  571. .ac-card-footer {
  572. display: flex;
  573. align-items: center;
  574. font-size: 20rpx;
  575. color: #bbb;
  576. }
  577. .ac-card-author {
  578. color: #888;
  579. }
  580. .ac-card-sep {
  581. margin: 0 10rpx;
  582. color: #ddd;
  583. }
  584. .ac-card-date {
  585. flex: 1;
  586. }
  587. .ac-card-fav {
  588. color: #ccc;
  589. }
  590. .ac-card-readcount {
  591. color: #ccc;
  592. }
  593. /* ---- 加载更多 / 没有更多 ---- */
  594. .ac-loading-more,
  595. .ac-no-more {
  596. text-align: center;
  597. padding: 30rpx;
  598. }
  599. .ac-loading-text {
  600. font-size: 24rpx;
  601. color: #bbb;
  602. }
  603. .ac-no-more-text {
  604. font-size: 24rpx;
  605. color: #ddd;
  606. }
  607. .ac-bottom-gap {
  608. height: 140rpx;
  609. }
  610. /* ---- 浮动发布按钮 ---- */
  611. .ac-fab {
  612. position: fixed;
  613. right: 36rpx;
  614. bottom: 100rpx;
  615. width: 96rpx;
  616. height: 96rpx;
  617. background: linear-gradient(135deg, #F97316, #EA580C);
  618. color: #fff;
  619. border-radius: 50%;
  620. display: flex;
  621. align-items: center;
  622. justify-content: center;
  623. box-shadow: 0 6rpx 20rpx rgba(249,115,22,0.35);
  624. z-index: 100;
  625. }
  626. .ac-fab:active {
  627. transform: scale(0.92);
  628. opacity: 0.85;
  629. }
  630. .ac-fab-icon {
  631. font-size: 48rpx;
  632. font-weight: 300;
  633. line-height: 1;
  634. }
  635. </style>