高德地图api
This commit is contained in:
parent
e3861c7577
commit
ed1f939121
29
src/Amap.ts
29
src/Amap.ts
|
|
@ -1,14 +1,12 @@
|
|||
import { Path, Point } from 'oak-domain/src/types/Geo';
|
||||
|
||||
export class AmapInstance {
|
||||
key: string;
|
||||
constructor(key: string) {
|
||||
this.key = key;
|
||||
}
|
||||
async getDrivingPath(data: Path) {
|
||||
const from = data[0];
|
||||
const to = data[1]
|
||||
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=${KEY}`;
|
||||
async getDrivingPath(data: { from: [number, number], to: [number, number] }) {
|
||||
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();
|
||||
if (jsonData.status !== '1') {
|
||||
|
|
@ -17,9 +15,9 @@ export class AmapInstance {
|
|||
return Promise.resolve(jsonData);
|
||||
}
|
||||
|
||||
async regeo(location: Point) {
|
||||
const locationStr = location.join();
|
||||
const result = await global.fetch(`https://restapi.amap.com/v3/geocode/regeo?location=${locationStr}&key=${this.key}`);
|
||||
async regeo(data: { longitude: number, latitude: number }) {
|
||||
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();
|
||||
if (jsonData.status !== '1') {
|
||||
throw new Error(JSON.stringify(jsonData));
|
||||
|
|
@ -27,7 +25,8 @@ export class AmapInstance {
|
|||
return Promise.resolve(jsonData);
|
||||
}
|
||||
|
||||
async ipLoc(ip: string) {
|
||||
async ipLoc(data: { ip: string }) {
|
||||
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();
|
||||
|
|
@ -37,9 +36,9 @@ export class AmapInstance {
|
|||
return Promise.resolve(jsonData);
|
||||
}
|
||||
|
||||
async getDistrict(keywords:string, subdistrict:string) {
|
||||
const url = `https://restapi.amap.com/v3/config/district?keywords=${keywords}&subdistrict=${subdistrict}&key=${KEY}`;
|
||||
|
||||
async getDistrict(data: { keywords:string, subdistrict:string }) {
|
||||
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();
|
||||
if (jsonData.status !== '1') {
|
||||
|
|
@ -48,9 +47,9 @@ export class AmapInstance {
|
|||
return Promise.resolve(jsonData);
|
||||
}
|
||||
|
||||
async geocode(address:string) {
|
||||
const url = `https://restapi.amap.com/v3/geocode/geo?address=${address}&key=${KEY}`;
|
||||
|
||||
async geocode(data: { address: string }) {
|
||||
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();
|
||||
if (jsonData.status !== '1') {
|
||||
|
|
|
|||
|
|
@ -0,0 +1,23 @@
|
|||
import { AmapInstance } from './Amap';
|
||||
|
||||
class AmapSDK {
|
||||
webKeyMap: Record<string, AmapInstance>;
|
||||
|
||||
constructor() {
|
||||
this.webKeyMap = {};
|
||||
}
|
||||
|
||||
getInstance(key: string) {
|
||||
if (this.webKeyMap[key]) {
|
||||
return this.webKeyMap[key];
|
||||
}
|
||||
const instance = new AmapInstance(key);
|
||||
Object.assign(this.webKeyMap, {
|
||||
[key]: instance,
|
||||
});
|
||||
return instance;
|
||||
}
|
||||
}
|
||||
|
||||
const SDK = new AmapSDK();
|
||||
export default SDK;
|
||||
|
|
@ -1,10 +0,0 @@
|
|||
export declare class WechatMpInstance {
|
||||
appId: string;
|
||||
appSecret: string;
|
||||
constructor(appId: string, appSecret: string);
|
||||
code2Session(code: string): Promise<{
|
||||
sessionKey: string;
|
||||
openId: string;
|
||||
unionId: string;
|
||||
}>;
|
||||
}
|
||||
|
|
@ -0,0 +1,33 @@
|
|||
import { WechatMpInstance } from './WechatMp';
|
||||
|
||||
class WechatSDK {
|
||||
mpMap: Record<string, WechatMpInstance>;
|
||||
publicMap: Record<string, any>;
|
||||
|
||||
constructor() {
|
||||
this.mpMap = {};
|
||||
this.publicMap = {};
|
||||
}
|
||||
|
||||
getInstance(
|
||||
appId: string,
|
||||
appSecret: string,
|
||||
type: 'wechatMp' | 'wechatPublic'
|
||||
) {
|
||||
if (type === 'wechatMp') {
|
||||
if (this.mpMap[appId]) {
|
||||
return this.mpMap[appId];
|
||||
}
|
||||
const instance = new WechatMpInstance(appId, appSecret);
|
||||
Object.assign(this.mpMap, {
|
||||
[appId]: instance,
|
||||
});
|
||||
return instance;
|
||||
} else {
|
||||
throw new Error('public not implemented');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const SDK = new WechatSDK();
|
||||
export default SDK;
|
||||
|
|
@ -1,9 +0,0 @@
|
|||
import { WechatMpInstance } from "./WechatMp";
|
||||
declare class WechatInstanceContainer {
|
||||
mpMap: Record<string, WechatMpInstance>;
|
||||
publicMap: Record<string, any>;
|
||||
constructor();
|
||||
getInstance(appId: string, appSecret: string, type: 'wechatMp' | 'wechatPublic'): WechatMpInstance;
|
||||
}
|
||||
declare const Container: WechatInstanceContainer;
|
||||
export default Container;
|
||||
33
src/index.ts
33
src/index.ts
|
|
@ -1,30 +1,5 @@
|
|||
import { WechatMpInstance } from "./WechatMp";
|
||||
import WechatSDK from './WechatSDK';
|
||||
import AmapSDK from './AmapSDK';
|
||||
|
||||
class WechatInstanceContainer {
|
||||
mpMap: Record<string, WechatMpInstance>;
|
||||
publicMap: Record<string, any>;
|
||||
|
||||
constructor() {
|
||||
this.mpMap = {};
|
||||
this.publicMap = {};
|
||||
}
|
||||
|
||||
getInstance(appId: string, appSecret: string, type: 'wechatMp' | 'wechatPublic') {
|
||||
if (type === 'wechatMp') {
|
||||
if (this.mpMap[appId]) {
|
||||
return this.mpMap[appId];
|
||||
}
|
||||
const instance = new WechatMpInstance(appId, appSecret);
|
||||
Object.assign(this.mpMap, {
|
||||
[appId]: instance,
|
||||
});
|
||||
return instance;
|
||||
}
|
||||
else {
|
||||
throw new Error('public not implemented');
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const Container = new WechatInstanceContainer();
|
||||
export default Container;
|
||||
export * from './Amap';
|
||||
export { WechatSDK, AmapSDK };
|
||||
Loading…
Reference in New Issue