Browse Source

志愿者开始服务经纬度获取

jiayubo 2 days ago
parent
commit
500e9180f4

+ 5 - 6
api/volunteer.js

@@ -58,18 +58,17 @@ export function getVolunteerOrderInfo(data) {
 
 
 //志愿者滑动,开始订单
-export function getTimesByDate(secondOrderId) {
+export function getTimesByDate(data) {
     return request({
-        url: `/core/volunteer/orders/getTimesByDate/${secondOrderId}`,
-        method: 'get',
+      url: `/core/volunteer/orders/volunteerStartSecondOrder?${data}`,
+      method: 'post',
     })
 }
 //志愿者点击结束订单
 export function getVolunteerFinishSecondOrder(data) {
     return request({
-        url: `/core/volunteer/orders/volunteerFinishSecondOrder`,
-        method: 'post',
-        data: data
+        url: `/core/volunteer/orders/volunteerFinishSecondOrder?${data}`,
+        method: 'post'
     })
 }
 

+ 16 - 1
pages_classify/components/PositioningMap/index.vue

@@ -65,7 +65,22 @@ const geocodeAddress = (address) => {
         success: function (res) {
             console.log('初始化地图上下文', res);
             mapData.value = res.result;
-            markers.value = [{ latitude: res.result.location.lat, longitude: res.result.location.lng,id: 1, }];
+            markers.value = [{ 
+                latitude: res.result.location.lat, 
+                longitude: res.result.location.lng,
+                id: 1,
+                width: 20,
+                height: 20,
+                callout: {
+                    content: props.address,
+                    color: '#000000',
+                    fontSize: 12,
+                    borderRadius: 4,
+                    padding: 5,
+                    bgColor: '#ffffff',
+                    display: 'ALWAYS'
+                }
+            }];
         },
         fail: function (error) {
             console.error(error);

+ 151 - 15
pages_classify/pages/handle/index.vue

@@ -63,7 +63,7 @@
 <script setup>
 import { ref } from 'vue'
 import { onLoad, onUnload } from '@dcloudio/uni-app'
-import { getVolunteerOrderInfo, getTimesByDate } from '@/api/volunteer.js'
+import { getVolunteerOrderInfo, getTimesByDate, getVolunteerFinishSecondOrder } from '@/api/volunteer.js'
 import PositioningMap from '@/pages_classify/components/PositioningMap/index.vue'
 import Slide from '@/pages_classify/components/Slide/index.vue'
 import { useDict } from '@/utils/dict.js'
@@ -85,6 +85,90 @@ const onPhone = (phone) => {
 const dateData = ref('00:00:00')
 let timer = null
 
+// 获取当前位置信息
+const getCurrentLocation = () => {
+  return new Promise((resolve, reject) => {
+    // 先检查用户是否授权了位置权限
+    uni.getSetting({
+      success: (res) => {
+        if (!res.authSetting['scope.userLocation']) {
+          // 如果用户未授权,先请求授权
+          uni.authorize({
+            scope: 'scope.userLocation',
+            success: () => {
+              // 授权成功后获取位置
+              getLocation(resolve, reject);
+            },
+            fail: (err) => {
+              console.error('位置授权失败:', err);
+              uni.showModal({
+                title: '提示',
+                content: '需要获取您的位置信息,请在设置中打开位置权限',
+                confirmText: '去设置',
+                success: (res) => {
+                  if (res.confirm) {
+                    uni.openSetting();
+                  }
+                }
+              });
+              reject(err);
+            }
+          });
+        } else {
+          // 已授权,直接获取位置
+          getLocation(resolve, reject);
+        }
+      },
+      fail: (err) => {
+        console.error('获取设置失败:', err);
+        reject(err);
+      }
+    });
+  });
+}
+
+// 获取位置的具体实现
+const getLocation = (resolve, reject) => {
+  // 开发环境中使用模拟位置,避免权限问题
+  if (process.env.NODE_ENV === 'development') {
+    console.log('开发环境,使用模拟位置');
+    // 模拟重庆的位置
+    const mockLocation = {
+      longitude: 106.504962,
+      latitude: 29.533155
+    };
+    setTimeout(() => {
+      resolve(mockLocation);
+    }, 500);
+    return;
+  }
+
+  uni.getLocation({
+    type: 'gcj02',
+    isHighAccuracy: true, // 高精度
+    highAccuracyExpireTime: 3000, // 高精度过期时间
+    success: (res) => {
+      const { longitude, latitude } = res;
+      console.log('当前位置 - 经度:', longitude);
+      console.log('当前位置 - 纬度:', latitude);
+      resolve({
+        longitude,
+        latitude
+      });
+    },
+    fail: (err) => {
+      console.error('获取位置失败:', err);
+      // 如果获取失败,使用默认位置(重庆)
+      const defaultLocation = {
+        longitude: 106.504962,
+        latitude: 29.533155
+      };
+      console.log('使用默认位置:', defaultLocation);
+      resolve(defaultLocation);
+    }
+  });
+}
+
 const slideData = computed(() => {
   //服务已开始,待上传图片
   if (orderStatus.value) {
@@ -145,9 +229,22 @@ const getOrderDetail = async () => {
   }
 }
 
-const change = (e) => {
+const change = async (e) => {
   if (e.state && !orderStatus.value) {
-    getTimesByDate(secondOrderId.value).then((res) => {
+    try {
+      uni.showLoading({
+        title: '获取位置信息...'
+      })
+      
+      // 获取开始服务时的位置
+      const locationData = await getCurrentLocation()
+      
+      // 构建参数字符串
+      const params = `secondOrderId=${secondOrderId.value}&serviceStartLongitude=${locationData.longitude.toString()}&serviceStartLatitude=${locationData.latitude.toString()}`
+      
+      // 调用开始服务接口,通过URL查询参数传递位置信息
+      const res = await getTimesByDate(params)
+      
       if (res.code === 200) {
         uni.showToast({
           title: '已开始服务',
@@ -159,26 +256,65 @@ const change = (e) => {
           },
         })
       }
-    })
+    } catch (error) {
+      console.error('开始服务失败', error)
+      uni.showToast({
+        title: '开始服务失败',
+        icon: 'none'
+      })
+    } finally {
+      uni.hideLoading()
+    }
   }
+  
   if (e.state && orderStatus.value) {
-    uni.showToast({
-      title: '操作成功',
-      icon: 'success',
-      success: () => {
-        setTimeout(() => {
-          uni.redirectTo({
-            url: `/pages_classify/pages/order/index?secondOrderId=${secondOrderId.value}`,
-          })
-        }, 800)
-      },
-    })
+    try {
+      uni.showLoading({
+        title: '获取位置信息...'
+      })
+      
+      // 获取结束服务时的位置
+      const locationData = await getCurrentLocation()
+      
+      // 构建参数字符串
+      const params = `secondOrderId=${secondOrderId.value}&serviceFinishLongitude=${locationData.longitude.toString()}&serviceFinishLatitude=${locationData.latitude.toString()}`
+      
+      // 调用结束服务接口,通过URL查询参数传递位置信息
+      await getVolunteerFinishSecondOrder(params)
+      
+      uni.showToast({
+        title: '操作成功',
+        icon: 'success',
+        success: () => {
+          setTimeout(() => {
+            uni.redirectTo({
+              url: `/pages_classify/pages/order/index?secondOrderId=${secondOrderId.value}`,
+            })
+          }, 800)
+        },
+      })
+    } catch (error) {
+      console.error('结束服务失败', error)
+      uni.showToast({
+        title: '结束服务失败',
+        icon: 'none'
+      })
+    } finally {
+      uni.hideLoading()
+    }
   }
 }
 
 onLoad((options) => {
   secondOrderId.value = options.secondOrderId
   getOrderDetail()
+  
+  // 可选地获取当前位置
+  getCurrentLocation().then(location => {
+    console.log('页面加载时的位置:', location);
+  }).catch(err => {
+    console.log('位置获取失败,将在需要时使用默认位置');
+  });
 })
 onUnload(() => {
   clearInterval(timer)