短信验证模块
最近使用到手机短信校验,分享一下我的实现方式,也希望能够了解更好的实现方式 使用短信平台:阿里大鱼(阿里巴巴) 使用语言:node.js 数据库:redis(可用任意数据库或文件操作替代) 实现代码如下:
//安装依赖
//redis 数据库操作模块
npm install redis
//taobao API 操作模块。ES6请使用topapi-node
npm install top
//AuthController
var redisClient = require('../bin/redisClient');
var regx = /^(13|15|17|18|14)[0-9]{9}$/;
var top = require("top");
var client = top.createClient({
appkey:'******',
appsecret:'*******',
REST_URL: 'http://gw.api.taobao.com/router/rest'
});
var params_check = {
extend:"",
sms_type:"normal",
sms_free_sign_name:"注册验证",
sms_param:'{"product":"网站名","code":"'+parseInt(code)+'"}',
rec_num:tel,
sms_template_code:"SMS_1020033"
};
module.exports = {
/**
* 发送短信验证
* @param req
* @param res
*/
"smsCheck":function(req,res){
var tel = req.query.tel;
if (!tel || !regx.exec(tel))return res.json({errMsg: "tel is no true", errCode: "400"});
//生成4位数字的随机数
var code = Math.floor(Math.random() * (9999 - 999 + 1) + 999);
//检查用户是否已经注册
User.findOne({tel: tel}).exec(function findOneCB(err, user) {
if (user) return res.json({errMsg: "该手机号已被注册", errCode: "400"});
redisClient.exists("register:" + tel, function (err, result) {
if (err)return res.json({errMsg: "服务器出错,请重试", errCode: "500"});
if (result == 1)return res.json({errMsg: "请求过于频繁,请稍候重试", errCode: "423"});
//发送短信
client.invoke('alibaba.aliqin.fc.sms.num.send', params_check, [], null, 'GET', function (err, result) {
if (!err) {
//发送成功
redisClient.multi()
//限制访问频率60秒
.set("register:" + tel, code)
.expire("register:" + tel, 60)
.hset("code:" + tel, "code",code)
.hset("code:" + tel,"count",0)
.exec(function (err, replies) {
if (!err)return res.json({errMsg: "ok", errCode: 0});
});
} else {
res.json({errMsg: "服务器出错,请重试", errCode: "500"});
}
});
});
});
},
/**
* 检验验证码
* @param req
* @param res
*/
"checkRandom":function(req,res){
var code = req.body.code;
var tel = req.body.tel;
redisClient.hgetall("code:"+tel,function(err,result){
if (err)return res.status(500).json({errMsg:"服务器出错,请重试",errCode:"500"});
//服务器不存在校验码或已被删除
if(!result)return res.status(400).json({errMsg:"验证码失效,请重新获取验证码",errCode:"404"});
if(result.code == code){
return res.status(200).json({errMsg:"ok",errCode:"0"});
}else if(result.code != code){
if(result.count >=100)redisClient.del("code:" + tel);
else redisClient.hset("code:" + tel,"count",Number(result.count)+1);
return res.status(400).json({errMsg:"验证码不一致",errCode:"1"});
}
});
},
/**
* 提交注册
*/
"register":function(req,res){
var data = req.body;
if(!data || !data.tel ||!data.password || !data.code || !regx.exec(data.tel))return res.json({errMsg:"请输入有效内容",errCode:"400"});
redisClient.get("code:"+data.tel,function(err,result){
if (err)return res.json({errMsg:"服务器出错,请重试",errCode:"500"});
if(result != data.code) return res.json({errMsg:"验证码不一致",errCode:"1"});
//通过短信校验
var user = {
tel:data.tel,
password:data.password
};
User.create(user).exec(function createCB(err,createUser){
if(err){
return res.json({errMsg:"服务器出错,创建失败",errCode:500});
}
if(!err){
req.session.objectid = createUser.id;
return res.json({errMsg:"ok",errCode:0});
}
//清除缓存数据
redisClient.del("code:"+data.tel);
});
});
}
};