oak-external-sdk/lib/service/amap/Amap.js

98 lines
3.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AmapInstance = void 0;
require('../../utils/fetch');
const Exception_1 = require("oak-domain/lib/types/Exception");
class AmapInstance {
key;
constructor(key) {
this.key = key;
}
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}`;
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);
}
return jsonData;
}
async regeo(params) {
const { longitude, latitude } = params;
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);
}
return jsonData;
}
async ipLoc(data) {
const { ip } = data;
const url = `https://restapi.amap.com/v3/ip?key=${this.key}&ip=${ip}`;
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);
}
return jsonData;
}
async getDistrict(data) {
const { keywords, subdistrict } = data;
const url = `https://restapi.amap.com/v3/config/district?keywords=${keywords}&subdistrict=${subdistrict}&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);
}
return jsonData;
}
/**
* 高德这个接口搜索能力很弱,但高级接口要收费,只能先暂时用着
* @param data
* @returns
*/
async geocode(data) {
const { address, city } = data;
const url = `https://restapi.amap.com/v3/geocode/geo?address=${address}${city ? `&city=${city}` : ''}&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);
}
return jsonData;
}
}
exports.AmapInstance = AmapInstance;