支持es编译

This commit is contained in:
Wang Kejun 2023-09-05 17:20:13 +08:00
parent a6f8cec2bb
commit 173c17b093
46 changed files with 1802 additions and 3 deletions

8
es/AmapSDK.d.ts vendored Normal file
View File

@ -0,0 +1,8 @@
import { AmapInstance } from './service/amap/Amap';
declare class AmapSDK {
webKeyMap: Record<string, AmapInstance>;
constructor();
getInstance(key: string): AmapInstance;
}
declare const SDK: AmapSDK;
export default SDK;

19
es/AmapSDK.js Normal file
View File

@ -0,0 +1,19 @@
import { AmapInstance } from './service/amap/Amap';
class AmapSDK {
webKeyMap;
constructor() {
this.webKeyMap = {};
}
getInstance(key) {
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;

9
es/QiniuSDK.d.ts vendored Normal file
View File

@ -0,0 +1,9 @@
import { QiniuCloudInstance } from './service/qiniu/QiniuCloud';
declare class QiniuSDK {
qiniuMap: Record<string, QiniuCloudInstance>;
constructor();
getInstance(accessKey: string, accessSecret: string): QiniuCloudInstance;
}
declare const SDK: QiniuSDK;
export default SDK;
export { QiniuCloudInstance };

20
es/QiniuSDK.js Normal file
View File

@ -0,0 +1,20 @@
import { QiniuCloudInstance } from './service/qiniu/QiniuCloud';
class QiniuSDK {
qiniuMap;
constructor() {
this.qiniuMap = {};
}
getInstance(accessKey, accessSecret) {
if (this.qiniuMap[accessKey]) {
return this.qiniuMap[accessKey];
}
const instance = new QiniuCloudInstance(accessKey, accessSecret);
Object.assign(this.qiniuMap, {
[accessKey]: instance,
});
return instance;
}
}
const SDK = new QiniuSDK();
export default SDK;
export { QiniuCloudInstance };

11
es/SmsSdk.d.ts vendored Normal file
View File

@ -0,0 +1,11 @@
import { TencentSmsInstance } from './service/tencent/Sms';
import { AliSmsInstance } from './service/ali/Sms';
declare class SmsSDK {
tencentMap: Record<string, TencentSmsInstance>;
aliMap: Record<string, AliSmsInstance>;
constructor();
getInstance(origin: 'ali' | 'tencent', accessKey: string, accessSecret: string, region: string, endpoint: string, apiVersion?: string): TencentSmsInstance | AliSmsInstance;
}
declare const SDK: SmsSDK;
export default SDK;
export { TencentSmsInstance, AliSmsInstance };

42
es/SmsSdk.js Normal file
View File

@ -0,0 +1,42 @@
import { TencentSmsInstance } from './service/tencent/Sms';
import { AliSmsInstance } from './service/ali/Sms';
class SmsSDK {
tencentMap;
aliMap;
constructor() {
this.tencentMap = {};
this.aliMap = {};
}
getInstance(origin, accessKey, accessSecret, region, endpoint, apiVersion //阿里云独有
) {
if (origin === 'tencent') {
if (this.tencentMap[accessKey]) {
return this.tencentMap[accessKey];
}
const instance = new TencentSmsInstance(accessKey, accessSecret, region, endpoint);
Object.assign(this.tencentMap, {
[accessKey]: instance,
});
return instance;
}
else if (origin === 'ali') {
if (!apiVersion) {
throw new Error('阿里云短信apiVersion必须传入');
}
if (this.aliMap[accessKey]) {
return this.aliMap[accessKey];
}
const instance = new AliSmsInstance(accessKey, accessSecret, region, endpoint, apiVersion);
Object.assign(this.aliMap, {
[accessKey]: instance,
});
return instance;
}
else {
throw new Error(`${origin} not implemented`);
}
}
}
const SDK = new SmsSDK();
export default SDK;
export { TencentSmsInstance, AliSmsInstance };

23
es/WechatSDK.d.ts vendored Normal file
View File

@ -0,0 +1,23 @@
import { WechatMpInstance } from './service/wechat/WechatMp';
import { WechatPublicInstance } from './service/wechat/WechatPublic';
import { WechatWebInstance } from './service/wechat/WechatWeb';
declare class WechatSDK {
mpMap: Record<string, WechatMpInstance>;
publicMap: Record<string, WechatPublicInstance>;
webMap: Record<string, WechatWebInstance>;
constructor();
getInstance(appId: string, type: 'wechatMp' | 'wechatPublic' | 'web', appSecret?: string, accessToken?: string, externalRefreshFn?: (appId: string) => Promise<string>): WechatMpInstance | WechatPublicInstance | WechatWebInstance;
/**
*
* @param url
* @returns html
*/
analyzePublicArticle(url: string): Promise<{
title: string;
publishDate: number | undefined;
imageList: string[];
}>;
}
declare const SDK: WechatSDK;
export default SDK;
export { WechatMpInstance, WechatWebInstance, WechatPublicInstance };

93
es/WechatSDK.js Normal file
View File

@ -0,0 +1,93 @@
import { WechatMpInstance } from './service/wechat/WechatMp';
import { WechatPublicInstance } from './service/wechat/WechatPublic';
import { WechatWebInstance } from './service/wechat/WechatWeb';
import { load } from 'cheerio';
class WechatSDK {
mpMap;
publicMap;
webMap;
constructor() {
this.mpMap = {};
this.publicMap = {};
this.webMap = {};
}
getInstance(appId, type, appSecret, accessToken, externalRefreshFn) {
// type 支持web网站扫码登录
if (type === 'wechatMp') {
if (this.mpMap[appId]) {
return this.mpMap[appId];
}
const instance = new WechatMpInstance(appId, appSecret, accessToken, externalRefreshFn);
Object.assign(this.mpMap, {
[appId]: instance,
});
return instance;
}
else if (type === 'wechatPublic') {
if (this.publicMap[appId]) {
return this.publicMap[appId];
}
const instance = new WechatPublicInstance(appId, appSecret, accessToken, externalRefreshFn);
Object.assign(this.publicMap, {
[appId]: instance,
});
return instance;
}
else if (type === 'web') {
if (this.webMap[appId]) {
return this.webMap[appId];
}
const instance = new WechatWebInstance(appId, appSecret, accessToken, externalRefreshFn);
Object.assign(this.webMap, {
[appId]: instance,
});
return instance;
}
else {
throw new Error(`${type} not implemented`);
}
}
/**
* 解析微信公众号文章内容
* @param url 微信公众号链接
* @returns html
*/
async analyzePublicArticle(url) {
const response = await fetch(url);
const html = await response.text();
const $ = load(html);
const title = $('#activity-name') ? $('#activity-name').text()?.trim().replace(/\n/g, '') : '';
const ems = $('em');
const imgsElement = $('img');
const imageList = [];
for (let i = 0; i < imgsElement.length; i++) {
// 把 img 元素中的 src 内容提取出来,加入到数组中
const src = imgsElement[i].attribs['data-src'];
if (src && (src.includes('http') || src.includes('https'))) {
imageList.push(src);
}
}
let publishDate;
// $('em').toArray().forEach((element, index) => {
// if (index === 0) {
// publishDate = $(element).text();
// }
// });
const lines = html.split('\n');
for (let i = 0; i < lines.length; i++) {
if (lines[i].includes('var ct =')) {
const timeStr = lines[i].split('"')[1] + '000';
publishDate = Number(timeStr);
break;
}
}
return {
title,
publishDate,
imageList,
};
}
}
const SDK = new WechatSDK();
export default SDK;
export { WechatMpInstance, WechatWebInstance, WechatPublicInstance };

0
es/fetch/index.d.ts vendored Normal file
View File

2
es/fetch/index.js Normal file
View File

@ -0,0 +1,2 @@
"use strict";
require('isomorphic-fetch');

0
es/fetch/index.mp.d.ts vendored Normal file
View File

2
es/fetch/index.mp.js Normal file
View File

@ -0,0 +1,2 @@
"use strict";
global.fetch = global.fetch;

0
es/fetch/index.web.d.ts vendored Normal file
View File

2
es/fetch/index.web.js Normal file
View File

@ -0,0 +1,2 @@
"use strict";
global.fetch = fetch;

7
es/index.d.ts vendored Normal file
View File

@ -0,0 +1,7 @@
import WechatSDK, { WechatMpInstance, WechatPublicInstance, WechatWebInstance } from './WechatSDK';
import AmapSDK from './AmapSDK';
import QiniuSDK, { QiniuCloudInstance } from './QiniuSDK';
import SmsSdk, { TencentSmsInstance, AliSmsInstance } from './SmsSdk';
export * from './service/amap/Amap';
export { AmapSDK, QiniuSDK, WechatSDK, WechatMpInstance, WechatPublicInstance, WechatWebInstance, QiniuCloudInstance, SmsSdk, TencentSmsInstance, AliSmsInstance, };
export * from './types';

7
es/index.js Normal file
View File

@ -0,0 +1,7 @@
import WechatSDK, { WechatMpInstance, WechatPublicInstance, WechatWebInstance } from './WechatSDK';
import AmapSDK from './AmapSDK';
import QiniuSDK, { QiniuCloudInstance } from './QiniuSDK';
import SmsSdk, { TencentSmsInstance, AliSmsInstance } from './SmsSdk';
export * from './service/amap/Amap';
export { AmapSDK, QiniuSDK, WechatSDK, WechatMpInstance, WechatPublicInstance, WechatWebInstance, QiniuCloudInstance, SmsSdk, TencentSmsInstance, AliSmsInstance, };
export * from './types';

0
es/service/ali/Ali.d.ts vendored Normal file
View File

1
es/service/ali/Ali.js Normal file
View File

@ -0,0 +1 @@
"use strict";

20
es/service/ali/Sms.d.ts vendored Normal file
View File

@ -0,0 +1,20 @@
import Core from '@alicloud/pop-core/lib/rpc';
declare type SendSmsRequest = {
PhoneNumbers: string[];
TemplateCode: string;
SignName: string;
TemplateParam?: Record<string, string>;
SmsUpExtendCode?: string;
OutId?: string;
};
export declare class AliSmsInstance {
accessKeyId: string;
accessKeySecret: string;
regionId: string;
endpoint: string;
apiVersion: string;
client: Core;
constructor(accessKeyId: string, accessKeySecret: string, regionId: string, endpoint: string, apiVersion: string);
sendSms(params: SendSmsRequest): Promise<void>;
}
export {};

47
es/service/ali/Sms.js Normal file
View File

@ -0,0 +1,47 @@
import Core from '@alicloud/pop-core/lib/rpc';
export class AliSmsInstance {
accessKeyId;
accessKeySecret;
regionId;
endpoint;
apiVersion;
client;
constructor(accessKeyId, accessKeySecret, regionId, endpoint, apiVersion) {
this.accessKeyId = accessKeyId;
this.accessKeySecret = accessKeySecret;
this.regionId = regionId;
this.endpoint = endpoint;
this.apiVersion = apiVersion;
this.client = new Core({
accessKeyId: this.accessKeyId,
accessKeySecret: this.accessKeySecret,
endpoint: this.endpoint || 'dysmsapi.aliyuncs.com',
apiVersion: this.apiVersion,
});
}
async sendSms(params) {
const { PhoneNumbers, TemplateParam = {}, TemplateCode, SignName, } = params;
const param = Object.assign({
regionId: this.regionId,
}, {
PhoneNumbers: PhoneNumbers.join(','),
TemplateParam: JSON.stringify(TemplateParam),
TemplateCode: TemplateCode,
SignName: SignName,
});
try {
// const data = await this.client.request<SendSmsResponse>(
// 'SendSms',
// param,
// {
// method: 'POST',
// }
// );
// return data;
}
catch (err) {
console.error(err);
throw err;
}
}
}

24
es/service/ali/Sms.mp.d.ts vendored Normal file
View File

@ -0,0 +1,24 @@
declare type SendSmsRequest = {
PhoneNumbers: string[];
TemplateCode: string;
SignName: string;
TemplateParam?: Record<string, string>;
SmsUpExtendCode?: string;
OutId?: string;
};
declare type SendSmsResponse = {
Code: 'OK' | string;
Message: string;
BizId: string;
RequestId: string;
};
export declare class AliSmsInstance {
secretId: string;
secretKey: string;
region: string;
endpoint: string;
client: any;
constructor(secretId: string, secretKey: string, region: string, endpoint: string);
sendSms(params: SendSmsRequest): Promise<SendSmsResponse>;
}
export {};

29
es/service/ali/Sms.mp.js Normal file
View File

@ -0,0 +1,29 @@
export class AliSmsInstance {
secretId;
secretKey;
region;
endpoint;
client;
constructor(secretId, secretKey, region, endpoint) {
this.secretId = secretId;
this.secretKey = secretKey;
this.region = region;
this.endpoint = endpoint;
const clientConfig = {
credential: {
secretId: this.secretId,
secretKey: this.secretKey,
},
region: this.region,
profile: {
httpProfile: {
endpoint: this.endpoint || 'dysmsapi.aliyuncs.com',
},
},
};
}
async sendSms(params) {
console.log('mp走不到这里');
return {};
}
}

24
es/service/ali/Sms.web.d.ts vendored Normal file
View File

@ -0,0 +1,24 @@
declare type SendSmsRequest = {
PhoneNumbers: string[];
TemplateCode: string;
SignName: string;
TemplateParam?: Record<string, string>;
SmsUpExtendCode?: string;
OutId?: string;
};
declare type SendSmsResponse = {
Code: 'OK' | string;
Message: string;
BizId: string;
RequestId: string;
};
export declare class AliSmsInstance {
secretId: string;
secretKey: string;
region: string;
endpoint: string;
client: any;
constructor(secretId: string, secretKey: string, region: string, endpoint: string);
sendSms(params: SendSmsRequest): Promise<SendSmsResponse>;
}
export {};

29
es/service/ali/Sms.web.js Normal file
View File

@ -0,0 +1,29 @@
export class AliSmsInstance {
secretId;
secretKey;
region;
endpoint;
client;
constructor(secretId, secretKey, region, endpoint) {
this.secretId = secretId;
this.secretKey = secretKey;
this.region = region;
this.endpoint = endpoint;
const clientConfig = {
credential: {
secretId: this.secretId,
secretKey: this.secretKey,
},
region: this.region,
profile: {
httpProfile: {
endpoint: this.endpoint || 'dysmsapi.aliyuncs.com',
},
},
};
}
async sendSms(params) {
console.log('web走不到这里');
return {};
}
}

22
es/service/amap/Amap.d.ts vendored Normal file
View File

@ -0,0 +1,22 @@
export declare class AmapInstance {
key: string;
constructor(key: string);
getDrivingPath(data: {
from: [number, number];
to: [number, number];
}): Promise<any>;
regeo(data: {
longitude: number;
latitude: number;
}): Promise<any>;
ipLoc(data: {
ip: string;
}): Promise<any>;
getDistrict(data: {
keywords: string;
subdistrict: string;
}): Promise<any>;
geocode(data: {
address: string;
}): Promise<any>;
}

56
es/service/amap/Amap.js Normal file
View File

@ -0,0 +1,56 @@
require('../../fetch');
export 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}`;
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(data) {
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));
}
return Promise.resolve(jsonData);
}
async ipLoc(data) {
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();
if (jsonData.status !== '1') {
throw new Error(JSON.stringify(jsonData));
}
return Promise.resolve(jsonData);
}
async getDistrict(data) {
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') {
throw new Error(JSON.stringify(jsonData));
}
return Promise.resolve(jsonData);
}
async geocode(data) {
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') {
throw new Error(JSON.stringify(jsonData));
}
return Promise.resolve(jsonData);
}
}

66
es/service/qiniu/QiniuCloud.d.ts vendored Normal file
View File

@ -0,0 +1,66 @@
export declare class QiniuCloudInstance {
private accessKey;
private secretKey;
constructor(accessKey: string, secretKey: string);
/**
*
* https://developer.qiniu.com/kodo/1312/upload
* @param uploadHost
* @param domain
* @param bucket
* @param key
* @returns
*/
getUploadInfo(uploadHost: string, domain: string, bucket: string, key?: string): {
key: string | undefined;
uploadToken: string;
uploadHost: string;
bucket: string;
domain: string;
};
/**
* token
* @param method
* @param path
* @param host
* @param rawQuery
* @param contentType
* @param bodyStr
* @returns
*/
getLiveToken(method: 'GET' | 'POST' | 'PUT' | 'DELETE', path: string, host: string, rawQuery?: string, contentType?: string, bodyStr?: string): string;
getLiveStream(hub: string, method: 'GET' | 'POST' | 'PUT' | 'DELETE', streamTitle: string, host: string, publishDomain: string, playDomain: string, publishKey: string, playKey: string, expireAt: number): Promise<{
streamTitle: string;
hub: string;
rtmpPushUrl: string;
rtmpPlayUrl: string;
pcPushUrl: string;
streamKey: string;
expireAt: number;
}>;
/**
*
* @param publishDomain
* @param playDomain
* @param hub
* @param publishKey
* @param playKey
* @param streamTitle
* @param expireAt
* @returns
*/
getStreamObj(publishDomain: string, playDomain: string, hub: string, publishKey: string, playKey: string, streamTitle: string, expireAt: number): {
streamTitle: string;
hub: string;
rtmpPushUrl: string;
rtmpPlayUrl: string;
pcPushUrl: string;
streamKey: string;
expireAt: number;
};
getPlayBackUrl(hub: string, playBackDomain: string, streamTitle: string, start: number, end: number, method: 'GET' | 'POST' | 'PUT' | 'DELETE', host: string, rawQuery?: string): Promise<string>;
private getToken;
private base64ToUrlSafe;
private hmacSha1;
private urlSafeBase64Encode;
}

View File

@ -0,0 +1,176 @@
require('../../fetch');
import crypto from 'crypto';
import { Md5 } from 'ts-md5';
import { Buffer } from 'buffer';
export class QiniuCloudInstance {
accessKey;
secretKey;
constructor(accessKey, secretKey) {
this.accessKey = accessKey;
this.secretKey = secretKey;
}
/**
* 计算客户端上传七牛需要的凭证
* https://developer.qiniu.com/kodo/1312/upload
* @param uploadHost
* @param domain
* @param bucket
* @param key
* @returns
*/
getUploadInfo(uploadHost, domain, bucket, key) {
try {
const scope = key ? `${bucket}:${key}` : bucket;
const uploadToken = this.getToken(scope);
return {
key,
uploadToken,
uploadHost,
bucket,
domain,
};
}
catch (err) {
throw err;
}
}
/**
* 计算直播需要的token
* @param method
* @param path
* @param host
* @param rawQuery
* @param contentType
* @param bodyStr
* @returns
*/
getLiveToken(method, path, host, rawQuery, contentType, bodyStr) {
// 1. 添加 Path
let data = `${method} ${path}`;
if (rawQuery) {
data += `?${rawQuery}`;
}
data += `\nHost: ${host}`;
if (contentType) {
data += `\nContent-Type: ${contentType}`;
}
data += '\n\n';
if (bodyStr &&
contentType &&
contentType !== 'application/octet-stream') {
data += bodyStr;
}
const sign = this.hmacSha1(data, this.secretKey);
const encodedSign = this.base64ToUrlSafe(sign);
const toke = 'Qiniu ' + this.accessKey + ':' + encodedSign;
return toke;
}
async getLiveStream(hub, method, streamTitle, host, publishDomain, playDomain, publishKey, playKey, expireAt) {
// 七牛创建直播流接口路径
const path = `/v2/hubs/${hub}/streams`;
// 如果用户没给streamTitle那么随机生成一个
let key = streamTitle;
if (!key) {
key = `class${new Date().getTime()}`;
}
const bodyStr = JSON.stringify({
key,
});
const contentType = 'application/json';
const token = this.getLiveToken(method, path, host);
const url = `https://pili.qiniuapi.com/v2/hubs/${hub}/streams`;
await global.fetch(url, {
method: 'POST',
headers: {
Authorization: token,
'Content-Type': contentType,
},
body: bodyStr,
mode: 'no-cors',
});
const obj = this.getStreamObj(publishDomain, playDomain, hub, publishKey, playKey, streamTitle, expireAt);
return obj;
}
/**
* 计算直播流地址相关信息
* @param publishDomain
* @param playDomain
* @param hub
* @param publishKey
* @param playKey
* @param streamTitle
* @param expireAt
* @returns
*/
getStreamObj(publishDomain, playDomain, hub, publishKey, playKey, streamTitle, expireAt) {
const signStr = `/${hub}/${streamTitle}?expire=${expireAt}`;
const sourcePath = `/${hub}/${streamTitle}`;
const token = this.base64ToUrlSafe(this.hmacSha1(signStr, publishKey));
const rtmpPushUrl = `rtmp://${publishDomain}${signStr}&token=${token}`;
// 生成播放地址
const t = expireAt.toString(16).toLowerCase();
const playSign = Md5.hashStr(playKey + sourcePath + t)
.toString()
.toLowerCase();
const rtmpPlayUrl = `https://${playDomain}${sourcePath}.m3u8?sign=${playSign}&t=${t}`;
// obs推流需要的地址和串流密钥
const pcPushUrl = `rtmp://${publishDomain}/${hub}/`;
const streamKey = `${streamTitle}?expire=${expireAt}&token=${token}`;
return {
streamTitle,
hub,
rtmpPushUrl,
rtmpPlayUrl,
pcPushUrl,
streamKey,
expireAt,
};
}
async getPlayBackUrl(hub, playBackDomain, streamTitle, start, end, method, host, rawQuery) {
const encodeStreamTitle = this.base64ToUrlSafe(streamTitle);
const path = `/v2/hubs/${hub}/streams/${encodeStreamTitle}/saveas`;
const bodyStr = JSON.stringify({
fname: streamTitle,
start,
end,
});
const contentType = 'application/json';
const token = this.getLiveToken(method, path, host, rawQuery, contentType, bodyStr);
const url = `https://pili.qiniuapi.com${path}`;
await global.fetch(url, {
method: 'POST',
headers: {
Authorization: token,
'Content-Type': contentType,
},
body: bodyStr,
mode: 'no-cors',
});
return `https://${playBackDomain}/${streamTitle}.m3u8`;
}
getToken(scope) {
// 构造策略
const putPolicy = {
scope: scope,
deadline: 3600 + Math.floor(Date.now() / 1000),
};
// 构造凭证
const encodedFlags = this.urlSafeBase64Encode(JSON.stringify(putPolicy));
const encoded = this.hmacSha1(encodedFlags, this.secretKey);
const encodedSign = this.base64ToUrlSafe(encoded);
const uploadToken = this.accessKey + ':' + encodedSign + ':' + encodedFlags;
return uploadToken;
}
base64ToUrlSafe(v) {
return v.replace(/\//g, '_').replace(/\+/g, '-');
}
hmacSha1(encodedFlags, secretKey) {
const hmac = crypto.createHmac('sha1', secretKey);
hmac.update(encodedFlags);
return hmac.digest('base64');
}
urlSafeBase64Encode(jsonFlags) {
const encoded = Buffer.from(jsonFlags).toString('base64');
return this.base64ToUrlSafe(encoded);
}
}

11
es/service/tencent/Sms.d.ts vendored Normal file
View File

@ -0,0 +1,11 @@
import { Client } from 'tencentcloud-sdk-nodejs/tencentcloud/services/sms/v20210111/sms_client';
import { SendSmsRequest, SendSmsResponse } from 'tencentcloud-sdk-nodejs/tencentcloud/services/sms/v20210111/sms_models';
export declare class TencentSmsInstance {
secretId: string;
secretKey: string;
region: string;
endpoint: string;
client: Client;
constructor(secretId: string, secretKey: string, region: string, endpoint: string);
sendSms(params: SendSmsRequest): Promise<SendSmsResponse>;
}

45
es/service/tencent/Sms.js Normal file
View File

@ -0,0 +1,45 @@
import { Client } from 'tencentcloud-sdk-nodejs/tencentcloud/services/sms/v20210111/sms_client';
const SmsClient = Client;
export class TencentSmsInstance {
secretId;
secretKey;
region;
endpoint;
client;
constructor(secretId, secretKey, region, endpoint) {
this.secretId = secretId;
this.secretKey = secretKey;
this.region = region;
this.endpoint = endpoint;
const clientConfig = {
credential: {
secretId: this.secretId,
secretKey: this.secretKey,
},
region: this.region,
profile: {
httpProfile: {
endpoint: this.endpoint || 'sms.tencentcloudapi.com',
},
},
};
// 实例化要请求产品的client对象,clientProfile是可选的
this.client = new SmsClient(clientConfig);
}
async sendSms(params) {
// const params: SendSmsRequest = {
// PhoneNumberSet: [],
// TemplateParamSet: [],
// SmsSdkAppId: '',
// TemplateId: '',
// };
try {
const data = await this.client.SendSms(params);
return data;
}
catch (err) {
console.error(err);
throw err;
}
}
}

10
es/service/tencent/Sms.mp.d.ts vendored Normal file
View File

@ -0,0 +1,10 @@
import { SendSmsRequest, SendSmsResponse } from 'tencentcloud-sdk-nodejs/tencentcloud/services/sms/v20210111/sms_models';
export declare class TencentSmsInstance {
secretId: string;
secretKey: string;
region: string;
endpoint: string;
client: any;
constructor(secretId: string, secretKey: string, region: string, endpoint: string);
sendSms(params: SendSmsRequest): Promise<SendSmsResponse>;
}

View File

@ -0,0 +1,29 @@
export class TencentSmsInstance {
secretId;
secretKey;
region;
endpoint;
client;
constructor(secretId, secretKey, region, endpoint) {
this.secretId = secretId;
this.secretKey = secretKey;
this.region = region;
this.endpoint = endpoint;
const clientConfig = {
credential: {
secretId: this.secretId,
secretKey: this.secretKey,
},
region: this.region,
profile: {
httpProfile: {
endpoint: this.endpoint || 'sms.tencentcloudapi.com',
},
},
};
}
async sendSms(params) {
console.log('mp走不到这里');
return {};
}
}

10
es/service/tencent/Sms.web.d.ts vendored Normal file
View File

@ -0,0 +1,10 @@
import { SendSmsRequest, SendSmsResponse } from 'tencentcloud-sdk-nodejs/tencentcloud/services/sms/v20210111/sms_models';
export declare class TencentSmsInstance {
secretId: string;
secretKey: string;
region: string;
endpoint: string;
client: any;
constructor(secretId: string, secretKey: string, region: string, endpoint: string);
sendSms(params: SendSmsRequest): Promise<SendSmsResponse>;
}

View File

@ -0,0 +1,29 @@
export class TencentSmsInstance {
secretId;
secretKey;
region;
endpoint;
client;
constructor(secretId, secretKey, region, endpoint) {
this.secretId = secretId;
this.secretKey = secretKey;
this.region = region;
this.endpoint = endpoint;
const clientConfig = {
credential: {
secretId: this.secretId,
secretKey: this.secretKey,
},
region: this.region,
profile: {
httpProfile: {
endpoint: this.endpoint || 'sms.tencentcloudapi.com',
},
},
};
}
async sendSms(params) {
console.log('web走不到这里');
return {};
}
}

53
es/service/wechat/WechatMp.d.ts vendored Normal file
View File

@ -0,0 +1,53 @@
export declare class WechatMpInstance {
appId: string;
appSecret?: string;
private accessToken?;
private refreshAccessTokenHandler?;
private externalRefreshFn?;
constructor(appId: string, appSecret?: string, accessToken?: string, externalRefreshFn?: (appId: string) => Promise<string>);
private getAccessToken;
private access;
code2Session(code: string): Promise<{
sessionKey: string;
openId: string;
unionId: string;
}>;
private refreshAccessToken;
decryptData(sessionKey: string, encryptedData: string, iv: string, signature: string): any;
getMpUnlimitWxaCode({ scene, page, envVersion, width, autoColor, lineColor, isHyaline, }: {
scene: string;
page: string;
envVersion?: 'release' | 'trial' | 'develop';
width?: number;
autoColor?: boolean;
lineColor?: {
r: number;
g: number;
b: number;
};
isHyaline?: true;
}): Promise<ArrayBuffer>;
getUserPhoneNumber(code: string): Promise<{
phoneNumber: string;
purePhoneNumber: string;
countryCode: number;
watermark: {
timestamp: number;
appid: string;
};
}>;
/**
*
* @param param0
* @returns
* https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/mp-message-management/subscribe-message/sendMessage.html
*/
sendSubscribedMessage({ templateId, page, openId, data, state, lang, }: {
templateId: string;
page?: string;
openId: string;
data: object;
state?: 'developer' | 'trial' | 'formal';
lang?: 'zh_CN' | 'zh_TW' | 'en_US' | 'zh_HK';
}): Promise<any>;
}

View File

@ -0,0 +1,162 @@
require('../../fetch');
import crypto from 'crypto';
import { Buffer } from 'buffer';
export class WechatMpInstance {
appId;
appSecret;
accessToken;
refreshAccessTokenHandler;
externalRefreshFn;
constructor(appId, appSecret, accessToken, externalRefreshFn) {
this.appId = appId;
this.appSecret = appSecret;
this.externalRefreshFn = externalRefreshFn;
if (!appSecret && !externalRefreshFn) {
throw new Error('appSecret和externalRefreshFn必须至少支持一个');
}
if (accessToken) {
this.accessToken = accessToken;
}
else {
this.refreshAccessToken();
}
}
async getAccessToken() {
while (true) {
if (this.accessToken) {
return this.accessToken;
}
await new Promise((resolve) => setTimeout(() => resolve(0), 500));
}
}
async access(url, init, fresh) {
const response = await global.fetch(url, init);
const { headers, status } = response;
if (![200, 201].includes(status)) {
throw new Error(`微信服务器返回不正确应答:${status}`);
}
const contentType = headers['Content-Type'] || headers.get('Content-Type');
if (contentType.includes('application/json')) {
const json = await response.json();
if (typeof json.errcode === 'number' && json.errcode !== 0) {
if ([42001, 40001].includes(json.errcode)) {
if (fresh) {
throw new Error('刚刷新的token不可能马上过期请检查是否有并发刷新token的逻辑');
}
console.log(JSON.stringify(json));
return this.refreshAccessToken(url, init);
}
throw new Error(`调用微信接口返回出错code是${json.errcode},信息是${json.errmsg}`);
}
return json;
}
if (contentType.includes('text') ||
contentType.includes('xml') ||
contentType.includes('html')) {
const data = await response.text();
return data;
}
if (contentType.includes('application/octet-stream')) {
return await response.arrayBuffer();
}
return response;
}
async code2Session(code) {
const result = await this.access(`https://api.weixin.qq.com/sns/jscode2session?appid=${this.appId}&secret=${this.appSecret}&js_code=${code}&grant_type=authorization_code`);
const { session_key, openid, unionid } = JSON.parse(result); // 这里微信返回的数据竟然是text/plain
return {
sessionKey: session_key,
openId: openid,
unionId: unionid,
};
}
async refreshAccessToken(url, init) {
const result = this.externalRefreshFn
? await this.externalRefreshFn(this.appId)
: await this.access(`https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=${this.appId}&secret=${this.appSecret}`);
const { access_token, expires_in } = result;
this.accessToken = access_token;
if (process.env.NODE_ENV === 'development') {
console.log(`小程序获得新的accessToken。appId:[${this.appId}], token: [${access_token}]`);
}
// 生成下次刷新的定时器
this.refreshAccessTokenHandler = setTimeout(() => {
this.refreshAccessToken();
}, (expires_in - 10) * 1000);
if (url) {
return this.access(url, init, true);
}
}
decryptData(sessionKey, encryptedData, iv, signature) {
const skBuf = Buffer.from(sessionKey, 'base64');
// const edBuf = Buffer.from(encryptedData, 'base64');
const ivBuf = Buffer.from(iv, 'base64');
const decipher = crypto.createDecipheriv('aes-128-cbc', skBuf, ivBuf);
// 设置自动 padding 为 true删除填充补位
decipher.setAutoPadding(true);
let decoded = decipher.update(encryptedData, 'base64', 'utf8');
decoded += decipher.final('utf8');
const data = JSON.parse(decoded);
if (data.watermark.appid !== this.appId) {
throw new Error('Illegal Buffer');
}
return data;
}
async getMpUnlimitWxaCode({ scene, page, envVersion = 'release', width, autoColor, lineColor, isHyaline, }) {
const token = await this.getAccessToken();
const result = await this.access(`https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=${token}`, {
method: 'POST',
headers: {
'Content-type': 'application/json',
Accept: 'image/jpg',
},
body: JSON.stringify({
// access_token: this.accessToken,
scene,
page,
env_version: envVersion,
width,
auto_color: autoColor,
line_color: lineColor,
is_hyaline: isHyaline,
}),
});
return (await result.arrayBuffer());
}
async getUserPhoneNumber(code) {
const token = await this.getAccessToken();
const result = (await this.access(`https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=${token}`, {
method: 'POST',
headers: {
'Content-type': 'application/json',
},
body: JSON.stringify({
code,
}),
}));
return result.phone_info;
}
/**
* 发送订阅消息
* @param param0
* @returns
* https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/mp-message-management/subscribe-message/sendMessage.html
*/
async sendSubscribedMessage({ templateId, page, openId, data, state, lang, }) {
const token = await this.getAccessToken();
/**
* 实测若用户未订阅会抛出errcode: 43101, errmsg: user refuse to accept the msg
*/
return this.access(`https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=${token}`, {
body: JSON.stringify({
template_id: templateId,
page,
touser: openId,
data,
miniprogram_state: state || 'formal',
lang: lang || 'zh_CN',
}),
method: 'post',
});
}
}

95
es/service/wechat/WechatPublic.d.ts vendored Normal file
View File

@ -0,0 +1,95 @@
declare type TextServeMessageOption = {
openId: string;
type: 'text';
content: string;
};
declare type NewsServeMessageOption = {
openId: string;
type: 'news';
title: string;
description?: string;
url: string;
picurl?: string;
};
declare type MpServeMessageOption = {
openId: string;
type: 'mp';
data: {
title: string;
appId: string;
pagepath: string;
thumbnailId: string;
};
};
declare type ServeMessageOption = TextServeMessageOption | NewsServeMessageOption | MpServeMessageOption;
export declare class WechatPublicInstance {
appId: string;
appSecret?: string;
private accessToken?;
private refreshAccessTokenHandler?;
private externalRefreshFn?;
constructor(appId: string, appSecret?: string, accessToken?: string, externalRefreshFn?: (appId: string) => Promise<string>);
private getAccessToken;
private access;
code2Session(code: string): Promise<{
accessToken: string;
openId: string;
unionId: string;
scope: string;
refreshToken: string;
isSnapshotUser: boolean;
atExpiredAt: number;
rtExpiredAt: number;
}>;
refreshUserAccessToken(refreshToken: string): Promise<{
accessToken: string;
refreshToken: string;
atExpiredAt: number;
scope: string;
}>;
getUserInfo(accessToken: string, openId: string): Promise<{
nickname: string;
gender: string | undefined;
avatar: string;
}>;
private refreshAccessToken;
decryptData(sessionKey: string, encryptedData: string, iv: string, signature: string): any;
getQrCode(options: {
sceneId?: number;
sceneStr?: string;
expireSeconds?: number;
isPermanent?: boolean;
}): Promise<{
ticket: any;
url: any;
expireSeconds: any;
}>;
sendTemplateMessage(options: {
openId: string;
templateId: string;
url?: string;
data: Object;
miniProgram?: {
appid: string;
pagepath: string;
};
clientMsgId?: string;
}): Promise<any>;
sendServeMessage(options: ServeMessageOption): Promise<any>;
batchGetArticle(options: {
offset?: number;
count: number;
noContent?: 0 | 1;
}): Promise<any>;
getTicket(): Promise<string>;
private randomString;
signatureJsSDK(options: {
url: string;
}): Promise<{
signature: string;
noncestr: string;
timestamp: number;
appId: string;
}>;
}
export {};

View File

@ -0,0 +1,399 @@
require('../../fetch');
import crypto from 'crypto';
import { Buffer } from 'buffer';
export class WechatPublicInstance {
appId;
appSecret;
accessToken;
refreshAccessTokenHandler;
externalRefreshFn;
constructor(appId, appSecret, accessToken, externalRefreshFn) {
this.appId = appId;
this.appSecret = appSecret;
this.externalRefreshFn = externalRefreshFn;
if (!appSecret && !externalRefreshFn) {
throw new Error('appSecret和externalRefreshFn必须至少支持一个');
}
if (accessToken) {
this.accessToken = accessToken;
}
else {
this.refreshAccessToken();
}
}
async getAccessToken() {
while (true) {
if (this.accessToken) {
return this.accessToken;
}
await new Promise((resolve) => setTimeout(() => resolve(0), 500));
}
}
async access(url, mockData, init) {
if (process.env.NODE_ENV === 'development') {
return mockData;
}
const response = await global.fetch(url, init);
const { headers, status } = response;
if (![200, 201].includes(status)) {
throw new Error(`微信服务器返回不正确应答:${status}`);
}
const contentType = headers['Content-Type'] || headers.get('Content-Type');
if (contentType.includes('application/json')) {
const json = await response.json();
if (typeof json.errcode === 'number' && json.errcode !== 0) {
if ([40001, 42001].includes(json.errcode)) {
return this.refreshAccessToken(url, init);
}
throw new Error(`调用微信接口返回出错code是${json.errcode},信息是${json.errmsg}`);
}
return json;
}
if (contentType.includes('text') ||
contentType.includes('xml') ||
contentType.includes('html')) {
const data = await response.text();
return data;
}
if (contentType.includes('application/octet-stream')) {
return await response.arrayBuffer();
}
return response;
}
async code2Session(code) {
const result = await this.access(`https://api.weixin.qq.com/sns/oauth2/access_token?appid=${this.appId}&secret=${this.appSecret}&code=${code}&grant_type=authorization_code`, {
access_token: 'aaa',
openid: code,
unionid: code,
refresh_token: 'aaa',
is_snapshotuser: false,
expires_in: 30,
scope: 'userinfo',
});
const { access_token, openid, unionid, scope, refresh_token, is_snapshotuser, expires_in, } = typeof result === 'string' ? JSON.parse(result) : result; // 这里微信返回的数据有时候竟然是text/plain
return {
accessToken: access_token,
openId: openid,
unionId: unionid,
scope: scope,
refreshToken: refresh_token,
isSnapshotUser: !!is_snapshotuser,
atExpiredAt: Date.now() + expires_in * 1000,
rtExpiredAt: Date.now() + 30 * 86400 * 1000,
};
}
async refreshUserAccessToken(refreshToken) {
const result = await this.access(`https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=${this.appId}&grant_type=refresh_token&refresh_token=${refreshToken}`, {
access_token: 'aaa',
refresh_token: 'aaa',
expires_in: 30,
scope: 'userinfo',
});
const { access_token, refresh_token, expires_in, scope } = result;
return {
accessToken: access_token,
refreshToken: refresh_token,
atExpiredAt: Date.now() + expires_in * 1000,
scope: scope,
};
}
async getUserInfo(accessToken, openId) {
const result = await this.access(`https://api.weixin.qq.com/sns/userinfo?access_token=${accessToken}&openid=${openId}&lang=zh_CN`, {
nickname: '码农哥',
sex: 1,
headimgurl: 'https://www.ertongzy.com/uploads/allimg/161005/2021233Y7-0.jpg',
});
const { nickname, sex, headimgurl } = result;
return {
nickname: nickname,
gender: sex === 1 ? 'male' : sex === 2 ? 'female' : undefined,
avatar: headimgurl,
};
}
async refreshAccessToken(url, init) {
const result = this.externalRefreshFn
? await this.externalRefreshFn(this.appId)
: await this.access(`https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=${this.appId}&secret=${this.appSecret}`, { access_token: 'mockToken', expires_in: 600 });
const { access_token, expires_in } = result;
this.accessToken = access_token;
// 生成下次刷新的定时器
console.log((expires_in - 10) * 1000);
this.refreshAccessTokenHandler = setTimeout(() => {
this.refreshAccessToken();
}, (expires_in - 10) * 1000);
if (url) {
return this.access(url, {}, init);
}
}
decryptData(sessionKey, encryptedData, iv, signature) {
const skBuf = Buffer.from(sessionKey, 'base64');
// const edBuf = Buffer.from(encryptedData, 'base64');
const ivBuf = Buffer.from(iv, 'base64');
const decipher = crypto.createDecipheriv('aes-128-cbc', skBuf, ivBuf);
// 设置自动 padding 为 true删除填充补位
decipher.setAutoPadding(true);
let decoded = decipher.update(encryptedData, 'base64', 'utf8');
decoded += decipher.final('utf8');
const data = JSON.parse(decoded);
if (data.watermark.appid !== this.appId) {
throw new Error('Illegal Buffer');
}
return data;
}
async getQrCode(options) {
const { sceneId, sceneStr, expireSeconds, isPermanent } = options;
if (!sceneId && !sceneStr) {
throw new Error('Missing sceneId or sceneStr');
}
const scene = sceneId
? {
scene_id: sceneId,
}
: {
scene_str: sceneStr,
};
let actionName = sceneId ? 'QR_SCENE' : 'QR_STR_SCENE';
let myInit = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
expire_seconds: expireSeconds,
action_name: actionName,
action_info: {
scene,
},
}),
};
if (isPermanent) {
actionName = sceneId ? 'QR_LIMIT_SCENE' : 'QR_LIMIT_STR_SCENE';
myInit = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
action_name: actionName,
action_info: {
scene,
},
}),
};
}
const token = await this.getAccessToken();
const result = await this.access(`https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=${token}`, {
ticket: `ticket${Date.now()}`,
url: `http://mock/q/${sceneId ? sceneId : sceneStr}`,
expireSeconds: expireSeconds,
}, myInit);
return {
ticket: result.ticket,
url: result.url,
expireSeconds: result.expire_seconds,
};
}
async sendTemplateMessage(options) {
const { openId, templateId, url, data, miniProgram, clientMsgId } = options;
const myInit = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
touser: openId,
template_id: templateId,
url,
miniProgram,
client_msg_id: clientMsgId,
data,
}),
};
const token = await this.getAccessToken();
const result = await this.access(`https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=${token}`, {
errcode: 0,
errmsg: 'ok',
msgid: Date.now(),
}, myInit);
const { errcode } = result;
if (errcode === 0) {
return Object.assign({ success: true }, result);
}
return Object.assign({ success: false }, result);
}
async sendServeMessage(options) {
const { openId, type } = options;
const myInit = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
};
switch (type) {
case 'text': {
Object.assign(myInit, {
body: JSON.stringify({
touser: openId,
msgtype: 'text',
text: {
content: options.content,
},
}),
});
break;
}
case 'news': {
Object.assign(myInit, {
body: JSON.stringify({
touser: openId,
msgtype: 'news',
news: {
articles: [
{
title: options.title,
description: options.description,
url: options.url,
picurl: options.picurl,
},
],
},
}),
});
break;
}
case 'mp': {
Object.assign(myInit, {
body: JSON.stringify({
touser: openId,
msgtype: 'miniprogrampage',
miniprogrampage: {
title: options.data.title,
appid: options.data.appId,
pagepath: options.data.pagepath,
thumb_media_id: options.data.thumbnailId,
},
}),
});
break;
}
default: {
throw new Error('当前消息类型暂不支持');
}
}
const token = await this.getAccessToken();
const result = await this.access(`https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=${token}`, {
errcode: 0,
errmsg: 'ok',
}, myInit);
const { errcode } = result;
if (errcode === 0) {
return Object.assign({ success: true }, result);
}
return Object.assign({ success: false }, result);
}
async batchGetArticle(options) {
const { offset, count, noContent } = options;
const myInit = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
offset,
count,
no_content: noContent,
}),
};
const token = await this.getAccessToken();
const result = await this.access(`https://api.weixin.qq.com/cgi-bin/freepublish/batchget?access_token=${token}`, {
total_count: 1,
item_count: 1,
item: [
{
article_id: 'test',
content: {
news_item: [
{
title: '测试文章',
author: '测试作者',
digest: '测试摘要',
content: '测试内容',
content_source_url: '',
thumb_media_id: 'TEST_MEDIA_ID',
show_cover_pic: 1,
need_open_comment: 0,
only_fans_can_comment: 0,
url: 'TEST_ARTICLE_URL',
is_deleted: false,
},
],
},
update_time: Date.now(),
},
],
}, myInit);
const { errcode } = result;
if (!errcode) {
return result;
}
throw new Error(JSON.stringify(result));
}
async getTicket() {
const myInit = {
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
};
const token = await this.getAccessToken();
const result = (await this.access(`https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=${token}&type=jsapi`, {
ticket: `ticket${Date.now()}`,
expires_in: 30,
}, myInit));
const { ticket } = result;
return ticket;
}
randomString() {
let len = 16;
let $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';
/** **默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1****/
let maxPos = $chars.length;
let pwd = '';
for (let i = 0; i < len; i++) {
pwd += $chars.charAt(Math.floor(Math.random() * maxPos));
}
return pwd;
}
async signatureJsSDK(options) {
const url = options.url;
const noncestr = this.randomString();
const timestamp = parseInt((Date.now() / 1000).toString(), 10);
const jsapi_ticket = await this.getTicket();
const contentArray = {
noncestr,
jsapi_ticket,
timestamp,
url,
};
let zhimaString = '';
Object.keys(contentArray)
.sort()
.forEach((ele, idx) => {
if (idx > 0) {
zhimaString += '&';
}
zhimaString += ele;
zhimaString += '=';
zhimaString += contentArray[ele];
});
return {
signature: crypto
.createHash('sha1')
.update(zhimaString)
.digest('hex'),
noncestr,
timestamp,
appId: this.appId,
};
}
}

17
es/service/wechat/WechatWeb.d.ts vendored Normal file
View File

@ -0,0 +1,17 @@
export declare class WechatWebInstance {
appId: string;
appSecret?: string;
accessToken?: string;
refreshAccessTokenHandler?: any;
private externalRefreshFn?;
constructor(appId: string, appSecret?: string, accessToken?: string, externalRefreshFn?: (appId: string) => Promise<string>);
private getAccessToken;
private access;
code2Session(code: string): Promise<{
sessionKey: string;
openId: string;
unionId: string;
}>;
private refreshAccessToken;
decryptData(sessionKey: string, encryptedData: string, iv: string, signature: string): any;
}

View File

@ -0,0 +1,99 @@
require('../../fetch');
import crypto from 'crypto';
import { Buffer } from 'buffer';
export class WechatWebInstance {
appId;
appSecret;
accessToken;
refreshAccessTokenHandler;
externalRefreshFn;
constructor(appId, appSecret, accessToken, externalRefreshFn) {
this.appId = appId;
this.appSecret = appSecret;
this.externalRefreshFn = externalRefreshFn;
if (!appSecret && !externalRefreshFn) {
throw new Error('appSecret和externalRefreshFn必须至少支持一个');
}
if (accessToken) {
this.accessToken = accessToken;
}
else {
this.refreshAccessToken();
}
}
async getAccessToken() {
while (true) {
if (this.accessToken) {
return this.accessToken;
}
await new Promise((resolve) => setTimeout(() => resolve(0), 500));
}
}
async access(url, mockData, init) {
if (process.env.NODE_ENV === 'development') {
return mockData;
}
const response = await global.fetch(url, init);
const { headers, status } = response;
if (![200, 201].includes(status)) {
throw new Error(`微信服务器返回不正确应答:${status}`);
}
const contentType = headers['Content-Type'] || headers.get('Content-Type');
if (contentType.includes('application/json')) {
const json = await response.json();
if (typeof json.errcode === 'number' && json.errcode !== 0) {
if ([40001, 42001].includes(json.errcode)) {
return this.refreshAccessToken();
}
throw new Error(`调用微信接口返回出错code是${json.errcode},信息是${json.errmsg}`);
}
return json;
}
if (contentType.includes('text') ||
contentType.includes('xml') ||
contentType.includes('html')) {
const data = await response.text();
return data;
}
if (contentType.includes('application/octet-stream')) {
return await response.arrayBuffer();
}
return response;
}
async code2Session(code) {
const result = await this.access(`https://api.weixin.qq.com/sns/oauth2/access_token?appid=${this.appId}&secret=${this.appSecret}&code=${code}&grant_type=authorization_code`, { session_key: 'aaa', openid: code, unionid: code });
const { session_key, openid, unionid } = typeof result === 'string' ? JSON.parse(result) : result; // 这里微信返回的数据有时候竟然是text/plain
return {
sessionKey: session_key,
openId: openid,
unionId: unionid,
};
}
async refreshAccessToken(url, init) {
const result = this.externalRefreshFn ? await this.externalRefreshFn(this.appId) : await this.access(`https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=${this.appId}&secret=${this.appSecret}`, { access_token: 'mockToken', expires_in: 600 });
const { access_token, expires_in } = result;
this.accessToken = access_token;
// 生成下次刷新的定时器
this.refreshAccessTokenHandler = setTimeout(() => {
this.refreshAccessToken();
}, (expires_in - 10) * 1000);
if (url) {
return this.access(url, init);
}
}
decryptData(sessionKey, encryptedData, iv, signature) {
const skBuf = Buffer.from(sessionKey, 'base64');
// const edBuf = Buffer.from(encryptedData, 'base64');
const ivBuf = Buffer.from(iv, 'base64');
const decipher = crypto.createDecipheriv('aes-128-cbc', skBuf, ivBuf);
// 设置自动 padding 为 true删除填充补位
decipher.setAutoPadding(true);
let decoded = decipher.update(encryptedData, 'base64', 'utf8');
decoded += decipher.final('utf8');
const data = JSON.parse(decoded);
if (data.watermark.appid !== this.appId) {
throw new Error('Illegal Buffer');
}
return data;
}
}

19
es/types/Wechat.d.ts vendored Normal file
View File

@ -0,0 +1,19 @@
/**
*
* https://developers.weixin.qq.com/doc/offiaccount/Message_Management/Receiving_standard_messages.html
* https://developers.weixin.qq.com/doc/offiaccount/Message_Management/Receiving_event_pushes.html
* https://developers.weixin.qq.com/doc/offiaccount/Message_Management/Template_Message_Interface.html#%E4%BA%8B%E4%BB%B6%E6%8E%A8%E9%80%81
*
* xmljson对象
*/
export declare type WechatPublicEventData = {
ToUserName: string;
FromUserName: string;
CreateTime: string;
MsgType: string;
Event: string;
Content: string;
EventKey: string;
MsgID: string;
Status: string;
};

1
es/types/Wechat.js Normal file
View File

@ -0,0 +1 @@
export {};

1
es/types/index.d.ts vendored Normal file
View File

@ -0,0 +1 @@
export * from './Wechat';

1
es/types/index.js Normal file
View File

@ -0,0 +1 @@
export * from './Wechat';

View File

@ -5,13 +5,18 @@
"author": {
"name": "XuChang"
},
"main": "lib/index.js",
"main": "lib/index",
"module": "es/index",
"types": "es/index.d.ts",
"typings": "es/index.d.ts",
"files": [
"lib/**/*"
"lib/**/*",
"es/**/*"
],
"scripts": {
"test": "ts-node test/test.ts",
"build": "tsc"
"build": "tsc -p tsconfig.json",
"build-es": "tsc -p tsconfig.es.json"
},
"license": "ISC",
"devDependencies": {

74
tsconfig.es.json Normal file
View File

@ -0,0 +1,74 @@
{
"compilerOptions": {
"jsx": "preserve",
/* Basic Options */
// "incremental": true, /* Enable incremental compilation */
"target": "ESNext", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */
"module": "ESNext", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
"lib": [
"dom",
"dom.iterable",
"esnext"
],
// "lib": [], /* Specify library files to be included in the compilation. */
// "allowJs": true, /* Allow javascript files to be compiled. */
// "checkJs": true, /* Report errors in .js files. */
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
"declaration": true, /* Generates corresponding '.d.ts' file. */
"importHelpers": true,
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
// "sourceMap": true, /* Generates corresponding '.map' file. */
// "outFile": "./", /* Concatenate and emit output to single file. */
"outDir": "es", /* Redirect output structure to the directory. */
"rootDir": "src", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
// "composite": true, /* Enable project compilation */
// "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */
// "removeComments": true, /* Do not emit comments to output. */
// "noEmit": true, /* Do not emit outputs. */
// "importHelpers": true, /* Import emit helpers from 'tslib'. */
"downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */
/* Strict Type-Checking Options */
"strict": true, /* Enable all strict type-checking options. */
// "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
// "strictNullChecks": true, /* Enable strict null checks. */
// "strictFunctionTypes": true, /* Enable strict checking of function types. */
// "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
/* Additional Checks */
// "noUnusedLocals": true, /* Report errors on unused locals. */
// "noUnusedParameters": true, /* Report errors on unused parameters. */
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
/* Module Resolution Options */
"moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
// "typeRoots": [], /* List of folders to include type definitions from. */
// "types": [], /* Type declaration files to be included in compilation. */
"allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
/* Source Map Options */
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
// "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */
/* Experimental Options */
// "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
/* Advanced Options */
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
},
"include": [
"src/**/*" ],
"exclude": [
"node_modules",
"**/*.spec.ts",
"test"
]
}