home.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  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. <view class="map-tips" @click="toPage('allGardenMap')">
  27. 飞鸟入驻管理果场详情,点击查看
  28. </view>
  29. </view>
  30. <view class="type-wrap">
  31. <up-scroll-list indicatorColor="#F3C11D33" indicatorActiveColor="#F3C11D" :indicatorWidth="30" :indicatorBarWidth="13">
  32. <view class="item-type">
  33. <image class="type-img" :src="`${config.BASIC_IMG}home/type-icon.png`" alt="" />
  34. <view class="type-text">
  35. 全部
  36. </view>
  37. </view>
  38. <view class="item-type" v-for="(item, index) in typeList" :key="index">
  39. <image class="type-img" :src="item.image" alt="" />
  40. <view class="type-text">
  41. {{item.name}}
  42. </view>
  43. </view>
  44. </up-scroll-list>
  45. </view>
  46. <!-- 好味热卖 -->
  47. <view class="home-hot">
  48. <hot-component-vue></hot-component-vue>
  49. </view>
  50. <!-- 有味果园 -->
  51. <view class="garden-wrap">
  52. <view class="garden-content">
  53. <view class="hot-title">
  54. <view class="title-l">
  55. 有味果园
  56. </view>
  57. <view class="title-btn" @click="toPage('gardenList')">
  58. 查看全部<up-icon size="10" name="arrow-right"></up-icon>
  59. </view>
  60. </view>
  61. <view class="garden-list">
  62. <view
  63. class="garden-item"
  64. v-for="farm in gardenList"
  65. :key="farm.farmId || farm.id"
  66. @click="goGardenItem(farm)"
  67. >
  68. <view class="garden-l">
  69. <image
  70. class="garden-img"
  71. :src="farm.img || `${config.BASIC_IMG}home/garden.png`"
  72. mode=""
  73. ></image>
  74. <text class="img-text">无人机实拍视频</text>
  75. </view>
  76. <view class="garden-r">
  77. <view class="garden-title">
  78. {{ farm.name }}
  79. </view>
  80. <view class="garden-info">
  81. 品种:荔枝({{ farm.pz }})
  82. </view>
  83. <view class="garden-info">
  84. 位置:{{ (farm.cityName + farm.countyName) || '—' }}
  85. </view>
  86. <view class="garden-btn-group">
  87. <view class="btn-second">
  88. 有味指数
  89. <text>{{ farm.envScore || '—' }}分</text>
  90. </view>
  91. <view class="btn-primary" @click.stop="goSourceReport(farm)">
  92. 溯源报告
  93. </view>
  94. </view>
  95. </view>
  96. </view>
  97. </view>
  98. </view>
  99. </view>
  100. </view>
  101. </template>
  102. <script setup>
  103. import { ref, onMounted } from 'vue'
  104. import config from "@/api/config.js"
  105. import HOME from '@/api/home'
  106. import hotComponentVue from './hotComponent.vue'
  107. const typeSearch = ref(null)
  108. function toPage(path) {
  109. uni.navigateTo({
  110. url: `/pages/tabBar/home/subPages/${path}`
  111. });
  112. }
  113. function goGardenItem(farm) {
  114. const id = farm?.farmId
  115. uni.navigateTo({
  116. url: `/pages/tabBar/home/subPages/gardenItem?id=${id}`
  117. });
  118. }
  119. function goSourceReport(farm) {
  120. const id = farm?.farmId
  121. uni.navigateTo({
  122. url: `/pages/tabBar/home/subPages/sourceReport?id=${id}`
  123. });
  124. }
  125. const latitude = ref(23.099994)
  126. const longitude = ref(113.324520)
  127. const markers = ref([])
  128. const includePoints = ref([])
  129. const mapCtx = ref(null)
  130. const mapPoint = '../../../static/map/point.png'
  131. const farmList = ref([]) // 新增果园列表数据
  132. const scale = ref(10)
  133. const gardenList = ref([])
  134. onMounted(async () => {
  135. // #ifdef MP-WEIXIN
  136. mapCtx.value = uni.createMapContext('mapId')
  137. mapCtx.value.on('markerClusterClick', res => {
  138. console.log('markerClusterClick', res)
  139. })
  140. getTypeList()
  141. getFarmData()
  142. bindEvent()
  143. // #endif
  144. })
  145. const typeList = ref([])
  146. function getTypeList() {
  147. HOME.fetchTypeList().then(({data}) => {
  148. typeList.value = data
  149. })
  150. }
  151. function getFarmData() {
  152. HOME.getAllFarm().then(({data}) => {
  153. farmList.value = (data || []).sort((a, b) => {
  154. // 优先排序:lighten为true的排前面,然后按recommend排序
  155. if (a.lighten !== b.lighten) return b.lighten ? 1 : -1
  156. return (b.recommend || 0) - (a.recommend || 0)
  157. })
  158. gardenList.value = data.filter(item => item.recommend !== null)
  159. if(farmList.value.length > 0) {
  160. // 设置地图中心点为第一个果园的位置
  161. const firstPoint = parsePoint(farmList.value[0].point)
  162. latitude.value = firstPoint.latitude
  163. longitude.value = firstPoint.longitude
  164. // 生成标记点
  165. generateMarkers(farmList.value)
  166. }
  167. })
  168. }
  169. // 解析POINT字符串为经纬度对象
  170. function parsePoint(pointStr) {
  171. if(!pointStr) return {longitude: 113.324520, latitude: 23.099994}
  172. const regex = /POINT\(([^ ]+) ([^ ]+)\)/
  173. const match = pointStr.match(regex)
  174. if(match) {
  175. return {
  176. longitude: parseFloat(match[1]),
  177. latitude: parseFloat(match[2])
  178. }
  179. }
  180. return {longitude: 113.324520, latitude: 23.099994} // 默认值
  181. }
  182. // 生成标记点数据
  183. function generateMarkers(farms) {
  184. const newMarkers = farms.map((farm, index) => {
  185. const point = parsePoint(farm.point)
  186. return {
  187. id: farm.farmId || index + 1, // 使用farmId作为唯一标识
  188. latitude: point.latitude,
  189. longitude: point.longitude,
  190. iconPath: mapPoint, // 使用果园图标
  191. width: 20,
  192. height: 20,
  193. joinCluster: true,
  194. // callout: { // 添加气泡标签
  195. // content: farm.name,
  196. // color: '#000',
  197. // fontSize: 14,
  198. // borderRadius: 4,
  199. // bgColor: '#FFCB3C',
  200. // padding: 8,
  201. // display: 'ALWAYS'
  202. // },
  203. label: {
  204. content: farm.name, // 显示果园名称
  205. color: '#000',
  206. fontSize: 12,
  207. bgColor: '#FFCB3C',
  208. borderRadius: 8,
  209. padding: 5,
  210. anchorX: 0,
  211. anchorY: -50,
  212. borderWidth: 1,
  213. borderColor: '#FFCB3C',
  214. textAlign: 'center'
  215. },
  216. customData: farm // 存储原始数据用于点击事件
  217. }
  218. })
  219. // 更新包含的点位范围
  220. includePoints.value = newMarkers.map(marker => ({
  221. latitude: marker.latitude,
  222. longitude: marker.longitude
  223. }))
  224. // #ifdef MP-WEIXIN
  225. mapCtx.value.addMarkers({
  226. markers: newMarkers,
  227. clear: true,
  228. complete(res) {
  229. console.log('添加标记点完成', res)
  230. }
  231. })
  232. // #else
  233. markers.value = newMarkers
  234. // #endif
  235. }
  236. const bindEvent = () => {
  237. // #ifdef MP-WEIXIN
  238. mapCtx.value.initMarkerCluster({
  239. enableDefaultStyle: false,
  240. zoomOnClick: true,
  241. gridSize: 40,
  242. complete(res) {
  243. console.log('initMarkerCluster', res)
  244. }
  245. })
  246. mapCtx.value.on('markerClusterCreate', res => {
  247. console.log('clusterCreate', res)
  248. const clusters = res.clusters
  249. const clusterMarkers = clusters.map(cluster => {
  250. // 获取聚合中的第一个果园
  251. const firstMarker = farmList.value.find(m => m.farmId === cluster.markerIds[0])
  252. return {
  253. ...cluster.center,
  254. width: 20,
  255. height: 20,
  256. clusterId: cluster.clusterId,
  257. iconPath: mapPoint, // 使用果园图标
  258. label: {
  259. content: firstMarker?.name, // 显示果园名称
  260. color: '#000',
  261. fontSize: 12,
  262. bgColor: '#FFCB3C',
  263. borderRadius: 8,
  264. padding: 5,
  265. anchorX: 0,
  266. anchorY: -50,
  267. borderWidth: 1,
  268. borderColor: '#FFCB3C',
  269. textAlign: 'center'
  270. },
  271. customData: {
  272. isCluster: true,
  273. markerIds: cluster.markerIds,
  274. firstMarkerData: firstMarker?.customData
  275. }
  276. }
  277. })
  278. mapCtx.value.addMarkers({
  279. markers: clusterMarkers,
  280. clear: false,
  281. complete(res) {
  282. }
  283. })
  284. })
  285. // #endif
  286. }
  287. const onMarkerTap = (e) => {
  288. const markerId = e.detail.markerId
  289. const marker = markers.value.find(m => m.id === markerId)
  290. if(marker && marker.customData) {
  291. console.log('点击了果园:', marker.customData.name)
  292. // 可以在这里跳转到果园详情页
  293. uni.navigateTo({
  294. url: `/pages/tabBar/home/subPages/gardenItem?id=${marker.customData.farmId}`
  295. })
  296. }
  297. }
  298. const onCalloutTap = (e) => {
  299. console.log('点击了气泡:', e)
  300. onMarkerTap(e) // 调用相同的处理逻辑
  301. }
  302. const onLabelTap = (e) => {
  303. console.log('点击了标签:', e)
  304. onMarkerTap(e) // 调用相同的处理逻辑
  305. }
  306. const removeMarkers = () => {
  307. // #ifdef MP-WEIXIN
  308. mapCtx.value.addMarkers({
  309. clear: true,
  310. markers: []
  311. })
  312. // #else
  313. markers.value = []
  314. // #endif
  315. }
  316. </script>
  317. <style lang="scss" scoped>
  318. .home-wrap {
  319. background-color: #F2F3F5;
  320. padding: 0 0 16rpx 0;
  321. .home-top {
  322. padding: 20rpx 24rpx 0 24rpx;
  323. background: linear-gradient(#FFFFFF, rgba(242, 243, 245, 0));
  324. position: relative;
  325. }
  326. .map-tips {
  327. position: absolute;
  328. bottom: 0%;
  329. left: 24rpx;
  330. height: 70rpx;
  331. width: calc(100% - 48rpx);
  332. background: linear-gradient(rgba(102, 102, 102, 0), rgba(0, 0, 0, 0.55));
  333. display: flex;
  334. align-items: center;
  335. font-size: 24rpx;
  336. color: #FFFFFF;
  337. padding-left: 20rpx;
  338. box-sizing: border-box;
  339. border-radius: 0 0 16rpx 16rpx;
  340. }
  341. .home-search {
  342. display: flex;
  343. align-items: center;
  344. width: 100%;
  345. border: 2rpx solid #FFD95E;
  346. border-radius: 40rpx;
  347. margin-bottom: 40rpx;
  348. .search-wrap {
  349. flex: 1;
  350. // padding-left: 22rpx;
  351. ::v-deep {
  352. .u-search {
  353. .u-search__content {
  354. background-color: transparent !important;
  355. .u-search__content__input {
  356. background-color: transparent !important;
  357. }
  358. }
  359. .u-search__action {
  360. text-align: center;
  361. line-height: 52rpx;
  362. border-radius: 40rpx;
  363. font-size: 28rpx;
  364. background-color: #FFD95E;
  365. margin: 6rpx 8rpx;
  366. width: 112rpx;
  367. height: 52rpx;
  368. }
  369. }
  370. }
  371. }
  372. .search-btn {
  373. text-align: center;
  374. line-height: 52rpx;
  375. border-radius: 40rpx;
  376. font-size: 28rpx;
  377. background-color: #FFD95E;
  378. margin: 6rpx 8rpx;
  379. width: 112rpx;
  380. height: 52rpx;
  381. }
  382. }
  383. .type-wrap {
  384. margin: 20rpx;
  385. padding: 20rpx;
  386. background-color: #fff;
  387. border-radius: 16rpx;
  388. height: 200rpx;
  389. box-sizing: border-box;
  390. .item-type {
  391. text-align: center;
  392. font-size: 24rpx;
  393. .type-img {
  394. width: 92rpx;
  395. height: 92rpx;
  396. }
  397. }
  398. .item-type + .item-type {
  399. padding-left: 22rpx;
  400. }
  401. }
  402. .home-hot {
  403. padding: 0 20rpx 20rpx;
  404. }
  405. .garden-wrap {
  406. margin: 0 20rpx 20rpx;
  407. background: linear-gradient(#FFFFFF, rgba(255, 255, 255, 0));
  408. padding: 2rpx;
  409. border-radius: 16rpx;
  410. .garden-content{
  411. background: linear-gradient(#fff6d8 30rpx, #FFFFFF 120rpx);
  412. border-radius: 16rpx;
  413. padding: 20rpx;
  414. box-sizing: border-box;
  415. .garden-list {
  416. .garden-item {
  417. display: flex;
  418. .garden-l {
  419. position: relative;
  420. .garden-img {
  421. width: 254rpx;
  422. height: 200rpx;
  423. object-fit: cover;
  424. border-radius: 10rpx;
  425. }
  426. .img-text {
  427. position: absolute;
  428. left: 0%;
  429. top: 0%;
  430. background: rgba(0, 0, 0, 0.3);
  431. backdrop-filter: 8rpx;
  432. border-radius: 10rpx 0 10rpx 0;
  433. font-size: 20rpx;
  434. color: #FFFFFF;
  435. padding: 6rpx 14rpx;
  436. }
  437. }
  438. .garden-r {
  439. padding-left: 20rpx;
  440. .garden-title {
  441. font-size: 28rpx;
  442. line-height: 42rpx;
  443. color: #000000;
  444. font-weight: 600;
  445. padding-bottom: 10rpx;
  446. }
  447. .garden-info {
  448. color: rgba(0, 0, 0, 0.5);
  449. font-size: 24rpx;
  450. line-height: 36rpx;
  451. }
  452. .garden-btn-group {
  453. padding-top: 20rpx;
  454. display: flex;
  455. align-items: center;
  456. .btn-second {
  457. padding: 0 20rpx;
  458. border-radius: 40rpx;
  459. font-size: 24rpx;
  460. color: #C49600;
  461. background: rgba(255, 217, 94, 0.2);
  462. height: 56rpx;
  463. line-height: 56rpx;
  464. }
  465. .btn-primary {
  466. padding: 0 20rpx;
  467. border-radius: 40rpx;
  468. font-size: 24rpx;
  469. color: #000000;
  470. background: #FFD95E;
  471. font-weight: bold;
  472. height: 56rpx;
  473. line-height: 56rpx;
  474. }
  475. }
  476. }
  477. }
  478. .garden-item + .garden-item {
  479. margin-top: 20rpx;
  480. }
  481. }
  482. }
  483. }
  484. .hot-title {
  485. display: flex;
  486. align-items: center;
  487. justify-content: space-between;
  488. padding-bottom: 20rpx;
  489. .title-l {
  490. font-family: 'PangMenZhengDao';
  491. font-size: 32rpx;
  492. .title-color {
  493. color: #F3C11D ;
  494. }
  495. }
  496. .title-btn {
  497. color: rgba(0, 0, 0, 0.6);
  498. font-size: 24rpx;
  499. display: inline-flex;
  500. align-items: center;
  501. }
  502. }
  503. ::v-deep {
  504. .u-scroll-list__indicator {
  505. margin-top: 20rpx;
  506. }
  507. }
  508. }
  509. </style>