<!DOCTYPE html>
<html>
<head>
<title>
手机验证码测试
</title>
<link rel='stylesheet' href='/stylesheets/style.css' />
</head>
<body>
<input type="phone" id='phoneNum'>
<input type="button" value='获取验证码' id='getVerificationCode'>
<br>
<input type="text" id='verificationCode'>
<input type="button" value='验证' id='verify'>
<script src="https://cdn.bootcss.com/jquery/2.2.4/jquery.js"></script>
<script>
$('#getVerificationCode').click(function () {
var phone = $('#phoneNum').val();
$.ajax({
url: '/getverificationcode',
data: { phone: phone },
success: function (data) {
alert(data);
},
type: "POST"
})
});
$('#verify').click(function () {
var phone = $('#phoneNum').val();
var code = $('#verificationCode').val();
$.ajax({
url: '/verify',
data: { phone: phone, code: code },
success: function (data) {
alert(data);
},
type: "POST"
})
});
</script>
</body>
</html>
var express = require('express');
var router = express.Router();
var SMS = require('../sms');
/* 打开首页 */
router.get('/', function (req, res, next) {
res.render('index', { title: 'Express' });
});
/* 请求发送验证码*/
router.post('/getverificationcode', (req, res, next) => {
var code = "";
//生成验证码
for (var i = 0; i < 4; i++) {
code += Math.floor(Math.random() * 10)
}
//将手机号码和验证码放进session
req.session['code'] = code;
req.session['phone'] = req.body.phone;
//这一步是关键,现在我仅仅是通过返回值的形式告诉用户的验证码,但是可以换成短信的形式来告诉用户,就可以在这里操作的。
SMS.send(req.body.phone, code,function(data){
res.end(data.msg)
});
})
/* 校验验证码*/
router.post('/verify', (req, res, next) => {
//获取用户传过来的手机号码和code
var phone = req.body.phone;
var code = req.body.code;
//比对手机号码和验证码
if (phone == req.session['phone'] && code == req.session['code']) {
//比对成功后删除以免多次验证
delete req.session.phone;
delete req.session.code;
res.end('验证成功')
}
res.end('验证失败')
})
module.exports = router;

var express = require('express');
var router = express.Router();
/* GET home page. */
router.get('/', function (req, res, next) {
res.render('index', { title: 'Express' });
});
router.post('/getverificationcode', (req, res, next) => {
var code = "";
//生成验证码
for (var i = 0; i < 4; i++) {
code += Math.floor(Math.random() * 10)
}
//将手机号码和验证码放进session,如果愿意,也可以设置一下过期时间等。
req.session['code'] = code;
req.session['phone'] = req.body.phone;
//准备参数
var text = `【传智NeeKin】亲爱的${name},您的验证码是${code}。有效期为10分钟,请尽快验证`
var content = querystring.stringify({
apikey: 'xxxxxxxxxxxxxxxxxxxxxx',
mobile: req.body.phone,
text: text
});
//给请求接口的客户端准备参数
var option = {
host: 'sms.yunpian.com',
path: '/v2/sms/single_send.json',
method: 'POST',
headers: {
"Content-Length": Buffer.byteLength(content),
}
}
//加载http模块去请求接口并且打印一下请求结果
var req2 = http.request(option, function (res2) {
console.log("statusCode: ", res2.statusCode);
console.log("headers: ", res2.headers);
var _data = '';
res2.on('data', function (chunk) {
_data += chunk;
});
res2.on('end', function () {
console.log("\n--->>\nresult:", _data)
});
});
req2.write(content);
req2.end();
res.end('获取验证码OK');
})
var SMS = {
send: function (mobile, code,cb) {
var text = `【传智NeeKin】亲爱的${mobile},您的验证码是${code}。有效期为10分钟,请尽快验证`
var content = querystring.stringify({
apikey: 'xxxxxxxxxxxxxxxxxxxxxx',
mobile: mobile,
text: text
});
var option = {
host: 'sms.yunpian.com',
path: '/v2/sms/single_send.json',
method: 'POST',
headers: {
"Content-Length": Buffer.byteLength(content),
}
}
var req = http.request(option, function (res) {
var _data = '';
res.on('data', function (chunk) {
_data += chunk;
});
res.on('end', function () {
cb(_data);
});
});
req.write(content);
req.end();
}
}{
"code": 0,
"msg": "发送成功",
"count": 1,
"fee": 0.05,
"unit": "RMB",
"mobile": "13200000000",
"sid": 3310228982
}req.session['phone'] = req.body.phone;
SMS.send(req.body.phone, code,function(data){
res.end(data.msg)
});
803.82 KB, 阅读权限: 10, 下载次数: 2
| 欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) | 黑马程序员IT技术论坛 X3.2 |