发送短信验证码-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")

}

})

}

}

原文地址:https://www.cnblogs.com/Guernicas/p/10662175.html

时间: 2024-08-24 00:43:08

发送短信验证码-node+阿里云短信的相关文章

阿里云优惠券 – 阿里云短信优惠券 满1000减50 满3000减150 满5000减250 满10000减500

阿里云优惠券 – 云数据库RDS MYSQL代金券 满5000减250.满10000减500 本文分享阿里云短信优惠券.阿里云短信代金券,用于购买阿里云短信资源包\套餐包: 1.阿里云新用户可领取总金额为1000元,面额分为满1000减50.满3000减150.满5000减250.满10000减500,总金额为500+250+150+50×2=1000元.去阿里云官方领取优惠券页面领券:阿里云1000元短信代金券 2.阿里云老用户可领取总金额270元,面额分别为满5000减150.满2000减6

使用阿里云短信服务发送短信验证码

阿里云短发服务使用流程: 1.在阿里云上完成短信服务的购买. 2.导入相关的jar包. <!-- 阿里云短信服务 --> <dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-core</artifactId> <version>4.1.0</version> </dependency> <!-- 阿里云短

thinkphp5 阿里云短信 发送多参数的短信

有的朋友用阿里oss的时候可能会发送多参数的短信, 例如短信模版是  您好${code1},收到您的联系方式${code2},您的地址为${code3},我们会尽快派送. 类似于这样的多参数模版,首先第一关,可能是参数过长,怎么办,直接去阿里申请售后就可以了,和他们反馈一下,然后他们会给你把字数限制解除,然后你就可以传递超过20个字的内容了. 这一步完事,下一步就是接入阿里oss了,首先下载阿里oss的 类包了,去阿里官网下载就可以了,我这里整合的是thinkphp5.下载完成后具体步骤如下 1

阿里云短信服务验证码模板

<?php namespace app\api\controller; use think\Db; use think\Request; use think\Controller; /** * 阿里云短信验证码发送类 * @author Administrator * */ class Smscode { // 保存错误信息 public $error; // Access Key ID private $accessKeyId = ''; // Access Access Key Secret

composer阿里云短信服务不支持传参为数值

composer 阿里云短信服务使用 xuying/aliyun_mns /** * TODO SMS SERVICE * @param $tmp * @param $moblie * @param $name * @param int $len * @return int */private function mns($tmp,$moblie,$name,$len=4){ $count=[]; for($i =0;$i<$len; $i++){ $count[] =rand(1, 9); }

阿里云短信服务调用例子-Python

阿里云短信服务调用例子 阿里云官方文档https://helpcdn.aliyun.com/document_detail/101893.html 首先需要安装阿里云PythonSDK(下面是python3版本的安装方式) pip install aliyun-python-sdk-core-v3 阿里云官方调用例子 #!/usr/bin/env python #coding=utf-8 from aliyunsdkcore.client import AcsClient from aliyun

调用阿里云短信服务

package com.example.demo.untils; /** * Created by JQY on 2019/5/15 */ import com.aliyuncs.DefaultAcsClient; import com.aliyuncs.IAcsClient; import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest; import com.aliyuncs.dysmsapi.model.v20170525.Send

阿里云短信验证_基于阿里云OpenAPI实现

阿里云短信服务 背景简介: 短信验证以及短信通知,目前已经应用的非常广泛,最近因项目需要,需要将原来的短信接口换成阿里云的的短信服务,原项目集成的短信服务能够实现短信的发送以及短信的验证整个过程,简单的来说,原来的短息服务,只需应用申请获取短信,短息服务器会发送短信到指定的手机,用户将验证码发送到短信服务商的服务器,服务器做出验证返回是否通过,而阿里云仅提供短信发送服务,需要自己开发短信的验证.下面简单的介绍一下: 1.获取阿里云AccessKey 用户->Accesskeys:需要自己创建一个

阿里云短信验证

一.开通阿里云短信服务,申请签名和模板,记住accessKeyId和accessKeySecret,具体操作可搜到 二.点击短信服务的帮助文档,点击旧版的开发指南,(新版未试用),下载java版本,访问dysms_java\java\api_demo\alicom-dysms-api\src\main\java\com\alicom\dysms\api 中的smsdemo文件,导入dysms_java\java\api_demo\alicom-dysms-api\libs的两个包,然后就可以按照