articles.vue 13 KB

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