Nodejs实现短信验证码功能


Posted in NodeJs onFebruary 09, 2017

使用Nodejs的开发者愈来越多,基于Nodejs的后台开发也多了起来,像短信验证码、短信群发、国际短信这些需求,完全可以采用第三方接口来实现,云片就提供了这样的接口。

Nodejs

// 修改为您的apikey.可在官网(https://www.yunpian.com)登录后获取
var https = require('https');
var qs = require('querystring');
var apikey = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
// 修改为您要发送的手机号码,多个号码用逗号隔开
var mobile = 'xxxxxxxxxxx';
// 修改为您要发送的短信内容
var text = '【云片网】您的验证码是1234';
// 指定发送的模板编号
var tpl_id = 1;
// 指定发送模板的内容
var tpl_value = {'#code#':'1234','#company#':'yunpian'};
// 语音短信的内容
var code = '1234';
// 查询账户信息https地址
var get_user_info_uri = '/v2/user/get.json';
// 智能匹配模板发送https地址
var sms_host = 'sms.yunpian.com';
var voice_host = 'voice.yunpian.com';
send_sms_uri = '/v2/sms/single_send.json';
// 指定模板发送接口https地址
send_tpl_sms_uri = '/v2/sms/tpl_single_send.json';
// 发送语音验证码接口https地址
send_voice_uri = '/v2/voice/send.json';
query_user_info(get_user_info_uri,apikey);
send_sms(send_sms_uri,apikey,mobile,text);
send_tpl_sms(send_tpl_sms_uri,apikey,mobile,tpl_id,tpl_value);
send_voice_sms(send_voice_uri,apikey,mobile,code);
function query_user_info(uri,apikey){
 var post_data = { 
 'apikey': apikey, 
 };//这是需要提交的数据
 var content = qs.stringify(post_data); 
 post(uri,content,sms_host);
}
function send_sms(uri,apikey,mobile,text){
 var post_data = { 
 'apikey': apikey, 
 'mobile':mobile,
 'text':text,
 };//这是需要提交的数据 
 var content = qs.stringify(post_data); 
 post(uri,content,sms_host);
}
function send_tpl_sms(uri,apikey,mobile,tpl_id,tpl_value){
 var post_data = { 
 'apikey': apikey,
 'mobile':mobile,
 'tpl_id':tpl_id,
 'tpl_value':qs.stringify(tpl_value), 
 };//这是需要提交的数据 
 var content = qs.stringify(post_data); 
 post(uri,content,sms_host); 
}
function send_voice_sms(uri,apikey,mobile,code){
 var post_data = { 
 'apikey': apikey,
 'mobile':mobile,
 'code':code,
 };//这是需要提交的数据 
 var content = qs.stringify(post_data); 
 console.log(content);
 post(uri,content,voice_host); 
}
function post(uri,content,host){
 var options = { 
  hostname: host,
  port: 443, 
  path: uri, 
  method: 'POST', 
  headers: { 
   'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' 
  } 
 };
 var req = https.request(options, function (res) { 
  // console.log('STATUS: ' + res.statusCode); 
  // console.log('HEADERS: ' + JSON.stringify(res.headers)); 
  res.setEncoding('utf8'); 
  res.on('data', function (chunk) { 
   console.log('BODY: ' + chunk); 
  }); 
 }); 
 //console.log(content);
 req.write(content); 
 req.end(); 
}

上面就是云片的全部接口,在实际使用的过程中,可以根据自己的需求,选择对应的接口使用,具体的可以看这篇文章如何使用云片API发送短信验证码,里面讲了如何使用单发短信API、群发短信API、不同短信内容批量发送API,很实用。

另外最重要的是,云片的服务还不错,短信的到达率比较高,出了问题也有人及时回复,这点在国内所有SaaS厂家中,算是做得很好的一家。

NodeJs 相关文章推荐
14款NodeJS Web框架推荐
Jul 11 NodeJs
轻松创建nodejs服务器(6):作出响应
Dec 18 NodeJs
nodejs中实现阻塞实例
Mar 24 NodeJs
浅谈NodeJS中require路径问题
May 07 NodeJs
Nodejs实战心得之eventproxy模块控制并发
Oct 27 NodeJs
浅析nodejs实现Websocket的数据接收与发送
Nov 19 NodeJs
nodejs处理图片的中间件node-images详解
May 08 NodeJs
nodeJS实现路由功能实例代码
Jun 08 NodeJs
关于Mac下安装nodejs、npm和cnpm的教程
Apr 11 NodeJs
nodejs 使用 js 模块的方法实例详解
Dec 04 NodeJs
nodejs微信开发之接入指南
Mar 17 NodeJs
在NodeJs中使用node-schedule增加定时器任务的方法
Jun 08 NodeJs
Express与NodeJs创建服务器的两种方法
Feb 06 #NodeJs
使用nodejs下载风景壁纸
Feb 05 #NodeJs
nodeJs链接Mysql做增删改查的简单操作
Feb 04 #NodeJs
nodejs基础应用
Feb 03 #NodeJs
nodejs基础知识
Feb 03 #NodeJs
windows 下安装nodejs 环境变量设置
Feb 02 #NodeJs
图片上传之FileAPI与NodeJs
Jan 24 #NodeJs
You might like
用PHP和ACCESS写聊天室(十)
2006/10/09 PHP
php中mysql模块部分功能的简单封装
2011/09/30 PHP
PHP实现GIF图片验证码
2015/11/04 PHP
深入理解PHP内核(一)
2015/11/10 PHP
ThinkPHP3.1.x修改成功与失败跳转页面的方法
2017/09/29 PHP
PHP微信支付结果通知与回调策略分析
2019/01/10 PHP
JavaScript中的this关键字介绍与使用实例
2013/06/21 Javascript
提交按钮的name='submit'引起的js失效问题及原因
2015/02/25 Javascript
JavaScript实现为input与textarea自定义hover,focus效果的方法
2015/08/21 Javascript
BootStrap表单宽度设置方法
2017/03/10 Javascript
关于Vue在ie10下空白页的debug小结
2018/05/02 Javascript
小程序实现悬浮搜索框
2019/07/12 Javascript
Vue+Java 通过websocket实现服务器与客户端双向通信操作
2020/09/22 Javascript
spyder常用快捷键(分享)
2017/07/19 Python
Python统计纯文本文件中英文单词出现个数的方法总结【测试可用】
2018/07/25 Python
在python中利用最小二乘拟合二次抛物线函数的方法
2018/12/29 Python
如何在django里上传csv文件并进行入库处理的方法
2019/01/02 Python
Python 保持登录状态进行接口测试的方法示例
2019/08/06 Python
Python谱减法语音降噪实例
2019/12/18 Python
Python编译成.so文件进行加密后调用的实现
2019/12/23 Python
python从内存地址上加载python对象过程详解
2020/01/08 Python
python实现坦克大战
2020/04/24 Python
python神经网络编程实现手写数字识别
2020/05/27 Python
2021年值得向Python开发者推荐的VS Code扩展插件
2021/01/25 Python
马来西亚与新加坡长途巴士售票网站:BusOnlineTicket.com
2018/11/05 全球购物
男方父母证婚词
2014/01/12 职场文书
2014学雷锋活动心得体会
2014/03/10 职场文书
学校课外活动总结
2014/05/08 职场文书
群众路线四风问题整改措施
2014/09/27 职场文书
工作作风整顿个人剖析材料
2014/10/11 职场文书
乡镇群众路线专项整治方案
2014/11/03 职场文书
员工2014年度工作总结
2014/12/09 职场文书
劳动保障个人工作总结
2015/03/04 职场文书
2015年党员岗位承诺书
2015/04/27 职场文书
background-position百分比原理详解
2021/05/08 HTML / CSS
Python软件包安装的三种常见方法
2022/07/07 Python