This commit is contained in:
梁朝伟 2022-05-10 15:32:22 +08:00
parent 09d59302f0
commit b1a46b9576
3 changed files with 87 additions and 0 deletions

View File

@ -0,0 +1,68 @@
import { Path, Point } from '../../oak-domain/src/types/Geo';
export class AmapInstance {
key: string;
private static instance: AmapInstance;
constructor(key: string) {
this.key = key;
}
static getInstance(key: string): AmapInstance {
if (!AmapInstance.instance || (AmapInstance.instance && AmapInstance.instance.key !== key)) {
this.instance = new AmapInstance(key);
}
return AmapInstance.instance;
}
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}`;
const result = await global.fetch(url);
const jsonData = await result.json();
if (jsonData.status !== '1') {
throw new Error(JSON.stringify(jsonData));
}
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}`);
const jsonData = await result.json();
if (jsonData.status !== '1') {
throw new Error(JSON.stringify(jsonData));
}
return Promise.resolve(jsonData);
}
async ipLoc(ip: string) {
const url = `https://restapi.amap.com/v3/ip?key=${this.key}&ip=${ip}`;
const result = await global.fetch(url);
const jsonData = await result.json();
if (jsonData.status !== '1') {
throw new Error(JSON.stringify(jsonData));
}
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}`;
const result = await global.fetch(url);
const jsonData = await result.json();
if (jsonData.status !== '1') {
throw new Error(JSON.stringify(jsonData));
}
return Promise.resolve(jsonData);
}
async geocode(address:string) {
const url = `https://restapi.amap.com/v3/geocode/geo?address=${address}&key=${KEY}`;
const result = await global.fetch(url);
const jsonData = await result.json();
if (jsonData.status !== '1') {
throw new Error(JSON.stringify(jsonData));
}
return Promise.resolve(jsonData);
}
}

10
src/WechatMp.d.ts vendored Normal file
View File

@ -0,0 +1,10 @@
export declare class WechatMpInstance {
appId: string;
appSecret: string;
constructor(appId: string, appSecret: string);
code2Session(code: string): Promise<{
sessionKey: string;
openId: string;
unionId: string;
}>;
}

9
src/index.d.ts vendored Normal file
View File

@ -0,0 +1,9 @@
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;