小程序农机手端

index.jsx 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import Taro from "@tarojs/taro"
  2. import { useState, useEffect } from "react"
  3. import { View, Map, Image } from "@tarojs/components"
  4. import withLayout from '@/layouts'
  5. import { getMachineryList } from '@/services/machinery'
  6. import { useModel } from "@/store"
  7. import CustomNav from "@/components/CustomNav"
  8. import m1 from '@/assets/machinery/greenMachinery.png'
  9. import m2 from '@/assets/machinery/orangeMachinery.png'
  10. import m3 from '@/assets/machinery/repairMachinery.png'
  11. import MachineryModel from './Model'
  12. import './style.less'
  13. export default withLayout((props) => {
  14. const { router } = props
  15. const { id } = router.params
  16. const [show, setShow] = useState(false)
  17. const { location } = useModel('location')
  18. const [machineryList, setMachineryList] = useState([])
  19. const [markerList, setMarkerList] = useState([])
  20. const [current, setCurrent] = useState()
  21. const [lot, setLot] = useState(112.106514)
  22. const [lat, setLat] = useState(32.685927)
  23. const locList = [
  24. {
  25. longitude: 112.106514,
  26. latitude: 32.685927
  27. },
  28. {
  29. longitude: 112.100406,
  30. latitude: 32.673406
  31. },
  32. {
  33. longitude: 112.06397,
  34. latitude: 32.670488
  35. },
  36. {
  37. longitude: 112.05434,
  38. latitude: 32.685684
  39. },
  40. {
  41. longitude: 112.058724,
  42. latitude: 32.701302
  43. },
  44. {
  45. longitude: 112.015102,
  46. latitude: 32.665504
  47. },
  48. {
  49. longitude: 112.010503,
  50. latitude: 32.685502
  51. },
  52. {
  53. longitude: 112.097747,
  54. latitude: 32.651764
  55. },
  56. {
  57. longitude: 112.149705,
  58. latitude: 32.664957
  59. },
  60. {
  61. longitude: 112.16185,
  62. latitude: 32.701241
  63. },
  64. ]
  65. const handleClick = (e) => {
  66. setCurrent(machineryList[e.markerId])
  67. setShow(true)
  68. }
  69. const goDetail = (val) => {
  70. Taro.navigateTo({ url: `/pages/machineryDetail/index?id=${val}` });
  71. }
  72. const goList = () => {
  73. Taro.navigateTo({ url: '/pages/machineryList/index' });
  74. }
  75. const changeRegion = () => {
  76. setShow(false)
  77. }
  78. useEffect(() => {
  79. if (id && machineryList && markerList.length != 0) {
  80. machineryList.forEach((item, index) => {
  81. if (item.machineryId == id) {
  82. setLot(markerList[index].longitude)
  83. setLat(markerList[index].latitude)
  84. setCurrent(machineryList[index])
  85. setShow(true)
  86. return
  87. }
  88. })
  89. }
  90. }, [id, machineryList, markerList])
  91. useEffect(() => {
  92. getMachineryList({ location: location }).then((res) => {
  93. setMachineryList(res.records)
  94. setMarkerList(res.records.map((item, index) => {
  95. return {
  96. id: index,
  97. longitude: locList[index % 10].longitude,
  98. latitude: locList[index % 10].latitude,
  99. iconPath: item.status == 0 ? m3 : (!item.jobStatus || item.jobStatus == 3) ? m2 : m1,
  100. width: 50,
  101. height: 50
  102. }
  103. }))
  104. })
  105. }, [])
  106. return (
  107. <View className='page-index'>
  108. <View className='index-navbar'>
  109. <CustomNav title='我的农机' />
  110. </View>
  111. <View className='index-container machineryMap'>
  112. <Map
  113. longitude={lot}
  114. latitude={lat}
  115. markers={markerList}
  116. className='map'
  117. onMarkerTap={handleClick}
  118. onRegionChange={changeRegion}
  119. />
  120. <MachineryModel visible={show} value={current} goDetail={goDetail} goList={goList} />
  121. </View>
  122. </View>
  123. )
  124. })