articles.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. <template>
  2. <view class="container" :style="{ '--font-scale': fontScale }">
  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:' + whiteColor : ''"
  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 { BrandColors } from '../../utils/brand-colors.js'
  87. import { getArticleCategories, getArticleList } from '../../utils/api.js'
  88. import config from '@/config.js'
  89. var dimensionConfig = {
  90. all: { label: '全部', color: BrandColors.slate500, theme: 'mind-wisdom', tagline: '探索知识,滋养成长', quote: '学而时习之,不亦说乎' },
  91. body: { label: '身', color: BrandColors.body, theme: 'health', tagline: '主动健康,从知识开始', quote: '健康是人生的第一财富' },
  92. mind: { label: '心', color: BrandColors.mind, theme: 'mind-wisdom', tagline: '探索内心世界,培养健康心智', quote: '知己知彼,百战不殆' },
  93. wisdom: { label: '智', color: BrandColors.wisdom, theme: 'wisdom', tagline: '聪明学习,快乐成长', quote: '学而不思则罔,思而不学则殆' },
  94. action:{ label: '行', color: BrandColors.success, theme: 'action', tagline: '知行合一,方得始终', quote: '纸上得来终觉浅,绝知此事要躬行' },
  95. wealth:{ label: '富', color: BrandColors.wealth, theme: 'wealth', tagline: '理财智慧,从小培养', quote: '君子爱财,取之有道' }
  96. }
  97. import fontScaleMixin from '@/mixins/font-scale.js'
  98. export default {
  99. mixins: [fontScaleMixin],
  100. components: { },
  101. data() {
  102. return {
  103. whiteColor: BrandColors.white,
  104. dimensionList: Object.keys(dimensionConfig).map(function(k) {
  105. return { code: k, label: dimensionConfig[k].label, color: dimensionConfig[k].color }
  106. }),
  107. currentDimension: 'all',
  108. categoryList: [{ value: '', label: '全部' }],
  109. categoryMap: {},
  110. currentCategory: '',
  111. articles: [],
  112. page: 1,
  113. size: 10,
  114. total: 0,
  115. loading: false,
  116. loadingMore: false,
  117. noMore: false,
  118. isLoggedIn: false
  119. }
  120. },
  121. computed: {
  122. bannerTheme: function() {
  123. return dimensionConfig[this.currentDimension] ? dimensionConfig[this.currentDimension].theme : 'mind-wisdom'
  124. },
  125. bannerTagline: function() {
  126. return dimensionConfig[this.currentDimension] ? dimensionConfig[this.currentDimension].tagline : ''
  127. },
  128. bannerQuote: function() {
  129. return dimensionConfig[this.currentDimension] ? dimensionConfig[this.currentDimension].quote : ''
  130. }
  131. },
  132. onLoad: function(options) {
  133. this.isLoggedIn = !!uni.getStorageSync('token')
  134. if (options && options.dimension && dimensionConfig[options.dimension]) {
  135. this.currentDimension = options.dimension
  136. }
  137. this.loadCategories()
  138. },
  139. onShow: function() {
  140. this.isLoggedIn = !!uni.getStorageSync('token')
  141. this.currentChildId = uni.getStorageSync('currentChildId') || null
  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.currentChildId) {
  190. params.memberId = parseInt(this.currentChildId)
  191. }
  192. if (this.currentCategory) {
  193. params.categoryId = this.currentCategory
  194. }
  195. if (this.currentDimension !== 'all') {
  196. params.dimensionCode = this.currentDimension
  197. }
  198. const res = await getArticleList(params)
  199. if (res.code === 200 && res.data) {
  200. var pageData = res.data
  201. var list = pageData.records || []
  202. var mapped = list.map(function(item) {
  203. return {
  204. id: item.id,
  205. title: item.title || '',
  206. summary: self.stripHtml(item.summary || item.content),
  207. coverImage: item.coverImage || '',
  208. author: item.author || '浠艾福',
  209. categoryName: item.categoryName || item.category || '',
  210. publishDate: item.publishedAt ? item.publishedAt.slice(0, 10) : '',
  211. readCount: item.viewCount || 0,
  212. contentType: item.contentType || '',
  213. difficultyLevel: item.difficultyLevel || 0
  214. }
  215. })
  216. if (this.page === 1) {
  217. this.articles = mapped
  218. } else {
  219. this.articles = this.articles.concat(mapped)
  220. }
  221. this.total = pageData.total || 0
  222. this.noMore = this.articles.length >= this.total
  223. } else {
  224. if (this.page === 1) {
  225. this.articles = []
  226. }
  227. }
  228. } catch (e) {
  229. uni.showToast({ title: '加载失败', icon: 'none' })
  230. } finally {
  231. this.loading = false
  232. }
  233. },
  234. onLoadMore() {
  235. if (this.noMore || this.loading) return
  236. this.page++
  237. this.loadArticles()
  238. },
  239. goDetail(id) {
  240. uni.navigateTo({ url: '/pages/article-center/article-detail?id=' + id })
  241. },
  242. stripHtml: function(html) {
  243. if (!html) return ''
  244. return html
  245. .replace(/<style[\s\S]*?<\/style>/gi, ' ')
  246. .replace(/<script[\s\S]*?<\/script>/gi, ' ')
  247. .replace(/<[^>]+>/g, ' ')
  248. .replace(/&nbsp;/gi, ' ')
  249. .replace(/&lt;/gi, '<')
  250. .replace(/&gt;/gi, '>')
  251. .replace(/&amp;/gi, '&')
  252. .replace(/&quot;/gi, '"')
  253. .replace(/\s+/g, ' ')
  254. .trim()
  255. },getImageUrl: function(path) {
  256. if (!path || path === 'null' || path === 'undefined' || path === '/null') return ''
  257. var s = String(path).trim()
  258. if (!s || s === 'null' || s === '/null') return ''
  259. if (s.indexOf('http://') === 0 || s.indexOf('https://') === 0) return s
  260. return config.API_BASE_URL + (s.charAt(0) === '/' ? s : '/' + s)
  261. }
  262. }
  263. }
  264. </script>
  265. <style scoped lang="scss">
  266. // ===== 页面级局部调色板(单页专有色阶;同值才映射全局令牌,约定见 uni.scss §14) =====
  267. // 文章类型标签四色(类型语义,本页专有;article 浅底已提全局 $tint-info-soft)
  268. $c-type-article-ink: #2b7fc4; // 文章(蓝)
  269. $c-type-knowledge-bg: #e8f8ee; // 知识(绿浅底)
  270. $c-type-knowledge-ink:#2a9055; // 知识(绿)
  271. $c-type-course-bg: #fef3e2; // 课程(橙浅底)
  272. $c-type-course-ink: #c47a2b; // 课程(橙)
  273. $c-type-tip-bg: #f3e8fd; // 贴士(紫浅底)
  274. $c-type-tip-ink: #7b2bc4; // 贴士(紫)
  275. .container {
  276. display: flex;
  277. flex-direction: column;
  278. min-height: 100vh;
  279. background: $app-bg;
  280. }
  281. /* 筛选条 */
  282. .filter-section {
  283. background: $app-surface;
  284. padding: 16rpx 0 12rpx;
  285. border-bottom: 1rpx solid $facade-fill-3;
  286. }
  287. .dimension-scroll,
  288. .category-scroll {
  289. white-space: nowrap;
  290. }
  291. .dimension-scroll {
  292. border-bottom: 1rpx solid $facade-fill;
  293. padding-bottom: 12rpx;
  294. }
  295. .filter-chips {
  296. display: flex;
  297. padding: 0 20rpx;
  298. gap: 16rpx;
  299. }
  300. .filter-chip {
  301. display: inline-block;
  302. padding: 8rpx 24rpx;
  303. font-size: calc(24rpx * var(--font-scale, 1));
  304. color: $facade-text-body;
  305. background: $facade-fill;
  306. border-radius: 24rpx;
  307. flex-shrink: 0;
  308. }
  309. .filter-chip.active {
  310. background: $brand-primary;
  311. color: $app-surface;
  312. }
  313. .filter-chip:first-child {
  314. margin-left: 4rpx;
  315. }
  316. /* 文章元信息标签 */
  317. .article-meta {
  318. display: flex;
  319. align-items: center;
  320. flex-wrap: wrap;
  321. margin-bottom: 10rpx;
  322. }
  323. .article-type-tags {
  324. display: flex;
  325. align-items: center;
  326. gap: 8rpx;
  327. margin-left: auto;
  328. }
  329. .type-tag {
  330. font-size: calc(18rpx * var(--font-scale, 1));
  331. padding: 2rpx 10rpx;
  332. border-radius: 8rpx;
  333. background: $facade-fill;
  334. color: $facade-text-body;
  335. }
  336. .type-tag.type-article {
  337. background: $tint-info-soft;
  338. color: $c-type-article-ink;
  339. }
  340. .type-tag.type-knowledge {
  341. background: $c-type-knowledge-bg;
  342. color: $c-type-knowledge-ink;
  343. }
  344. .type-tag.type-course {
  345. background: $c-type-course-bg;
  346. color: $c-type-course-ink;
  347. }
  348. .type-tag.type-tip {
  349. background: $c-type-tip-bg;
  350. color: $c-type-tip-ink;
  351. }
  352. .difficulty-tag {
  353. font-size: calc(18rpx * var(--font-scale, 1));
  354. color: $dim-wealth;
  355. }
  356. /* 文章列表 */
  357. .article-list {
  358. flex: 1;
  359. height: calc(100vh - 220rpx);
  360. }
  361. .loading-wrap,
  362. .empty-wrap {
  363. display: flex;
  364. flex-direction: column;
  365. align-items: center;
  366. padding-top: 120rpx;
  367. }
  368. .loading-text {
  369. font-size: calc(26rpx * var(--font-scale, 1));
  370. color: $facade-text-muted;
  371. }
  372. .empty-icon {
  373. font-size: calc($uni-font-size-6xl * var(--font-scale, 1));
  374. margin-bottom: 24rpx;
  375. }
  376. .empty-text {
  377. font-size: calc(28rpx * var(--font-scale, 1));
  378. color: $facade-text-muted;
  379. }
  380. .article-grid {
  381. padding: 20rpx 24rpx;
  382. }
  383. .article-card {
  384. background: $app-surface;
  385. border-radius: 16rpx;
  386. overflow: hidden;
  387. margin-bottom: 20rpx;
  388. box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.06);
  389. }
  390. .article-cover {
  391. width: 100%;
  392. height: 280rpx;
  393. background: linear-gradient(135deg, $accent-indigo, $accent-indigo-deep);
  394. }
  395. .article-body {
  396. padding: 20rpx;
  397. }
  398. .article-meta {
  399. display: flex;
  400. align-items: center;
  401. margin-bottom: 10rpx;
  402. }
  403. .article-category {
  404. font-size: calc(20rpx * var(--font-scale, 1));
  405. color: $brand-primary;
  406. background: rgba(249,115,22,0.1);
  407. padding: 2rpx 12rpx;
  408. border-radius: 8rpx;
  409. margin-right: 12rpx;
  410. }
  411. .article-date {
  412. font-size: calc(20rpx * var(--font-scale, 1));
  413. color: $facade-text-muted;
  414. }
  415. .article-title {
  416. display: block;
  417. font-size: calc(28rpx * var(--font-scale, 1));
  418. font-weight: bold;
  419. color: $facade-text-strong;
  420. margin-bottom: 8rpx;
  421. line-height: 1.4;
  422. }
  423. .article-summary {
  424. display: block;
  425. font-size: calc(24rpx * var(--font-scale, 1));
  426. color: $facade-text-body;
  427. line-height: 1.5;
  428. margin-bottom: 12rpx;
  429. overflow: hidden;
  430. text-overflow: ellipsis;
  431. display: -webkit-box;
  432. -webkit-line-clamp: 2;
  433. -webkit-box-orient: vertical;
  434. }
  435. .article-footer {
  436. display: flex;
  437. justify-content: space-between;
  438. align-items: center;
  439. }
  440. .article-author {
  441. font-size: calc(20rpx * var(--font-scale, 1));
  442. color: $facade-text-muted;
  443. }
  444. .article-read-count {
  445. font-size: calc(20rpx * var(--font-scale, 1));
  446. color: $facade-text-ghost-light;
  447. }
  448. .loading-more,
  449. .no-more {
  450. text-align: center;
  451. padding: $uni-space-page;
  452. }
  453. .no-more-text {
  454. font-size: calc(24rpx * var(--font-scale, 1));
  455. color: $facade-text-ghost;
  456. }
  457. /* 未登录提示 */
  458. .login-hint-bar {
  459. text-align: center;
  460. padding: 20rpx;
  461. background: $hint-cream-bg;
  462. border-top: 1rpx solid $hint-cream-border;
  463. margin: 20rpx 24rpx 0;
  464. border-radius: 16rpx;
  465. }
  466. .login-hint-text {
  467. font-size: calc(24rpx * var(--font-scale, 1));
  468. color: $hint-gold-ink;
  469. }
  470. /* 底部占位 */
  471. .bottom-spacer {
  472. height: 120rpx;
  473. }
  474. </style>