index.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. <template>
  2. <view class="location-box">
  3. <!-- 地图 -->
  4. <view class="map">
  5. <map
  6. id="myMap"
  7. ref="mapRef"
  8. class="map-box"
  9. scale="14"
  10. :latitude="latitud.latitude"
  11. :longitude="latitud.longitude"
  12. :markers="markers"
  13. :circles="circles"
  14. show-location
  15. show-compass
  16. >
  17. </map>
  18. </view>
  19. <!-- 打卡 -->
  20. <view class="box-cont">
  21. <view class="visit-operate">
  22. <view class="title"> <text>选择拜访对象</text></view>
  23. <view class="visit-box">
  24. <picker-view indicator-class="select" class="visit-list" @change="visitScroll">
  25. <picker-view-column>
  26. <view
  27. class="info-row"
  28. v-for="(item, index) in locationList"
  29. :key="index"
  30. :class="{ act: act === index }"
  31. >
  32. <view> {{ item.signEntName }} ({{ item.dis }} )</view>
  33. </view>
  34. </picker-view-column>
  35. </picker-view>
  36. <!-- 打卡 -->
  37. <view class="sign-box">
  38. <view class="sign-in" @click="signFn" v-if="notClockedIn">
  39. <view>签到</view>
  40. </view>
  41. <view class="sign-in" @click="toDetail" v-else>
  42. <view>签退</view>
  43. </view>
  44. </view>
  45. </view>
  46. <view class="check-in-time" v-if="!notClockedIn">
  47. <view>签到时间: {{ signResultTime }}</view>
  48. </view>
  49. </view>
  50. </view>
  51. </view>
  52. </template>
  53. <script setup lang="ts">
  54. import dayjs from 'dayjs'
  55. import { onLoad, onShow } from '@dcloudio/uni-app'
  56. import { getCurrentInstance, ref, reactive, onUnmounted } from 'vue'
  57. import { getWxLocationApi } from '@/wxUtils/index'
  58. import { debounce } from '@/utils/index'
  59. import { useTask } from '@/store/task'
  60. import {
  61. getDictPointApi,
  62. getPointSignInfoApi,
  63. signTempApi,
  64. getSignTempResultApi
  65. } from '@/service/modules/getTask'
  66. import { getDistance } from '@/utils/index'
  67. const useTaskStore = useTask()
  68. const { ctx }: any = getCurrentInstance()
  69. const mapRef = ref()
  70. const latitud = reactive({
  71. latitude: 39.909,
  72. longitude: 116.39742
  73. })
  74. const markers = ref<any[]>([])
  75. const circles = ref<any[]>([])
  76. const myMap = ref()
  77. const taskTypeId = ref('')
  78. const title = ref('')
  79. // 定时器
  80. const timer = ref()
  81. onLoad(async (e: any) => {
  82. // 保存当前任务类型和标题
  83. title.value = e?.title
  84. taskTypeId.value = e?.value
  85. myMap.value = uni.createMapContext('myMap', ctx)
  86. // 获取可打卡范围
  87. await getDictPoint()
  88. })
  89. const getLocation = async () => {
  90. try {
  91. // 获取位置
  92. const res: any = await getWxLocationApi()
  93. const { latitude, longitude } = res || {}
  94. // 当本次坐标与上次坐标不相同时
  95. if (latitud.latitude !== latitude || latitud.longitude !== longitude) {
  96. latitud.latitude = latitude
  97. latitud.longitude = longitude
  98. flag.value = true
  99. setMapRegion() // 设置地图区域
  100. // 获取医院列表
  101. await fetchHospitalList()
  102. }
  103. } catch (err) {
  104. handleLocationError()
  105. }
  106. }
  107. // 提取获取医院列表的函数
  108. const fetchHospitalList = async () => {
  109. try {
  110. await getPointSignInfo()
  111. } catch (err) {
  112. console.error('获取终端失败,请联系管理员')
  113. }
  114. }
  115. // 错误处理函数
  116. const handleLocationError = () => {
  117. flag.value = false
  118. // 清除定时器
  119. if (timer.value) {
  120. clearInterval(timer.value)
  121. timer.value = null
  122. }
  123. uni.showModal({
  124. title: '错误提示',
  125. showCancel: false,
  126. content: '获取定位失败'
  127. })
  128. }
  129. const mapRadius = ref()
  130. const getDictPoint = async () => {
  131. uni.showLoading()
  132. const res = await getDictPointApi()
  133. mapRadius.value = res.data[0].value
  134. uni.hideLoading()
  135. }
  136. // 设置地图范围
  137. const setMapRegion = () => {
  138. myMap.value.getRegion({
  139. success() {
  140. // 获取东经西经维度
  141. circles.value.push({
  142. latitude: latitud.latitude,
  143. longitude: latitud.longitude,
  144. color: '#ffffff',
  145. fillColor: '#7cb5ec88',
  146. radius: Number(mapRadius.value),
  147. strokeWidth: 2
  148. })
  149. }
  150. })
  151. }
  152. const hospitalList = ref<any[]>([])
  153. const getPointSignInfo = async () => {
  154. const res = await getPointSignInfoApi(latitud)
  155. hospitalList.value = res.data
  156. calcDistance()
  157. }
  158. const curentDistance = ref()
  159. // 计算当前地址和医院的距离,得到最短的距离
  160. async function calcDistance() {
  161. //! 数据处理
  162. if (!hospitalList.value.length) return
  163. let hospitalMap = hospitalList.value.map((item: any) => {
  164. // 计算每个地点与当前位置的距离
  165. item.distance = getDistance(
  166. latitud.longitude,
  167. latitud.latitude,
  168. item!.longitude,
  169. item!.latitude
  170. )
  171. return item
  172. })
  173. // 根据距离排序
  174. hospitalMap.sort((itemA: any, itemB: any) => {
  175. return itemA.distance - itemB.distance > 0 ? 1 : -1
  176. })
  177. hospitalList.value = hospitalMap
  178. // 默认设置最近的地点
  179. curentDistance.value = hospitalMap[0]
  180. // 获取第一个医院的签到结果
  181. getsignTempResult()
  182. setMarkers(curentDistance.value)
  183. checkSignInState()
  184. }
  185. // 设置marker
  186. const setMarkers = (distanceItem: any) => {
  187. markers.value = [
  188. {
  189. id: Number(distanceItem.signEntId),
  190. latitude: distanceItem.latitude,
  191. longitude: distanceItem.longitude,
  192. width: 25,
  193. height: 35,
  194. callout: {
  195. display: 'BYCLICK',
  196. content: distanceItem.signEntName,
  197. color: 'black',
  198. borderRadius: 8,
  199. bgColor: '#ffffff',
  200. padding: 10,
  201. textAlign: 'center'
  202. }
  203. }
  204. ]
  205. }
  206. // 是否在范围内
  207. const withinTheScopeOf = ref(false)
  208. const locationList = ref<any[]>([])
  209. // 检查可以签到状态
  210. const checkSignInState = () => {
  211. // 计算当前距离距离选择地点的距离
  212. let result = getDistance(
  213. latitud.longitude,
  214. latitud.latitude,
  215. curentDistance?.value?.longitude,
  216. curentDistance?.value?.latitude
  217. )
  218. result = result * 1000
  219. // 半径范围与距离判断
  220. withinTheScopeOf.value = mapRadius.value >= result
  221. // 对地点进行过滤,去除超过当前半径范围内的
  222. locationList.value = hospitalList.value.filter((item) => {
  223. let itemResult = getDistance(latitud.longitude, latitud.latitude, item.longitude, item.latitude)
  224. itemResult = itemResult * 1000
  225. if (mapRadius.value > itemResult) {
  226. item.value = item.signEntId
  227. item.label = item.signEntName
  228. return item
  229. }
  230. })
  231. }
  232. const act = ref(0)
  233. // 拜访对象滚动
  234. const visitScroll = (event: any) => {
  235. let curr = event.detail.value[0]
  236. act.value = curr
  237. // 设置当前选中的地方,重置markers
  238. curentDistance.value = locationList.value[curr]
  239. getSignInfo()
  240. setMarkers(curentDistance.value)
  241. }
  242. const getSignInfo = debounce(() => {
  243. getsignTempResult()
  244. }, 500)
  245. const getsignTempResult = async () => {
  246. uni.showLoading({
  247. mask: true,
  248. title: '获取签到信息中'
  249. })
  250. const obj = {
  251. locationId: curentDistance.value?.signEntId,
  252. taskTypeId: taskTypeId.value,
  253. combine: true,
  254. signTime: dayjs().format('YYYY-MM-DD HH:mm:ss')
  255. }
  256. const res = await getSignTempResultApi(obj)
  257. if (res.data) {
  258. notClockedIn.value = false
  259. signResultTime.value = res.data
  260. } else {
  261. notClockedIn.value = true
  262. signResultTime.value = ''
  263. }
  264. uni.hideLoading()
  265. }
  266. const notClockedIn = ref(true)
  267. const signResultTime = ref('')
  268. const signFn = async () => {
  269. if (!curentDistance.value) {
  270. return uni.showToast({
  271. title: '请选择签到地点',
  272. icon: 'none'
  273. })
  274. }
  275. const obj = {
  276. locationId: curentDistance.value?.signEntId,
  277. taskTypeId: taskTypeId.value,
  278. combine: true,
  279. signTime: dayjs().format('YYYY-MM-DD HH:mm:ss')
  280. }
  281. const res = await signTempApi(obj)
  282. if (res.code === 0) {
  283. uni.showToast({
  284. title: '签到成功',
  285. icon: 'none'
  286. })
  287. notClockedIn.value = false
  288. signResultTime.value = res.data
  289. }
  290. }
  291. let flag = ref(false)
  292. onShow(async () => {
  293. Lock.value = false
  294. // 获取当前坐标 并且每分钟更新一次当前坐标
  295. await getLocation()
  296. // 仅当获取定位成功后才启动定时器
  297. if (flag.value) {
  298. timer.value = setInterval(() => {
  299. getLocation()
  300. }, 60 * 1000)
  301. }
  302. })
  303. const Lock = ref(false)
  304. // 签退去填写表单
  305. const toDetail = () => {
  306. if (Lock.value) return
  307. uni.showLoading({
  308. mask: true,
  309. title: '加载中'
  310. })
  311. Lock.value = true
  312. let TIME: number = 10
  313. // 签退时间
  314. const signOutTime = dayjs(signResultTime.value)
  315. // 获取当前时间
  316. const currentTime = dayjs()
  317. // 计算时间差是否超过15分钟
  318. if (currentTime.diff(signOutTime, 'minute') > TIME) {
  319. const data = {
  320. address: curentDistance.value?.signEntName,
  321. locationId: curentDistance.value?.signEntId,
  322. signTime: signResultTime.value,
  323. signEntType: curentDistance.value?.signEntType,
  324. longitude: curentDistance.value?.longitude,
  325. latitude: curentDistance.value?.latitude,
  326. checkOutTime: dayjs().format('YYYY-MM-DD HH:mm:ss')
  327. }
  328. useTaskStore.setSignAddressInfo(data)
  329. uni.navigateTo({
  330. url: `/pages-sub-task/task/index?value=${taskTypeId.value}&title=${title.value}`
  331. })
  332. uni.hideLoading()
  333. setTimeout(() => {
  334. Lock.value = false
  335. }, 3000)
  336. } else {
  337. uni.showToast({
  338. title: `签到和签退时间间隔不可少于${TIME}分钟。`,
  339. icon: 'none'
  340. })
  341. }
  342. }
  343. // 卸载
  344. onUnmounted(() => {
  345. if (timer.value) {
  346. clearInterval(timer.value)
  347. }
  348. })
  349. </script>
  350. <style lang="scss" scoped>
  351. .location-box {
  352. height: 100vh;
  353. display: flex;
  354. flex-direction: column;
  355. .map {
  356. height: 450rpx;
  357. .map-box {
  358. width: 100%;
  359. height: 470rpx;
  360. position: relative;
  361. }
  362. }
  363. .box-cont {
  364. flex: 1;
  365. background-color: #fff;
  366. position: relative;
  367. z-index: 99;
  368. padding: 30rpx;
  369. border-radius: 16px;
  370. .visit-operate {
  371. .title {
  372. font-size: 28rpx;
  373. color: #333;
  374. font-weight: 600;
  375. line-height: 40rpx;
  376. }
  377. .visit-box {
  378. flex: 1;
  379. margin-top: 40rpx;
  380. .visit-list {
  381. height: 350rpx;
  382. }
  383. .info-row {
  384. height: 45rpx;
  385. line-height: 60rpx;
  386. text-align: center;
  387. }
  388. .sign-box {
  389. margin-top: 30rpx;
  390. display: flex;
  391. align-items: center;
  392. justify-content: center;
  393. .sign-in {
  394. width: 160rpx;
  395. height: 160rpx;
  396. border-radius: 50%;
  397. background-color: #6eb657;
  398. text-align: center;
  399. line-height: 160rpx;
  400. font-size: 36rpx;
  401. color: #fff;
  402. font-weight: 600;
  403. }
  404. }
  405. }
  406. .check-in-time {
  407. margin-top: 60rpx;
  408. text-align: center;
  409. font-size: 30rpx;
  410. color: #000;
  411. }
  412. }
  413. }
  414. }
  415. </style>