使用node結合阿里雲短信服務實現發送手機短信驗證碼

一、準備工作 

前端:

    表單

    提交方式--- get 、post

        整體提交

        ajax提交

    表單驗證

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

    圖形驗證碼

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

    短信驗證碼

        判斷是不是手機號

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

    第三方登錄

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

        appid appsecret appkey

後端:

    get

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

    post

        req.body

    短信驗證

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

        阿里雲短信服務

        https://www.aliyun.com/

image.png

image.png

image.png

image.png

        短信接口的調用--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

        image.png

   image.png

 開發:

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")

       }

    })

  }

}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章