diff --git a/src/Amap.ts b/src/Amap.ts index 0a09b7e..8e624b8 100644 --- a/src/Amap.ts +++ b/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') { diff --git a/src/AmapSDK.ts b/src/AmapSDK.ts new file mode 100644 index 0000000..2c326d9 --- /dev/null +++ b/src/AmapSDK.ts @@ -0,0 +1,23 @@ +import { AmapInstance } from './Amap'; + +class AmapSDK { + webKeyMap: Record; + + 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; diff --git a/src/WechatMp.d.ts b/src/WechatMp.d.ts deleted file mode 100644 index 06c89ec..0000000 --- a/src/WechatMp.d.ts +++ /dev/null @@ -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; - }>; -} diff --git a/src/WechatSDK.ts b/src/WechatSDK.ts new file mode 100644 index 0000000..3845165 --- /dev/null +++ b/src/WechatSDK.ts @@ -0,0 +1,33 @@ +import { WechatMpInstance } from './WechatMp'; + +class WechatSDK { + mpMap: Record; + publicMap: Record; + + 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; diff --git a/src/index.d.ts b/src/index.d.ts deleted file mode 100644 index 1d2a30e..0000000 --- a/src/index.d.ts +++ /dev/null @@ -1,9 +0,0 @@ -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; diff --git a/src/index.ts b/src/index.ts index f62ad9b..4f922df 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,30 +1,5 @@ -import { WechatMpInstance } from "./WechatMp"; +import WechatSDK from './WechatSDK'; +import AmapSDK from './AmapSDK'; -class WechatInstanceContainer { - mpMap: Record; - publicMap: Record; - - 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; \ No newline at end of file +export * from './Amap'; +export { WechatSDK, AmapSDK }; \ No newline at end of file