棋牌源码分享之短信接口,(NODEJS+cron)发送验证码短信

棋牌源码分享之短信接口,(NODEJS+cron)发送验证码短信

如果你的定时需求是简单的setInterval()与setTimeout()计时器所无法满足的比较复杂的定时规则,推荐使用cron来配置。

下面我们再来讲讲Cron风格定时器传入的参数具体代表什么,先来看看上面执行结果,如下图

从输出结果可以看出,传入的’30 * * * **'带来的结果是每分钟的30秒时都会执行,下面来看看这个传入参数分别代码什么

通配符解释

┬ ┬ ┬ ┬ ┬ ┬ │ │ │ │ │ | │ │ │ │ │
└ day of week (0 - 7) (0 or 7 is Sun) │ │ │ │
└───── month (1 - 12) │ │ │
└────────── day of month (1 - 31) │ │
└─────────────── hour (0 - 23) │
└──────────────────── minute (0 - 59)
└───────────────────────── second (0 - 59, OPTIONAL)


```javascript


var configs = config.mysql();

var client = require('mysql').createConnection(
{ host: configs.HOST, 
user: configs.USER, 
password: configs.PSWD, 
database: configs.DB, 
port: configs.PORT }
); 

 ClientConnectionReady = function(client) { 
 console.log('正在连接数据库...'); 
 client.query('use qp_games139com', function(error, results) { 
 if (error) { 

 console.log('Connection Error:' + error.message); client.end(); 
 return;
  }

   // setTimeout(Get_message, 1000 * 2, client);

        var CronJob = require('cron').CronJob;
        var job = new CronJob('*/2 * * * * *', function() {

            const d = new Date().toLocaleString();
            console.log(d);
            Get_message(client);
        }, null, true, 'Asia/Shanghai');
        job.start();


});
}

//遍历所有数据 

Get_message = function(client) { 
var sql = "SELECT * FROM t_users_message where state = 0 "; client.query(sql, function(error, results, fields) { 
if (error) { console.log('查询记录出错:' + error.message); 
client.end(); 
return;
 }

    if (results.length == 0) {
        console.log('暂时没有记录'.blue);
        return;
    } else {

        for (var i = 0; i < results.length; i++) {

            send(results[i]['id'],results[i]['imno'], results[i]['content'],
            function(err, smsId) {
                console.log(err, smsId);
                if (err == '0000') {

                    var sql = "update t_users_message set state=1 where id = ?  ";
                    var values = [smsId];
                    var sqls = client.format(sql, values);
                   // log.error(sqls);
                    client.query(sql, values,
                    function(error, results) {
                        if (error) {
                            console.log('Update Error:' + error.message);
                            return;
                        }
                    });

                }

            });

        }

    }
});
//client.end();
}

send = function(id, mobile, content, callback) {

var contents = querystring.stringify({

    account: 'cf_fleaphp',
    mobile: mobile,
    password: 'e5aab66587efffd664b5ad8acab6d112',
    content: content

});

var options = {
    host: '106.ihuyi.com',
    path: '/webservice/sms.php?method=Submit',
    method: 'POST',
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
        'Content-Length': contents.length
    }
}

var req = http.request(options,
function(res) {
    res.setEncoding('utf8');
    res.on('data',
    function(data) {
       // console.log("data:", data); //一段html代码
        content = data.replace(/(\r|\n|( xmlns="http:\/\/106.ihuyi.com\/"))/g, "");
        var doc = new DOMParser().parseFromString(content);
        var result = doc.lastChild;
        var json = {};
        var node = {};
        for (node = result.firstChild; node !== null; node = node.nextSibling) {
            json[node.tagName] = node.firstChild.data;
        }

        if (parseInt(json.code) === 2) {
            return callback('0000', id);
        } else {
            return callback(new Error(json.code,json.msg));
        }
    });

}).on("error", callback);

req.write(contents);
req.end;
};

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yuecpnet

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值