articles.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. <template>
  2. <view class="container">
  3. <!-- 品牌头部 -->
  4. <PageBanner :theme="bannerTheme"
  5. :tagline="bannerTagline"
  6. :quote="bannerQuote" />
  7. <!-- 维度筛选 -->
  8. <view class="filter-section">
  9. <scroll-view scroll-x enable-flex show-scrollbar="false" class="dimension-scroll">
  10. <view class="filter-chips">
  11. <view
  12. v-for="dim in dimensionList"
  13. :key="dim.code"
  14. :class="['filter-chip', currentDimension === dim.code ? 'active' : '']"
  15. :style="currentDimension === dim.code ? 'background:' + dim.color + ';color:#fff' : ''"
  16. @click="onDimensionChange(dim.code)"
  17. >
  18. {{ dim.label }}
  19. </view>
  20. </view>
  21. </scroll-view>
  22. </view>
  23. <!-- 分类筛选 -->
  24. <view class="filter-section" v-if="categoryList.length > 1">
  25. <scroll-view scroll-x enable-flex show-scrollbar="false" class="category-scroll">
  26. <view class="filter-chips">
  27. <view
  28. v-for="cat in categoryList"
  29. :key="cat.value"
  30. :class="['filter-chip', currentCategory === cat.value ? 'active' : '']"
  31. @click="onCategoryChange(cat.value)"
  32. >
  33. {{ cat.label }}
  34. </view>
  35. </view>
  36. </scroll-view>
  37. </view>
  38. <!-- 知识列表 -->
  39. <scroll-view scroll-y class="article-list" @scrolltolower="onLoadMore">
  40. <view v-if="loading && articles.length === 0" class="loading-wrap">
  41. <text class="loading-text">加载中...</text>
  42. </view>
  43. <view v-else-if="articles.length === 0" class="empty-wrap">
  44. <text class="empty-icon">📚</text>
  45. <text class="empty-text">暂无相关知识</text>
  46. </view>
  47. <view v-else class="article-grid">
  48. <view
  49. v-for="item in articles"
  50. :key="item.id"
  51. class="article-card"
  52. @click="goDetail(item.id)"
  53. >
  54. <image
  55. class="article-cover"
  56. :src="getImageUrl(item.coverImage) || '/static/default-article.png'"
  57. mode="aspectFill"
  58. />
  59. <view class="article-body">
  60. <view class="article-meta">
  61. <text class="article-category">{{ item.categoryName || '知识' }}</text>
  62. <view class="article-type-tags">
  63. <text v-if="item.contentType" class="type-tag" :class="'type-' + item.contentType">{{ contentTypeLabel(item.contentType) }}</text>
  64. <text v-if="item.difficultyLevel > 0" class="difficulty-tag">{{ '★'.repeat(item.difficultyLevel) }}</text>
  65. </view>
  66. </view>
  67. <text class="article-title">{{ item.title }}</text>
  68. <text class="article-summary">{{ item.summary || item.content }}</text>
  69. <view class="article-footer">
  70. <text class="article-author">{{ item.author || '浠艾福' }}</text>
  71. <text class="article-read-count">阅读 {{ item.readCount || 0 }}</text>
  72. </view>
  73. </view>
  74. </view>
  75. </view>
  76. <view v-if="loadingMore" class="loading-more">
  77. <text class="loading-text">加载中...</text>
  78. </view>
  79. <view v-if="noMore && articles.length > 0" class="no-more">
  80. <text class="no-more-text">— 没有更多了 —</text>
  81. </view>
  82. <view v-if="!isLoggedIn" class="login-hint-bar">
  83. <text class="login-hint-text">🔒 登录后解锁全部内容</text>
  84. </view>
  85. <view class="bottom-spacer"></view>
  86. </scroll-view>
  87. </view>
  88. </template>
  89. <script>
  90. import PageBanner from '../../components/PageBanner.vue'
  91. import { getArticleCategories, getArticleList } from '../../utils/api.js'
  92. import config from '@/config.js'
  93. var dimensionConfig = {
  94. all: { label: '全部', color: '#64748B', theme: 'mind-wisdom', tagline: '探索知识,滋养成长', quote: '学而时习之,不亦说乎' },
  95. body: { label: '身', color: '#FF8C42', theme: 'health', tagline: '主动健康,从知识开始', quote: '健康是人生的第一财富' },
  96. mind: { label: '心', color: '#FF6B9D', theme: 'mind-wisdom', tagline: '探索内心世界,培养健康心智', quote: '知己知彼,百战不殆' },
  97. wisdom: { label: '智', color: '#6366F1', theme: 'wisdom', tagline: '聪明学习,快乐成长', quote: '学而不思则罔,思而不学则殆' },
  98. action:{ label: '行', color: '#10B981', theme: 'action', tagline: '知行合一,方得始终', quote: '纸上得来终觉浅,绝知此事要躬行' },
  99. wealth:{ label: '富', color: '#F59E0B', theme: 'wealth', tagline: '理财智慧,从小培养', quote: '君子爱财,取之有道' }
  100. }
  101. export default {
  102. components: { PageBanner },
  103. data() {
  104. return {
  105. dimensionList: Object.keys(dimensionConfig).map(function(k) {
  106. return { code: k, label: dimensionConfig[k].label, color: dimensionConfig[k].color }
  107. }),
  108. currentDimension: 'all',
  109. categoryList: [{ value: '', label: '全部' }],
  110. categoryMap: {},
  111. currentCategory: '',
  112. articles: [],
  113. page: 1,
  114. size: 10,
  115. total: 0,
  116. loading: false,
  117. loadingMore: false,
  118. noMore: false,
  119. isLoggedIn: false
  120. }
  121. },
  122. computed: {
  123. bannerTheme: function() {
  124. return dimensionConfig[this.currentDimension] ? dimensionConfig[this.currentDimension].theme : 'mind-wisdom'
  125. },
  126. bannerTagline: function() {
  127. return dimensionConfig[this.currentDimension] ? dimensionConfig[this.currentDimension].tagline : ''
  128. },
  129. bannerQuote: function() {
  130. return dimensionConfig[this.currentDimension] ? dimensionConfig[this.currentDimension].quote : ''
  131. }
  132. },
  133. onLoad: function(options) {
  134. this.isLoggedIn = !!uni.getStorageSync('token')
  135. if (options && options.dimension && dimensionConfig[options.dimension]) {
  136. this.currentDimension = options.dimension
  137. }
  138. this.loadCategories()
  139. },
  140. onShow: function() {
  141. this.isLoggedIn = !!uni.getStorageSync('token')
  142. },
  143. methods: {
  144. contentTypeLabel: function(type) {
  145. var labels = { article: '文章', knowledge: '知识点', course: '课程', tip: '贴士' }
  146. return labels[type] || type
  147. },
  148. async loadCategories() {
  149. try {
  150. const res = await getArticleCategories()
  151. if (res.code === 200 && res.data) {
  152. const cats = Array.isArray(res.data) ? res.data : []
  153. var map = {}
  154. cats.forEach(function(c) { map[c.id] = c.name })
  155. this.categoryMap = map
  156. this.categoryList = [{ value: '', label: '全部' }].concat(cats.map(function(c) {
  157. return { value: c.id, label: c.name }
  158. }))
  159. }
  160. } catch (e) {
  161. this.categoryList = [
  162. { value: '', label: '全部' },
  163. { value: 1, label: '心理健康' },
  164. { value: 2, label: '情绪管理' }
  165. ]
  166. }
  167. this.loadArticles()
  168. },
  169. onDimensionChange: function(code) {
  170. this.currentDimension = code
  171. this.page = 1
  172. this.articles = []
  173. this.noMore = false
  174. this.loadArticles()
  175. },
  176. onCategoryChange: function(id) {
  177. this.currentCategory = id
  178. this.page = 1
  179. this.articles = []
  180. this.noMore = false
  181. this.loadArticles()
  182. },
  183. async loadArticles() {
  184. if (this.loading) return
  185. this.loading = true
  186. try {
  187. var params = { page: this.page, size: this.size }
  188. if (this.currentCategory) {
  189. params.categoryId = this.currentCategory
  190. }
  191. if (this.currentDimension !== 'all') {
  192. params.dimensionCode = this.currentDimension
  193. }
  194. const res = await getArticleList(params)
  195. if (res.code === 200 && res.data) {
  196. var pageData = res.data
  197. var list = pageData.records || []
  198. var mapped = list.map(function(item) {
  199. return {
  200. id: item.id,
  201. title: item.title || '',
  202. summary: item.summary || '',
  203. content: item.content || '',
  204. coverImage: item.coverImage || '',
  205. author: item.author || '浠艾福',
  206. categoryName: item.categoryName || item.category || '',
  207. publishDate: item.publishedAt ? item.publishedAt.slice(0, 10) : '',
  208. readCount: item.viewCount || 0,
  209. contentType: item.contentType || '',
  210. difficultyLevel: item.difficultyLevel || 0
  211. }
  212. })
  213. if (this.page === 1) {
  214. this.articles = mapped
  215. } else {
  216. this.articles = this.articles.concat(mapped)
  217. }
  218. this.total = pageData.total || 0
  219. this.noMore = this.articles.length >= this.total
  220. } else {
  221. if (this.page === 1) {
  222. this.articles = []
  223. }
  224. }
  225. } catch (e) {
  226. uni.showToast({ title: '加载失败', icon: 'none' })
  227. } finally {
  228. this.loading = false
  229. }
  230. },
  231. onLoadMore() {
  232. if (this.noMore || this.loading) return
  233. this.page++
  234. this.loadArticles()
  235. },
  236. goDetail(id) {
  237. uni.navigateTo({ url: '/pages/article-center/article-detail?id=' + id })
  238. },
  239. getImageUrl: function(path) {
  240. return config.API_BASE_URL + path
  241. }
  242. }
  243. }
  244. </script>
  245. <style scoped>
  246. .container {
  247. display: flex;
  248. flex-direction: column;
  249. min-height: 100vh;
  250. background: #f5f7fa;
  251. }
  252. /* 筛选条 */
  253. .filter-section {
  254. background: #fff;
  255. padding: 16rpx 0 12rpx;
  256. border-bottom: 1rpx solid #eee;
  257. }
  258. .dimension-scroll,
  259. .category-scroll {
  260. white-space: nowrap;
  261. }
  262. .dimension-scroll {
  263. border-bottom: 1rpx solid #f0f0f0;
  264. padding-bottom: 12rpx;
  265. }
  266. .filter-chips {
  267. display: flex;
  268. padding: 0 20rpx;
  269. gap: 16rpx;
  270. }
  271. .filter-chip {
  272. display: inline-block;
  273. padding: 8rpx 24rpx;
  274. font-size: 24rpx;
  275. color: #666;
  276. background: #f0f0f0;
  277. border-radius: 30rpx;
  278. flex-shrink: 0;
  279. }
  280. .filter-chip.active {
  281. background: #5B9BD5;
  282. color: #fff;
  283. }
  284. .filter-chip:first-child {
  285. margin-left: 4rpx;
  286. }
  287. /* 文章元信息标签 */
  288. .article-meta {
  289. display: flex;
  290. align-items: center;
  291. flex-wrap: wrap;
  292. margin-bottom: 10rpx;
  293. }
  294. .article-type-tags {
  295. display: flex;
  296. align-items: center;
  297. gap: 8rpx;
  298. margin-left: auto;
  299. }
  300. .type-tag {
  301. font-size: 18rpx;
  302. padding: 2rpx 10rpx;
  303. border-radius: 6rpx;
  304. background: #f0f0f0;
  305. color: #666;
  306. }
  307. .type-tag.type-article {
  308. background: #e8f4fd;
  309. color: #2b7fc4;
  310. }
  311. .type-tag.type-knowledge {
  312. background: #e8f8ee;
  313. color: #2a9055;
  314. }
  315. .type-tag.type-course {
  316. background: #fef3e2;
  317. color: #c47a2b;
  318. }
  319. .type-tag.type-tip {
  320. background: #f3e8fd;
  321. color: #7b2bc4;
  322. }
  323. .difficulty-tag {
  324. font-size: 18rpx;
  325. color: #f59e0b;
  326. }
  327. /* 文章列表 */
  328. .article-list {
  329. flex: 1;
  330. height: calc(100vh - 220rpx);
  331. }
  332. .loading-wrap,
  333. .empty-wrap {
  334. display: flex;
  335. flex-direction: column;
  336. align-items: center;
  337. padding-top: 120rpx;
  338. }
  339. .loading-text {
  340. font-size: 26rpx;
  341. color: #999;
  342. }
  343. .empty-icon {
  344. font-size: 100rpx;
  345. margin-bottom: 24rpx;
  346. }
  347. .empty-text {
  348. font-size: 28rpx;
  349. color: #999;
  350. }
  351. .article-grid {
  352. padding: 20rpx 24rpx;
  353. }
  354. .article-card {
  355. background: #fff;
  356. border-radius: 16rpx;
  357. overflow: hidden;
  358. margin-bottom: 20rpx;
  359. box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.06);
  360. }
  361. .article-cover {
  362. width: 100%;
  363. height: 280rpx;
  364. background: linear-gradient(135deg, #667eea, #764ba2);
  365. }
  366. .article-body {
  367. padding: 20rpx;
  368. }
  369. .article-meta {
  370. display: flex;
  371. align-items: center;
  372. margin-bottom: 10rpx;
  373. }
  374. .article-category {
  375. font-size: 20rpx;
  376. color: #5B9BD5;
  377. background: rgba(91,155,213,0.1);
  378. padding: 2rpx 12rpx;
  379. border-radius: 8rpx;
  380. margin-right: 12rpx;
  381. }
  382. .article-date {
  383. font-size: 20rpx;
  384. color: #999;
  385. }
  386. .article-title {
  387. display: block;
  388. font-size: 28rpx;
  389. font-weight: bold;
  390. color: #333;
  391. margin-bottom: 8rpx;
  392. line-height: 1.4;
  393. }
  394. .article-summary {
  395. display: block;
  396. font-size: 24rpx;
  397. color: #666;
  398. line-height: 1.5;
  399. margin-bottom: 12rpx;
  400. overflow: hidden;
  401. text-overflow: ellipsis;
  402. display: -webkit-box;
  403. -webkit-line-clamp: 2;
  404. -webkit-box-orient: vertical;
  405. }
  406. .article-footer {
  407. display: flex;
  408. justify-content: space-between;
  409. align-items: center;
  410. }
  411. .article-author {
  412. font-size: 20rpx;
  413. color: #999;
  414. }
  415. .article-read-count {
  416. font-size: 20rpx;
  417. color: #bbb;
  418. }
  419. .loading-more,
  420. .no-more {
  421. text-align: center;
  422. padding: 30rpx;
  423. }
  424. .no-more-text {
  425. font-size: 24rpx;
  426. color: #ccc;
  427. }
  428. /* 未登录提示 */
  429. .login-hint-bar {
  430. text-align: center;
  431. padding: 20rpx;
  432. background: #fef9e7;
  433. border-top: 1rpx solid #f0e6c0;
  434. margin: 20rpx 24rpx 0;
  435. border-radius: 12rpx;
  436. }
  437. .login-hint-text {
  438. font-size: 24rpx;
  439. color: #b8860b;
  440. }
  441. /* 底部占位 */
  442. .bottom-spacer {
  443. height: 120rpx;
  444. }
  445. </style>