1. 程式人生 > >發送短信驗證碼-node+阿裏雲短信

發送短信驗證碼-node+阿裏雲短信

驗證碼 temp sig nod 一個 需要 則表達式 then The

一、準備工作

前端:

表單

提交方式--- get 、post

整體提交

ajax提交

表單驗證

正則表達式---不輕易自己寫正則,不是不寫,一定要考慮好兼容性(全面性)---- 提示信息的選擇性

圖形驗證碼

後端進行提供的一張圖片,並且這張圖片會對應一個字段,這個字段傳遞給前端,前端負責校驗即可

短信驗證碼

判斷是不是手機號

如果是,那麽就發送此手機號給後端,後端繼續進行操作

第三方登錄

qq登錄,微信登錄,微博登錄

appid appsecret appkey

後端:

get

url.parse(req.url, true).query

post

req.body

短信驗證

1、搞定短信包----發送短信

阿裏雲短信服務

https://www.aliyun.com/

短信接口的調用--https://help.aliyun.com/document_detail/57458.html?spm=5176.10629532.106.4.36c21cbe53q1qJ

秘鑰管理頁面 -- https://ak-console.aliyun.com/?spm=a2c4g.11186623.2.5.M6srOG#/accesskey

開發:

cnpm i @alicloud/sms-sdk -S

tool/mycode.js ---- 此處代碼不要更改,除非你有自己的賬號

const SMSClient = require(‘@alicloud/sms-sdk‘)

const accessKeyId = ‘LTAIZQoVVoPuBjU9‘

const secretAccessKey = ‘GfJuI2dLsCQh7Q56TmFxPTniXjkVnB‘

let smsClient = new SMSClient({accessKeyId, secretAccessKey})

exports.sendCode = function ( options ) {

smsClient.sendSMS({

PhoneNumbers: options.phoneNum,

SignName: ‘吳勛勛‘,//按照嚴格意義來說,此處的簽名和模板的ID都是可變的

TemplateCode: ‘SMS_111785721‘,

TemplateParam: ‘{code:‘+ options.code +‘}‘

}).then(function (res) {

let {Code}=res

if (Code === ‘OK‘) {

//處理返回參數

// console.log("111111111111111111111")

options.success(‘ok‘)

}

}, function (err) {

console.log(err)

})

}

需要調用發送短信的地方

users.js

var url = require(‘url‘);

var async = require(‘async‘);

var { MongoClient } = require(‘mongodb‘);

var mongourl = "mongodb://localhost:27017/bk1803";

var { sendCode } = require(‘./mycode.js‘)

module.exports = {

defaultRoute: ( req, res, next ) => {

res.render(‘users‘);

},

getPhoneCode( req, res, next ){

var { phoneNum } = url.parse( req.url, true ).query;

async.waterfall( [

( cb ) => {

MongoClient.connect( mongourl, ( err, db ) => {

if ( err ) throw err;

cb( null, db);

})

},

( db, cb ) => {

db.collection(‘users‘).find({phoneNum},{_id:0}).toArray( ( err, res ) => {

if ( err ) throw err;

if( res.length == 0 ) {

cb( null, 1);

}else {

cb( null, 0);

}

db.close();

})

}

], ( err, result ) => {

if ( err ) throw err;

if( result == 1) {

sendCode({

phoneNum,

code:‘3456‘,

success:function(data){

if(data == "ok"){

res.send("1")

}

}

})

}else{

res.send("0")

}

})

},

registerUserAction( req, res, next ){

var { phoneNum, password } = req.body;

async.waterfall( [

( cb ) => {

MongoClient.connect( mongourl, ( err, db ) => {

if ( err ) throw err;

cb( null, db);

})

},

( db, cb ) => {

db.collection(‘users‘).insert({phoneNum, password}, ( err, res ) =>{

if ( err ) throw err;

cb( null, ‘ok‘);

db.close()

})

}

], ( err, result ) => {

if ( err ) throw err;

if ( result == "ok" ){

res.send("1")

}else{

res.send("0")

}

})

}

}

發送短信驗證碼-node+阿裏雲短信