| import Taro from '@tarojs/taro'; | 
| import axios from 'axios'; | 
|   | 
| type LocationResponse = { | 
|   result?: { | 
|     ad_info?: { | 
|       adcode?: number; | 
|       city?: string; | 
|       district?: string; | 
|       nation?: string; | 
|       nation_code?: number; | 
|       province?: string; | 
|     }; | 
|     ip?: string; | 
|     location?: { | 
|       lat?: number; | 
|       lng?: number; | 
|     }; | 
|   }; | 
| }; | 
|   | 
| export class LocationUtils { | 
|   static wxLocation: LocationResponse; | 
|   | 
|   static pendingPromise: Promise<LocationResponse>; | 
|   | 
|   static currentProvinceName: string; | 
|   | 
|   static blackList = []; | 
|   | 
|   static async getLocation() { | 
|     if (!this.wxLocation) { | 
|       if (!this.pendingPromise) { | 
|         this.pendingPromise = this.getLocationImp().finally(() => { | 
|           this.pendingPromise = undefined; | 
|         }); | 
|       } | 
|       let res = await this.pendingPromise; | 
|   | 
|       this.currentProvinceName = res?.result?.ad_info?.province ?? ''; | 
|   | 
|       this.wxLocation = res; | 
|     } | 
|     return this.wxLocation; | 
|   } | 
|   | 
|   static async getLocationImp() { | 
|     let geocoderRes = await this.getLocationByGeocoder().catch(() => this.getLocationByIp()); | 
|     console.log('geocoderRes: ', geocoderRes); | 
|   | 
|     return geocoderRes; | 
|   } | 
|   | 
|   static async getLocationByGeocoder() { | 
|     let setting = await Taro.getSetting(); | 
|     if (!setting.authSetting['scope.userLocation']) { | 
|       await Taro.authorize({ | 
|         scope: 'scope.userLocation', | 
|       }); | 
|     } | 
|     let res = await Taro.getLocation({ | 
|       type: 'gcj02', | 
|     }); | 
|     return await this.geocoder({ latitude: res.latitude, longitude: res.longitude }); | 
|   } | 
|   | 
|   static async getBaseUrlByLocation() { | 
|     try { | 
|       let res = await this.getLocation(); | 
|       return this.blackList.includes(this.currentProvinceName) | 
|         ? process.env.BASE_URL_JX | 
|         : process.env.BASE_URL; | 
|     } catch (error) { | 
|       console.log('getLocation error: ', error); | 
|       return process.env.BASE_URL; | 
|     } | 
|   } | 
|   | 
|   static geocoder({ latitude, longitude }: { latitude?: number; longitude?: number }) { | 
|     return axios | 
|       .get<LocationResponse>( | 
|         `https://apis.map.qq.com/ws/geocoder/v1?location=${latitude},${longitude}&key=HH7BZ-5L2KI-TN3GW-U5HKU-MK5H3-VOBZH` | 
|       ) | 
|       .then((res) => res.data); | 
|   } | 
|   | 
|   static getLocationByIp() { | 
|     return axios | 
|       .get<LocationResponse>( | 
|         `https://apis.map.qq.com/ws/location/v1/ip?key=HH7BZ-5L2KI-TN3GW-U5HKU-MK5H3-VOBZH`, | 
|         { | 
|           timeout: 3000, | 
|         } | 
|       ) | 
|       .then((res) => res.data); | 
|   } | 
|   | 
|   static isProvinceChange(provinceName: string) { | 
|     return ( | 
|       provinceName !== this.currentProvinceName && | 
|       (this.blackList.includes(provinceName) || this.blackList.includes(this.currentProvinceName)) | 
|     ); | 
|   } | 
| } |