棋牌源码分享之短信接口,(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)
1 var configs = config.mysql(); 2 3 var client = require(\'mysql\').createConnection( 4 { host: configs.HOST, 5 user: configs.USER, 6 password: configs.PSWD, // 7 database: configs.DB, 8 port: configs.PORT }); //选择哪个库 9 10 ClientConnectionReady = function(client) { 11 console.log(\'正在连接数据库...\'); 12 client.query(\'use qp_games139com\', function(error, results) { 13 if (error) { 14 console.log(\'Connection Error:\' + error.message); 15 client.end(); return; 16 } 17 18 // setTimeout(Get_message, 1000 * 2, client); 19 20 var CronJob = require(\'cron\').CronJob; 21 var job = new CronJob(\'*/2 * * * * *\', function() { 22 23 const d = new Date().toLocaleString(); 24 console.log(d); 25 Get_message(client); 26 }, null, true, \'Asia/Shanghai\'); 27 job.start(); 28 29 30 }); 31 32 } 33 34 //遍历所有数据 35 36 Get_message = function(client) { 37 var sql = "SELECT * FROM t_users_message where state = 0 "; 38 client.query(sql, function(error, results, fields) { 39 if (error) { 40 console.log(\'查询记录出错:\' + error.message); 41 client.end(); 42 return; 43 } 44 45 if (results.length == 0) { 46 console.log(\'暂时没有记录\'.blue); 47 return; 48 } else { 49 50 for (var i = 0; i < results.length; i++) { 51 52 send(results[i][\'id\'],results[i][\'imno\'], results[i][\'content\'], 53 function(err, smsId) { 54 console.log(err, smsId); 55 if (err == \'0000\') { 56 57 var sql = "update t_users_message set state=1 where id = ? "; 58 var values = [smsId]; 59 var sqls = client.format(sql, values); 60 // log.error(sqls); 61 client.query(sql, values, 62 function(error, results) { 63 if (error) { 64 console.log(\'Update Error:\' + error.message); 65 return; 66 } 67 }); 68 69 } 70 71 }); 72 73 } 74 75 } 76 }); 77 //client.end(); 78 79 } 80 81 send = function(id, mobile, content, callback) { 82 83 var contents = querystring.stringify({ 84 85 account: \'cf_fleaphp\', 86 mobile: mobile, 87 password: \'e5aab66587efffd664b5ad8acab6d112\', 88 content: content 89 90 }); 91 92 var options = { 93 host: \'106.ihuyi.com\', 94 path: \'/webservice/sms.php?method=Submit\', 95 method: \'POST\', 96 headers: { 97 \'Content-Type\': \'application/x-www-form-urlencoded\', 98 \'Content-Length\': contents.length 99 } 100 } 101 102 var req = http.request(options, 103 function(res) { 104 res.setEncoding(\'utf8\'); 105 res.on(\'data\', 106 function(data) { 107 // console.log("data:", data); //一段html代码 108 content = data.replace(/(\r|\n|( xmlns="http:\/\/106.ihuyi.com\/"))/g, ""); 109 var doc = new DOMParser().parseFromString(content); 110 var result = doc.lastChild; 111 var json = {}; 112 var node = {}; 113 for (node = result.firstChild; node !== null; node = node.nextSibling) { 114 json[node.tagName] = node.firstChild.data; 115 } 116 117 if (parseInt(json.code) === 2) { 118 return callback(\'0000\', id); 119 } else { 120 return callback(new Error(json.code,json.msg)); 121 } 122 }); 123 124 }).on("error", callback); 125 126 req.write(contents); 127 req.end; 128 129 };