nodejs 搞定 阿里云短信API

时间:2021-10-04 03:46:47

exports.sendSms=function(phone,content,ret){

var d={
RecNum:phone,
Action:‘SingleSendSms‘,
SignName:‘????‘,
TemplateCode:‘?????‘,
ParamString:‘{"code":"‘+content+‘"}‘,
Format:‘json‘,
Version:‘2016-09-27‘,
SignatureMethod:‘HMAC-SHA1‘,
SignatureNonce:Math.random()+‘‘,
SignatureVersion:‘1.0‘,
AccessKeyId:‘????‘,
Timestamp:JSON.stringify(new Date()).replace(/\"/g,‘‘),
}

var q=buildQueryString(d);

var key=‘?????&‘;
var s=‘GET&%2F&‘+encodeURIComponent(q);

var sign=hmac(s,key);
var href=http://www.mamicode.com/‘Signature=‘+encodeURIComponent(sign)+‘&‘+encodeURI(buildQueryString1(d));

client = restify.createStringClient({
url: ‘https://sms.aliyuncs.com/?‘+href,

});

client.get(‘‘, function(err, req, res, data) {

ret(data);
});

}

function buildQueryString(d){
var i=0;
var ns=[];
for(var k in d){
ns[i]=k;
i++;
}
ns.sort();
var str=‘‘
for(var i in ns){
if(str) str+=‘&‘;
str+=encodeURIComponent(ns[i])+‘=‘+encodeURIComponent(d[ns[i]]);
}

return str;
}


function buildQueryString1(d){
var i=0;
var ns=[];
for(var k in d){
ns[i]=k;
i++;
}
ns.sort();
var str=‘‘
for(var i in ns){
if(str) str+=‘&‘;
str+=(ns[i])+‘=‘+(d[ns[i]]);
}

return str;
}

有“?”的地方自己替换相应的内容。