articles.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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. this.currentChildId = uni.getStorageSync('currentChildId') || null
  138. },
  139. methods: {
  140. contentTypeLabel: function(type) {
  141. var labels = { article: '文章', knowledge: '知识点', course: '课程', tip: '贴士' }
  142. return labels[type] || type
  143. },
  144. async loadCategories() {
  145. try {
  146. const res = await getArticleCategories()
  147. if (res.code === 200 && res.data) {
  148. const cats = Array.isArray(res.data) ? res.data : []
  149. var map = {}
  150. cats.forEach(function(c) { map[c.id] = c.name })
  151. this.categoryMap = map
  152. this.categoryList = [{ value: '', label: '全部' }].concat(cats.map(function(c) {
  153. return { value: c.id, label: c.name }
  154. }))
  155. }
  156. } catch (e) {
  157. this.categoryList = [
  158. { value: '', label: '全部' },
  159. { value: 1, label: '心理健康' },
  160. { value: 2, label: '情绪管理' }
  161. ]
  162. }
  163. this.loadArticles()
  164. },
  165. onDimensionChange: function(code) {
  166. this.currentDimension = code
  167. this.page = 1
  168. this.articles = []
  169. this.noMore = false
  170. this.loadArticles()
  171. },
  172. onCategoryChange: function(id) {
  173. this.currentCategory = id
  174. this.page = 1
  175. this.articles = []
  176. this.noMore = false
  177. this.loadArticles()
  178. },
  179. async loadArticles() {
  180. if (this.loading) return
  181. this.loading = true
  182. var self = this
  183. try {
  184. var params = { page: this.page, size: this.size }
  185. if (this.currentChildId) {
  186. params.memberId = parseInt(this.currentChildId)
  187. }
  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: self.stripHtml(item.summary || item.content),
  203. coverImage: item.coverImage || '',
  204. author: item.author || '浠艾福',
  205. categoryName: item.categoryName || item.category || '',
  206. publishDate: item.publishedAt ? item.publishedAt.slice(0, 10) : '',
  207. readCount: item.viewCount || 0,
  208. contentType: item.contentType || '',
  209. difficultyLevel: item.difficultyLevel || 0
  210. }
  211. })
  212. if (this.page === 1) {
  213. this.articles = mapped
  214. } else {
  215. this.articles = this.articles.concat(mapped)
  216. }
  217. this.total = pageData.total || 0
  218. this.noMore = this.articles.length >= this.total
  219. } else {
  220. if (this.page === 1) {
  221. this.articles = []
  222. }
  223. }
  224. } catch (e) {
  225. uni.showToast({ title: '加载失败', icon: 'none' })
  226. } finally {
  227. this.loading = false
  228. }
  229. },
  230. onLoadMore() {
  231. if (this.noMore || this.loading) return
  232. this.page++
  233. this.loadArticles()
  234. },
  235. goDetail(id) {
  236. uni.navigateTo({ url: '/pages/article-center/article-detail?id=' + id })
  237. },
  238. stripHtml: function(html) {
  239. if (!html) return ''
  240. return html
  241. .replace(/<style[\s\S]*?<\/style>/gi, ' ')
  242. .replace(/<script[\s\S]*?<\/script>/gi, ' ')
  243. .replace(/<[^>]+>/g, ' ')
  244. .replace(/&nbsp;/gi, ' ')
  245. .replace(/&lt;/gi, '<')
  246. .replace(/&gt;/gi, '>')
  247. .replace(/&amp;/gi, '&')
  248. .replace(/&quot;/gi, '"')
  249. .replace(/\s+/g, ' ')
  250. .trim()
  251. },
  252. getImageUrl: function(path) {
  253. return config.API_BASE_URL + path
  254. }
  255. }
  256. }
  257. </script>
  258. <style scoped>
  259. .container {
  260. display: flex;
  261. flex-direction: column;
  262. min-height: 100vh;
  263. background: #f5f7fa;
  264. }
  265. /* 筛选条 */
  266. .filter-section {
  267. background: #fff;
  268. padding: 16rpx 0 12rpx;
  269. border-bottom: 1rpx solid #eee;
  270. }
  271. .dimension-scroll,
  272. .category-scroll {
  273. white-space: nowrap;
  274. }
  275. .dimension-scroll {
  276. border-bottom: 1rpx solid #f0f0f0;
  277. padding-bottom: 12rpx;
  278. }
  279. .filter-chips {
  280. display: flex;
  281. padding: 0 20rpx;
  282. gap: 16rpx;
  283. }
  284. .filter-chip {
  285. display: inline-block;
  286. padding: 8rpx 24rpx;
  287. font-size: 24rpx;
  288. color: #666;
  289. background: #f0f0f0;
  290. border-radius: 30rpx;
  291. flex-shrink: 0;
  292. }
  293. .filter-chip.active {
  294. background: #5B9BD5;
  295. color: #fff;
  296. }
  297. .filter-chip:first-child {
  298. margin-left: 4rpx;
  299. }
  300. /* 文章元信息标签 */
  301. .article-meta {
  302. display: flex;
  303. align-items: center;
  304. flex-wrap: wrap;
  305. margin-bottom: 10rpx;
  306. }
  307. .article-type-tags {
  308. display: flex;
  309. align-items: center;
  310. gap: 8rpx;
  311. margin-left: auto;
  312. }
  313. .type-tag {
  314. font-size: 18rpx;
  315. padding: 2rpx 10rpx;
  316. border-radius: 6rpx;
  317. background: #f0f0f0;
  318. color: #666;
  319. }
  320. .type-tag.type-article {
  321. background: #e8f4fd;
  322. color: #2b7fc4;
  323. }
  324. .type-tag.type-knowledge {
  325. background: #e8f8ee;
  326. color: #2a9055;
  327. }
  328. .type-tag.type-course {
  329. background: #fef3e2;
  330. color: #c47a2b;
  331. }
  332. .type-tag.type-tip {
  333. background: #f3e8fd;
  334. color: #7b2bc4;
  335. }
  336. .difficulty-tag {
  337. font-size: 18rpx;
  338. color: #f59e0b;
  339. }
  340. /* 文章列表 */
  341. .article-list {
  342. flex: 1;
  343. height: calc(100vh - 220rpx);
  344. }
  345. .loading-wrap,
  346. .empty-wrap {
  347. display: flex;
  348. flex-direction: column;
  349. align-items: center;
  350. padding-top: 120rpx;
  351. }
  352. .loading-text {
  353. font-size: 26rpx;
  354. color: #999;
  355. }
  356. .empty-icon {
  357. font-size: 100rpx;
  358. margin-bottom: 24rpx;
  359. }
  360. .empty-text {
  361. font-size: 28rpx;
  362. color: #999;
  363. }
  364. .article-grid {
  365. padding: 20rpx 24rpx;
  366. }
  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. .article-cover {
  375. width: 100%;
  376. height: 280rpx;
  377. background: linear-gradient(135deg, #667eea, #764ba2);
  378. }
  379. .article-body {
  380. padding: 20rpx;
  381. }
  382. .article-meta {
  383. display: flex;
  384. align-items: center;
  385. margin-bottom: 10rpx;
  386. }
  387. .article-category {
  388. font-size: 20rpx;
  389. color: #5B9BD5;
  390. background: rgba(91,155,213,0.1);
  391. padding: 2rpx 12rpx;
  392. border-radius: 8rpx;
  393. margin-right: 12rpx;
  394. }
  395. .article-date {
  396. font-size: 20rpx;
  397. color: #999;
  398. }
  399. .article-title {
  400. display: block;
  401. font-size: 28rpx;
  402. font-weight: bold;
  403. color: #333;
  404. margin-bottom: 8rpx;
  405. line-height: 1.4;
  406. }
  407. .article-summary {
  408. display: block;
  409. font-size: 24rpx;
  410. color: #666;
  411. line-height: 1.5;
  412. margin-bottom: 12rpx;
  413. overflow: hidden;
  414. text-overflow: ellipsis;
  415. display: -webkit-box;
  416. -webkit-line-clamp: 2;
  417. -webkit-box-orient: vertical;
  418. }
  419. .article-footer {
  420. display: flex;
  421. justify-content: space-between;
  422. align-items: center;
  423. }
  424. .article-author {
  425. font-size: 20rpx;
  426. color: #999;
  427. }
  428. .article-read-count {
  429. font-size: 20rpx;
  430. color: #bbb;
  431. }
  432. .loading-more,
  433. .no-more {
  434. text-align: center;
  435. padding: 30rpx;
  436. }
  437. .no-more-text {
  438. font-size: 24rpx;
  439. color: #ccc;
  440. }
  441. /* 未登录提示 */
  442. .login-hint-bar {
  443. text-align: center;
  444. padding: 20rpx;
  445. background: #fef9e7;
  446. border-top: 1rpx solid #f0e6c0;
  447. margin: 20rpx 24rpx 0;
  448. border-radius: 12rpx;
  449. }
  450. .login-hint-text {
  451. font-size: 24rpx;
  452. color: #b8860b;
  453. }
  454. /* 底部占位 */
  455. .bottom-spacer {
  456. height: 120rpx;
  457. }
  458. </style>