四零语境前端代码仓库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

811 lines
25 KiB

1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
  1. <template>
  2. <view class="home-container">
  3. <!-- 開動頁面組件 -->
  4. <SplashScreen @close="onSplashClose" />
  5. <!-- 状态栏安全区域 -->
  6. <uv-status-bar></uv-status-bar>
  7. <!-- 顶部搜索栏 -->
  8. <view class="header">
  9. <view class="search-container" @click="goSearch">
  10. <uv-search placeholder="请输入要查询的内容" :show-action="false" shape="round" bg-color="#f5f5f5" color="#666"
  11. height="38" margin="0 200rpx 0 0" placeholderColor="#c6c6c6"></uv-search>
  12. </view>
  13. </view>
  14. <!-- Tab栏 -->
  15. <uv-tabs :list="tabs" :current="activeTab" keyName="title" @change="switchTab" :scrollable="true"
  16. :lineColor="'#000'" :activeStyle="{ color: '#000', fontWeight: '900' }"
  17. :inactiveStyle="{ color: '#606266' }"></uv-tabs>
  18. <!-- 轮播图 -->
  19. <view v-if="activeTab == 0">
  20. <view class="swiper-container" :class="{ 'tablet-swiper': isTablet }">
  21. <uv-swiper :list="bannerList" keyName="image" :height="swiperConfig.height"
  22. :radius="swiperConfig.radius" :previousMargin="swiperConfig.previousMargin"
  23. :nextMargin="swiperConfig.nextMargin" :displayMultipleItems="swiperConfig.displayMultipleItems"
  24. indicator indicatorInactiveColor="#fff" :loading="false" indicatorMode="dot"
  25. indicatorActiveColor="#F95A01" :autoplay="true" :interval="4000" :circular="true"
  26. @click="onBannerClick"></uv-swiper>
  27. </view>
  28. <view>
  29. <articleList :articleList="articleList" />
  30. </view>
  31. <!-- 根据labelBooksData动态渲染书籍区块 -->
  32. <view v-for="(labelData, labelIndex) in labelBooksData" :key="labelIndex" class="section">
  33. <view class="section-header" @click="goLabel(labelData.labelInfo)">
  34. <text class="section-title" v-if="labelData.labelInfo">{{ labelData.labelInfo.title }}</text>
  35. <view class="section-more">
  36. <text>更多</text>
  37. <uv-icon name="arrow-right" size="14" color="#888"></uv-icon>
  38. </view>
  39. </view>
  40. <!-- 第一个label今日更新样式 -->
  41. <!-- <scroll-view v-if="labelIndex === 0" show-scrollbar="false" class="content-scroll" scroll-x="true">
  42. <view class="content-list">
  43. <view v-for="(item, index) in labelData.books" :key="index" class="content-item"
  44. @click="goBook(item)">
  45. <view class="item-cover">
  46. <image :src="item.booksImg || '/static/default-image.png'" mode="aspectFill"></image>
  47. </view>
  48. <view class="item-info">
  49. <text class="item-title">{{ item.booksName }}</text>
  50. <text class="item-author">{{ item.booksAuthor }}</text>
  51. <view class="item-duration">
  52. <image src="/static/play-icon.png" class="item-icon" />
  53. <text>{{ item.duration }}</text>
  54. </view>
  55. </view>
  56. </view>
  57. </view>
  58. </scroll-view> -->
  59. <!-- 第二个label推荐书籍样式 -->
  60. <scroll-view v-if="labelIndex === 0" show-scrollbar="false" class="content-scroll" scroll-x="true">
  61. <view class="book-list">
  62. <view v-for="(book, index) in labelData.books" :key="index" class="book-item"
  63. @click="goBook(book)">
  64. <view class="book-cover">
  65. <image :src="book.booksImg || '/static/default-image.png'" mode="aspectFill"></image>
  66. <view class="book-overlay">
  67. <view class="book-duration" v-if="book.duration">
  68. <image src="/static/alarm-icon.png" class="book-duration-icon" />
  69. <text class="book-duration-text">{{ book.duration }}</text>
  70. </view>
  71. <view class="book-title">{{ book.booksName }}</view>
  72. </view>
  73. </view>
  74. </view>
  75. </view>
  76. </scroll-view>
  77. <!-- 第三个及以后的label网格样式 -->
  78. <view v-else class="book-grid">
  79. <view v-for="(book, index) in labelData.books" :key="index" class="book-grid-item"
  80. @click="goBook(book)">
  81. <view class="book-grid-cover">
  82. <image :src="book.booksImg || '/static/default-image.png'" mode="aspectFill"></image>
  83. </view>
  84. <view class="book-grid-info">
  85. <text class="book-grid-title">{{ book.booksName }}</text>
  86. <!-- <view class="book-grid-meta">
  87. <text class="book-grid-grade">{{ book.categoryName }}/</text>
  88. <image src="/static/play-icon.png" class="book-grid-duration-icon" />
  89. <text class="book-grid-duration">{{ book.duration }}</text>
  90. </view> -->
  91. </view>
  92. </view>
  93. </view>
  94. </view>
  95. <!-- 推荐内容列表 -->
  96. <view class="section">
  97. <view class="recommend-list">
  98. <view @click="goPlan(item.id, item.type)" v-for="(item, index) in recommendList" :key="index"
  99. class="recommend-item">
  100. <image :src="item.img" mode="widthFix" class="recommend-image"></image>
  101. </view>
  102. </view>
  103. </view>
  104. </view>
  105. <view v-else>
  106. <BookList :list="list" />
  107. </view>
  108. <!-- 视频播放弹窗 -->
  109. <VideoPopup ref="videoPopup" />
  110. </view>
  111. </template>
  112. <script>
  113. import SplashScreen from '../components/SplashScreen.vue'
  114. import VideoPopup from '../components/VideoPopup.vue'
  115. import BookList from '@/components/BookList.vue'
  116. import articleList from '@/components/articleList.vue'
  117. import list from '@/mixins/list'
  118. export default {
  119. components: {
  120. SplashScreen,
  121. VideoPopup,
  122. BookList,
  123. articleList,
  124. },
  125. mixins: [list],
  126. data() {
  127. return {
  128. // Tab数据
  129. tabs: [],
  130. activeTab: 0,
  131. // 轮播图数据
  132. bannerList: [
  133. ],
  134. // 书籍分类
  135. labels: [
  136. ],
  137. // 根据label获取的书籍数据(二维数组)
  138. labelBooksData: [],
  139. // 推荐列表数据
  140. recommendList: [
  141. ],
  142. mixinListApi: 'book.list',
  143. // 设备类型检测
  144. isTablet: false,
  145. articleList: []
  146. }
  147. },
  148. computed: {
  149. // 轮播图配置
  150. swiperConfig() {
  151. if (this.isTablet) {
  152. // 平板设备使用卡片式轮播
  153. return {
  154. height: "200",
  155. radius: "16",
  156. previousMargin: "60",
  157. nextMargin: "60",
  158. displayMultipleItems: 1.5
  159. }
  160. } else {
  161. // 手机设备使用普通轮播
  162. return {
  163. height: "121",
  164. radius: "12",
  165. previousMargin: "0",
  166. nextMargin: "0",
  167. displayMultipleItems: 1
  168. }
  169. }
  170. }
  171. },
  172. methods: {
  173. async getArticleList() {
  174. try {
  175. const articleRes = await this.$api.home.getArticle()
  176. if (articleRes.code === 200) {
  177. this.articleList = articleRes.result || []
  178. console.log('文章列表数据:', this.articleList)
  179. }
  180. } catch (error) {
  181. console.error('获取文章列表失败:', error)
  182. this.articleList = []
  183. }
  184. },
  185. mixinSetParams() {
  186. return {
  187. category: this.tabs[this.activeTab]?.id
  188. }
  189. },
  190. // 检测设备类型
  191. detectDevice() {
  192. // #ifdef H5
  193. const userAgent = navigator.userAgent
  194. const screenWidth = window.innerWidth || document.documentElement.clientWidth
  195. const screenHeight = window.innerHeight || document.documentElement.clientHeight
  196. // 判断是否为平板设备
  197. // 1. 屏幕宽度大于768px
  198. // 2. 或者是iPad设备
  199. this.isTablet = screenWidth >= 768 || /iPad|Android.*(?=.*\b(tablet|pad)\b)/i.test(userAgent)
  200. console.log('设备检测结果:', {
  201. screenWidth,
  202. screenHeight,
  203. userAgent,
  204. isTablet: this.isTablet
  205. })
  206. // #endif
  207. // #ifndef H5
  208. // 非H5环境,通过系统信息判断
  209. const systemInfo = uni.getSystemInfoSync()
  210. const screenWidth = systemInfo.screenWidth
  211. this.isTablet = screenWidth >= 768
  212. // #endif
  213. },
  214. // 開動頁面關閉處理
  215. onSplashClose() {
  216. console.log('開動頁面已關閉')
  217. // 可以在這裡添加其他邏輯,比如統計、初始化等
  218. },
  219. // 切换Tab
  220. async switchTab(e) {
  221. console.log('切换Tab:', e.index)
  222. this.activeTab = e.index
  223. if (e.index == 0) {
  224. this.getBooksByLabels()
  225. } else {
  226. this.initPage()
  227. this.getList(true)
  228. }
  229. },
  230. // 轮播图点击事件
  231. onBannerClick(index) {
  232. console.log('点击轮播图:', index)
  233. const bannerItem = this.bannerList[index]
  234. if (!bannerItem) return
  235. // 根据 typ 字段判断跳转类型
  236. switch (bannerItem.typ) {
  237. case '1': // 课程详情
  238. if (bannerItem.bookId) {
  239. uni.navigateTo({
  240. url: '/subPages/home/directory?id=' + bannerItem.bookId
  241. })
  242. }
  243. break
  244. case '2': // 视频播放
  245. if (bannerItem.video) {
  246. this.$refs.videoPopup.open(bannerItem.video)
  247. }
  248. break
  249. case '0': // 富文本内容
  250. if (bannerItem.content) {
  251. uni.navigateTo({
  252. url: '/subPages/home/richtext?content=' + encodeURIComponent(bannerItem.content)
  253. })
  254. }
  255. break
  256. default:
  257. console.log('未知的轮播图类型:', bannerItem.typ)
  258. }
  259. },
  260. // 跳转计划定制
  261. goPlan(id, type) {
  262. uni.navigateTo({
  263. url: '/subPages/home/plan?id=' + id + '&type=' + type
  264. })
  265. },
  266. goSearch() {
  267. uni.navigateTo({
  268. url: '/subPages/home/search'
  269. })
  270. },
  271. goBook(book) {
  272. uni.navigateTo({
  273. url: '/subPages/home/directory?id=' + book.id
  274. })
  275. },
  276. async getBanner() {
  277. const bannerRes = await this.$api.home.getBanner()
  278. if (bannerRes.code === 200) {
  279. this.bannerList = bannerRes.result.map(item => ({
  280. image: item.img,
  281. title: item.title,
  282. typ: item.typ,
  283. bookId: item.bookId,
  284. video: item.video,
  285. content: item.content,
  286. id: item.id
  287. }))
  288. }
  289. },
  290. async getSignup() {
  291. const signupRes = await this.$api.home.getLink()
  292. if (signupRes.code === 200) {
  293. this.recommendList = signupRes.result.map(item => ({
  294. img: item.img,
  295. id: item.id,
  296. type: item.type
  297. }))
  298. }
  299. },
  300. // 获取书籍分类
  301. async getCategory() {
  302. const categoryRes = await this.$api.book.category()
  303. if (categoryRes.code === 200) {
  304. this.tabs = categoryRes.result.map(item => ({
  305. title: item.title,
  306. id: item.id
  307. }))
  308. this.tabs.unshift({
  309. title: '为您推荐',
  310. })
  311. }
  312. },
  313. // 获取书籍标签
  314. async getLabel() {
  315. const labelRes = await this.$api.book.label()
  316. if (labelRes.code === 200) {
  317. this.labels = labelRes.result.map(item => ({
  318. title: item.lable,
  319. id: item.id
  320. }))
  321. }
  322. },
  323. // 根据label数组获取书籍数据
  324. async getBooksByLabels() {
  325. if (!this.labels || this.labels.length === 0) {
  326. console.log('labels数据为空,无法获取书籍')
  327. return
  328. }
  329. try {
  330. // 创建请求数组,每个label发起一次请求
  331. const requests = this.labels.map((label, index) =>
  332. this.$api.book.list({
  333. label: label.id,
  334. pageNo: 1,
  335. pageSize: 6,
  336. // category: this.tabs[this.activeTab].id
  337. }, false)
  338. .then(res => {
  339. if (res.code === 200) {
  340. this.$set(this.labelBooksData, index, {
  341. labelInfo: label,
  342. books: res.result.records || []
  343. })
  344. } else {
  345. console.error(`获取label ${label.title} 的书籍失败:`, res)
  346. this.$set(this.labelBooksData, index, {
  347. labelInfo: label,
  348. books: []
  349. })
  350. }
  351. })
  352. )
  353. // 并发执行所有请求
  354. // const responses = await Promise.all(requests)
  355. // // 创建二维数组存储结果
  356. // this.labelBooksData = responses.map((response, index) => {
  357. // if (response.code === 200) {
  358. // return {
  359. // labelInfo: this.labels[index],
  360. // books: response.result.records || []
  361. // }
  362. // } else {
  363. // console.error(`获取label ${this.labels[index].title} 的书籍失败:`, response)
  364. // return {
  365. // labelInfo: this.labels[index],
  366. // books: []
  367. // }
  368. // }
  369. // })
  370. console.log('根据label获取的书籍数据:', this.labelBooksData)
  371. } catch (error) {
  372. console.error('获取书籍数据失败:', error)
  373. this.labelBooksData = []
  374. }
  375. },
  376. goLabel(label) {
  377. uni.navigateTo({
  378. url: '/subPages/home/search?label=' + label.id
  379. })
  380. },
  381. },
  382. async onShow() {
  383. // 检测设备类型
  384. this.detectDevice()
  385. this.getArticleList()
  386. // 先获取基础数据
  387. await Promise.all([this.getBanner(), this.getSignup(), this.getCategory(), this.getLabel()])
  388. // 根据label数据获取对应的书籍
  389. await this.getBooksByLabels()
  390. },
  391. mounted() {
  392. // 页面挂载时也检测一次设备类型
  393. this.detectDevice()
  394. // #ifdef H5
  395. // 监听窗口大小变化
  396. window.addEventListener('resize', this.detectDevice)
  397. // #endif
  398. },
  399. beforeDestroy() {
  400. // #ifdef H5
  401. // 移除事件监听
  402. window.removeEventListener('resize', this.detectDevice)
  403. // #endif
  404. }
  405. }
  406. </script>
  407. <style lang="scss" scoped>
  408. .home-container {
  409. background: #fff;
  410. min-height: 100vh;
  411. padding-bottom: 80rpx;
  412. }
  413. // 顶部搜索栏
  414. .header {
  415. display: flex;
  416. align-items: center;
  417. padding: 6rpx 32rpx;
  418. background: #fff;
  419. .search-container {
  420. flex: 1;
  421. }
  422. }
  423. // 轮播图容器
  424. .swiper-container {
  425. margin: 20rpx;
  426. border-radius: 12rpx;
  427. overflow: hidden;
  428. // 平板设备的轮播图样式
  429. &.tablet-swiper {
  430. margin: 30rpx 0;
  431. border-radius: 16rpx;
  432. // 卡片式轮播的额外样式
  433. :deep(.uv-swiper) {
  434. .swiper-slide {
  435. transition: all 0.3s ease;
  436. transform-origin: center;
  437. // 非激活状态的卡片
  438. &:not(.swiper-slide-active) {
  439. transform: scale(0.9);
  440. opacity: 0.7;
  441. }
  442. // 激活状态的卡片
  443. &.swiper-slide-active {
  444. transform: scale(1);
  445. opacity: 1;
  446. z-index: 2;
  447. }
  448. }
  449. // 调整指示器位置
  450. .uv-swiper__indicator {
  451. bottom: -40rpx;
  452. }
  453. }
  454. }
  455. }
  456. // 内容区块
  457. .section {
  458. margin-top: 40rpx;
  459. .section-header {
  460. display: flex;
  461. align-items: center;
  462. justify-content: space-between;
  463. padding: 0 30rpx;
  464. margin-bottom: 24rpx;
  465. .section-title {
  466. font-size: 36rpx;
  467. // font-weight: 600;
  468. color: $primary-text-color;
  469. }
  470. .section-more {
  471. display: flex;
  472. align-items: center;
  473. gap: 4rpx;
  474. text {
  475. font-size: 24rpx;
  476. color: $secondary-text-color;
  477. }
  478. }
  479. }
  480. .content-scroll {
  481. white-space: nowrap;
  482. }
  483. }
  484. // 今日更新列表
  485. .content-list {
  486. display: flex;
  487. padding: 0 30rpx;
  488. gap: 32rpx;
  489. .content-item {
  490. flex-shrink: 0;
  491. width: 602rpx;
  492. height: 212rpx;
  493. display: flex;
  494. align-items: center;
  495. background: #F8F8F8;
  496. padding: 16rpx;
  497. border-radius: 16rpx;
  498. gap: 16rpx;
  499. .item-cover {
  500. width: 136rpx;
  501. height: 200rpx;
  502. border-radius: 16rpx;
  503. // overflow: hidden;
  504. image {
  505. width: 136rpx;
  506. height: 200rpx;
  507. }
  508. }
  509. .item-info {
  510. // padding-top: 20rpx;
  511. gap: 16rpx;
  512. display: flex;
  513. flex-direction: column;
  514. .item-title {
  515. font-size: 32rpx;
  516. font-weight: 700;
  517. color: $primary-text-color;
  518. letter-spacing: 0;
  519. line-height: 48rpx;
  520. // margin-bottom: 12rpx;
  521. overflow: hidden;
  522. text-overflow: ellipsis;
  523. white-space: nowrap;
  524. }
  525. .item-author {
  526. font-size: 24rpx;
  527. color: $secondary-text-color;
  528. // margin-bottom: 8rpx;
  529. letter-spacing: 0;
  530. overflow: hidden;
  531. text-overflow: ellipsis;
  532. white-space: nowrap;
  533. }
  534. .item-duration {
  535. gap: 12rpx;
  536. display: flex;
  537. align-items: center;
  538. font-size: 22rpx;
  539. letter-spacing: 0;
  540. color: $secondary-text-color;
  541. .item-icon {
  542. width: 22rpx;
  543. height: 25rpx;
  544. }
  545. }
  546. }
  547. }
  548. }
  549. // 推荐书籍列表
  550. .book-list {
  551. display: flex;
  552. padding: 0 30rpx;
  553. gap: 32rpx;
  554. .book-item {
  555. flex-shrink: 0;
  556. width: 270rpx;
  557. transition: transform 0.3s ease, box-shadow 0.3s ease;
  558. &:active {
  559. transform: scale(0.98);
  560. }
  561. .book-cover {
  562. width: 100%;
  563. height: 360rpx;
  564. border-radius: 16rpx;
  565. overflow: hidden;
  566. position: relative;
  567. box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.15);
  568. transition: box-shadow 0.3s ease, transform 0.3s ease;
  569. &:active {
  570. box-shadow: 0 8rpx 30rpx rgba(0, 0, 0, 0.25);
  571. transform: translateY(-2rpx);
  572. }
  573. image {
  574. width: 100%;
  575. height: 100%;
  576. transition: transform 0.3s ease;
  577. }
  578. .book-overlay {
  579. position: absolute;
  580. bottom: 0;
  581. left: 0;
  582. right: 0;
  583. width: 100%;
  584. height: 140rpx;
  585. padding: 20rpx 16rpx 12rpx;
  586. box-sizing: border-box;
  587. /* 优化的渐变遮罩效果 */
  588. background: linear-gradient(180deg,
  589. rgba(0, 0, 0, 0) 0%,
  590. rgba(0, 0, 0, 0.3) 30%,
  591. rgba(0, 0, 0, 0.7) 70%,
  592. rgba(0, 0, 0, 0.85) 100%);
  593. /* 增强的毛玻璃效果 */
  594. backdrop-filter: blur(8px) saturate(1.2);
  595. -webkit-backdrop-filter: blur(8px) saturate(1.2);
  596. /* 添加微妙的边框 */
  597. border-top: 1px solid rgba(255, 255, 255, 0.1);
  598. /* 平滑过渡效果 */
  599. transition: all 0.3s ease;
  600. .book-duration {
  601. display: flex;
  602. align-items: center;
  603. gap: 6rpx;
  604. margin-bottom: 8rpx;
  605. &-icon {
  606. width: 22rpx;
  607. height: 22rpx;
  608. opacity: 0.9;
  609. }
  610. &-text {
  611. font-size: 20rpx;
  612. font-weight: 500;
  613. color: rgba(255, 255, 255, 0.9);
  614. text-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
  615. }
  616. }
  617. .book-title {
  618. max-width: 220rpx;
  619. font-size: 24rpx;
  620. font-weight: 600;
  621. line-height: 1.3;
  622. color: #ffffff;
  623. text-shadow: 0 1px 3px rgba(0, 0, 0, 0.5);
  624. /* 文本截断优化 */
  625. display: -webkit-box;
  626. -webkit-box-orient: vertical;
  627. -webkit-line-clamp: 2;
  628. overflow: hidden;
  629. word-break: break-word;
  630. white-space: normal;
  631. }
  632. }
  633. }
  634. }
  635. }
  636. // 书籍网格布局
  637. .book-grid {
  638. display: flex;
  639. flex-wrap: wrap;
  640. padding: 0 30rpx;
  641. gap: 32rpx;
  642. .book-grid-item {
  643. width: 208rpx;
  644. display: flex;
  645. flex-direction: column;
  646. // backdrop-filter: red;
  647. .book-grid-cover {
  648. box-shadow: 0px 4px 4px 0px #C0BCBA75;
  649. width: 100%;
  650. height: 278rpx;
  651. border-radius: 16rpx;
  652. overflow: hidden;
  653. margin-bottom: 16rpx;
  654. image {
  655. width: 100%;
  656. height: 100%;
  657. }
  658. }
  659. .book-grid-info {
  660. width: 208rpx;
  661. padding: 6rpx;
  662. overflow: hidden;
  663. text-overflow: ellipsis;
  664. white-space: nowrap;
  665. .book-grid-title {
  666. font-size: 28rpx;
  667. font-weight: 700;
  668. color: $primary-text-color;
  669. margin-bottom: 14rpx;
  670. }
  671. .book-grid-meta {
  672. display: flex;
  673. align-items: center;
  674. // gap: 16rpx;
  675. .book-grid-duration-icon {
  676. width: 24rpx;
  677. height: 24rpx;
  678. margin-right: 12rpx;
  679. }
  680. .book-grid-grade {
  681. font-size: 24rpx;
  682. color: $secondary-text-color;
  683. margin-right: 8rpx;
  684. }
  685. .book-grid-duration {
  686. font-size: 24rpx;
  687. color: $secondary-text-color;
  688. }
  689. }
  690. }
  691. }
  692. }
  693. // 推荐列表样式
  694. .recommend-list {
  695. padding: 0 30rpx;
  696. .recommend-item {
  697. width: 100%;
  698. margin-bottom: 48rpx;
  699. border-radius: 32rpx;
  700. overflow: hidden;
  701. &:last-child {
  702. margin-bottom: 0;
  703. }
  704. .recommend-image {
  705. width: 100%;
  706. height: 200rpx;
  707. }
  708. }
  709. }
  710. </style>