Nodejs implements mobile phone registration SMS sending verification (Tencent Cloud)

foreword

Some time ago, I introduced how to implement email verification. Compared with mobile verification, it is better. It can verify the user's mobile phone number and prevent others from maliciously registering. This article introduces how to implement SMS sending in nodejs, taking Tencent Cloud's SMS verification as an example.

It’s just that SMS verification requires payment and requires some authentication information. It may not be as convenient as email registration. If you develop a website personally, it is recommended to check the email to send the verification code: nodejs/express realizes the email to send the verification code .

Prepare in advance

  1. First of all, you need a personal or enterprise certified account of Tencent Cloud. Individuals will give 100 pieces, and enterprises will give 1,000 pieces, which can be used for testing. Address: Tencent Cloud SMS Service .
  2. Then we need to authenticate the signature. We need to have our own company or registered personal website for authentication, that is, the part in front of the authentication message: [XX platform] . Address: Tencent Cloud SMS signature .
  3. The last step is to write the template, and it is easier to pass the review by using the standard template. Address: Tencent Cloud SMS Template .
    insert image description here

nodejs use

Dependency package

npm i tencentcloud-sdk-nodejs

code

Parameters that need to be modified by yourself:

  1. Get it secretIdwith secretKey: API Key Management .

  2. SMS App SmsSdkAppId: SDKAppID of the default app .
    insert image description here

  3. Approved templates TemplateId: Tencent Cloud SMS template management .
    insert image description here

  4. PhoneNumberSetFill in the mobile number you need to send to.

  5. TemplateParamSetFill in the template parameters, like the template if you are applying for mobile phone registration, the verification code is: {1}, valid within {2} minutes! , where {1} and {2} are parameters, we can dynamically pass in the parameters in the form of an array : verification code and valid time.

import tencentcloud from "tencentcloud-sdk-nodejs"

// 导入对应产品模块的client models。
const smsClient = tencentcloud.sms.v20210111.Client

/* 实例化要请求产品(以sms为例)的client对象 */
const client = new smsClient({
    
    
    credential: {
    
    
        /* 必填:腾讯云账户密钥对secretId,secretKey。
         * 你也可以直接在代码中写死密钥对,但是小心不要将代码复制、上传或者分享给他人,
         * 以免泄露密钥对危及你的财产安全。
         * SecretId、SecretKey 查询: https://console.cloud.tencent.com/cam/capi */
        secretId: '你的secretId',
        secretKey: '你的secretKey',
    },
    /* 必填:地域信息,可以直接填写字符串ap-guangzhou,支持的地域列表参考 https://cloud.tencent.com/document/api/382/52071#.E5.9C.B0.E5.9F.9F.E5.88.97.E8.A1.A8 */
    region: "ap-guangzhou",
    /* 非必填:
     * 客户端配置对象,可以指定超时时间等配置 */
    profile: {
    
    
        /* SDK默认用TC3-HMAC-SHA256进行签名,非必要请不要修改这个字段 */
        signMethod: "HmacSHA256",
        httpProfile: {
    
    
            /* SDK默认使用POST方法。
             * 如果你一定要使用GET方法,可以在这里设置。GET方法无法处理一些较大的请求 */
            reqMethod: "POST",
            /* SDK有默认的超时时间,非必要请不要进行调整
             * 如有需要请在代码中查阅以获取最新的默认值 */
            reqTimeout: 30,
            /**
             * 指定接入地域域名,默认就近地域接入域名为 sms.tencentcloudapi.com ,也支持指定地域域名访问,例如广州地域的域名为 sms.ap-guangzhou.tencentcloudapi.com
             */
            endpoint: "sms.tencentcloudapi.com"
        },
    },
})

/* 请求参数,根据调用的接口和实际情况,可以进一步设置请求参数
 * 属性可能是基本类型,也可能引用了另一个数据结构
 * 推荐使用IDE进行开发,可以方便的跳转查阅各个接口和数据结构的文档说明 */
const params = {
    
    
    /* 短信应用ID: 短信SmsSdkAppId在 [短信控制台] 添加应用后生成的实际SmsSdkAppId,示例如1400006666 */
    SmsSdkAppId: "你的SDKAppID",
    /* 短信签名内容: 使用 UTF-8 编码,必须填写已审核通过的签名,签名信息可登录 [短信控制台] 查看 */
    SignName: "你的标签名",
    /* 短信码号扩展号: 默认未开通,如需开通请联系 [sms helper] */
    ExtendCode: "",
    /* 国际/港澳台短信 senderid: 国内短信填空,默认未开通,如需开通请联系 [sms helper] */
    SenderId: "",
    /* 用户的 session 内容: 可以携带用户侧 ID 等上下文信息,server 会原样返回 */
    SessionContext: "",
    /* 下发手机号码,采用 e.164 标准,+[国家或地区码][手机号]
     * 示例如:+8613711112222, 其中前面有一个+号 ,86为国家码,13711112222为手机号,最多不要超过200个手机号*/
    PhoneNumberSet: ["+8613711112222"],
    /* 模板 ID: 必须填写已审核通过的模板 ID。模板ID可登录 [短信控制台] 查看 */
    TemplateId: "你的模板ID",
    /* 模板参数: 若无模板参数,则设置为空*/
    TemplateParamSet: ["666666", '10'],
}
// 通过client对象调用想要访问的接口,需要传入请求对象以及响应回调函数
client.SendSms(params, function (err, response) {
    
    
    // 请求异常返回,打印异常信息
    if (err) {
    
    
        console.log(err)
        return
    }
    // 请求正常返回,打印response对象
    console.log(response)
})

Effect

insert image description here

end words

If you think the article is not bad, please like and collect it. If you have any mistakes or suggestions, you can leave a message. Thank you~

Guess you like

Origin blog.csdn.net/weixin_43877799/article/details/123427817