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

784 lines
24 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" 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="2vw"
  23. height="30.6vw"
  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="aspectFill" 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. // max-width: 750px;
  416. // margin: 0 auto;
  417. }
  418. // 顶部搜索栏
  419. .header {
  420. display: flex;
  421. align-items: center;
  422. padding: 6rpx 32rpx;
  423. background: #fff;
  424. .search-container {
  425. flex: 1;
  426. }
  427. }
  428. // 轮播图容器
  429. .swiper-container {
  430. margin: 20rpx;
  431. border-radius: 12rpx;
  432. overflow: hidden;
  433. }
  434. // 内容区块
  435. .section {
  436. margin-top: 40rpx;
  437. .section-header {
  438. display: flex;
  439. align-items: center;
  440. justify-content: space-between;
  441. padding: 0 30rpx;
  442. margin-bottom: 24rpx;
  443. .section-title {
  444. font-size: 36rpx;
  445. // font-weight: 600;
  446. color: $primary-text-color;
  447. }
  448. .section-more {
  449. display: flex;
  450. align-items: center;
  451. gap: 4rpx;
  452. text {
  453. font-size: 24rpx;
  454. color: $secondary-text-color;
  455. }
  456. }
  457. }
  458. .content-scroll {
  459. white-space: nowrap;
  460. }
  461. }
  462. // 今日更新列表
  463. .content-list {
  464. display: flex;
  465. padding: 0 30rpx;
  466. gap: 32rpx;
  467. .content-item {
  468. flex-shrink: 0;
  469. width: 602rpx;
  470. height: 212rpx;
  471. display: flex;
  472. align-items: center;
  473. background: #F8F8F8;
  474. padding: 16rpx;
  475. border-radius: 16rpx;
  476. gap: 16rpx;
  477. .item-cover {
  478. width: 136rpx;
  479. height: 200rpx;
  480. border-radius: 16rpx;
  481. // overflow: hidden;
  482. image {
  483. width: 136rpx;
  484. height: 200rpx;
  485. }
  486. }
  487. .item-info {
  488. // padding-top: 20rpx;
  489. gap: 16rpx;
  490. display: flex;
  491. flex-direction: column;
  492. .item-title {
  493. font-size: 32rpx;
  494. font-weight: 700;
  495. color: $primary-text-color;
  496. letter-spacing: 0;
  497. line-height: 48rpx;
  498. // margin-bottom: 12rpx;
  499. overflow: hidden;
  500. text-overflow: ellipsis;
  501. white-space: nowrap;
  502. }
  503. .item-author {
  504. font-size: 24rpx;
  505. color: $secondary-text-color;
  506. // margin-bottom: 8rpx;
  507. letter-spacing: 0;
  508. overflow: hidden;
  509. text-overflow: ellipsis;
  510. white-space: nowrap;
  511. }
  512. .item-duration {
  513. gap: 12rpx;
  514. display: flex;
  515. align-items: center;
  516. font-size: 22rpx;
  517. letter-spacing: 0;
  518. color: $secondary-text-color;
  519. .item-icon {
  520. width: 22rpx;
  521. height: 25rpx;
  522. }
  523. }
  524. }
  525. }
  526. }
  527. // 推荐书籍列表
  528. .book-list {
  529. display: flex;
  530. padding: 0 30rpx;
  531. gap: 32rpx;
  532. .book-item {
  533. flex-shrink: 0;
  534. width: 270rpx;
  535. transition: transform 0.3s ease, box-shadow 0.3s ease;
  536. &:active {
  537. transform: scale(0.98);
  538. }
  539. .book-cover {
  540. width: 100%;
  541. height: 360rpx;
  542. border-radius: 16rpx;
  543. overflow: hidden;
  544. position: relative;
  545. box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.15);
  546. transition: box-shadow 0.3s ease, transform 0.3s ease;
  547. &:active {
  548. box-shadow: 0 8rpx 30rpx rgba(0, 0, 0, 0.25);
  549. transform: translateY(-2rpx);
  550. }
  551. image {
  552. width: 100%;
  553. height: 100%;
  554. transition: transform 0.3s ease;
  555. }
  556. .book-overlay {
  557. position: absolute;
  558. bottom: 0;
  559. left: 0;
  560. right: 0;
  561. width: 100%;
  562. height: 140rpx;
  563. padding: 20rpx 16rpx 12rpx;
  564. box-sizing: border-box;
  565. /* 优化的渐变遮罩效果 */
  566. background: linear-gradient(180deg,
  567. rgba(0, 0, 0, 0) 0%,
  568. rgba(0, 0, 0, 0.3) 30%,
  569. rgba(0, 0, 0, 0.7) 70%,
  570. rgba(0, 0, 0, 0.85) 100%);
  571. /* 增强的毛玻璃效果 */
  572. backdrop-filter: blur(8px) saturate(1.2);
  573. -webkit-backdrop-filter: blur(8px) saturate(1.2);
  574. /* 添加微妙的边框 */
  575. border-top: 1px solid rgba(255, 255, 255, 0.1);
  576. /* 平滑过渡效果 */
  577. transition: all 0.3s ease;
  578. .book-duration {
  579. display: flex;
  580. align-items: center;
  581. gap: 6rpx;
  582. margin-bottom: 8rpx;
  583. &-icon {
  584. width: 22rpx;
  585. height: 22rpx;
  586. opacity: 0.9;
  587. }
  588. &-text {
  589. font-size: 20rpx;
  590. font-weight: 500;
  591. color: rgba(255, 255, 255, 0.9);
  592. text-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
  593. }
  594. }
  595. .book-title {
  596. max-width: 220rpx;
  597. font-size: 24rpx;
  598. font-weight: 600;
  599. line-height: 1.3;
  600. color: #ffffff;
  601. text-shadow: 0 1px 3px rgba(0, 0, 0, 0.5);
  602. /* 文本截断优化 */
  603. display: -webkit-box;
  604. -webkit-box-orient: vertical;
  605. -webkit-line-clamp: 2;
  606. overflow: hidden;
  607. word-break: break-word;
  608. white-space: normal;
  609. }
  610. }
  611. }
  612. }
  613. }
  614. // 书籍网格布局
  615. .book-grid {
  616. display: flex;
  617. flex-wrap: wrap;
  618. padding: 0 30rpx;
  619. gap: 32rpx;
  620. .book-grid-item {
  621. width: 208rpx;
  622. display: flex;
  623. flex-direction: column;
  624. // backdrop-filter: red;
  625. .book-grid-cover {
  626. box-shadow: 0px 4px 4px 0px #C0BCBA75;
  627. width: 100%;
  628. height: 278rpx;
  629. border-radius: 16rpx;
  630. overflow: hidden;
  631. margin-bottom: 16rpx;
  632. image {
  633. width: 100%;
  634. height: 100%;
  635. }
  636. }
  637. .book-grid-info {
  638. width: 208rpx;
  639. padding: 6rpx;
  640. overflow: hidden;
  641. text-overflow: ellipsis;
  642. white-space: nowrap;
  643. .book-grid-title {
  644. font-size: 28rpx;
  645. font-weight: 700;
  646. color: $primary-text-color;
  647. margin-bottom: 14rpx;
  648. }
  649. .book-grid-meta {
  650. display: flex;
  651. align-items: center;
  652. // gap: 16rpx;
  653. .book-grid-duration-icon {
  654. width: 24rpx;
  655. height: 24rpx;
  656. margin-right: 12rpx;
  657. }
  658. .book-grid-grade {
  659. font-size: 24rpx;
  660. color: $secondary-text-color;
  661. margin-right: 8rpx;
  662. }
  663. .book-grid-duration {
  664. font-size: 24rpx;
  665. color: $secondary-text-color;
  666. }
  667. }
  668. }
  669. }
  670. }
  671. // 推荐列表样式
  672. .recommend-list {
  673. padding: 0 30rpx;
  674. .recommend-item {
  675. width: 100%;
  676. margin-bottom: 48rpx;
  677. &:last-child {
  678. margin-bottom: 0;
  679. }
  680. .recommend-image {
  681. width: 100%;
  682. height: 25vw;
  683. // height: 30.6vw;
  684. border-radius: 32rpx;
  685. }
  686. }
  687. }
  688. </style>