封装腾讯云短信服务 阿里云短信后面再实现
This commit is contained in:
parent
ae0b0fab21
commit
6d1aa03c81
|
|
@ -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 };
|
||||
|
|
@ -0,0 +1,46 @@
|
|||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.AliSmsInstance = exports.TencentSmsInstance = void 0;
|
||||
var tslib_1 = require("tslib");
|
||||
var Sms_1 = require("./service/tencent/Sms");
|
||||
Object.defineProperty(exports, "TencentSmsInstance", { enumerable: true, get: function () { return Sms_1.TencentSmsInstance; } });
|
||||
var Sms_2 = require("./service/ali/Sms");
|
||||
Object.defineProperty(exports, "AliSmsInstance", { enumerable: true, get: function () { return Sms_2.AliSmsInstance; } });
|
||||
var assert_1 = tslib_1.__importDefault(require("assert"));
|
||||
var SmsSDK = /** @class */ (function () {
|
||||
function SmsSDK() {
|
||||
this.tencentMap = {};
|
||||
this.aliMap = {};
|
||||
}
|
||||
SmsSDK.prototype.getInstance = function (origin, accessKey, accessSecret, region, endpoint, apiVersion //阿里云独有
|
||||
) {
|
||||
var _a, _b;
|
||||
if (origin === 'tencent') {
|
||||
if (this.tencentMap[accessKey]) {
|
||||
return this.tencentMap[accessKey];
|
||||
}
|
||||
var instance = new Sms_1.TencentSmsInstance(accessKey, accessSecret, region, endpoint);
|
||||
Object.assign(this.tencentMap, (_a = {},
|
||||
_a[accessKey] = instance,
|
||||
_a));
|
||||
return instance;
|
||||
}
|
||||
else if (origin === 'ali') {
|
||||
(0, assert_1.default)(apiVersion, '阿里云短信apiVersion必须传入');
|
||||
if (this.aliMap[accessKey]) {
|
||||
return this.aliMap[accessKey];
|
||||
}
|
||||
var instance = new Sms_2.AliSmsInstance(accessKey, accessSecret, region, endpoint, apiVersion);
|
||||
Object.assign(this.aliMap, (_b = {},
|
||||
_b[accessKey] = instance,
|
||||
_b));
|
||||
return instance;
|
||||
}
|
||||
else {
|
||||
throw new Error("".concat(origin, " not implemented"));
|
||||
}
|
||||
};
|
||||
return SmsSDK;
|
||||
}());
|
||||
var SDK = new SmsSDK();
|
||||
exports.default = SDK;
|
||||
|
|
@ -47,7 +47,7 @@ var WechatSDK = /** @class */ (function () {
|
|||
return instance;
|
||||
}
|
||||
else {
|
||||
throw new Error('public not implemented');
|
||||
throw new Error("".concat(type, " not implemented"));
|
||||
}
|
||||
};
|
||||
return WechatSDK;
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
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 { WechatSDK, AmapSDK, QiniuSDK, WechatMpInstance, WechatPublicInstance, WechatWebInstance, QiniuCloudInstance, };
|
||||
export { AmapSDK, QiniuSDK, WechatSDK, WechatMpInstance, WechatPublicInstance, WechatWebInstance, QiniuCloudInstance, SmsSdk, TencentSmsInstance, AliSmsInstance, };
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.QiniuCloudInstance = exports.WechatWebInstance = exports.WechatPublicInstance = exports.WechatMpInstance = exports.QiniuSDK = exports.AmapSDK = exports.WechatSDK = void 0;
|
||||
exports.AliSmsInstance = exports.TencentSmsInstance = exports.SmsSdk = exports.QiniuCloudInstance = exports.WechatWebInstance = exports.WechatPublicInstance = exports.WechatMpInstance = exports.WechatSDK = exports.QiniuSDK = exports.AmapSDK = void 0;
|
||||
var tslib_1 = require("tslib");
|
||||
var WechatSDK_1 = tslib_1.__importStar(require("./WechatSDK"));
|
||||
exports.WechatSDK = WechatSDK_1.default;
|
||||
|
|
@ -12,4 +12,8 @@ exports.AmapSDK = AmapSDK_1.default;
|
|||
var QiniuSDK_1 = tslib_1.__importStar(require("./QiniuSDK"));
|
||||
exports.QiniuSDK = QiniuSDK_1.default;
|
||||
Object.defineProperty(exports, "QiniuCloudInstance", { enumerable: true, get: function () { return QiniuSDK_1.QiniuCloudInstance; } });
|
||||
var SmsSdk_1 = tslib_1.__importStar(require("./SmsSdk"));
|
||||
exports.SmsSdk = SmsSdk_1.default;
|
||||
Object.defineProperty(exports, "TencentSmsInstance", { enumerable: true, get: function () { return SmsSdk_1.TencentSmsInstance; } });
|
||||
Object.defineProperty(exports, "AliSmsInstance", { enumerable: true, get: function () { return SmsSdk_1.AliSmsInstance; } });
|
||||
tslib_1.__exportStar(require("./service/amap/Amap"), exports);
|
||||
|
|
|
|||
|
|
@ -0,0 +1,18 @@
|
|||
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;
|
||||
constructor(accessKeyId: string, accessKeySecret: string, regionId: string, endpoint: string, apiVersion: string);
|
||||
sendSms(params: SendSmsRequest): Promise<void>;
|
||||
}
|
||||
export {};
|
||||
|
|
@ -0,0 +1,55 @@
|
|||
"use strict";
|
||||
// import Core from '@alicloud/pop-core/lib/rpc';
|
||||
//todo 引入阿里云的库 在前端跑的时候会出现fs模块不存在
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.AliSmsInstance = void 0;
|
||||
var tslib_1 = require("tslib");
|
||||
var AliSmsInstance = /** @class */ (function () {
|
||||
// client: Core;
|
||||
function AliSmsInstance(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,
|
||||
// });
|
||||
}
|
||||
AliSmsInstance.prototype.sendSms = function (params) {
|
||||
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
||||
var PhoneNumbers, _a, TemplateParam, TemplateCode, SignName, param;
|
||||
return tslib_1.__generator(this, function (_b) {
|
||||
PhoneNumbers = params.PhoneNumbers, _a = params.TemplateParam, TemplateParam = _a === void 0 ? {} : _a, TemplateCode = params.TemplateCode, SignName = params.SignName;
|
||||
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;
|
||||
}
|
||||
return [2 /*return*/];
|
||||
});
|
||||
});
|
||||
};
|
||||
return AliSmsInstance;
|
||||
}());
|
||||
exports.AliSmsInstance = AliSmsInstance;
|
||||
|
|
@ -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>;
|
||||
}
|
||||
|
|
@ -0,0 +1,50 @@
|
|||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.TencentSmsInstance = void 0;
|
||||
var tslib_1 = require("tslib");
|
||||
var sms_client_1 = require("tencentcloud-sdk-nodejs/tencentcloud/services/sms/v20210111/sms_client");
|
||||
var SmsClient = sms_client_1.Client;
|
||||
var TencentSmsInstance = /** @class */ (function () {
|
||||
function TencentSmsInstance(secretId, secretKey, region, endpoint) {
|
||||
this.secretId = secretId;
|
||||
this.secretKey = secretKey;
|
||||
this.region = region;
|
||||
this.endpoint = endpoint;
|
||||
var 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);
|
||||
}
|
||||
TencentSmsInstance.prototype.sendSms = function (params) {
|
||||
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
||||
var data, err_1;
|
||||
return tslib_1.__generator(this, function (_a) {
|
||||
switch (_a.label) {
|
||||
case 0:
|
||||
_a.trys.push([0, 2, , 3]);
|
||||
return [4 /*yield*/, this.client.SendSms(params)];
|
||||
case 1:
|
||||
data = _a.sent();
|
||||
return [2 /*return*/, data];
|
||||
case 2:
|
||||
err_1 = _a.sent();
|
||||
console.error(err_1);
|
||||
throw err_1;
|
||||
case 3: return [2 /*return*/];
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
return TencentSmsInstance;
|
||||
}());
|
||||
exports.TencentSmsInstance = TencentSmsInstance;
|
||||
|
|
@ -21,7 +21,9 @@
|
|||
"typescript": "~4.7.4"
|
||||
},
|
||||
"dependencies": {
|
||||
"ts-md5": "^1.3.1",
|
||||
"isomorphic-fetch": "^3.0.0"
|
||||
"@alicloud/pop-core": "^1.7.12",
|
||||
"isomorphic-fetch": "^3.0.0",
|
||||
"tencentcloud-sdk-nodejs": "^4.0.525",
|
||||
"ts-md5": "^1.3.1"
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,61 @@
|
|||
import { TencentSmsInstance } from './service/tencent/Sms';
|
||||
import { AliSmsInstance } from './service/ali/Sms';
|
||||
import assert from 'assert';
|
||||
|
||||
class SmsSDK {
|
||||
tencentMap: Record<string, TencentSmsInstance>;
|
||||
aliMap: Record<string, AliSmsInstance>;
|
||||
|
||||
constructor() {
|
||||
this.tencentMap = {};
|
||||
this.aliMap = {};
|
||||
}
|
||||
|
||||
getInstance(
|
||||
origin: 'ali' | 'tencent',
|
||||
accessKey: string,
|
||||
accessSecret: string,
|
||||
region: string,
|
||||
endpoint: string,
|
||||
apiVersion?: string //阿里云独有
|
||||
) {
|
||||
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') {
|
||||
assert(apiVersion, '阿里云短信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 };
|
||||
|
|
@ -47,7 +47,7 @@ class WechatSDK {
|
|||
});
|
||||
return instance;
|
||||
} else {
|
||||
throw new Error('public not implemented');
|
||||
throw new Error(`${type} not implemented`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,14 +1,18 @@
|
|||
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 {
|
||||
WechatSDK,
|
||||
AmapSDK,
|
||||
QiniuSDK,
|
||||
WechatSDK,
|
||||
WechatMpInstance,
|
||||
WechatPublicInstance,
|
||||
WechatWebInstance,
|
||||
QiniuCloudInstance,
|
||||
SmsSdk,
|
||||
TencentSmsInstance,
|
||||
AliSmsInstance,
|
||||
};
|
||||
|
|
@ -0,0 +1,82 @@
|
|||
// import Core from '@alicloud/pop-core/lib/rpc';
|
||||
//todo 引入阿里云的库 在前端跑的时候会出现fs模块不存在
|
||||
|
||||
type SendSmsRequest = {
|
||||
PhoneNumbers: string[];
|
||||
TemplateCode: string;
|
||||
SignName: string;
|
||||
TemplateParam?: Record<string, string>;
|
||||
SmsUpExtendCode?: string;
|
||||
OutId?: string;
|
||||
};
|
||||
|
||||
type SendSmsResponse = {
|
||||
Code: 'OK' | string;
|
||||
Message: string;
|
||||
BizId: string;
|
||||
RequestId: string;
|
||||
};
|
||||
|
||||
export 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
|
||||
) {
|
||||
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: SendSmsRequest) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,56 @@
|
|||
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';
|
||||
|
||||
const SmsClient = Client;
|
||||
|
||||
export class TencentSmsInstance {
|
||||
secretId: string;
|
||||
secretKey: string;
|
||||
region: string;
|
||||
endpoint: string;
|
||||
client: Client;
|
||||
|
||||
constructor(
|
||||
secretId: string,
|
||||
secretKey: string,
|
||||
region: string,
|
||||
endpoint: string
|
||||
) {
|
||||
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: SendSmsRequest) {
|
||||
// const params: SendSmsRequest = {
|
||||
// PhoneNumberSet: [],
|
||||
// TemplateParamSet: [],
|
||||
// SmsSdkAppId: '',
|
||||
// TemplateId: '',
|
||||
// };
|
||||
try {
|
||||
const data: SendSmsResponse = await this.client.SendSms(params);
|
||||
return data;
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue