articles.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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 lang="scss">
  262. // ===== 页面级局部调色板(单页专有色阶;同值才映射全局令牌,约定见 uni.scss §14) =====
  263. // 文章类型标签四色(类型语义,本页专有;article 浅底已提全局 $tint-info-soft)
  264. $c-type-article-ink: #2b7fc4; // 文章(蓝)
  265. $c-type-knowledge-bg: #e8f8ee; // 知识(绿浅底)
  266. $c-type-knowledge-ink:#2a9055; // 知识(绿)
  267. $c-type-course-bg: #fef3e2; // 课程(橙浅底)
  268. $c-type-course-ink: #c47a2b; // 课程(橙)
  269. $c-type-tip-bg: #f3e8fd; // 贴士(紫浅底)
  270. $c-type-tip-ink: #7b2bc4; // 贴士(紫)
  271. .container {
  272. display: flex;
  273. flex-direction: column;
  274. min-height: 100vh;
  275. background: $app-bg;
  276. }
  277. /* 筛选条 */
  278. .filter-section {
  279. background: $app-surface;
  280. padding: 16rpx 0 12rpx;
  281. border-bottom: 1rpx solid $facade-fill-3;
  282. }
  283. .dimension-scroll,
  284. .category-scroll {
  285. white-space: nowrap;
  286. }
  287. .dimension-scroll {
  288. border-bottom: 1rpx solid $facade-fill;
  289. padding-bottom: 12rpx;
  290. }
  291. .filter-chips {
  292. display: flex;
  293. padding: 0 20rpx;
  294. gap: 16rpx;
  295. }
  296. .filter-chip {
  297. display: inline-block;
  298. padding: 8rpx 24rpx;
  299. font-size: 24rpx;
  300. color: $facade-text-body;
  301. background: $facade-fill;
  302. border-radius: 30rpx;
  303. flex-shrink: 0;
  304. }
  305. .filter-chip.active {
  306. background: $brand-primary;
  307. color: $app-surface;
  308. }
  309. .filter-chip:first-child {
  310. margin-left: 4rpx;
  311. }
  312. /* 文章元信息标签 */
  313. .article-meta {
  314. display: flex;
  315. align-items: center;
  316. flex-wrap: wrap;
  317. margin-bottom: 10rpx;
  318. }
  319. .article-type-tags {
  320. display: flex;
  321. align-items: center;
  322. gap: 8rpx;
  323. margin-left: auto;
  324. }
  325. .type-tag {
  326. font-size: 18rpx;
  327. padding: 2rpx 10rpx;
  328. border-radius: 6rpx;
  329. background: $facade-fill;
  330. color: $facade-text-body;
  331. }
  332. .type-tag.type-article {
  333. background: $tint-info-soft;
  334. color: $c-type-article-ink;
  335. }
  336. .type-tag.type-knowledge {
  337. background: $c-type-knowledge-bg;
  338. color: $c-type-knowledge-ink;
  339. }
  340. .type-tag.type-course {
  341. background: $c-type-course-bg;
  342. color: $c-type-course-ink;
  343. }
  344. .type-tag.type-tip {
  345. background: $c-type-tip-bg;
  346. color: $c-type-tip-ink;
  347. }
  348. .difficulty-tag {
  349. font-size: 18rpx;
  350. color: $dim-wealth;
  351. }
  352. /* 文章列表 */
  353. .article-list {
  354. flex: 1;
  355. height: calc(100vh - 220rpx);
  356. }
  357. .loading-wrap,
  358. .empty-wrap {
  359. display: flex;
  360. flex-direction: column;
  361. align-items: center;
  362. padding-top: 120rpx;
  363. }
  364. .loading-text {
  365. font-size: 26rpx;
  366. color: $facade-text-muted;
  367. }
  368. .empty-icon {
  369. font-size: 100rpx;
  370. margin-bottom: 24rpx;
  371. }
  372. .empty-text {
  373. font-size: 28rpx;
  374. color: $facade-text-muted;
  375. }
  376. .article-grid {
  377. padding: 20rpx 24rpx;
  378. }
  379. .article-card {
  380. background: $app-surface;
  381. border-radius: 16rpx;
  382. overflow: hidden;
  383. margin-bottom: 20rpx;
  384. box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.06);
  385. }
  386. .article-cover {
  387. width: 100%;
  388. height: 280rpx;
  389. background: linear-gradient(135deg, $accent-indigo, $accent-indigo-deep);
  390. }
  391. .article-body {
  392. padding: 20rpx;
  393. }
  394. .article-meta {
  395. display: flex;
  396. align-items: center;
  397. margin-bottom: 10rpx;
  398. }
  399. .article-category {
  400. font-size: 20rpx;
  401. color: $brand-primary;
  402. background: rgba(249,115,22,0.1);
  403. padding: 2rpx 12rpx;
  404. border-radius: 8rpx;
  405. margin-right: 12rpx;
  406. }
  407. .article-date {
  408. font-size: 20rpx;
  409. color: $facade-text-muted;
  410. }
  411. .article-title {
  412. display: block;
  413. font-size: 28rpx;
  414. font-weight: bold;
  415. color: $facade-text-strong;
  416. margin-bottom: 8rpx;
  417. line-height: 1.4;
  418. }
  419. .article-summary {
  420. display: block;
  421. font-size: 24rpx;
  422. color: $facade-text-body;
  423. line-height: 1.5;
  424. margin-bottom: 12rpx;
  425. overflow: hidden;
  426. text-overflow: ellipsis;
  427. display: -webkit-box;
  428. -webkit-line-clamp: 2;
  429. -webkit-box-orient: vertical;
  430. }
  431. .article-footer {
  432. display: flex;
  433. justify-content: space-between;
  434. align-items: center;
  435. }
  436. .article-author {
  437. font-size: 20rpx;
  438. color: $facade-text-muted;
  439. }
  440. .article-read-count {
  441. font-size: 20rpx;
  442. color: $facade-text-ghost-light;
  443. }
  444. .loading-more,
  445. .no-more {
  446. text-align: center;
  447. padding: 30rpx;
  448. }
  449. .no-more-text {
  450. font-size: 24rpx;
  451. color: $facade-text-ghost;
  452. }
  453. /* 未登录提示 */
  454. .login-hint-bar {
  455. text-align: center;
  456. padding: 20rpx;
  457. background: $hint-cream-bg;
  458. border-top: 1rpx solid $hint-cream-border;
  459. margin: 20rpx 24rpx 0;
  460. border-radius: 12rpx;
  461. }
  462. .login-hint-text {
  463. font-size: 24rpx;
  464. color: $hint-gold-ink;
  465. }
  466. /* 底部占位 */
  467. .bottom-spacer {
  468. height: 120rpx;
  469. }
  470. </style>