From b1a46b95768891fb90384f6d6ed447cfadf5bb78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A2=81=E6=9C=9D=E4=BC=9F?= <2211960668@qq.com> Date: Tue, 10 May 2022 15:32:22 +0800 Subject: [PATCH] fix --- src/Amap.ts | 68 +++++++++++++++++++++++++++++++++++++++++++++++ src/WechatMp.d.ts | 10 +++++++ src/index.d.ts | 9 +++++++ 3 files changed, 87 insertions(+) create mode 100644 src/WechatMp.d.ts create mode 100644 src/index.d.ts diff --git a/src/Amap.ts b/src/Amap.ts index e69de29..733fb7b 100644 --- a/src/Amap.ts +++ b/src/Amap.ts @@ -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); + } +} \ No newline at end of file diff --git a/src/WechatMp.d.ts b/src/WechatMp.d.ts new file mode 100644 index 0000000..06c89ec --- /dev/null +++ b/src/WechatMp.d.ts @@ -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; + }>; +} diff --git a/src/index.d.ts b/src/index.d.ts new file mode 100644 index 0000000..1d2a30e --- /dev/null +++ b/src/index.d.ts @@ -0,0 +1,9 @@ +import { WechatMpInstance } from "./WechatMp"; +declare class WechatInstanceContainer { + mpMap: Record; + publicMap: Record; + constructor(); + getInstance(appId: string, appSecret: string, type: 'wechatMp' | 'wechatPublic'): WechatMpInstance; +} +declare const Container: WechatInstanceContainer; +export default Container;