From 3d9a675214409c251774d5989cd3252f74e467b2 Mon Sep 17 00:00:00 2001
From: Tevin <tingquanren@163.com>
Date: Wed, 21 Jul 2021 17:59:40 +0800
Subject: [PATCH] 将地理位置定位功能,转移到地址三级联动组件内部

---
 forms/chinaArea/CChinaArea.vue |   58 +++++++++++++++++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 55 insertions(+), 3 deletions(-)

diff --git a/forms/chinaArea/CChinaArea.vue b/forms/chinaArea/CChinaArea.vue
index 0b00296..087a6aa 100644
--- a/forms/chinaArea/CChinaArea.vue
+++ b/forms/chinaArea/CChinaArea.vue
@@ -31,11 +31,12 @@
 </template>
 
 <script>
+import Taro from '@tarojs/taro';
 import { AtInput, AtIcon } from 'taro-ui-vue';
 import ChinaLocations from './ChinaLocations';
 import './cChinaArea.scss';
 
-const { onReady, getLocationTree, getRegionNames } = ChinaLocations;
+const { onReady, getLocationTree, getRegionNames, getRegionCodes } = ChinaLocations;
 
 export default {
     name: 'CChinaArea',
@@ -46,8 +47,8 @@
     props: {
         // 表单数据资源(表单组件内部机制专用)
         itemRes: Object,
-        // 当地址获取经纬度时,设置加载中显示状态
-        loading: {
+        // 是否自动通过地理定位获取省市区
+        autoGeo: {
             type: Boolean,
             default: false,
         },
@@ -56,6 +57,7 @@
     },
     data() {
         return {
+            loading: false,
             range: [[], [], []],
             current: [0, 0, 0],
         };
@@ -153,6 +155,36 @@
             codes[2] = area.value;
             this.itemRes.onChange(codes);
         },
+        _getGeoLocation() {
+            this.loading = true;
+            Taro.getLocation({
+                type: 'wgs84',
+                success: res => {
+                    const latitude = res.latitude;
+                    const longitude = res.longitude;
+                    const myGeo = new BMap.Geocoder();
+                    // 根据坐标得到地址描述
+                    myGeo.getLocation(new BMap.Point(longitude, latitude), result => {
+                        if (!result) {
+                            this.locationLoading = false;
+                            return;
+                        }
+                        const address = result.addressComponents;
+                        const codes = getRegionCodes([
+                            address.province.replace(/省|市|自治区|特别行政区/g, ''),
+                            address.city,
+                            address.district,
+                        ]);
+                        if (codes && codes.length > 0) {
+                            this.itemRes.onChange(codes);
+                        }
+                        this.loading = false;
+                    });
+                },
+                cancel: res => (this.loading = false),
+                fail: err => (this.loading = false),
+            });
+        },
     },
     mounted() {
         if (process.env.TARO_ENV === 'weapp') {
@@ -160,6 +192,26 @@
                 this.handleOpen();
             });
         }
+        // 开启自动定位时,延迟 0.1 秒后发起定位
+        if (this.autoGeo) {
+            if (process.env.TARO_ENV === 'weapp') {
+                setTimeout(() => {
+                    // TODO:小程序中定位
+                }, 100);
+            } else if (process.env.TARO_ENV === 'h5') {
+                if (typeof wx === 'undefined') {
+                    console.warn('无法进行地理位置定位,wx 不存在!');
+                    return;
+                }
+                wx.ready(() => {
+                    if (typeof BMap === 'undefined') {
+                        console.warn('无法进行地理位置定位,BMap 不存在!');
+                        return;
+                    }
+                    this._getGeoLocation();
+                });
+            }
+        }
     },
 };
 </script>
\ No newline at end of file

--
Gitblit v1.9.1