home.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. <template>
  2. <view class="home-wrap base-container">
  3. <view class="home-top">
  4. <view class="home-search">
  5. <view class="search-wrap">
  6. <up-search placeholder="搜索农场名称" v-model="typeSearch"></up-search>
  7. </view>
  8. </view>
  9. <div class="home-box" @click="toPage('allGardenMap')">
  10. <video src="https://birdseye-img.sysuimars.com/youwei-uniapp/home/map-video.mp4" object-fit="cover"
  11. :show-fullscreen-btn="false" :autoplay="true" class="map-video" loop muted :show-progress="false"
  12. :show-play-btn="false">
  13. </video>
  14. <view class="map-tips">
  15. 飞鸟入驻管理果场详情,点击查看
  16. </view>
  17. </div>
  18. </view>
  19. <view class="type-wrap">
  20. <up-scroll-list :indicator="typeList && typeList.length>5" indicatorColor="#F3C11D33" indicatorActiveColor="#F3C11D"
  21. :indicatorWidth="30" :indicatorBarWidth="13" ref="scrollListRef">
  22. <view class="item-type">
  23. <image class="type-img" :src="`${config.BASIC_IMG}home/type-icon.png`" alt="" />
  24. <view class="type-text">
  25. 全部
  26. </view>
  27. </view>
  28. <view class="item-type" v-for="(item, index) in typeList" :key="index">
  29. <image class="type-img" :src="item.image" alt="" />
  30. <view class="type-text">
  31. {{item.name}}
  32. </view>
  33. </view>
  34. </up-scroll-list>
  35. </view>
  36. <!-- 好味热卖 -->
  37. <view class="home-hot" v-if="gardenList && gardenList.length">
  38. <hot-component-vue :farmId="gardenList[0].farmId"></hot-component-vue>
  39. </view>
  40. <!-- 有味果园 -->
  41. <view class="garden-wrap">
  42. <view class="garden-content">
  43. <view class="hot-title">
  44. <view class="title-l">
  45. 有味果园
  46. </view>
  47. <view class="title-btn" @click="toPage('gardenList')">
  48. 查看全部<up-icon size="10" name="arrow-right"></up-icon>
  49. </view>
  50. </view>
  51. <view class="garden-list">
  52. <view class="garden-item" v-for="farm in gardenList" :key="farm.farmId || farm.id"
  53. @click="goGardenItem(farm)">
  54. <view class="garden-l" v-if="farm.coverVideo">
  55. <video :id="`home-video-${farm.farmId || farm.id}`" :src="farm.coverVideo"
  56. :show-progress="false" :show-play-btn="true" :picture-in-picture-mode="[]"
  57. :show-background-playback-button="false" :show-center-play-btn="false"
  58. object-fit="cover" :show-fullscreen-btn="false" disable-picture-in-picture
  59. :autoplay="false" x5-video-player-type="h5" class="video-dom" loop muted>
  60. </video>
  61. </view>
  62. <view class="garden-l" v-else>
  63. <image class="garden-img" :src="`${config.BASIC_IMG}home/garden.png`" mode=""></image>
  64. <text class="img-text">无人机实拍视频</text>
  65. </view>
  66. <view class="garden-r">
  67. <view class="garden-title">
  68. {{ farm.name }}
  69. </view>
  70. <view class="garden-info">
  71. 品种:荔枝({{ farm.pz }})
  72. </view>
  73. <view class="garden-info">
  74. 位置:{{ (farm.cityName + farm.countyName) || '—' }}
  75. </view>
  76. <view class="garden-btn-group">
  77. <view class="btn-second">
  78. 有味指数
  79. <text>{{ farm.envScore || '—' }}分</text>
  80. </view>
  81. <view class="btn-primary" @click.stop="goSourceReport(farm)">
  82. 溯源报告
  83. </view>
  84. </view>
  85. </view>
  86. </view>
  87. </view>
  88. </view>
  89. </view>
  90. </view>
  91. </template>
  92. <script setup>
  93. import {
  94. ref,
  95. onMounted,
  96. onUnmounted,
  97. nextTick
  98. } from 'vue'
  99. import config from "@/api/config.js"
  100. import HOME from '@/api/home'
  101. import { useUserStore } from '@/store/user' // 引入 Pinia store
  102. import hotComponentVue from './hotComponent.vue'
  103. import {
  104. onLoad,
  105. onPageScroll
  106. } from "@dcloudio/uni-app"
  107. // 获取 user store
  108. const userStore = useUserStore()
  109. const typeSearch = ref(null)
  110. const gardenList = ref([])
  111. const typeList = ref([])
  112. const playingId = ref(null)
  113. function toPage(path) {
  114. uni.navigateTo({
  115. url: `/pages/tabBar/home/subPages/${path}`
  116. });
  117. }
  118. function goGardenItem(farm) {
  119. const id = farm?.farmId
  120. uni.navigateTo({
  121. url: `/pages/tabBar/home/subPages/gardenItem?farmId=${id}`
  122. });
  123. }
  124. function goSourceReport(farm) {
  125. const id = farm?.farmId
  126. uni.navigateTo({
  127. url: `/pages/tabBar/home/subPages/sourceReport?farmId=${id}`
  128. });
  129. }
  130. onMounted(() => {
  131. nextTick(() => {
  132. setTimeout(updateCenterVideo, 50)
  133. })
  134. })
  135. onPageScroll(() => handlePageScroll())
  136. onUnmounted(() => {
  137. pauseAll()
  138. })
  139. function throttle(fn, wait = 150) {
  140. let lastTime = 0
  141. let timer = null
  142. return function(...args) {
  143. const now = Date.now()
  144. if (now - lastTime >= wait) {
  145. lastTime = now
  146. fn.apply(this, args)
  147. } else {
  148. clearTimeout(timer)
  149. timer = setTimeout(() => {
  150. lastTime = Date.now()
  151. fn.apply(this, args)
  152. }, wait - (now - lastTime))
  153. }
  154. }
  155. }
  156. const handlePageScroll = throttle(() => {
  157. updateCenterVideo()
  158. }, 200)
  159. function updateCenterVideo() {
  160. const query = uni.createSelectorQuery()
  161. query.selectAll('.video-dom').fields({
  162. id: true,
  163. rect: true,
  164. size: true
  165. }, rects => {
  166. if (!rects || !rects.length) return
  167. const viewportHeight = uni.getSystemInfoSync().windowHeight
  168. const viewportCenter = viewportHeight / 2
  169. let minDistance = Number.POSITIVE_INFINITY
  170. let closestId = null
  171. rects.forEach(rect => {
  172. if (!rect) return
  173. const height = (typeof rect.height === 'number' ? rect.height : (rect.bottom - rect.top))
  174. const elementCenter = rect.top + height / 2
  175. if (rect.bottom < 0 || rect.top > viewportHeight) return
  176. const distance = Math.abs(elementCenter - viewportCenter)
  177. if (distance < minDistance) {
  178. minDistance = distance
  179. closestId = rect.id
  180. }
  181. })
  182. if (closestId && playingId.value !== closestId) {
  183. playOnly(closestId)
  184. }
  185. }).exec()
  186. }
  187. function playOnly(targetId) {
  188. const ctxs = []
  189. gardenList.value.forEach(item => {
  190. if (!item.coverVideo) return
  191. const id = `home-video-${item.farmId || item.id}`
  192. const ctx = uni.createVideoContext(id)
  193. ctxs.push({
  194. id,
  195. ctx
  196. })
  197. })
  198. ctxs.forEach(({
  199. id,
  200. ctx
  201. }) => {
  202. if (id === targetId) {
  203. try {
  204. ctx.play()
  205. } catch (e) {}
  206. playingId.value = id
  207. } else {
  208. try {
  209. ctx.pause()
  210. } catch (e) {}
  211. }
  212. })
  213. }
  214. function pauseAll() {
  215. gardenList.value.forEach(item => {
  216. if (!item.coverVideo) return
  217. const id = `home-video-${item.farmId || item.id}`
  218. try {
  219. uni.createVideoContext(id).pause()
  220. } catch (e) {}
  221. })
  222. }
  223. onLoad(async () => {
  224. console.log('onLoad')
  225. try {
  226. // 显示全局加载状态
  227. uni.showLoading({
  228. title: '加载中...',
  229. mask: true
  230. })
  231. // 【核心修改】等待token准备就绪
  232. await userStore.getTokenSafe()
  233. console.log('登录成功,token已就绪')
  234. // 现在可以安全地执行需要登录状态的请求
  235. await getTypeList()
  236. await getFarmData()
  237. // 隐藏加载状态
  238. uni.hideLoading()
  239. } catch (error) {
  240. console.error('初始化失败:', error)
  241. // 隐藏加载状态
  242. uni.hideLoading()
  243. // 登录失败处理,可以跳转到登录页或显示提示
  244. uni.showToast({
  245. title: '请先登录',
  246. icon: 'none'
  247. })
  248. }
  249. })
  250. async function getTypeList() {
  251. try {
  252. const { data } = await HOME.fetchTypeList()
  253. typeList.value = data
  254. } catch (error) {
  255. console.error('获取类型列表失败:', error)
  256. }
  257. }
  258. async function getFarmData() {
  259. try {
  260. const { data } = await HOME.getAllFarm()
  261. if (!data || !data.length) return
  262. gardenList.value = data.filter(item => item.recommend !== null)
  263. nextTick(() => setTimeout(updateCenterVideo, 50))
  264. } catch (error) {
  265. console.error('获取果园数据失败:', error)
  266. }
  267. }
  268. </script>
  269. <style lang="scss" scoped>
  270. .garden-l {
  271. ::v-deep {
  272. .uni-video-control-button {
  273. width: 10px;
  274. height: 10px;
  275. }
  276. }
  277. }
  278. /* 或者全局隐藏 */
  279. ::v-deep .uni-video-pip {
  280. display: none !important;
  281. }
  282. .home-wrap {
  283. background-color: #F2F3F5;
  284. padding: 0 0 16rpx 0;
  285. .home-top {
  286. padding: 20rpx 24rpx 0 24rpx;
  287. background: linear-gradient(#FFFFFF, rgba(242, 243, 245, 0));
  288. position: relative;
  289. }
  290. .map-video {
  291. width: 100%;
  292. height: 280rpx;
  293. border-radius: 16rpx;
  294. }
  295. .map-tips {
  296. position: absolute;
  297. bottom: 6rpx;
  298. left: 24rpx;
  299. height: 70rpx;
  300. width: calc(100% - 48rpx);
  301. background: linear-gradient(rgba(102, 102, 102, 0), rgba(0, 0, 0, 0.55));
  302. display: flex;
  303. align-items: center;
  304. font-size: 24rpx;
  305. color: #FFFFFF;
  306. padding-left: 20rpx;
  307. box-sizing: border-box;
  308. border-radius: 0 0 16rpx 16rpx;
  309. }
  310. .home-search {
  311. display: flex;
  312. align-items: center;
  313. width: 100%;
  314. border: 2rpx solid #FFD95E;
  315. border-radius: 40rpx;
  316. margin-bottom: 40rpx;
  317. .search-wrap {
  318. flex: 1;
  319. // padding-left: 22rpx;
  320. ::v-deep {
  321. .u-search {
  322. .u-search__content {
  323. background-color: transparent !important;
  324. .u-search__content__input {
  325. background-color: transparent !important;
  326. }
  327. }
  328. .u-search__action {
  329. text-align: center;
  330. line-height: 52rpx;
  331. border-radius: 40rpx;
  332. font-size: 28rpx;
  333. background-color: #FFD95E;
  334. margin: 6rpx 8rpx;
  335. width: 112rpx;
  336. height: 52rpx;
  337. }
  338. }
  339. }
  340. }
  341. .search-btn {
  342. text-align: center;
  343. line-height: 52rpx;
  344. border-radius: 40rpx;
  345. font-size: 28rpx;
  346. background-color: #FFD95E;
  347. margin: 6rpx 8rpx;
  348. width: 112rpx;
  349. height: 52rpx;
  350. }
  351. }
  352. .type-wrap {
  353. margin: 20rpx;
  354. padding: 20rpx;
  355. background-color: #fff;
  356. border-radius: 16rpx;
  357. height: 200rpx;
  358. box-sizing: border-box;
  359. // 覆盖 up-scroll-list 的默认样式,实现两端对齐
  360. :deep(.u-scroll-list__scroll-view__content) {
  361. display: flex;
  362. justify-content: space-between;
  363. align-items: center;
  364. width: 100%;
  365. min-width: 100%;
  366. }
  367. .item-type {
  368. text-align: center;
  369. font-size: 24rpx;
  370. flex-shrink: 0;
  371. .type-img {
  372. width: 92rpx;
  373. height: 92rpx;
  374. }
  375. }
  376. .item-type+.item-type {
  377. padding-left: 22rpx;
  378. }
  379. }
  380. .home-hot {
  381. padding: 0 20rpx 20rpx;
  382. }
  383. .garden-wrap {
  384. margin: 0 20rpx 20rpx;
  385. background: linear-gradient(#FFFFFF, rgba(255, 255, 255, 0));
  386. padding: 2rpx;
  387. border-radius: 16rpx;
  388. .garden-content {
  389. background: linear-gradient(#fff6d8 30rpx, #FFFFFF 120rpx);
  390. border-radius: 16rpx;
  391. padding: 20rpx;
  392. box-sizing: border-box;
  393. .garden-list {
  394. .garden-item {
  395. display: flex;
  396. .garden-l {
  397. position: relative;
  398. .video-dom {
  399. width: 234rpx;
  400. height: 200rpx;
  401. object-fit: cover;
  402. border-radius: 10rpx;
  403. }
  404. .garden-img {
  405. width: 254rpx;
  406. height: 200rpx;
  407. object-fit: cover;
  408. border-radius: 10rpx;
  409. }
  410. .img-text {
  411. position: absolute;
  412. left: 0%;
  413. top: 0%;
  414. background: rgba(0, 0, 0, 0.3);
  415. backdrop-filter: 8rpx;
  416. border-radius: 10rpx 0 10rpx 0;
  417. font-size: 20rpx;
  418. color: #FFFFFF;
  419. padding: 6rpx 14rpx;
  420. }
  421. }
  422. .garden-r {
  423. padding-left: 20rpx;
  424. .garden-title {
  425. font-size: 28rpx;
  426. line-height: 42rpx;
  427. color: #000000;
  428. font-weight: 600;
  429. padding-bottom: 10rpx;
  430. }
  431. .garden-info {
  432. color: rgba(0, 0, 0, 0.5);
  433. font-size: 24rpx;
  434. line-height: 36rpx;
  435. }
  436. .garden-btn-group {
  437. padding-top: 20rpx;
  438. display: flex;
  439. align-items: center;
  440. .btn-second {
  441. padding: 0 20rpx;
  442. border-radius: 40rpx;
  443. font-size: 24rpx;
  444. color: #C49600;
  445. background: rgba(255, 217, 94, 0.2);
  446. height: 56rpx;
  447. line-height: 56rpx;
  448. }
  449. .btn-primary {
  450. padding: 0 20rpx;
  451. border-radius: 40rpx;
  452. font-size: 24rpx;
  453. color: #000000;
  454. background: #FFD95E;
  455. font-weight: bold;
  456. height: 56rpx;
  457. line-height: 56rpx;
  458. margin-left: 20rpx;
  459. }
  460. }
  461. }
  462. }
  463. .garden-item+.garden-item {
  464. margin-top: 20rpx;
  465. }
  466. }
  467. }
  468. }
  469. .hot-title {
  470. display: flex;
  471. align-items: center;
  472. justify-content: space-between;
  473. padding-bottom: 20rpx;
  474. .title-l {
  475. font-family: 'PangMenZhengDao';
  476. font-size: 32rpx;
  477. .title-color {
  478. color: #F3C11D;
  479. }
  480. }
  481. .title-btn {
  482. color: rgba(0, 0, 0, 0.6);
  483. font-size: 24rpx;
  484. display: inline-flex;
  485. align-items: center;
  486. }
  487. }
  488. ::v-deep {
  489. .u-scroll-list__indicator {
  490. margin-top: 20rpx;
  491. }
  492. }
  493. }
  494. </style>