home.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  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. <!-- <map
  10. id="mapId"
  11. :latitude="latitude"
  12. :longitude="longitude"
  13. :scale="scale"
  14. @markertap="onMarkerTap"
  15. @callouttap="onCalloutTap"
  16. @labeltap="onLabelTap"
  17. :markers="markers"
  18. :include-points="includePoints"
  19. :enable-poi="false"
  20. :enable-traffic="false"
  21. :enable-satellite="true"
  22. :enable-building="false"
  23. show-location
  24. style="width: 100%; height: 280rpx;clip-path: inset(0rpx round 16rpx);"
  25. ></map> -->
  26. <video src="https://birdseye-img.sysuimars.com/youwei-uniapp/home/map-video.mp4" object-fit="cover" :show-fullscreen-btn="false"
  27. :autoplay="true" class="map-video" loop muted :show-progress="false" :show-play-btn="false">
  28. </video>
  29. <view class="map-tips" @click="toPage('allGardenMap')">
  30. 飞鸟入驻管理果场详情,点击查看
  31. </view>
  32. </view>
  33. <view class="type-wrap">
  34. <up-scroll-list
  35. :indicator="typeList.length>5"
  36. indicatorColor="#F3C11D33"
  37. indicatorActiveColor="#F3C11D"
  38. :indicatorWidth="30"
  39. :indicatorBarWidth="13"
  40. ref="scrollListRef"
  41. >
  42. <view class="item-type">
  43. <image class="type-img" :src="`${config.BASIC_IMG}home/type-icon.png`" alt="" />
  44. <view class="type-text">
  45. 全部
  46. </view>
  47. </view>
  48. <view class="item-type" v-for="(item, index) in typeList" :key="index">
  49. <image class="type-img" :src="item.image" alt="" />
  50. <view class="type-text">
  51. {{item.name}}
  52. </view>
  53. </view>
  54. </up-scroll-list>
  55. </view>
  56. <!-- 好味热卖 -->
  57. <view class="home-hot" v-if="gardenList && gardenList.length">
  58. <hot-component-vue :farmId="gardenList[0].farmId"></hot-component-vue>
  59. </view>
  60. <!-- 有味果园 -->
  61. <view class="garden-wrap">
  62. <view class="garden-content">
  63. <view class="hot-title">
  64. <view class="title-l">
  65. 有味果园
  66. </view>
  67. <view class="title-btn" @click="toPage('gardenList')">
  68. 查看全部<up-icon size="10" name="arrow-right"></up-icon>
  69. </view>
  70. </view>
  71. <view class="garden-list">
  72. <view
  73. class="garden-item"
  74. v-for="farm in gardenList"
  75. :key="farm.farmId || farm.id"
  76. @click="goGardenItem(farm)"
  77. >
  78. <view class="garden-l" v-if="farm.coverVideo">
  79. <video :id="`home-video-${farm.farmId || farm.id}`" :src="farm.coverVideo" :show-progress="false" :show-play-btn="true"
  80. :picture-in-picture-mode="[]" :show-background-playback-button="false" :show-center-play-btn="false" object-fit="cover" :show-fullscreen-btn="false"
  81. disable-picture-in-picture :autoplay="false" x5-video-player-type="h5" class="video-dom"
  82. loop muted>
  83. </video>
  84. </view>
  85. <view class="garden-l" v-else>
  86. <image class="garden-img" :src="`${config.BASIC_IMG}home/garden.png`" mode=""></image>
  87. <text class="img-text">无人机实拍视频</text>
  88. </view>
  89. <view class="garden-r">
  90. <view class="garden-title">
  91. {{ farm.name }}
  92. </view>
  93. <view class="garden-info">
  94. 品种:荔枝({{ farm.pz }})
  95. </view>
  96. <view class="garden-info">
  97. 位置:{{ (farm.cityName + farm.countyName) || '—' }}
  98. </view>
  99. <view class="garden-btn-group">
  100. <view class="btn-second">
  101. 有味指数
  102. <text>{{ farm.envScore || '—' }}分</text>
  103. </view>
  104. <view class="btn-primary" @click.stop="goSourceReport(farm)">
  105. 溯源报告
  106. </view>
  107. </view>
  108. </view>
  109. </view>
  110. </view>
  111. </view>
  112. </view>
  113. </view>
  114. </template>
  115. <script setup>
  116. import { ref, onMounted, onUnmounted, nextTick } from 'vue'
  117. import config from "@/api/config.js"
  118. import HOME from '@/api/home'
  119. import hotComponentVue from './hotComponent.vue'
  120. import { onLoad, onPageScroll } from "@dcloudio/uni-app"
  121. const typeSearch = ref(null)
  122. function toPage(path) {
  123. uni.navigateTo({
  124. url: `/pages/tabBar/home/subPages/${path}`
  125. });
  126. }
  127. function goGardenItem(farm) {
  128. const id = farm?.farmId
  129. uni.navigateTo({
  130. url: `/pages/tabBar/home/subPages/gardenItem?farmId=${id}`
  131. });
  132. }
  133. function goSourceReport(farm) {
  134. const id = farm?.farmId
  135. uni.navigateTo({
  136. url: `/pages/tabBar/home/subPages/sourceReport?farmId=${id}`
  137. });
  138. }
  139. onMounted(() => {
  140. // 初始计算
  141. nextTick(() => {
  142. setTimeout(updateCenterVideo, 50)
  143. })
  144. })
  145. onPageScroll(() => handlePageScroll())
  146. onUnmounted(() => {
  147. // 离开页面时全部暂停
  148. pauseAll()
  149. })
  150. const playingId = ref(null)
  151. function throttle(fn, wait = 150) {
  152. let lastTime = 0
  153. let timer = null
  154. return function(...args) {
  155. const now = Date.now()
  156. if (now - lastTime >= wait) {
  157. lastTime = now
  158. fn.apply(this, args)
  159. } else {
  160. clearTimeout(timer)
  161. timer = setTimeout(() => {
  162. lastTime = Date.now()
  163. fn.apply(this, args)
  164. }, wait - (now - lastTime))
  165. }
  166. }
  167. }
  168. const handlePageScroll = throttle(() => {
  169. updateCenterVideo()
  170. }, 200)
  171. function updateCenterVideo() {
  172. // 计算视口中心点附近的 video,播放该 video,其余暂停
  173. const query = uni.createSelectorQuery()
  174. query.selectAll('.video-dom').fields({ id: true, rect: true, size: true }, rects => {
  175. if (!rects || !rects.length) return
  176. const viewportHeight = uni.getSystemInfoSync().windowHeight
  177. const viewportCenter = viewportHeight / 2
  178. let minDistance = Number.POSITIVE_INFINITY
  179. let closestId = null
  180. rects.forEach(rect => {
  181. if (!rect) return
  182. const height = (typeof rect.height === 'number' ? rect.height : (rect.bottom - rect.top))
  183. const elementCenter = rect.top + height / 2
  184. // 仅考虑在可视区域内的元素
  185. if (rect.bottom < 0 || rect.top > viewportHeight) return
  186. const distance = Math.abs(elementCenter - viewportCenter)
  187. if (distance < minDistance) {
  188. minDistance = distance
  189. closestId = rect.id
  190. }
  191. })
  192. if (closestId && playingId.value !== closestId) {
  193. playOnly(closestId)
  194. }
  195. }).exec()
  196. }
  197. function playOnly(targetId) {
  198. // 暂停所有,再播放目标
  199. const ctxs = []
  200. gardenList.value.forEach(item => {
  201. if (!item.coverVideo) return
  202. const id = `home-video-${item.farmId || item.id}`
  203. const ctx = uni.createVideoContext(id)
  204. ctxs.push({ id, ctx })
  205. })
  206. ctxs.forEach(({ id, ctx }) => {
  207. if (id === targetId) {
  208. try { ctx.play() } catch (e) {}
  209. playingId.value = id
  210. } else {
  211. try { ctx.pause() } catch (e) {}
  212. }
  213. })
  214. }
  215. function pauseAll() {
  216. gardenList.value.forEach(item => {
  217. if (!item.coverVideo) return
  218. const id = `home-video-${item.farmId || item.id}`
  219. try { uni.createVideoContext(id).pause() } catch (e) {}
  220. })
  221. }
  222. const latitude = ref(23.099994)
  223. const longitude = ref(113.324520)
  224. const markers = ref([])
  225. const includePoints = ref([])
  226. const mapCtx = ref(null)
  227. const mapPoint = '../../../static/map/point.png'
  228. const farmList = ref([]) // 新增果园列表数据
  229. const scale = ref(10)
  230. const gardenList = ref([])
  231. onLoad(async () => {
  232. // #ifdef MP-WEIXIN
  233. // mapCtx.value = uni.createMapContext('mapId')
  234. // mapCtx.value.on('markerClusterClick', res => {
  235. // console.log('markerClusterClick', res)
  236. // })
  237. // bindEvent()
  238. // #endif
  239. getTypeList()
  240. getFarmData()
  241. })
  242. const typeList = ref([])
  243. function getTypeList() {
  244. HOME.fetchTypeList().then(({data}) => {
  245. typeList.value = data
  246. })
  247. }
  248. function getFarmData() {
  249. HOME.getAllFarm().then(({data}) => {
  250. gardenList.value = data.filter(item => item.recommend !== null)
  251. nextTick(() => setTimeout(updateCenterVideo, 50))
  252. // farmList.value = (data || []).sort((a, b) => {
  253. // // 优先排序:lighten为true的排前面,然后按recommend排序
  254. // if (a.lighten !== b.lighten) return b.lighten ? 1 : -1
  255. // return (b.recommend || 0) - (a.recommend || 0)
  256. // })
  257. // if(farmList.value.length > 0) {
  258. // // 设置地图中心点为第一个果园的位置
  259. // const firstPoint = parsePoint(farmList.value[0].point)
  260. // latitude.value = firstPoint.latitude
  261. // longitude.value = firstPoint.longitude
  262. // // 生成标记点
  263. // generateMarkers(farmList.value)
  264. // }
  265. })
  266. }
  267. // 解析POINT字符串为经纬度对象
  268. function parsePoint(pointStr) {
  269. if(!pointStr) return {longitude: 113.324520, latitude: 23.099994}
  270. const regex = /POINT\(([^ ]+) ([^ ]+)\)/
  271. const match = pointStr.match(regex)
  272. if(match) {
  273. return {
  274. longitude: parseFloat(match[1]),
  275. latitude: parseFloat(match[2])
  276. }
  277. }
  278. return {longitude: 113.324520, latitude: 23.099994} // 默认值
  279. }
  280. // 生成标记点数据
  281. function generateMarkers(farms) {
  282. const newMarkers = farms.map((farm, index) => {
  283. const point = parsePoint(farm.point)
  284. return {
  285. id: farm.farmId || index + 1, // 使用farmId作为唯一标识
  286. latitude: point.latitude,
  287. longitude: point.longitude,
  288. iconPath: mapPoint, // 使用果园图标
  289. width: 20,
  290. height: 20,
  291. joinCluster: true,
  292. // callout: { // 添加气泡标签
  293. // content: farm.name,
  294. // color: '#000',
  295. // fontSize: 14,
  296. // borderRadius: 4,
  297. // bgColor: '#FFCB3C',
  298. // padding: 8,
  299. // display: 'ALWAYS'
  300. // },
  301. label: {
  302. content: farm.name, // 显示果园名称
  303. color: '#000',
  304. fontSize: 12,
  305. bgColor: '#FFCB3C',
  306. borderRadius: 8,
  307. padding: 5,
  308. anchorX: 0,
  309. anchorY: -50,
  310. borderWidth: 1,
  311. borderColor: '#FFCB3C',
  312. textAlign: 'center'
  313. },
  314. customData: farm // 存储原始数据用于点击事件
  315. }
  316. })
  317. // 更新包含的点位范围
  318. includePoints.value = newMarkers.map(marker => ({
  319. latitude: marker.latitude,
  320. longitude: marker.longitude
  321. }))
  322. // #ifdef MP-WEIXIN
  323. mapCtx.value.addMarkers({
  324. markers: newMarkers,
  325. clear: true,
  326. complete(res) {
  327. console.log('添加标记点完成', res)
  328. }
  329. })
  330. // #else
  331. markers.value = newMarkers
  332. // #endif
  333. }
  334. const bindEvent = () => {
  335. // #ifdef MP-WEIXIN
  336. mapCtx.value.initMarkerCluster({
  337. enableDefaultStyle: false,
  338. zoomOnClick: true,
  339. gridSize: 40,
  340. complete(res) {
  341. console.log('initMarkerCluster', res)
  342. }
  343. })
  344. mapCtx.value.on('markerClusterCreate', res => {
  345. console.log('clusterCreate', res)
  346. const clusters = res.clusters
  347. const clusterMarkers = clusters.map(cluster => {
  348. // 获取聚合中的第一个果园
  349. const firstMarker = farmList.value.find(m => m.farmId === cluster.markerIds[0])
  350. return {
  351. ...cluster.center,
  352. width: 20,
  353. height: 20,
  354. clusterId: cluster.clusterId,
  355. iconPath: mapPoint, // 使用果园图标
  356. label: {
  357. content: firstMarker?.name, // 显示果园名称
  358. color: '#000',
  359. fontSize: 12,
  360. bgColor: '#FFCB3C',
  361. borderRadius: 8,
  362. padding: 5,
  363. anchorX: 0,
  364. anchorY: -50,
  365. borderWidth: 1,
  366. borderColor: '#FFCB3C',
  367. textAlign: 'center'
  368. },
  369. customData: {
  370. isCluster: true,
  371. markerIds: cluster.markerIds,
  372. firstMarkerData: firstMarker?.customData
  373. }
  374. }
  375. })
  376. mapCtx.value.addMarkers({
  377. markers: clusterMarkers,
  378. clear: false,
  379. complete(res) {
  380. }
  381. })
  382. })
  383. // #endif
  384. }
  385. const onMarkerTap = (e) => {
  386. const markerId = e.detail.markerId
  387. const marker = markers.value.find(m => m.id === markerId)
  388. if(marker && marker.customData) {
  389. console.log('点击了果园:', marker.customData.name)
  390. // 可以在这里跳转到果园详情页
  391. uni.navigateTo({
  392. url: `/pages/tabBar/home/subPages/gardenItem?farmId=${marker.customData.farmId}`
  393. })
  394. }
  395. }
  396. const onCalloutTap = (e) => {
  397. console.log('点击了气泡:', e)
  398. onMarkerTap(e) // 调用相同的处理逻辑
  399. }
  400. const onLabelTap = (e) => {
  401. console.log('点击了标签:', e)
  402. onMarkerTap(e) // 调用相同的处理逻辑
  403. }
  404. const removeMarkers = () => {
  405. // #ifdef MP-WEIXIN
  406. mapCtx.value.addMarkers({
  407. clear: true,
  408. markers: []
  409. })
  410. // #else
  411. markers.value = []
  412. // #endif
  413. }
  414. </script>
  415. <style lang="scss" scoped>
  416. .garden-l {
  417. ::v-deep {
  418. .uni-video-control-button {
  419. width: 10px;
  420. height: 10px;
  421. }
  422. }
  423. }
  424. /* 或者全局隐藏 */
  425. ::v-deep .uni-video-pip {
  426. display: none !important;
  427. }
  428. .home-wrap {
  429. background-color: #F2F3F5;
  430. padding: 0 0 16rpx 0;
  431. .home-top {
  432. padding: 20rpx 24rpx 0 24rpx;
  433. background: linear-gradient(#FFFFFF, rgba(242, 243, 245, 0));
  434. position: relative;
  435. }
  436. .map-video {
  437. width: 100%;
  438. height: 280rpx;
  439. border-radius: 16rpx;
  440. }
  441. .map-tips {
  442. position: absolute;
  443. bottom: 6rpx;
  444. left: 24rpx;
  445. height: 70rpx;
  446. width: calc(100% - 48rpx);
  447. background: linear-gradient(rgba(102, 102, 102, 0), rgba(0, 0, 0, 0.55));
  448. display: flex;
  449. align-items: center;
  450. font-size: 24rpx;
  451. color: #FFFFFF;
  452. padding-left: 20rpx;
  453. box-sizing: border-box;
  454. border-radius: 0 0 16rpx 16rpx;
  455. }
  456. .home-search {
  457. display: flex;
  458. align-items: center;
  459. width: 100%;
  460. border: 2rpx solid #FFD95E;
  461. border-radius: 40rpx;
  462. margin-bottom: 40rpx;
  463. .search-wrap {
  464. flex: 1;
  465. // padding-left: 22rpx;
  466. ::v-deep {
  467. .u-search {
  468. .u-search__content {
  469. background-color: transparent !important;
  470. .u-search__content__input {
  471. background-color: transparent !important;
  472. }
  473. }
  474. .u-search__action {
  475. text-align: center;
  476. line-height: 52rpx;
  477. border-radius: 40rpx;
  478. font-size: 28rpx;
  479. background-color: #FFD95E;
  480. margin: 6rpx 8rpx;
  481. width: 112rpx;
  482. height: 52rpx;
  483. }
  484. }
  485. }
  486. }
  487. .search-btn {
  488. text-align: center;
  489. line-height: 52rpx;
  490. border-radius: 40rpx;
  491. font-size: 28rpx;
  492. background-color: #FFD95E;
  493. margin: 6rpx 8rpx;
  494. width: 112rpx;
  495. height: 52rpx;
  496. }
  497. }
  498. .type-wrap {
  499. margin: 20rpx;
  500. padding: 20rpx;
  501. background-color: #fff;
  502. border-radius: 16rpx;
  503. height: 200rpx;
  504. box-sizing: border-box;
  505. // 覆盖 up-scroll-list 的默认样式,实现两端对齐
  506. :deep(.u-scroll-list__scroll-view__content) {
  507. display: flex;
  508. justify-content: space-between;
  509. align-items: center;
  510. width: 100%;
  511. min-width: 100%;
  512. }
  513. .item-type {
  514. text-align: center;
  515. font-size: 24rpx;
  516. flex-shrink: 0;
  517. .type-img {
  518. width: 92rpx;
  519. height: 92rpx;
  520. }
  521. }
  522. .item-type + .item-type {
  523. padding-left: 22rpx;
  524. }
  525. }
  526. .home-hot {
  527. padding: 0 20rpx 20rpx;
  528. }
  529. .garden-wrap {
  530. margin: 0 20rpx 20rpx;
  531. background: linear-gradient(#FFFFFF, rgba(255, 255, 255, 0));
  532. padding: 2rpx;
  533. border-radius: 16rpx;
  534. .garden-content{
  535. background: linear-gradient(#fff6d8 30rpx, #FFFFFF 120rpx);
  536. border-radius: 16rpx;
  537. padding: 20rpx;
  538. box-sizing: border-box;
  539. .garden-list {
  540. .garden-item {
  541. display: flex;
  542. .garden-l {
  543. position: relative;
  544. .video-dom {
  545. width: 234rpx;
  546. height: 200rpx;
  547. object-fit: cover;
  548. border-radius: 10rpx;
  549. }
  550. .garden-img {
  551. width: 254rpx;
  552. height: 200rpx;
  553. object-fit: cover;
  554. border-radius: 10rpx;
  555. }
  556. .img-text {
  557. position: absolute;
  558. left: 0%;
  559. top: 0%;
  560. background: rgba(0, 0, 0, 0.3);
  561. backdrop-filter: 8rpx;
  562. border-radius: 10rpx 0 10rpx 0;
  563. font-size: 20rpx;
  564. color: #FFFFFF;
  565. padding: 6rpx 14rpx;
  566. }
  567. }
  568. .garden-r {
  569. padding-left: 20rpx;
  570. .garden-title {
  571. font-size: 28rpx;
  572. line-height: 42rpx;
  573. color: #000000;
  574. font-weight: 600;
  575. padding-bottom: 10rpx;
  576. }
  577. .garden-info {
  578. color: rgba(0, 0, 0, 0.5);
  579. font-size: 24rpx;
  580. line-height: 36rpx;
  581. }
  582. .garden-btn-group {
  583. padding-top: 20rpx;
  584. display: flex;
  585. align-items: center;
  586. .btn-second {
  587. padding: 0 20rpx;
  588. border-radius: 40rpx;
  589. font-size: 24rpx;
  590. color: #C49600;
  591. background: rgba(255, 217, 94, 0.2);
  592. height: 56rpx;
  593. line-height: 56rpx;
  594. }
  595. .btn-primary {
  596. padding: 0 20rpx;
  597. border-radius: 40rpx;
  598. font-size: 24rpx;
  599. color: #000000;
  600. background: #FFD95E;
  601. font-weight: bold;
  602. height: 56rpx;
  603. line-height: 56rpx;
  604. margin-left: 20rpx;
  605. }
  606. }
  607. }
  608. }
  609. .garden-item + .garden-item {
  610. margin-top: 20rpx;
  611. }
  612. }
  613. }
  614. }
  615. .hot-title {
  616. display: flex;
  617. align-items: center;
  618. justify-content: space-between;
  619. padding-bottom: 20rpx;
  620. .title-l {
  621. font-family: 'PangMenZhengDao';
  622. font-size: 32rpx;
  623. .title-color {
  624. color: #F3C11D ;
  625. }
  626. }
  627. .title-btn {
  628. color: rgba(0, 0, 0, 0.6);
  629. font-size: 24rpx;
  630. display: inline-flex;
  631. align-items: center;
  632. }
  633. }
  634. ::v-deep {
  635. .u-scroll-list__indicator {
  636. margin-top: 20rpx;
  637. }
  638. }
  639. }
  640. </style>