手機驗證碼登錄注冊--


前端點擊發送驗證碼---后端生成驗證碼----后端調用運營商----運營商將驗證碼發到用戶手機---前端輸入---后端對比驗證
下載tenxun運營商代碼
分析:
// 安裝md5加密npm i blueimp-md5 --save
// 用戶密碼也需要這個加密-一般暴力破解需要無限的測試-較安全的是在加上一個反向md5還有鹽
var md5 = require('blueimp-md5');
// npm i moment -- save
// 日期處理類
var moment = require('moment');
// npm i js-base64 --save
// 將圖片轉換為64編碼
var Base64 = require('js-base64').Base64;
// npm i request --save
var request = require('request');

/*生成指定長度的隨機數*/
function randomCode(length) {
var chars = ['0','1','2','3','4','5','6','7','8','9'];
var result = "";
for(var i = 0; i < length ; i ++) {
var index = Math.ceil(Math.random()*9);
result += chars[index];
}
return result;
}
exports.randomCode = randomCode;

/*向指定號碼發送指定驗證碼*/
function sendCode(phone, code, callback) {
var ACCOUNT_SID = '8aaf070855b647ab0155b9f80994058a';
var AUTH_TOKEN = 'aa8aa679414e49df8908ea5b3d043c24';
var Rest_URL = 'https://app.cloopen.com:8883';
var AppID = '8aaf070855b647ab0155b9f809f90590';
//1. 准備請求url
/*
1.使用MD5加密(賬戶Id + 賬戶授權令牌 + 時間戳)。其中賬戶Id和賬戶授權令牌根據url的驗證級別對應主賬戶。
時間戳是當前系統時間,格式"yyyyMMddHHmmss"。時間戳有效時間為24小時,如:20140416142030
2.SigParameter參數需要大寫,如不能寫成sig=abcdefg而應該寫成sig=ABCDEFG
*/
var sigParameter = '';
var time = moment().format('YYYYMMDDHHmmss');
sigParameter = md5(ACCOUNT_SID+AUTH_TOKEN+time);
var url = Rest_URL+'/2013-12-26/Accounts/'+ACCOUNT_SID+'/SMS/TemplateSMS?sig='+sigParameter;

//2. 准備請求體
var body = {
to : phone,
appId : AppID,
templateId : '1',
"datas":[code,"1"]
}

//3. 准備請求頭
/*
a.使用Base64編碼(賬戶Id + 冒號 + 時間戳)其中賬戶Id根據url的驗證級別對應主賬戶
b.冒號為英文冒號
c.時間戳是當前系統時間,格式"yyyyMMddHHmmss",需與SigParameter中時間戳相同。
*/
var authorization = ACCOUNT_SID + ':' + time;
authorization = Base64.encode(authorization);
var headers = {
'Accept' :'application/json',
'Content-Type' :'application/json;charset=utf-8',
'Content-Length': JSON.stringify(body).length+'',
'Authorization' : authorization
}

//4. 發送請求, 並得到返回的結果, 調用callback
request({
method : 'POST',
url : url,
headers : headers,
body : body,
json : true
}, function (error, response, body) {
console.log(error, response, body);
callback(body.statusCode==='000000');
});
}
exports.sendCode = sendCode;

/*
sendCode('18912989092', randomCode(6), function (success) {
console.log(success);
})*/

 后端接口

// 引入下載的運營商代碼-要用到里邊的方法
const sms_util = require('./../util/sms_util');
sendCode('18912989092', randomCode(6), function (success) {
console.log(success);
})


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM