帮助中心 > 通信 > API文档 > 代码示例 > NodeJS

NodeJS

var https = require("https")
var CryptoJS = require('C:/Users/Administrator/AppData/Roaming/npm/node_modules/crypto-js');

function random(lower, upper) {
    return Math.floor(Math.random() * (upper - lower)) + lower;
}

function sortDict(dict){
    var dict2={},
        keys = Object.keys(dict).sort();
    for (var i = 0, n = keys.length, key; i < n; ++i) {
        key = keys[i];
        dict2[key] = dict[key];
    }
    return dict2;
}

var accessId = '*****';
var accessSecret = '*****';

var params = {
    'accessId': accessId,
    'templateCode': 100001,
    'timestamp': Math.round(Date.now()/1000),
    'nonce': random(10000000, 99999999),
    'phone':'13800000000',
    'templateVars': JSON.stringify({"code":"123123","min":5})
}

sorted_params = sortDict(params);

var params_str = "";
for(var key in sorted_params) {
    params_str += (key + '=' + sorted_params[key] + '&')
}
params_str = params_str.substring(0, params_str.length - 1)
params['signature'] = CryptoJS.HmacSHA1(params_str, accessSecret).toString(CryptoJS.enc.Base64);

let data = require('querystring').stringify(params); 
var options = {
    host:"api.yisu.com",
    port:443,
    strictSSL: false,
    rejectUnauthorized: false,
    path:"/sms/sendSms",
    method:"POST",
    headers:{
        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
        'Content-Length' : data.length,
    }
}

var req = https.request(options, function(res){
    var responseStr = '';
    res.on('data', function (chunk) {
        responseStr += chunk;
    });
    res.on('end', function() {
        console.log(responseStr);
    });
});
req.write(data);
req.end();
免费拨打  400 100 2938 免费拨打 400 100 2938
24小时售后技术支持 24小时售后技术支持
返回顶部 返回顶部