点击这里给我发消息
帮助中心

工作时间:8:30 - 17:30

NODE.JS

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("短信发送服务器响应失败。"));
            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;