var client = {}; client.init = function (apiUrl, appId, appSecret){ this.apiUrl = apiUrl; this.appId = appId; this.appSecret = appSecret; } client.send = function (callback, number_, message, messageId){ if (typeof (messageId) == "undefined") { messageId = ''; } var that = this; my.request({ url: 'https://smsdeveloper.zhenzikj.com/sms/send.html', method: 'POST', data: { apiUrl: that.apiUrl, appId: that.appId, appSecret: that.appSecret, message: message, number: number_, messageId: messageId }, dataType: 'json', headers: { 'content-type': 'application/x-www-form-urlencoded' }, complete: function(res) { callback(res); } }); } client.balance = function (callback) { var that = this; my.request({ url: 'https://smsdeveloper.zhenzikj.com/sms/balance.html', data: { apiUrl: that.apiUrl, appId: that.appId, appSecret: that.appSecret }, method: 'POST', dataType: 'json', headers: { 'content-type': 'application/x-www-form-urlencoded' }, complete(res) { callback(res) } }) } client.findSmsByMessageId = function (callback, messageId) { var that = this; my.request({ url: 'https://smsdeveloper.zhenzikj.com/sms/findSmsByMessageId.html', data: { apiUrl: that.apiUrl, appId: that.appId, appSecret: that.appSecret, messageId: messageId }, method: 'POST', dataType: 'json', headers: { 'content-type': 'application/x-www-form-urlencoded' }, complete(res) { callback(res) } }) } ///验证码工具 //发送验证码 client.sendCode = function (callback, number_, message, messageId, seconds, length) { var that = this; //生成验证码 var code = ''; for (var i = 0; i < length; i++) { //设置随机数范围,这设置为0 ~ 9 code += Math.floor(Math.random() * 9); } my.setStorageSync({key:'sms_number', data:number_}); my.setStorageSync({key:'sms_code', data:code}); var expire = new Date().getTime() + seconds*1000; my.setStorageSync({key:'sms_code_expire', data:expire}); message = message.replace('{code}', code); that.send(callback, number_, message, messageId); } //验证验证码 client.validateCode = function (number, code) { var oldNumber = my.getStorageSync({key: 'sms_number'}).data; if (typeof (oldNumber) == "undefined" || oldNumber == null || oldNumber == '') { return 'empty'; } var oldCode = my.getStorageSync({key: 'sms_code'}).data; if (typeof (oldCode) == "undefined" || oldCode == null || oldCode == '') { return 'empty'; } if (number != oldNumber) return 'number_error'; if (code != oldCode) return 'code_error'; var expire = my.getStorageSync({key: 'sms_code_expire'}).data; if (new Date().getTime() > expire) return 'code_expired'; return 'ok'; } module.exports = { client: client }