Node發送短信驗證碼

 

1. 簡介

咱們在日常的開發中常常會遇到在註冊的時候要發送手機短信驗證碼,或者是在一些安全性校驗的時候要發送手機短信驗證碼, 因而找了不少短信驗證碼平臺,可是支持nodejs的平臺不多,今天就介紹一個少有的支持node的短信驗證平臺:雲片網node

2. 雲片網

2.1. 優勢

  • 和運營商同樣的到達率
  • 支持高併發
  • 5秒必達
  • 價格便宜

2.2. 資費和產品

3. 快速開始

  • 在雲片網上快速註冊並進行開發者認證
  • 新建一個系統並獲取APIKEY

  • 查看發送單條和多條短信的api接口

  • 代碼示例
// config.js
export default {
    SMS:{
        URL:'https://sms.yunpian.com/v2/sms/single_send.json',
        // 不能修改,不要給別人看
        APIKEY:'79b60d4f9bhu78hjh9jf3f400e57e074b4',
    }
}
// login.js

/*
 * 發送手機驗證碼
 * GET /api/login/sendMobileCode
 * */
import request from 'request'
router.get('/sendMobileCode',function (req,res,next) {
    // 手機號
    let mobile=req.query.message
    // 6位隨機驗證碼
    let random=Math.ceil(Math.random()*1000000)
    // 短信內容
    let text=`【雲片網】您的驗證碼是${random}`
    // 報文請求頭
    let options = {
        method:'post',
        url: config.SMS.URL,
        headers: {
            'Content-Type':'application/x-www-form-urlencoded;charset=utf-8'
        },
        form:{
            apikey:config.SMS.APIKEY,
            mobile:mobile,
            text:text,
        }
    }

    request(options, function(error, response, body) {
        if (!error && response.statusCode == 200) {
           res.send({
               status:true,
               message:body,
               checkCode:random
           })
        }else if (!error && response.statusCode != 200) {
            res.send({
                status:false,
                message:body
            })
        }else{
            res.send({
                status:false,
                message:error
            })
        }
    })

})
相關文章
相關標籤/搜索