zoukankan      html  css  js  c++  java
  • 发送短信验证码-node+阿里云短信

    一、准备工作  

    前端:

        表单

        提交方式--- 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")

           }

        })

      }

    }

  • 相关阅读:
    Vasya And Password(CodeForces
    Romaji (CodeForces
    Sorting a Three-Valued Sequence(三值的排序)
    Factorials 阶乘
    Factorial(hdu 1124)
    EXTENDED LIGHTS OUT
    kali更换root默认登陆
    api-ms-win-crt-string-l1-1-0.dll丢失解决办法
    vim 缩进
    bash: cd: .ssh/: Permission denied
  • 原文地址:https://www.cnblogs.com/Guernicas/p/10662175.html
Copyright © 2011-2022 走看看