With cloud development to achieve a small program to send SMS verification code

When recently landed a small program code, use the SMS verification code requirements, and he studied under, to implement SMS verification code is still very easy to use in conjunction with cloud cloud development function.

Old rules, look at the effect of FIG.


This is the code I call landing Tencent cloud messaging platform to send. In fact, only the following core code so much

is not feeling particularly simple to implement, how should I say, in fact, that we call the code a few lines, you can send text messages to achieve, but the audit Tencent cloud messaging template is relatively complicated, there we go apply for SMS templates, SMS templates can be used only after approval.
We tried to get it implemented in code and then take you to a simple application for SMS templates under study.

First, install the node library

In fact, we here used the cloud cloud development function, we are calling to send text messages in the cloud in function. Why call in the cloud function in it, because we do SMS, libraries need to use a text message sent Tencent cloud, but this library is a library node, you can only call a function in the cloud.

Before installing this library, we need to create a cloud function, create a cloud on the function, in fact, I've said many times, the students do not know, go look at my history essay, I look or recorded Getting Started video cloud development "five hours zero-based entry applet cloud development,"
I will back this section to record a video out.

After you've created a cloud function, right-click to open the terminal, open a terminal and enter the following command in the terminal to install qcloudsms_js library

npm install qcloudsms_js


It should be noted, before we install the libraries need to download and configure the node npm environment variables, here I also have to write the article
"Configuring nodeJs installation and npm global environment variables"

Second, write cloud function

After the above libraries installed, since we can write a function of the cloud.
In fact, it is very simple to write the code, it is below the corresponding annotation and I have written out.

Phone number, and random verification code here to send dynamically passed in.

Third, call the cloud function

调用云函数这里也很简单,我们需要传入手机号和验证码

手机号这里,我做了一个输入框,可以动态的输入。验证码的话,我写了一个方法来随机生成数字和字母的组合验证码。

我等下会把完整的代码贴出来给大家。

这样我们输入完手机号以后,点击发送短信按钮,就可以成功的发送短信给到对应的手机号了。

  • 可以看到我们生成的随机验证码如下

    我们手机接受到的短信验证码如下

    这样我们做登陆或者做校验时,用户手机短信收到的验证码,和我们随机生成的验证码一样,即代表用户验证成功。
    完整的index.js代码给大家贴出来
    var chars = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
    let phone = ''
    Page({
    //获取随机验证码,n代表几位
    generateMixed(n) {
    var res = "";
    for (var i = 0; i < n; i++) {
      var id = Math.ceil(Math.random() * 35);
      res += chars[id];
    }
    return res;
    },
    //调用云函数发送短信
    sendSMS() {
    if (phone.length != 11) {
      wx.showToast({
        icon: 'none',
        title: '输入11位手机号',
      })
      return
    }
    let code = this.generateMixed(4)
    console.log('本地生成的验证码', code)
    wx.cloud.callFunction({
      name: "sendSms",
      data: {
        phone: phone,
        code: code //生成4位的验证码
      }
    }).then(res => {
      console.log('发送成功', res)
    }).catch(res => {
      console.log('发送失败', res)
    })
    },
    //获取要发送的手机号
    getPhone(event) {
    console.log(event.detail.value)
    phone = event.detail.value
    },
    })

    index.wxml如下

    到这里我们的短信验证码的发送就完整的实现了,是不是很简单。

短信发送参数的设置与获取

首先是去腾讯云自己开通短信功能,然后需要自己去申请模板,填写签名。

我这里把所需要的参数,都给大家标准出来了。大家只需要自己去官网设置对应的模板和签名,然后审核通过后,把对应的参数放到我们的云函数里即可。

短信验证的原理讲解

在网上找了一张短信验证的原理图,如下

大家可以对照这看下,这个原理图。对应的源码我上面其实已经给大家贴出来了。
如果大家觉得不完整,我也已经把完整源码放到网盘里了,有需要的同学可以到我公号里回复“短信”获取源码。

后面我还会分享更多小程序相关的知识点出来,请持续关注。

Guess you like

Origin blog.51cto.com/14368928/2463958