index.vue 18 KB

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