四零语境前端代码仓库
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.

815 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. if (uni.getStorageSync('token')) {
  387. this.$store.dispatch('getUserInfo');
  388. }
  389. // 先获取基础数据
  390. await Promise.all([this.getBanner(), this.getSignup(), this.getCategory(), this.getLabel()])
  391. // 根据label数据获取对应的书籍
  392. await this.getBooksByLabels()
  393. },
  394. mounted() {
  395. // 页面挂载时也检测一次设备类型
  396. this.detectDevice()
  397. // #ifdef H5
  398. // 监听窗口大小变化
  399. window.addEventListener('resize', this.detectDevice)
  400. // #endif
  401. },
  402. beforeDestroy() {
  403. // #ifdef H5
  404. // 移除事件监听
  405. window.removeEventListener('resize', this.detectDevice)
  406. // #endif
  407. }
  408. }
  409. </script>
  410. <style lang="scss" scoped>
  411. .home-container {
  412. background: #fff;
  413. min-height: 100vh;
  414. padding-bottom: 80rpx;
  415. }
  416. // 顶部搜索栏
  417. .header {
  418. display: flex;
  419. align-items: center;
  420. padding: 6rpx 32rpx;
  421. background: #fff;
  422. .search-container {
  423. flex: 1;
  424. }
  425. }
  426. // 轮播图容器
  427. .swiper-container {
  428. margin: 20rpx;
  429. border-radius: 12rpx;
  430. overflow: hidden;
  431. // 平板设备的轮播图样式
  432. &.tablet-swiper {
  433. margin: 30rpx 0;
  434. border-radius: 16rpx;
  435. // 卡片式轮播的额外样式
  436. :deep(.uv-swiper) {
  437. .swiper-slide {
  438. transition: all 0.3s ease;
  439. transform-origin: center;
  440. // 非激活状态的卡片
  441. &:not(.swiper-slide-active) {
  442. transform: scale(0.9);
  443. opacity: 0.7;
  444. }
  445. // 激活状态的卡片
  446. &.swiper-slide-active {
  447. transform: scale(1);
  448. opacity: 1;
  449. z-index: 2;
  450. }
  451. }
  452. // 调整指示器位置
  453. .uv-swiper__indicator {
  454. bottom: -40rpx;
  455. }
  456. }
  457. }
  458. }
  459. // 内容区块
  460. .section {
  461. margin-top: 40rpx;
  462. .section-header {
  463. display: flex;
  464. align-items: center;
  465. justify-content: space-between;
  466. padding: 0 30rpx;
  467. margin-bottom: 24rpx;
  468. .section-title {
  469. font-size: 36rpx;
  470. // font-weight: 600;
  471. color: $primary-text-color;
  472. }
  473. .section-more {
  474. display: flex;
  475. align-items: center;
  476. gap: 4rpx;
  477. text {
  478. font-size: 24rpx;
  479. color: $secondary-text-color;
  480. }
  481. }
  482. }
  483. .content-scroll {
  484. white-space: nowrap;
  485. }
  486. }
  487. // 今日更新列表
  488. .content-list {
  489. display: flex;
  490. padding: 0 30rpx;
  491. gap: 32rpx;
  492. .content-item {
  493. flex-shrink: 0;
  494. width: 602rpx;
  495. height: 212rpx;
  496. display: flex;
  497. align-items: center;
  498. background: #F8F8F8;
  499. padding: 16rpx;
  500. border-radius: 16rpx;
  501. gap: 16rpx;
  502. .item-cover {
  503. width: 136rpx;
  504. height: 200rpx;
  505. border-radius: 16rpx;
  506. // overflow: hidden;
  507. image {
  508. width: 136rpx;
  509. height: 200rpx;
  510. }
  511. }
  512. .item-info {
  513. // padding-top: 20rpx;
  514. gap: 16rpx;
  515. display: flex;
  516. flex-direction: column;
  517. .item-title {
  518. font-size: 32rpx;
  519. font-weight: 700;
  520. color: $primary-text-color;
  521. letter-spacing: 0;
  522. line-height: 48rpx;
  523. // margin-bottom: 12rpx;
  524. overflow: hidden;
  525. text-overflow: ellipsis;
  526. white-space: nowrap;
  527. }
  528. .item-author {
  529. font-size: 24rpx;
  530. color: $secondary-text-color;
  531. // margin-bottom: 8rpx;
  532. letter-spacing: 0;
  533. overflow: hidden;
  534. text-overflow: ellipsis;
  535. white-space: nowrap;
  536. }
  537. .item-duration {
  538. gap: 12rpx;
  539. display: flex;
  540. align-items: center;
  541. font-size: 22rpx;
  542. letter-spacing: 0;
  543. color: $secondary-text-color;
  544. .item-icon {
  545. width: 22rpx;
  546. height: 25rpx;
  547. }
  548. }
  549. }
  550. }
  551. }
  552. // 推荐书籍列表
  553. .book-list {
  554. display: flex;
  555. padding: 0 30rpx;
  556. gap: 32rpx;
  557. .book-item {
  558. flex-shrink: 0;
  559. width: 270rpx;
  560. transition: transform 0.3s ease, box-shadow 0.3s ease;
  561. &:active {
  562. transform: scale(0.98);
  563. }
  564. .book-cover {
  565. width: 100%;
  566. height: 360rpx;
  567. border-radius: 16rpx;
  568. overflow: hidden;
  569. position: relative;
  570. box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.15);
  571. transition: box-shadow 0.3s ease, transform 0.3s ease;
  572. &:active {
  573. box-shadow: 0 8rpx 30rpx rgba(0, 0, 0, 0.25);
  574. transform: translateY(-2rpx);
  575. }
  576. image {
  577. width: 100%;
  578. height: 100%;
  579. transition: transform 0.3s ease;
  580. }
  581. .book-overlay {
  582. position: absolute;
  583. bottom: 0;
  584. left: 0;
  585. right: 0;
  586. width: 100%;
  587. height: 140rpx;
  588. padding: 20rpx 16rpx 12rpx;
  589. box-sizing: border-box;
  590. /* 优化的渐变遮罩效果 */
  591. background: linear-gradient(180deg,
  592. rgba(0, 0, 0, 0) 0%,
  593. rgba(0, 0, 0, 0.3) 30%,
  594. rgba(0, 0, 0, 0.7) 70%,
  595. rgba(0, 0, 0, 0.85) 100%);
  596. /* 增强的毛玻璃效果 */
  597. backdrop-filter: blur(8px) saturate(1.2);
  598. -webkit-backdrop-filter: blur(8px) saturate(1.2);
  599. /* 添加微妙的边框 */
  600. border-top: 1px solid rgba(255, 255, 255, 0.1);
  601. /* 平滑过渡效果 */
  602. transition: all 0.3s ease;
  603. .book-duration {
  604. display: flex;
  605. align-items: center;
  606. gap: 6rpx;
  607. margin-bottom: 8rpx;
  608. &-icon {
  609. width: 22rpx;
  610. height: 22rpx;
  611. opacity: 0.9;
  612. }
  613. &-text {
  614. font-size: 20rpx;
  615. font-weight: 500;
  616. color: rgba(255, 255, 255, 0.9);
  617. text-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
  618. }
  619. }
  620. .book-title {
  621. max-width: 220rpx;
  622. font-size: 24rpx;
  623. font-weight: 600;
  624. line-height: 1.3;
  625. color: #ffffff;
  626. text-shadow: 0 1px 3px rgba(0, 0, 0, 0.5);
  627. /* 文本截断优化 */
  628. display: -webkit-box;
  629. -webkit-box-orient: vertical;
  630. -webkit-line-clamp: 2;
  631. overflow: hidden;
  632. word-break: break-word;
  633. white-space: normal;
  634. }
  635. }
  636. }
  637. }
  638. }
  639. // 书籍网格布局
  640. .book-grid {
  641. display: flex;
  642. flex-wrap: wrap;
  643. padding: 0 30rpx;
  644. gap: 32rpx;
  645. .book-grid-item {
  646. width: 208rpx;
  647. display: flex;
  648. flex-direction: column;
  649. // backdrop-filter: red;
  650. .book-grid-cover {
  651. box-shadow: 0px 4px 4px 0px #C0BCBA75;
  652. width: 100%;
  653. height: 278rpx;
  654. border-radius: 16rpx;
  655. overflow: hidden;
  656. margin-bottom: 16rpx;
  657. image {
  658. width: 100%;
  659. height: 100%;
  660. }
  661. }
  662. .book-grid-info {
  663. width: 208rpx;
  664. padding: 6rpx;
  665. overflow: hidden;
  666. text-overflow: ellipsis;
  667. white-space: nowrap;
  668. .book-grid-title {
  669. font-size: 28rpx;
  670. font-weight: 700;
  671. color: $primary-text-color;
  672. margin-bottom: 14rpx;
  673. }
  674. .book-grid-meta {
  675. display: flex;
  676. align-items: center;
  677. // gap: 16rpx;
  678. .book-grid-duration-icon {
  679. width: 24rpx;
  680. height: 24rpx;
  681. margin-right: 12rpx;
  682. }
  683. .book-grid-grade {
  684. font-size: 24rpx;
  685. color: $secondary-text-color;
  686. margin-right: 8rpx;
  687. }
  688. .book-grid-duration {
  689. font-size: 24rpx;
  690. color: $secondary-text-color;
  691. }
  692. }
  693. }
  694. }
  695. }
  696. // 推荐列表样式
  697. .recommend-list {
  698. padding: 0 30rpx;
  699. .recommend-item {
  700. width: 100%;
  701. margin-bottom: 48rpx;
  702. border-radius: 32rpx;
  703. overflow: hidden;
  704. &:last-child {
  705. margin-bottom: 0;
  706. }
  707. .recommend-image {
  708. width: 100%;
  709. height: 200rpx;
  710. }
  711. }
  712. }
  713. </style>