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://sms.106jiekou.com/utf8/sms.aspx"; var _userAgent = "node-106jiekou"; /** * sms constructure. * @param account 用戶名 * @param password 接口密碼 */ var sms = function(account, password) { this.spidex = require("spidex"); this.spidex.setDefaultUserAgent(_userAgent); this.account = account; this.password = password; }; /** * send an SMS. * @param mobile * @param content * @param callback */ sms.prototype.send = function(mobile, content, callback) { var data = { account : this.account, password : this.password, mobile : mobile, content : content }; this.spidex.post(_baseUri, function(html, status) { if(status !== 200) { callback(new Error("短信發(fā)送服務(wù)器響應(yīng)失敗。")); return; } html = html.replaceAll("\r", ""); html = html.replaceAll("\n", ""); html = html.replaceAll(" xmlns=\"http://sms.106jiekou.com/\"", ""); //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 = sms;
友情鏈接:106短信平臺 | 發(fā)短信平臺 | 短信接口平臺 | 短信接口平臺哪個好 | 國際短信 | 短信通 | 短信接口應(yīng)用
關(guān)于我們 | 產(chǎn)品與服務(wù) | 解決方案 | 客戶案例 | 聯(lián)系我們 | 幫助指南 | 付款方式
Copyright © 2008-2018 短信通 . 速度網(wǎng)絡(luò)有限公司 短信接口應(yīng)用第一平臺 All rights reserved. 浙ICP備10040624號-2