This commit is contained in:
Wang Kejun 2023-09-27 15:51:54 +08:00
parent 254bfeed76
commit 355ca0084f
2 changed files with 83 additions and 21 deletions

View File

@ -1,5 +1,5 @@
require('../../fetch');
import { OakExternalException, } from 'oak-domain/lib/types/Exception';
import { OakExternalException, OakNetworkException, } from 'oak-domain/lib/types/Exception';
export class AmapInstance {
key;
constructor(key) {
@ -8,8 +8,14 @@ export class AmapInstance {
async getDrivingPath(data) {
const { from, to } = data;
const url = `http://restapi.amap.com/v3/direction/driving?origin=${from[0].toFixed(6)},${from[1].toFixed(6)}&destination=${to[0].toFixed(6)},${to[1].toFixed(6)}&strategy=10&key=${this.key}`;
const result = await global.fetch(url);
const jsonData = await result.json();
let response;
try {
response = await global.fetch(url);
}
catch (err) {
throw new OakNetworkException(`访问amap接口失败${url}`);
}
const jsonData = await response.json();
if (jsonData.status !== '1') {
throw new OakExternalException('amap', jsonData.infocode, jsonData.info);
}
@ -17,8 +23,15 @@ export class AmapInstance {
}
async regeo(data) {
const { longitude, latitude } = data;
const result = await global.fetch(`https://restapi.amap.com/v3/geocode/regeo?location=${longitude},${latitude}&key=${this.key}`);
const jsonData = await result.json();
const url = `https://restapi.amap.com/v3/geocode/regeo?location=${longitude},${latitude}&key=${this.key}`;
let response;
try {
response = await global.fetch(url);
}
catch (err) {
throw new OakNetworkException(`访问amap接口失败${url}`);
}
const jsonData = await response.json();
if (jsonData.status !== '1') {
throw new OakExternalException('amap', jsonData.infocode, jsonData.info);
}
@ -27,8 +40,14 @@ export class AmapInstance {
async ipLoc(data) {
const { ip } = data;
const url = `https://restapi.amap.com/v3/ip?key=${this.key}&ip=${ip}`;
const result = await global.fetch(url);
const jsonData = await result.json();
let response;
try {
response = await global.fetch(url);
}
catch (err) {
throw new OakNetworkException(`访问amap接口失败${url}`);
}
const jsonData = await response.json();
if (jsonData.status !== '1') {
throw new OakExternalException('amap', jsonData.infocode, jsonData.info);
}
@ -37,8 +56,14 @@ export class AmapInstance {
async getDistrict(data) {
const { keywords, subdistrict } = data;
const url = `https://restapi.amap.com/v3/config/district?keywords=${keywords}&subdistrict=${subdistrict}&key=${this.key}`;
const result = await global.fetch(url);
const jsonData = await result.json();
let response;
try {
response = await global.fetch(url);
}
catch (err) {
throw new OakNetworkException(`访问amap接口失败${url}`);
}
const jsonData = await response.json();
if (jsonData.status !== '1') {
throw new OakExternalException('amap', jsonData.infocode, jsonData.info);
}
@ -47,8 +72,14 @@ export class AmapInstance {
async geocode(data) {
const { address } = data;
const url = `https://restapi.amap.com/v3/geocode/geo?address=${address}&key=${this.key}`;
const result = await global.fetch(url);
const jsonData = await result.json();
let response;
try {
response = await global.fetch(url);
}
catch (err) {
throw new OakNetworkException(`访问amap接口失败${url}`);
}
const jsonData = await response.json();
if (jsonData.status !== '1') {
throw new OakExternalException('amap', jsonData.infocode, jsonData.info);
}

View File

@ -11,8 +11,14 @@ class AmapInstance {
async getDrivingPath(data) {
const { from, to } = data;
const url = `http://restapi.amap.com/v3/direction/driving?origin=${from[0].toFixed(6)},${from[1].toFixed(6)}&destination=${to[0].toFixed(6)},${to[1].toFixed(6)}&strategy=10&key=${this.key}`;
const result = await global.fetch(url);
const jsonData = await result.json();
let response;
try {
response = await global.fetch(url);
}
catch (err) {
throw new Exception_1.OakNetworkException(`访问amap接口失败${url}`);
}
const jsonData = await response.json();
if (jsonData.status !== '1') {
throw new Exception_1.OakExternalException('amap', jsonData.infocode, jsonData.info);
}
@ -20,8 +26,15 @@ class AmapInstance {
}
async regeo(data) {
const { longitude, latitude } = data;
const result = await global.fetch(`https://restapi.amap.com/v3/geocode/regeo?location=${longitude},${latitude}&key=${this.key}`);
const jsonData = await result.json();
const url = `https://restapi.amap.com/v3/geocode/regeo?location=${longitude},${latitude}&key=${this.key}`;
let response;
try {
response = await global.fetch(url);
}
catch (err) {
throw new Exception_1.OakNetworkException(`访问amap接口失败${url}`);
}
const jsonData = await response.json();
if (jsonData.status !== '1') {
throw new Exception_1.OakExternalException('amap', jsonData.infocode, jsonData.info);
}
@ -30,8 +43,14 @@ class AmapInstance {
async ipLoc(data) {
const { ip } = data;
const url = `https://restapi.amap.com/v3/ip?key=${this.key}&ip=${ip}`;
const result = await global.fetch(url);
const jsonData = await result.json();
let response;
try {
response = await global.fetch(url);
}
catch (err) {
throw new Exception_1.OakNetworkException(`访问amap接口失败${url}`);
}
const jsonData = await response.json();
if (jsonData.status !== '1') {
throw new Exception_1.OakExternalException('amap', jsonData.infocode, jsonData.info);
}
@ -40,8 +59,14 @@ class AmapInstance {
async getDistrict(data) {
const { keywords, subdistrict } = data;
const url = `https://restapi.amap.com/v3/config/district?keywords=${keywords}&subdistrict=${subdistrict}&key=${this.key}`;
const result = await global.fetch(url);
const jsonData = await result.json();
let response;
try {
response = await global.fetch(url);
}
catch (err) {
throw new Exception_1.OakNetworkException(`访问amap接口失败${url}`);
}
const jsonData = await response.json();
if (jsonData.status !== '1') {
throw new Exception_1.OakExternalException('amap', jsonData.infocode, jsonData.info);
}
@ -50,8 +75,14 @@ class AmapInstance {
async geocode(data) {
const { address } = data;
const url = `https://restapi.amap.com/v3/geocode/geo?address=${address}&key=${this.key}`;
const result = await global.fetch(url);
const jsonData = await result.json();
let response;
try {
response = await global.fetch(url);
}
catch (err) {
throw new Exception_1.OakNetworkException(`访问amap接口失败${url}`);
}
const jsonData = await response.json();
if (jsonData.status !== '1') {
throw new Exception_1.OakExternalException('amap', jsonData.infocode, jsonData.info);
}