短信验证码(阿里)
const Core = require('@alicloud/pop-core');
const express = require('express');
const router = express.Router()
const { formatData } = require('../utils/tools')
router.get('/:PhoneNumbers', (req, res) => {
const { PhoneNumbers } = req.params
let randomNum = (parseInt((Math.random() * 9900) + 100)).toString();
randomNum = randomNum.padStart(4, '0');
const TemplateParam = `{"code":${randomNum}}`
req.session.Sms = randomNum
var client = new Core({
accessKeyId: 'LTAI4G2vNjhrGTESjtA9axB6',
accessKeySecret: 'QFNGnTnhvR5P1GDMZFz9ahFPm6zWwm',
endpoint: 'https://dysmsapi.aliyuncs.com',
apiVersion: '2017-05-25'
});
var params = {
"RegionId": "cn-hangzhou",
"PhoneNumbers": PhoneNumbers,
"SignName": "ugoshop",
"TemplateCode": "SMS_201715882",
"TemplateParam": TemplateParam
}
var requestOption = {
method: 'POST'
};
client.request('SendSms', params, requestOption).then(() => {
res.send(formatData(1001));
}, (ex) => {
console.log(ex);
})
})
module.exports = router;
聚合数据的nodejs模板
phonecheck.js
var request = require("request");
var querystring = require("querystring");
const express = require("express");
const Router = express.Router(); //Router==app
//验证用户是否存在 :get请求
Router.get("/check", async (req, res) => {
let { phone } = req.query; //获取电话号码
let str = ""; //准备拼接四位随机数:这里就是发到用户手机的随机数
for (let i = 0; i < 4; i++) {
str += parseInt(Math.random() * 10); //0-9的随机数
}
var queryData = querystring.stringify({
mobile: phone, // 接受短信的用户手机号码
tpl_id: "144712", // 您申请的短信模板ID,根据实际情况修改
tpl_value: "#code#=" + str, // 您设置的模板变量,根据实际情况修改
key: "360467c49f4c1899234735dbac01ea94", // 应用APPKEY(应用详细页查询)
});
var queryUrl = "http://v.juhe.cn/sms/send?" + queryData;
request(queryUrl, function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body); // 打印接口返回内容
var jsonObj = JSON.parse(body); // 解析接口返回的JSON内容
console.log(jsonObj);
// res.send(jsonObj); //实际的需求:这个不用返回,直接存到数据库中,点击提交的时候拿到的验证码和这个验证码对比就行
res.send("发送成功");
} else {
console.log("请求异常");
res.send("发送失败");
}
});
});
module.exports = Router; //导出路由对象
index.js
//希望在这里查询数据,返回给前端,叫做路由
const express = require("express");
const Router = express.Router(); //Router==app
//引入子路由
const phonecRouter = require("./module/phonecheck");
//子路由的选择
Router.use("/phone", phonecRouter); // /phone/check
module.exports = Router; //导出路由对象
server.js
//引入express模块:先安装express: npm i express
let express = require('express');//绝对路径
// 引入json解析中间件
var bodyParser = require('body-parser');
const { json } = require('body-parser');
//引入总路由:routers/index.js
const allRouter = require('./routers/index');
const { port } = require('./config.json');
//实例化对象
let app = express();
// 添加json解析
app.use(bodyParser.json());//帮我们解析post提交的数据 {"name","gaoyuanyuan","psw":123456}
app.use(bodyParser.urlencoded({ extended: false })); //帮我们解析post提交的键值对数据 name=gaoyuanyuan&psw=123456
app.use(allRouter);//使用总路由
//开启一个静态资源服务器:借助express的一个中间件 static
app.use(express.static('./'));//相当于apache里面的www默认的静态资源目录
//开启服务器:执行这个文件就是开启服务器了 node 06 server.js
//端口号设置:1000-60000
app.listen(port, () => {
//当服务器开启成功就执行这个回调函数
console.log(`服务器开启成功,请访问${port}端口`);
});