articles.vue 12 KB

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