index.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. <template>
  2. <view class="ac-container">
  3. <!-- 自定义导航栏 -->
  4. <view class="kc-nav">
  5. <view class="kc-nav-left" @click="onBack">
  6. <text class="kc-back-icon">←</text>
  7. </view>
  8. <view class="kc-nav-search" @click="focusSearch">
  9. <text class="kc-search-icon">🔍</text>
  10. <input
  11. v-if="searchFocused || searchKeyword"
  12. class="kc-search-input"
  13. v-model="searchKeyword"
  14. placeholder="搜索文章..."
  15. confirm-type="search"
  16. @confirm="onSearch"
  17. @blur="onSearchBlur"
  18. />
  19. <text v-else class="kc-search-placeholder">搜索文章...</text>
  20. </view>
  21. <view class="kc-nav-right" @click="goEdit">
  22. <text class="kc-add-btn">+</text>
  23. </view>
  24. </view>
  25. <!-- Tab 导航 -->
  26. <view class="ac-tabs">
  27. <scroll-view scroll-x enable-flex show-scrollbar="false" class="tab-scroll">
  28. <view
  29. v-for="tab in tabList"
  30. :key="tab.code"
  31. :class="['tab-item', currentTab === tab.code ? 'active' : '']"
  32. :style="currentTab === tab.code ? 'color:' + tab.color + ';border-bottom-color:' + tab.color : ''"
  33. @click="onTabChange(tab.code)"
  34. >
  35. {{ tab.name }}
  36. </view>
  37. </scroll-view>
  38. </view>
  39. <!-- 文章列表 -->
  40. <scroll-view scroll-y class="ac-list" @scrolltolower="onLoadMore">
  41. <!-- 骨架屏 -->
  42. <view v-if="loading && articles.length === 0" class="ac-skeleton">
  43. <view v-for="n in 3" :key="n" class="skeleton-card">
  44. <view class="skeleton-cover"></view>
  45. <view class="skeleton-body">
  46. <view class="skeleton-line skeleton-title"></view>
  47. <view class="skeleton-line skeleton-summary"></view>
  48. <view class="skeleton-line skeleton-summary short"></view>
  49. <view class="skeleton-line skeleton-meta"></view>
  50. </view>
  51. </view>
  52. </view>
  53. <!-- 空状态 -->
  54. <view v-else-if="articles.length === 0" class="ac-empty">
  55. <image class="empty-img" src="/static/empty-article.png" mode="aspectFit"></image>
  56. <text class="empty-text">暂无文章</text>
  57. </view>
  58. <!-- 文章列表 -->
  59. <view v-else class="article-list">
  60. <view
  61. v-for="item in articles"
  62. :key="item.id"
  63. class="article-card"
  64. @click="goDetail(item.id)"
  65. >
  66. <image
  67. class="card-cover"
  68. :src="item.coverImage || '/static/default-article.png'"
  69. mode="aspectFill"
  70. />
  71. <view class="card-body">
  72. <view class="card-tags">
  73. <text class="card-category">{{ item.categoryName || '' }}</text>
  74. <text class="card-readtime">{{ item.readTime || 3 }}分钟</text>
  75. </view>
  76. <text class="card-title">{{ item.title }}</text>
  77. <text class="card-summary">{{ item.summary || '' }}</text>
  78. <view class="card-footer">
  79. <text class="card-author">{{ item.author || '浠艾福' }}</text>
  80. <text class="card-date">{{ formatDate(item.publishedAt) }}</text>
  81. <text class="card-fav">收藏 {{ item.favCount || 0 }}</text>
  82. </view>
  83. <!-- 五维彩条 -->
  84. <view v-if="item.relatedDimensions" class="card-dimensions">
  85. <view
  86. v-for="dim in parseDimensions(item.relatedDimensions)"
  87. :key="dim.code"
  88. class="dim-dot"
  89. :style="'background:' + dim.color"
  90. ></view>
  91. </view>
  92. </view>
  93. </view>
  94. </view>
  95. <!-- 加载更多 -->
  96. <view v-if="loadingMore" class="loading-more">
  97. <text class="loading-text">加载中...</text>
  98. </view>
  99. <view v-if="noMore && articles.length > 0" class="no-more">
  100. <text class="no-more-text">— 没有更多了 —</text>
  101. </view>
  102. <!-- 底部占位 -->
  103. <view class="bottom-spacer"></view>
  104. </scroll-view>
  105. <!-- 浮动发布按钮 -->
  106. <view class="ac-fab" @click="goEdit">
  107. <text class="fab-icon">+</text>
  108. </view>
  109. </view>
  110. </template>
  111. <script>
  112. import { getArticleList } from '@/utils/api.js'
  113. export default {
  114. data() {
  115. return {
  116. tabList: [
  117. { code: '', name: '全部', color: '#F97316' },
  118. { code: 'body', name: '身', color: '#FF8C42' },
  119. { code: 'mind', name: '心', color: '#FF6B9D' },
  120. { code: 'wisdom', name: '智', color: '#6366F1' },
  121. { code: 'action', name: '行', color: '#10B981' },
  122. { code: 'wealth', name: '富', color: '#F59E0B' }
  123. ],
  124. currentTab: '',
  125. articles: [],
  126. page: 1,
  127. size: 10,
  128. total: 0,
  129. loading: false,
  130. loadingMore: false,
  131. noMore: false,
  132. searchKeyword: '',
  133. searchFocused: false,
  134. isLoggedIn: false
  135. }
  136. },
  137. onLoad(options) {
  138. // 支持从外部传入 dimension 参数,默认选中对应 tab
  139. if (options && options.dimension) {
  140. this.currentTab = options.dimension
  141. }
  142. this.isLoggedIn = !!uni.getStorageSync('token')
  143. this.loadArticles()
  144. },
  145. onShow() {
  146. this.isLoggedIn = !!uni.getStorageSync('token')
  147. },
  148. methods: {
  149. async loadArticles(isLoadMore) {
  150. if (!isLoadMore) {
  151. if (this.loading) return
  152. this.loading = true
  153. this.page = 1
  154. this.articles = []
  155. this.noMore = false
  156. } else {
  157. if (this.loadingMore || this.noMore) return
  158. this.loadingMore = true
  159. }
  160. try {
  161. var params = { page: this.page, size: this.size }
  162. if (this.currentTab) {
  163. params.dimensionCode = this.currentTab
  164. }
  165. if (this.searchKeyword && this.searchKeyword.trim()) {
  166. params.keyword = this.searchKeyword.trim()
  167. }
  168. var res = await getArticleList(params)
  169. if (res.code === 200 && res.data) {
  170. var list = res.data.records || []
  171. if (this.page === 1) {
  172. this.articles = list
  173. } else {
  174. this.articles = this.articles.concat(list)
  175. }
  176. this.total = res.data.total || 0
  177. this.noMore = this.articles.length >= this.total
  178. }
  179. } catch (e) {
  180. uni.showToast({ title: '加载失败', icon: 'none' })
  181. } finally {
  182. this.loading = false
  183. this.loadingMore = false
  184. }
  185. },
  186. onTabChange(code) {
  187. if (this.currentTab === code) return
  188. this.currentTab = code
  189. this.searchKeyword = ''
  190. this.searchFocused = false
  191. this.page = 1
  192. this.articles = []
  193. this.noMore = false
  194. this.loadArticles()
  195. },
  196. onLoadMore() {
  197. if (this.noMore || this.loading || this.loadingMore) return
  198. this.page++
  199. this.loadArticles(true)
  200. },
  201. goDetail(id) {
  202. uni.navigateTo({ url: '/pages/article-center/article-detail?id=' + id })
  203. },
  204. goEdit() {
  205. var isLoggedIn = !!uni.getStorageSync('token')
  206. if (isLoggedIn) {
  207. uni.navigateTo({ url: '/pages/article-center/article-edit' })
  208. } else {
  209. uni.navigateTo({ url: '/pages/login/login?redirect=' + encodeURIComponent('/pages/article-center/index') })
  210. }
  211. },
  212. formatDate(dateStr) {
  213. if (!dateStr) return ''
  214. return dateStr.slice(0, 10)
  215. },
  216. parseDimensions(str) {
  217. if (!str) return []
  218. var dimMap = {
  219. body: { code: 'body', color: '#FF8C42', name: '身' },
  220. mind: { code: 'mind', color: '#FF6B9D', name: '心' },
  221. wisdom: { code: 'wisdom', color: '#6366F1', name: '智' },
  222. action: { code: 'action', color: '#10B981', name: '行' },
  223. wealth: { code: 'wealth', color: '#F59E0B', name: '富' }
  224. }
  225. var codes = str.split(',').map(function(s) { return s.trim().toLowerCase() })
  226. return codes.filter(function(c) { return dimMap[c] }).map(function(c) { return dimMap[c] })
  227. },
  228. focusSearch() {
  229. this.searchFocused = true
  230. },
  231. onSearch() {
  232. this.page = 1
  233. this.articles = []
  234. this.noMore = false
  235. this.loadArticles()
  236. },
  237. onSearchBlur() {
  238. if (!this.searchKeyword) {
  239. this.searchFocused = false
  240. }
  241. },
  242. onBack() {
  243. uni.navigateBack()
  244. }
  245. }
  246. }
  247. </script>
  248. <style scoped>
  249. .ac-container {
  250. min-height: 100vh;
  251. background: #f5f7fa;
  252. position: relative;
  253. }
  254. /* 自定义导航栏 */
  255. .kc-nav {
  256. display: flex;
  257. align-items: center;
  258. padding: 80rpx 20rpx 16rpx;
  259. background: linear-gradient(135deg, #F97316, #EA580C);
  260. position: sticky;
  261. top: 0;
  262. z-index: 100;
  263. }
  264. .kc-nav-left {
  265. width: 50rpx;
  266. height: 50rpx;
  267. display: flex;
  268. align-items: center;
  269. justify-content: center;
  270. flex-shrink: 0;
  271. }
  272. .kc-back-icon {
  273. font-size: 36rpx;
  274. color: #fff;
  275. font-weight: bold;
  276. }
  277. .kc-nav-search {
  278. flex: 1;
  279. display: flex;
  280. align-items: center;
  281. background: rgba(255,255,255,0.25);
  282. border-radius: 32rpx;
  283. padding: 10rpx 20rpx;
  284. margin: 0 16rpx;
  285. }
  286. .kc-search-icon {
  287. font-size: 24rpx;
  288. margin-right: 10rpx;
  289. flex-shrink: 0;
  290. }
  291. .kc-search-placeholder {
  292. font-size: 24rpx;
  293. color: rgba(255,255,255,0.7);
  294. }
  295. .kc-search-input {
  296. flex: 1;
  297. font-size: 24rpx;
  298. color: #fff;
  299. background: transparent;
  300. }
  301. .kc-search-input::placeholder {
  302. color: rgba(255,255,255,0.7);
  303. }
  304. .kc-nav-right {
  305. width: 50rpx;
  306. height: 50rpx;
  307. display: flex;
  308. align-items: center;
  309. justify-content: center;
  310. flex-shrink: 0;
  311. }
  312. .kc-add-btn {
  313. font-size: 48rpx;
  314. color: #fff;
  315. font-weight: 300;
  316. line-height: 1;
  317. }
  318. .ac-tabs {
  319. background: #fff;
  320. padding: 16rpx 0 12rpx;
  321. border-bottom: 1rpx solid #eee;
  322. position: sticky;
  323. top: 0;
  324. z-index: 10;
  325. }
  326. .tab-scroll {
  327. white-space: nowrap;
  328. padding: 0 20rpx;
  329. }
  330. .tab-item {
  331. display: inline-block;
  332. padding: 8rpx 24rpx;
  333. font-size: 26rpx;
  334. color: #666;
  335. margin-right: 12rpx;
  336. border-bottom: 3rpx solid transparent;
  337. flex-shrink: 0;
  338. }
  339. .tab-item.active {
  340. font-weight: bold;
  341. }
  342. .ac-list {
  343. height: calc(100vh - 230rpx);
  344. }
  345. /* 骨架屏 */
  346. .ac-skeleton { padding: 20rpx 24rpx; }
  347. .skeleton-card {
  348. background: #fff;
  349. border-radius: 16rpx;
  350. overflow: hidden;
  351. margin-bottom: 20rpx;
  352. }
  353. .skeleton-cover { height: 280rpx; background: linear-gradient(90deg, #f0f0f0 25%, #e0e0e0 50%, #f0f0f0 75%); background-size: 200% 100%; animation: shimmer 1.5s infinite; }
  354. .skeleton-body { padding: 20rpx; }
  355. .skeleton-line { height: 24rpx; background: linear-gradient(90deg, #f0f0f0 25%, #e0e0e0 50%, #f0f0f0 75%); background-size: 200% 100%; border-radius: 4rpx; margin-bottom: 12rpx; }
  356. .skeleton-title { width: 60%; }
  357. .skeleton-summary { width: 90%; }
  358. .skeleton-summary.short { width: 50%; }
  359. .skeleton-meta { width: 40%; height: 18rpx; }
  360. @keyframes shimmer { 0% { background-position: -200% 0; } 100% { background-position: 200% 0; } }
  361. /* 空状态 */
  362. .ac-empty { display: flex; flex-direction: column; align-items: center; padding-top: 200rpx; }
  363. .empty-img { width: 200rpx; height: 200rpx; margin-bottom: 24rpx; }
  364. .empty-text { font-size: 28rpx; color: #999; }
  365. /* 文章卡片 */
  366. .article-list { padding: 20rpx 24rpx; }
  367. .article-card {
  368. background: #fff;
  369. border-radius: 16rpx;
  370. overflow: hidden;
  371. margin-bottom: 20rpx;
  372. box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.06);
  373. }
  374. .card-cover { width: 100%; height: 280rpx; background: #f0f0f0; }
  375. .card-body { padding: 20rpx; }
  376. .card-tags { display: flex; align-items: center; margin-bottom: 10rpx; }
  377. .card-category { font-size: 20rpx; color: #5B9BD5; background: rgba(91,155,213,0.1); padding: 2rpx 12rpx; border-radius: 8rpx; margin-right: 12rpx; }
  378. .card-readtime { font-size: 20rpx; color: #999; }
  379. .card-title { display: block; font-size: 28rpx; font-weight: bold; color: #333; margin-bottom: 8rpx; line-height: 1.4; overflow: hidden; text-overflow: ellipsis; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
  380. .card-summary { display: block; font-size: 24rpx; color: #666; line-height: 1.5; margin-bottom: 12rpx; overflow: hidden; text-overflow: ellipsis; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
  381. .card-footer { display: flex; align-items: center; font-size: 20rpx; color: #999; }
  382. .card-author { margin-right: 16rpx; color: #5B9BD5; }
  383. .card-date { margin-right: 16rpx; }
  384. .card-fav { margin-left: auto; }
  385. /* 五维彩条 */
  386. .card-dimensions { display: flex; margin-top: 10rpx; gap: 8rpx; }
  387. .dim-dot { width: 20rpx; height: 20rpx; border-radius: 50%; }
  388. /* 加载更多 */
  389. .loading-more, .no-more { text-align: center; padding: 30rpx; }
  390. .loading-text { font-size: 24rpx; color: #999; }
  391. .no-more-text { font-size: 24rpx; color: #ccc; }
  392. .bottom-spacer { height: 120rpx; }
  393. /* 浮动按钮 */
  394. .ac-fab {
  395. position: fixed;
  396. right: 40rpx;
  397. bottom: 100rpx;
  398. width: 100rpx;
  399. height: 100rpx;
  400. background: linear-gradient(135deg, #F97316, #EA580C);
  401. color: #fff;
  402. border-radius: 50%;
  403. display: flex;
  404. align-items: center;
  405. justify-content: center;
  406. box-shadow: 0 4rpx 16rpx rgba(249,115,22,0.4);
  407. z-index: 100;
  408. }
  409. .fab-icon { font-size: 48rpx; font-weight: bold; line-height: 1; }
  410. </style>