天翼云发短信测试通过
This commit is contained in:
parent
54ae64afc1
commit
7a65b250f7
|
|
@ -7,6 +7,10 @@ type SendSmsRequest = {
|
||||||
smsUpExtendCode?: string;
|
smsUpExtendCode?: string;
|
||||||
outId?: string;
|
outId?: string;
|
||||||
};
|
};
|
||||||
|
type DescribeSmsTemplateListRequest = {
|
||||||
|
PageIndex: number;
|
||||||
|
PageSize: number;
|
||||||
|
};
|
||||||
export declare class AliSmsInstance {
|
export declare class AliSmsInstance {
|
||||||
accessKeyId: string;
|
accessKeyId: string;
|
||||||
accessKeySecret: string;
|
accessKeySecret: string;
|
||||||
|
|
@ -14,6 +18,6 @@ export declare class AliSmsInstance {
|
||||||
client: Dysmsapi20170525;
|
client: Dysmsapi20170525;
|
||||||
constructor(accessKeyId: string, accessKeySecret: string, endpoint?: string);
|
constructor(accessKeyId: string, accessKeySecret: string, endpoint?: string);
|
||||||
sendSms(params: SendSmsRequest): Promise<$Dysmsapi20170525.SendSmsResponseBody>;
|
sendSms(params: SendSmsRequest): Promise<$Dysmsapi20170525.SendSmsResponseBody>;
|
||||||
syncTemplate(params: $Dysmsapi20170525.QuerySmsTemplateListRequest): Promise<$Dysmsapi20170525.QuerySmsTemplateListResponseBody>;
|
syncTemplate(params: DescribeSmsTemplateListRequest): Promise<$Dysmsapi20170525.QuerySmsTemplateListResponseBody>;
|
||||||
}
|
}
|
||||||
export {};
|
export {};
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,4 @@
|
||||||
import crypto from 'crypto';
|
import crypto from 'crypto';
|
||||||
import { Buffer } from 'buffer';
|
|
||||||
import { OakNetworkException, } from 'oak-domain/lib/types/Exception';
|
import { OakNetworkException, } from 'oak-domain/lib/types/Exception';
|
||||||
function format(date, layout) {
|
function format(date, layout) {
|
||||||
function pad(num, digit) {
|
function pad(num, digit) {
|
||||||
|
|
@ -108,7 +107,10 @@ export class CTYunSmsInstance {
|
||||||
return response.json();
|
return response.json();
|
||||||
}
|
}
|
||||||
hmacsha256(data, key) {
|
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;
|
return hmac;
|
||||||
}
|
}
|
||||||
sha256(data) {
|
sha256(data) {
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,10 @@ type SendSmsRequest = {
|
||||||
smsUpExtendCode?: string;
|
smsUpExtendCode?: string;
|
||||||
outId?: string;
|
outId?: string;
|
||||||
};
|
};
|
||||||
|
type DescribeSmsTemplateListRequest = {
|
||||||
|
PageIndex: number;
|
||||||
|
PageSize: number;
|
||||||
|
};
|
||||||
export declare class AliSmsInstance {
|
export declare class AliSmsInstance {
|
||||||
accessKeyId: string;
|
accessKeyId: string;
|
||||||
accessKeySecret: string;
|
accessKeySecret: string;
|
||||||
|
|
@ -14,6 +18,6 @@ export declare class AliSmsInstance {
|
||||||
client: Dysmsapi20170525;
|
client: Dysmsapi20170525;
|
||||||
constructor(accessKeyId: string, accessKeySecret: string, endpoint?: string);
|
constructor(accessKeyId: string, accessKeySecret: string, endpoint?: string);
|
||||||
sendSms(params: SendSmsRequest): Promise<$Dysmsapi20170525.SendSmsResponseBody>;
|
sendSms(params: SendSmsRequest): Promise<$Dysmsapi20170525.SendSmsResponseBody>;
|
||||||
syncTemplate(params: $Dysmsapi20170525.QuerySmsTemplateListRequest): Promise<$Dysmsapi20170525.QuerySmsTemplateListResponseBody>;
|
syncTemplate(params: DescribeSmsTemplateListRequest): Promise<$Dysmsapi20170525.QuerySmsTemplateListResponseBody>;
|
||||||
}
|
}
|
||||||
export {};
|
export {};
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
||||||
exports.CTYunSmsInstance = void 0;
|
exports.CTYunSmsInstance = void 0;
|
||||||
const tslib_1 = require("tslib");
|
const tslib_1 = require("tslib");
|
||||||
const crypto_1 = tslib_1.__importDefault(require("crypto"));
|
const crypto_1 = tslib_1.__importDefault(require("crypto"));
|
||||||
const buffer_1 = require("buffer");
|
|
||||||
const Exception_1 = require("oak-domain/lib/types/Exception");
|
const Exception_1 = require("oak-domain/lib/types/Exception");
|
||||||
function format(date, layout) {
|
function format(date, layout) {
|
||||||
function pad(num, digit) {
|
function pad(num, digit) {
|
||||||
|
|
@ -85,10 +84,10 @@ class CTYunSmsInstance {
|
||||||
const rawString = `${headerStr}\n${calculateContentHash}`;
|
const rawString = `${headerStr}\n${calculateContentHash}`;
|
||||||
// SETUP5:构造签名
|
// SETUP5:构造签名
|
||||||
const signTime = this.hmacsha256(timestamp, this.securityKey);
|
const signTime = this.hmacsha256(timestamp, this.securityKey);
|
||||||
const signAK = this.hmacsha256(this.accessKey, buffer_1.Buffer.from(signTime, 'hex'));
|
const signAK = this.hmacsha256(this.accessKey, Buffer.from(signTime, 'hex'));
|
||||||
const signDate = this.hmacsha256(timestamp.slice(0, 8), buffer_1.Buffer.from(signAK, 'hex'));
|
const signDate = this.hmacsha256(timestamp.slice(0, 8), Buffer.from(signAK, 'hex'));
|
||||||
const sign = this.hmacsha256(rawString, buffer_1.Buffer.from(signDate, 'hex'));
|
const sign = this.hmacsha256(rawString, Buffer.from(signDate, 'hex'));
|
||||||
const signature = buffer_1.Buffer.from(sign, 'hex').toString('base64');
|
const signature = Buffer.from(sign, 'hex').toString('base64');
|
||||||
// SETUP:6 构造请求头
|
// SETUP:6 构造请求头
|
||||||
const signatureHeader = `${this.accessKey} Headers=ctyun-eop-request-id;eop-date Signature=${signature}`;
|
const signatureHeader = `${this.accessKey} Headers=ctyun-eop-request-id;eop-date Signature=${signature}`;
|
||||||
const headers = {
|
const headers = {
|
||||||
|
|
@ -112,7 +111,10 @@ class CTYunSmsInstance {
|
||||||
return response.json();
|
return response.json();
|
||||||
}
|
}
|
||||||
hmacsha256(data, key) {
|
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;
|
return hmac;
|
||||||
}
|
}
|
||||||
sha256(data) {
|
sha256(data) {
|
||||||
|
|
|
||||||
|
|
@ -19,6 +19,11 @@ type SendSmsResponse = {
|
||||||
RequestId: string;
|
RequestId: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
type DescribeSmsTemplateListRequest = {
|
||||||
|
PageIndex: number;
|
||||||
|
PageSize: number;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
export class AliSmsInstance {
|
export class AliSmsInstance {
|
||||||
accessKeyId: string;
|
accessKeyId: string;
|
||||||
|
|
@ -67,16 +72,14 @@ export class AliSmsInstance {
|
||||||
);
|
);
|
||||||
const { statusCode, body } = data;
|
const { statusCode, body } = data;
|
||||||
if (statusCode != 200) {
|
if (statusCode != 200) {
|
||||||
throw new Error(
|
throw new Error(`sendSms接口返回状态码错误,为${statusCode}`);
|
||||||
`sendSms接口返回状态码错误,为${statusCode}`
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
return body;
|
return body;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
async syncTemplate(params: $Dysmsapi20170525.QuerySmsTemplateListRequest) {
|
async syncTemplate(params: DescribeSmsTemplateListRequest) {
|
||||||
const { PageIndex, PageSize } = params;
|
const { PageIndex, PageSize } = params;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,10 @@
|
||||||
import crypto from 'crypto';
|
import crypto from 'crypto';
|
||||||
import { Buffer } from 'buffer';
|
|
||||||
import * as querystring from 'querystring';
|
import * as querystring from 'querystring';
|
||||||
import {
|
import {
|
||||||
OakExternalException,
|
OakExternalException,
|
||||||
OakNetworkException,
|
OakNetworkException,
|
||||||
} from 'oak-domain/lib/types/Exception';
|
} from 'oak-domain/lib/types/Exception';
|
||||||
|
|
||||||
|
|
||||||
type SendSmsRequest = {
|
type SendSmsRequest = {
|
||||||
phoneNumber: string; // 接收短信的手机号码。格式:国内短信:无任何前缀的11位手机号码,例如1381111****。多个手机号码使用英文","隔开,最多支持一次提交200个手机号码。
|
phoneNumber: string; // 接收短信的手机号码。格式:国内短信:无任何前缀的11位手机号码,例如1381111****。多个手机号码使用英文","隔开,最多支持一次提交200个手机号码。
|
||||||
templateCode: string;
|
templateCode: string;
|
||||||
|
|
@ -187,7 +185,10 @@ export class CTYunSmsInstance {
|
||||||
}
|
}
|
||||||
|
|
||||||
private hmacsha256(data: any, key: Buffer | string) {
|
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;
|
return hmac;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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();
|
||||||
|
|
||||||
|
|
||||||
Loading…
Reference in New Issue