index.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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. const getResult = ref(false)
  234. // 拜访对象滚动
  235. const visitScroll = async (event: any) => {
  236. getResult.value = true
  237. uni.showLoading({
  238. mask: true,
  239. title: '获取签到信息中'
  240. })
  241. let curr = event.detail.value[0]
  242. act.value = curr
  243. // 设置当前选中的地方,重置markers
  244. curentDistance.value = locationList.value[curr]
  245. await getSignInfo()
  246. setMarkers(curentDistance.value)
  247. uni.hideLoading()
  248. }
  249. const getSignInfo = debounce(() => {
  250. getsignTempResult()
  251. }, 500)
  252. const getsignTempResult = async () => {
  253. uni.showLoading({
  254. mask: true,
  255. title: '获取签到信息中'
  256. })
  257. const obj = {
  258. locationId: curentDistance.value?.signEntId,
  259. taskTypeId: taskTypeId.value,
  260. combine: true,
  261. signTime: dayjs().format('YYYY-MM-DD HH:mm:ss')
  262. }
  263. const res = await getSignTempResultApi(obj)
  264. if (res.data) {
  265. notClockedIn.value = false
  266. signResultTime.value = res.data
  267. } else {
  268. notClockedIn.value = true
  269. signResultTime.value = ''
  270. }
  271. getResult.value = false
  272. uni.hideLoading()
  273. }
  274. const notClockedIn = ref(true)
  275. const signResultTime = ref('')
  276. const signFn = async () => {
  277. if (!curentDistance.value) {
  278. return uni.showToast({
  279. title: '请选择签到地点',
  280. icon: 'none'
  281. })
  282. }
  283. const obj = {
  284. locationId: curentDistance.value?.signEntId,
  285. taskTypeId: taskTypeId.value,
  286. combine: true,
  287. signTime: dayjs().format('YYYY-MM-DD HH:mm:ss')
  288. }
  289. const res = await signTempApi(obj)
  290. if (res.code === 0) {
  291. uni.showToast({
  292. title: '签到成功',
  293. icon: 'none'
  294. })
  295. notClockedIn.value = false
  296. signResultTime.value = res.data
  297. }
  298. }
  299. let flag = ref(false)
  300. onShow(async () => {
  301. Lock.value = false
  302. // 获取当前坐标 并且每分钟更新一次当前坐标
  303. await getLocation()
  304. // 仅当获取定位成功后才启动定时器
  305. if (flag.value) {
  306. timer.value = setInterval(() => {
  307. getLocation()
  308. }, 60 * 1000)
  309. }
  310. })
  311. const Lock = ref(false)
  312. // 签退去填写表单
  313. const toDetail = debounce(() => {
  314. setTimeout(() => {
  315. if (getResult.value) return
  316. uni.showLoading({
  317. mask: true,
  318. title: '加载中'
  319. })
  320. let TIME: number = 10
  321. // 签退时间
  322. const signOutTime = dayjs(signResultTime.value)
  323. // 获取当前时间
  324. const currentTime = dayjs()
  325. // 计算时间差是否超过15分钟
  326. if (currentTime.diff(signOutTime, 'minute') > TIME) {
  327. const data = {
  328. address: curentDistance.value?.signEntName,
  329. locationId: curentDistance.value?.signEntId,
  330. signTime: signResultTime.value,
  331. signEntType: curentDistance.value?.signEntType,
  332. longitude: curentDistance.value?.longitude,
  333. latitude: curentDistance.value?.latitude,
  334. checkOutTime: dayjs().format('YYYY-MM-DD HH:mm:ss')
  335. }
  336. useTaskStore.setSignAddressInfo(data)
  337. uni.navigateTo({
  338. url: `/pages-sub-task/task/index?value=${taskTypeId.value}&title=${title.value}`
  339. })
  340. uni.hideLoading()
  341. setTimeout(() => {
  342. Lock.value = false
  343. }, 3000)
  344. } else {
  345. uni.showToast({
  346. title: `签到和签退时间间隔不可少于${TIME}分钟。`,
  347. icon: 'none'
  348. })
  349. }
  350. }, 1000)
  351. }, 500)
  352. // 卸载
  353. onUnmounted(() => {
  354. if (timer.value) {
  355. clearInterval(timer.value)
  356. }
  357. })
  358. </script>
  359. <style lang="scss" scoped>
  360. .location-box {
  361. height: 100vh;
  362. display: flex;
  363. flex-direction: column;
  364. .map {
  365. height: 450rpx;
  366. .map-box {
  367. width: 100%;
  368. height: 470rpx;
  369. position: relative;
  370. }
  371. }
  372. .box-cont {
  373. flex: 1;
  374. background-color: #fff;
  375. position: relative;
  376. z-index: 99;
  377. padding: 30rpx;
  378. border-radius: 16px;
  379. .visit-operate {
  380. .title {
  381. font-size: 28rpx;
  382. color: #333;
  383. font-weight: 600;
  384. line-height: 40rpx;
  385. }
  386. .visit-box {
  387. flex: 1;
  388. margin-top: 40rpx;
  389. .visit-list {
  390. height: 350rpx;
  391. }
  392. .info-row {
  393. height: 45rpx;
  394. line-height: 60rpx;
  395. text-align: center;
  396. }
  397. .sign-box {
  398. margin-top: 30rpx;
  399. display: flex;
  400. align-items: center;
  401. justify-content: center;
  402. .sign-in {
  403. width: 160rpx;
  404. height: 160rpx;
  405. border-radius: 50%;
  406. background-color: #6eb657;
  407. text-align: center;
  408. line-height: 160rpx;
  409. font-size: 36rpx;
  410. color: #fff;
  411. font-weight: 600;
  412. }
  413. }
  414. }
  415. .check-in-time {
  416. margin-top: 60rpx;
  417. text-align: center;
  418. font-size: 30rpx;
  419. color: #000;
  420. }
  421. }
  422. }
  423. }
  424. </style>