zoukankan      html  css  js  c++  java
  • Node 接入阿里云实现短信验证码

    本文介绍在案例云开通短信服务的流程以及在Node项目中使用的方法。

    一、开通阿里云短信服务

    登陆阿里云,然后进入 https://dysms.console.aliyun.com/dysms.htm 。

    选择国内消息,在“签名管理”里添加签名,获取 SignName;在“模板管理”里添加模板,获得 TemplateCode 。

     

    二、获取 AccessKey

    要调用短信服务,需要通过 AccessKey 鉴权。登陆阿里云,进入 https://usercenter.console.aliyun.com/ ,创建 AccessKey,得到 accessKeyId 和 accessKeySecret 。

    三、费用支付

    短信服务是收费的,可以按发送次数计费,也可以购买套餐。跟手机用户向联通移动付费的方式相似,会在月底出账单,欠费的话会停掉短信服务。

    为避免欠费,可以在阿里云帐户内存入一定余额。进入 https://usercenter2.aliyun.com/home 查看:

     

    四、Node调用

    @alicloud/pop-core 是阿里提供的核心库,需要在项目中引入:

    npm install @alicloud/pop-core -S

    下面是 sms.js 示例代码,其中 SignName / TemplateCode / accessKeyId / accessKeySecret 都要从阿里云获取,请替换为实际值。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    /**
     * sms.send(手机号) 发送短信验证码
     * sms.verify(手机号,验证码) 校验验证码是否正确
     **/
     
    const Core = require('@alicloud/pop-core');
    const _ = require('lodash');
     
    // 阿里云控制台 - 短信服务 - 国内消息
    const SignName = "东方网络";
    const TemplateCode = "SMS_123456";
     
    // https://usercenter.console.aliyun.com/
    const accessKeyId = "ljksdhfjklJKGKGKJHK";
    const accessKeySecret = "HKAJSHDIU90800980jkahsd";
     
    var client = new Core({
        accessKeyId,
        accessKeySecret,
        endpoint: 'https://dysmsapi.aliyuncs.com',
        apiVersion: '2017-05-25'
    });
     
    // 保存手机号和验证码的对应关系
    // phone_code_list = {'18855551234':['1024']}
    var phone_code_list = {};
     
    exports.send = function(phone) {
        // 生成验证码
        var code = "" + _.random(9) + _.random(9) + _.random(9) + _.random(9);
        return new Promise((resolve, reject) => {
            try {
                client.request('SendSms', {
                    RegionId: "cn-hangzhou",
                    PhoneNumbers: phone,
                    SignName,
                    TemplateCode,
                    TemplateParam: "{code:" + code + "}"
                }, {
                    method: 'POST'
                }).then((result) => {
                    if (result.Message && result.Message == "OK" && result.Code && result.Code == "OK") { // 短信发送成功
                        // 保存验证码
                        if (phone_code_list[phone]) {
                            phone_code_list[phone].push(code);
                        else {
                            phone_code_list[phone] = [code];
                        }
                        // 三分钟后删除验证码
                        setTimeout(() => {
                            _.pull(phone_code_list[phone], code);
                            if (phone_code_list[phone] && phone_code_list[phone].length == 0) {
                                delete phone_code_list[phone];
                            }
                        }, 3 * 60 * 1000)
                        resolve(result)
                    else {
                        reject(result)
                    }
                }, (ex) => {
                    reject(ex)
                })
            catch (error) {
                reject(error)
            }
        })
    }
     
    exports.verify = function(phone, code) {
        return (phone_code_list[phone].indexOf(code) > -1)
    }

    调用方式参考:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    const sms = require("./util/sms.js")
     
    // 发送验证码
    sms.send("18855551234").then((result) => {
        console.log("短信发送成功")
        console.log(result)
    }, (ex) => {
        console.log("短信发送失败")
        console.log(ex)
    });
     
    // 校验用户提交的验证码
    var isCodeRight = sms.verify("18855551234","0000"); // 返回true/false
  • 相关阅读:
    UI进阶--UIPikcerView实现餐点搭配示例
    UI基础--UITableView实现仿QQ好友列表页面
    通知和代理
    UI基础--UITableView实现仿QQ聊天页面
    UI基础--UITableView实现仿微博页面
    UI基础--UITableView,UITableViewDataSource,UITableViewDelegate的一些属性和方法
    UI基础--使用UIScrollView、UIPageControl、NSTimer实现图片循环播放
    UI基础--UIScrollView和UIScrollViewDelegate
    NSURLSession 简介
    Core Animation
  • 原文地址:https://www.cnblogs.com/luoguixin/p/14381599.html
Copyright © 2011-2022 走看看