home.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  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" @click="toTabBar(0)">
  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" @click="toTabBar(item.id)">
  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. // 【核心修改】等待token准备就绪
  227. await userStore.getTokenSafe()
  228. console.log('登录成功,token已就绪')
  229. // 现在可以安全地执行需要登录状态的请求
  230. await getTypeList()
  231. await getFarmData()
  232. } catch (error) {
  233. console.error('初始化失败:', error)
  234. // 登录失败处理,可以跳转到登录页或显示提示
  235. uni.showToast({
  236. title: '请先登录',
  237. icon: 'none'
  238. })
  239. }
  240. })
  241. async function getTypeList() {
  242. try {
  243. const { data } = await HOME.fetchTypeList()
  244. typeList.value = data
  245. } catch (error) {
  246. console.error('获取类型列表失败:', error)
  247. }
  248. }
  249. function toTabBar(typeId) {
  250. uni.setStorageSync('selectedCategoryType', typeId);
  251. uni.switchTab({
  252. url: '/pages/tabBar/discover/discover',
  253. });
  254. }
  255. async function getFarmData() {
  256. try {
  257. const { data } = await HOME.getAllFarm()
  258. if (!data || !data.length) return
  259. gardenList.value = data.filter(item => item.recommend !== null)
  260. nextTick(() => setTimeout(updateCenterVideo, 50))
  261. } catch (error) {
  262. console.error('获取果园数据失败:', error)
  263. }
  264. }
  265. </script>
  266. <style lang="scss" scoped>
  267. .garden-l {
  268. ::v-deep {
  269. .uni-video-control-button {
  270. width: 10px;
  271. height: 10px;
  272. }
  273. }
  274. }
  275. /* 或者全局隐藏 */
  276. ::v-deep .uni-video-pip {
  277. display: none !important;
  278. }
  279. .home-wrap {
  280. background-color: #F2F3F5;
  281. padding: 0 0 16rpx 0;
  282. .home-top {
  283. padding: 20rpx 24rpx 0 24rpx;
  284. background: linear-gradient(#FFFFFF, rgba(242, 243, 245, 0));
  285. position: relative;
  286. }
  287. .map-video {
  288. width: 100%;
  289. height: 280rpx;
  290. border-radius: 16rpx;
  291. }
  292. .map-tips {
  293. position: absolute;
  294. bottom: 6rpx;
  295. left: 24rpx;
  296. height: 70rpx;
  297. width: calc(100% - 48rpx);
  298. background: linear-gradient(rgba(102, 102, 102, 0), rgba(0, 0, 0, 0.55));
  299. display: flex;
  300. align-items: center;
  301. font-size: 24rpx;
  302. color: #FFFFFF;
  303. padding-left: 20rpx;
  304. box-sizing: border-box;
  305. border-radius: 0 0 16rpx 16rpx;
  306. }
  307. .home-search {
  308. display: flex;
  309. align-items: center;
  310. width: 100%;
  311. border: 2rpx solid #FFD95E;
  312. border-radius: 40rpx;
  313. margin-bottom: 40rpx;
  314. .search-wrap {
  315. flex: 1;
  316. // padding-left: 22rpx;
  317. ::v-deep {
  318. .u-search {
  319. .u-search__content {
  320. background-color: transparent !important;
  321. .u-search__content__input {
  322. background-color: transparent !important;
  323. }
  324. }
  325. .u-search__action {
  326. text-align: center;
  327. line-height: 52rpx;
  328. border-radius: 40rpx;
  329. font-size: 28rpx;
  330. background-color: #FFD95E;
  331. margin: 6rpx 8rpx;
  332. width: 112rpx;
  333. height: 52rpx;
  334. }
  335. }
  336. }
  337. }
  338. .search-btn {
  339. text-align: center;
  340. line-height: 52rpx;
  341. border-radius: 40rpx;
  342. font-size: 28rpx;
  343. background-color: #FFD95E;
  344. margin: 6rpx 8rpx;
  345. width: 112rpx;
  346. height: 52rpx;
  347. }
  348. }
  349. .type-wrap {
  350. margin: 20rpx;
  351. padding: 20rpx;
  352. background-color: #fff;
  353. border-radius: 16rpx;
  354. height: 200rpx;
  355. box-sizing: border-box;
  356. // 覆盖 up-scroll-list 的默认样式,实现两端对齐
  357. :deep(.u-scroll-list__scroll-view__content) {
  358. display: flex;
  359. justify-content: space-between;
  360. align-items: center;
  361. width: 100%;
  362. min-width: 100%;
  363. }
  364. .item-type {
  365. text-align: center;
  366. font-size: 24rpx;
  367. flex-shrink: 0;
  368. .type-img {
  369. width: 92rpx;
  370. height: 92rpx;
  371. }
  372. }
  373. .item-type+.item-type {
  374. padding-left: 22rpx;
  375. }
  376. }
  377. .home-hot {
  378. padding: 0 20rpx 20rpx;
  379. }
  380. .garden-wrap {
  381. margin: 0 20rpx 20rpx;
  382. background: linear-gradient(#FFFFFF, rgba(255, 255, 255, 0));
  383. padding: 2rpx;
  384. border-radius: 16rpx;
  385. .garden-content {
  386. background: linear-gradient(#fff6d8 30rpx, #FFFFFF 120rpx);
  387. border-radius: 16rpx;
  388. padding: 20rpx;
  389. box-sizing: border-box;
  390. .garden-list {
  391. .garden-item {
  392. display: flex;
  393. .garden-l {
  394. position: relative;
  395. .video-dom {
  396. width: 234rpx;
  397. height: 200rpx;
  398. object-fit: cover;
  399. border-radius: 10rpx;
  400. }
  401. .garden-img {
  402. width: 254rpx;
  403. height: 200rpx;
  404. object-fit: cover;
  405. border-radius: 10rpx;
  406. }
  407. .img-text {
  408. position: absolute;
  409. left: 0%;
  410. top: 0%;
  411. background: rgba(0, 0, 0, 0.3);
  412. backdrop-filter: 8rpx;
  413. border-radius: 10rpx 0 10rpx 0;
  414. font-size: 20rpx;
  415. color: #FFFFFF;
  416. padding: 6rpx 14rpx;
  417. }
  418. }
  419. .garden-r {
  420. padding-left: 20rpx;
  421. .garden-title {
  422. font-size: 28rpx;
  423. line-height: 42rpx;
  424. color: #000000;
  425. font-weight: 600;
  426. padding-bottom: 10rpx;
  427. }
  428. .garden-info {
  429. color: rgba(0, 0, 0, 0.5);
  430. font-size: 24rpx;
  431. line-height: 36rpx;
  432. }
  433. .garden-btn-group {
  434. padding-top: 20rpx;
  435. display: flex;
  436. align-items: center;
  437. .btn-second {
  438. padding: 0 20rpx;
  439. border-radius: 40rpx;
  440. font-size: 24rpx;
  441. color: #C49600;
  442. background: rgba(255, 217, 94, 0.2);
  443. height: 56rpx;
  444. line-height: 56rpx;
  445. }
  446. .btn-primary {
  447. padding: 0 20rpx;
  448. border-radius: 40rpx;
  449. font-size: 24rpx;
  450. color: #000000;
  451. background: #FFD95E;
  452. font-weight: bold;
  453. height: 56rpx;
  454. line-height: 56rpx;
  455. margin-left: 20rpx;
  456. }
  457. }
  458. }
  459. }
  460. .garden-item+.garden-item {
  461. margin-top: 20rpx;
  462. }
  463. }
  464. }
  465. }
  466. .hot-title {
  467. display: flex;
  468. align-items: center;
  469. justify-content: space-between;
  470. padding-bottom: 20rpx;
  471. .title-l {
  472. font-family: 'PangMenZhengDao';
  473. font-size: 32rpx;
  474. .title-color {
  475. color: #F3C11D;
  476. }
  477. }
  478. .title-btn {
  479. color: rgba(0, 0, 0, 0.6);
  480. font-size: 24rpx;
  481. display: inline-flex;
  482. align-items: center;
  483. }
  484. }
  485. ::v-deep {
  486. .u-scroll-list__indicator {
  487. margin-top: 20rpx;
  488. }
  489. }
  490. }
  491. </style>