articles.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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" v-if="item.summary">{{ item.summary }}</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. var self = this
  187. try {
  188. var params = { page: this.page, size: this.size }
  189. if (this.currentCategory) {
  190. params.categoryId = this.currentCategory
  191. }
  192. if (this.currentDimension !== 'all') {
  193. params.dimensionCode = this.currentDimension
  194. }
  195. const res = await getArticleList(params)
  196. if (res.code === 200 && res.data) {
  197. var pageData = res.data
  198. var list = pageData.records || []
  199. var mapped = list.map(function(item) {
  200. return {
  201. id: item.id,
  202. title: item.title || '',
  203. summary: self.stripHtml(item.summary || 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. stripHtml: function(html) {
  240. if (!html) return ''
  241. return html
  242. .replace(/<style[\s\S]*?<\/style>/gi, ' ')
  243. .replace(/<script[\s\S]*?<\/script>/gi, ' ')
  244. .replace(/<[^>]+>/g, ' ')
  245. .replace(/&nbsp;/gi, ' ')
  246. .replace(/&lt;/gi, '<')
  247. .replace(/&gt;/gi, '>')
  248. .replace(/&amp;/gi, '&')
  249. .replace(/&quot;/gi, '"')
  250. .replace(/\s+/g, ' ')
  251. .trim()
  252. },
  253. getImageUrl: function(path) {
  254. return config.API_BASE_URL + path
  255. }
  256. }
  257. }
  258. </script>
  259. <style scoped>
  260. .container {
  261. display: flex;
  262. flex-direction: column;
  263. min-height: 100vh;
  264. background: #f5f7fa;
  265. }
  266. /* 筛选条 */
  267. .filter-section {
  268. background: #fff;
  269. padding: 16rpx 0 12rpx;
  270. border-bottom: 1rpx solid #eee;
  271. }
  272. .dimension-scroll,
  273. .category-scroll {
  274. white-space: nowrap;
  275. }
  276. .dimension-scroll {
  277. border-bottom: 1rpx solid #f0f0f0;
  278. padding-bottom: 12rpx;
  279. }
  280. .filter-chips {
  281. display: flex;
  282. padding: 0 20rpx;
  283. gap: 16rpx;
  284. }
  285. .filter-chip {
  286. display: inline-block;
  287. padding: 8rpx 24rpx;
  288. font-size: 24rpx;
  289. color: #666;
  290. background: #f0f0f0;
  291. border-radius: 30rpx;
  292. flex-shrink: 0;
  293. }
  294. .filter-chip.active {
  295. background: #5B9BD5;
  296. color: #fff;
  297. }
  298. .filter-chip:first-child {
  299. margin-left: 4rpx;
  300. }
  301. /* 文章元信息标签 */
  302. .article-meta {
  303. display: flex;
  304. align-items: center;
  305. flex-wrap: wrap;
  306. margin-bottom: 10rpx;
  307. }
  308. .article-type-tags {
  309. display: flex;
  310. align-items: center;
  311. gap: 8rpx;
  312. margin-left: auto;
  313. }
  314. .type-tag {
  315. font-size: 18rpx;
  316. padding: 2rpx 10rpx;
  317. border-radius: 6rpx;
  318. background: #f0f0f0;
  319. color: #666;
  320. }
  321. .type-tag.type-article {
  322. background: #e8f4fd;
  323. color: #2b7fc4;
  324. }
  325. .type-tag.type-knowledge {
  326. background: #e8f8ee;
  327. color: #2a9055;
  328. }
  329. .type-tag.type-course {
  330. background: #fef3e2;
  331. color: #c47a2b;
  332. }
  333. .type-tag.type-tip {
  334. background: #f3e8fd;
  335. color: #7b2bc4;
  336. }
  337. .difficulty-tag {
  338. font-size: 18rpx;
  339. color: #f59e0b;
  340. }
  341. /* 文章列表 */
  342. .article-list {
  343. flex: 1;
  344. height: calc(100vh - 220rpx);
  345. }
  346. .loading-wrap,
  347. .empty-wrap {
  348. display: flex;
  349. flex-direction: column;
  350. align-items: center;
  351. padding-top: 120rpx;
  352. }
  353. .loading-text {
  354. font-size: 26rpx;
  355. color: #999;
  356. }
  357. .empty-icon {
  358. font-size: 100rpx;
  359. margin-bottom: 24rpx;
  360. }
  361. .empty-text {
  362. font-size: 28rpx;
  363. color: #999;
  364. }
  365. .article-grid {
  366. padding: 20rpx 24rpx;
  367. }
  368. .article-card {
  369. background: #fff;
  370. border-radius: 16rpx;
  371. overflow: hidden;
  372. margin-bottom: 20rpx;
  373. box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.06);
  374. }
  375. .article-cover {
  376. width: 100%;
  377. height: 280rpx;
  378. background: linear-gradient(135deg, #667eea, #764ba2);
  379. }
  380. .article-body {
  381. padding: 20rpx;
  382. }
  383. .article-meta {
  384. display: flex;
  385. align-items: center;
  386. margin-bottom: 10rpx;
  387. }
  388. .article-category {
  389. font-size: 20rpx;
  390. color: #5B9BD5;
  391. background: rgba(91,155,213,0.1);
  392. padding: 2rpx 12rpx;
  393. border-radius: 8rpx;
  394. margin-right: 12rpx;
  395. }
  396. .article-date {
  397. font-size: 20rpx;
  398. color: #999;
  399. }
  400. .article-title {
  401. display: block;
  402. font-size: 28rpx;
  403. font-weight: bold;
  404. color: #333;
  405. margin-bottom: 8rpx;
  406. line-height: 1.4;
  407. }
  408. .article-summary {
  409. display: block;
  410. font-size: 24rpx;
  411. color: #666;
  412. line-height: 1.5;
  413. margin-bottom: 12rpx;
  414. overflow: hidden;
  415. text-overflow: ellipsis;
  416. display: -webkit-box;
  417. -webkit-line-clamp: 2;
  418. -webkit-box-orient: vertical;
  419. }
  420. .article-footer {
  421. display: flex;
  422. justify-content: space-between;
  423. align-items: center;
  424. }
  425. .article-author {
  426. font-size: 20rpx;
  427. color: #999;
  428. }
  429. .article-read-count {
  430. font-size: 20rpx;
  431. color: #bbb;
  432. }
  433. .loading-more,
  434. .no-more {
  435. text-align: center;
  436. padding: 30rpx;
  437. }
  438. .no-more-text {
  439. font-size: 24rpx;
  440. color: #ccc;
  441. }
  442. /* 未登录提示 */
  443. .login-hint-bar {
  444. text-align: center;
  445. padding: 20rpx;
  446. background: #fef9e7;
  447. border-top: 1rpx solid #f0e6c0;
  448. margin: 20rpx 24rpx 0;
  449. border-radius: 12rpx;
  450. }
  451. .login-hint-text {
  452. font-size: 24rpx;
  453. color: #b8860b;
  454. }
  455. /* 底部占位 */
  456. .bottom-spacer {
  457. height: 120rpx;
  458. }
  459. </style>