天翼云发短信测试通过

This commit is contained in:
wkj 2024-01-05 17:10:28 +08:00
parent 54ae64afc1
commit 7a65b250f7
7 changed files with 79 additions and 17 deletions

View File

@ -7,6 +7,10 @@ type SendSmsRequest = {
smsUpExtendCode?: string;
outId?: string;
};
type DescribeSmsTemplateListRequest = {
PageIndex: number;
PageSize: number;
};
export declare class AliSmsInstance {
accessKeyId: string;
accessKeySecret: string;
@ -14,6 +18,6 @@ export declare class AliSmsInstance {
client: Dysmsapi20170525;
constructor(accessKeyId: string, accessKeySecret: string, endpoint?: string);
sendSms(params: SendSmsRequest): Promise<$Dysmsapi20170525.SendSmsResponseBody>;
syncTemplate(params: $Dysmsapi20170525.QuerySmsTemplateListRequest): Promise<$Dysmsapi20170525.QuerySmsTemplateListResponseBody>;
syncTemplate(params: DescribeSmsTemplateListRequest): Promise<$Dysmsapi20170525.QuerySmsTemplateListResponseBody>;
}
export {};

View File

@ -1,5 +1,4 @@
import crypto from 'crypto';
import { Buffer } from 'buffer';
import { OakNetworkException, } from 'oak-domain/lib/types/Exception';
function format(date, layout) {
function pad(num, digit) {
@ -108,7 +107,10 @@ export class CTYunSmsInstance {
return response.json();
}
hmacsha256(data, key) {
const hmac = crypto.createHmac('sha1', key).update(data).digest('hex');
const hmac = crypto
.createHmac('sha256', key)
.update(data)
.digest('hex');
return hmac;
}
sha256(data) {

View File

@ -7,6 +7,10 @@ type SendSmsRequest = {
smsUpExtendCode?: string;
outId?: string;
};
type DescribeSmsTemplateListRequest = {
PageIndex: number;
PageSize: number;
};
export declare class AliSmsInstance {
accessKeyId: string;
accessKeySecret: string;
@ -14,6 +18,6 @@ export declare class AliSmsInstance {
client: Dysmsapi20170525;
constructor(accessKeyId: string, accessKeySecret: string, endpoint?: string);
sendSms(params: SendSmsRequest): Promise<$Dysmsapi20170525.SendSmsResponseBody>;
syncTemplate(params: $Dysmsapi20170525.QuerySmsTemplateListRequest): Promise<$Dysmsapi20170525.QuerySmsTemplateListResponseBody>;
syncTemplate(params: DescribeSmsTemplateListRequest): Promise<$Dysmsapi20170525.QuerySmsTemplateListResponseBody>;
}
export {};

View File

@ -3,7 +3,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
exports.CTYunSmsInstance = void 0;
const tslib_1 = require("tslib");
const crypto_1 = tslib_1.__importDefault(require("crypto"));
const buffer_1 = require("buffer");
const Exception_1 = require("oak-domain/lib/types/Exception");
function format(date, layout) {
function pad(num, digit) {
@ -85,10 +84,10 @@ class CTYunSmsInstance {
const rawString = `${headerStr}\n${calculateContentHash}`;
// SETUP5:构造签名
const signTime = this.hmacsha256(timestamp, this.securityKey);
const signAK = this.hmacsha256(this.accessKey, buffer_1.Buffer.from(signTime, 'hex'));
const signDate = this.hmacsha256(timestamp.slice(0, 8), buffer_1.Buffer.from(signAK, 'hex'));
const sign = this.hmacsha256(rawString, buffer_1.Buffer.from(signDate, 'hex'));
const signature = buffer_1.Buffer.from(sign, 'hex').toString('base64');
const signAK = this.hmacsha256(this.accessKey, Buffer.from(signTime, 'hex'));
const signDate = this.hmacsha256(timestamp.slice(0, 8), Buffer.from(signAK, 'hex'));
const sign = this.hmacsha256(rawString, Buffer.from(signDate, 'hex'));
const signature = Buffer.from(sign, 'hex').toString('base64');
// SETUP:6 构造请求头
const signatureHeader = `${this.accessKey} Headers=ctyun-eop-request-id;eop-date Signature=${signature}`;
const headers = {
@ -112,7 +111,10 @@ class CTYunSmsInstance {
return response.json();
}
hmacsha256(data, key) {
const hmac = crypto_1.default.createHmac('sha1', key).update(data).digest('hex');
const hmac = crypto_1.default
.createHmac('sha256', key)
.update(data)
.digest('hex');
return hmac;
}
sha256(data) {

View File

@ -19,6 +19,11 @@ type SendSmsResponse = {
RequestId: string;
};
type DescribeSmsTemplateListRequest = {
PageIndex: number;
PageSize: number;
};
export class AliSmsInstance {
accessKeyId: string;
@ -67,16 +72,14 @@ export class AliSmsInstance {
);
const { statusCode, body } = data;
if (statusCode != 200) {
throw new Error(
`sendSms接口返回状态码错误${statusCode}`
);
throw new Error(`sendSms接口返回状态码错误${statusCode}`);
}
return body;
} catch (error) {
throw error;
}
}
async syncTemplate(params: $Dysmsapi20170525.QuerySmsTemplateListRequest) {
async syncTemplate(params: DescribeSmsTemplateListRequest) {
const { PageIndex, PageSize } = params;
try {

View File

@ -1,12 +1,10 @@
import crypto from 'crypto';
import { Buffer } from 'buffer';
import * as querystring from 'querystring';
import {
OakExternalException,
OakNetworkException,
} from 'oak-domain/lib/types/Exception';
type SendSmsRequest = {
phoneNumber: string; // 接收短信的手机号码。格式国内短信无任何前缀的11位手机号码例如1381111****。多个手机号码使用英文","隔开最多支持一次提交200个手机号码。
templateCode: string;
@ -187,7 +185,10 @@ export class CTYunSmsInstance {
}
private hmacsha256(data: any, key: Buffer | string) {
const hmac = crypto.createHmac('sha1', key).update(data).digest('hex');
const hmac = crypto
.createHmac('sha256', key)
.update(data)
.digest('hex');
return hmac;
}

46
test/testSms.ts Normal file
View File

@ -0,0 +1,46 @@
//测试天翼云发短信
import { CTYunSmsInstance, SmsSdk } from '../src/index';
const accessKey = '';
const securityKey = '';
async function sendSms() {
const instance = SmsSdk.getInstance(
'ctyun',
accessKey,
securityKey,
'sms-global.ctapi.ctyun.cn'
) as CTYunSmsInstance;
const data = await instance.sendSms({
phoneNumber: '13372548015',
templateCode: 'SMS64124870510',
templateParam: { code: '1111' },
signName: '天翼云测试',
});
console.log(data);
}
async function syncTemplate() {
const instance = SmsSdk.getInstance(
'ctyun',
accessKey,
securityKey,
'sms-global.ctapi.ctyun.cn'
) as CTYunSmsInstance;
const data = await instance.syncTemplate({
pageIndex: 1,
pageSize: 50
});
console.log(data);
}
// sendSms()
// syncTemplate();