短信验证码/通知 - 代码示例 * * * * * [[PHP](smscode/PHP.md)][[JAVA](smscode/JAVA.md)][[ASP](smscode/ASP.md) [[ASP.NET(C#)](smscode/ASPNET.md)][[JSP](smscode/JSP.md)][[C](smscode/C.md)][[C++](smscode/C++.md)][[GO](smscode/GO.md)][[NODE.JS](smscode/NODEJS.md)][[PYTHON](smscode/PYTHON.md)][[RUBY](smscode/RUBY.md)][[VB6](smscode/VB6.md)] NODEJS代码示例 ~~~ /** *接口类型:企讯云短信接口,支持发送验证码短信、订单通知短信等。 *接口地址:http://service.qixunpush.com/sendSMS *账户注册:请联系客服开通账户 *注意事项: *(1)调试期间,请使用用系统默认的短信内容:您的验证码是:【变量】。请不要把验证码泄露给其他人。 *(2)请使用 account 及 password 来调用接口,可联系客服获取; *(3)该代码仅供接入企讯云短信接口参考使用,客户可根据实际需要自行编写; */ String.prototype.replaceAll = function(reallyDo, replaceWith, ignoreCase) { if (!RegExp.prototype.isPrototypeOf(reallyDo)) { return this.replace(new RegExp(reallyDo, (ignoreCase ? "gi": "g")), replaceWith); } else { return this.replace(reallyDo, replaceWith); } }; var dom = require('xmldom').DOMParser; var _baseUri = "http://service.qixunpush.com/sendSMS"; var _userAgent = "node-qixun-module by allen"; /** * qixun constructure. * 用户名 和 密码 请联系客服获取 * @param account * @param password */ var qixun = function(account, password) { this.spidex = require("spidex"); this.spidex.setDefaultUserAgent(_userAgent); this.account = account; this.password = password; this.mobile = "139xxxxxxxx"; this.content = "您的验证码是:123456。请不要把验证码泄露给其他人。"; }; /** * send an SMS. * @param mobile * @param content * @param callback */ qixun.prototype.send = function(mobile, content, callback) { var data = { account : this.account, password : this.password, mobile : this.mobile, content : this.content }; this.spidex.post(_baseUri, function(html, status) { if(status !== 200) { callback(new Error("短信发送服务器响应失败。")); return; } html = html.replaceAll("\r", ""); html = html.replaceAll("\n", ""); html = html.replaceAll(" xmlns=\"http://service.qixunpush.com/sendSMS\"", ""); //console.log(html); var doc = new dom().parseFromString(html); var result = doc.lastChild; var json = {}; for(var node = result.firstChild; node !== null; node = node.nextSibling) { json[node.tagName] = node.firstChild.data; } //console.log(json); if(json.code == "2") { callback(null, json.smsid); } else { callback(new Error(json.msg, parseInt(json.code))); } }, data, "utf8").on("err", function(e) { callback(e); }); }; module.exports = qixun; ~~~