articles.vue 13 KB

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