API文档

NodeJS

  1. var https = require("https")
  2. var CryptoJS = require('C:/Users/Administrator/AppData/Roaming/npm/node_modules/crypto-js');
  3. function random(lower, upper) {
  4. return Math.floor(Math.random() * (upper - lower)) + lower;
  5. }
  6. function sortDict(dict){
  7. var dict2={},
  8. keys = Object.keys(dict).sort();
  9. for (var i = 0, n = keys.length, key; i < n; ++i) {
  10. key = keys[i];
  11. dict2[key] = dict[key];
  12. }
  13. return dict2;
  14. }
  15. var accessId = '*****';
  16. var accessSecret = '*****';
  17. var params = {
  18. 'accessId': accessId,
  19. 'templateCode': 100001,
  20. 'timestamp': Math.round(Date.now()/1000),
  21. 'nonce': random(10000000, 99999999),
  22. 'phone':'13800000000',
  23. 'templateVars': JSON.stringify({"code":"123123","min":5})
  24. }
  25. sorted_params = sortDict(params);
  26. var params_str = "";
  27. for(var key in sorted_params) {
  28. params_str += (key + '=' + sorted_params[key] + '&')
  29. }
  30. params_str = params_str.substring(0, params_str.length - 1)
  31. params['signature'] = CryptoJS.HmacSHA1(params_str, accessSecret).toString(CryptoJS.enc.Base64);
  32. let data = require('querystring').stringify(params);
  33. var options = {
  34. host:"api.yisu.com",
  35. port:443,
  36. strictSSL: false,
  37. rejectUnauthorized: false,
  38. path:"/sms/sendSms",
  39. method:"POST",
  40. headers:{
  41. 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
  42. 'Content-Length' : data.length,
  43. }
  44. }
  45. var req = https.request(options, function(res){
  46. var responseStr = '';
  47. res.on('data', function (chunk) {
  48. responseStr += chunk;
  49. });
  50. res.on('end', function() {
  51. console.log(responseStr);
  52. });
  53. });
  54. req.write(data);
  55. req.end();