【NodeJs】UniSMS 实现短信验证码功能

 承接上文 ,上次用的是 短信宝平台 认证已经通过 后续又新增要求 平台相当麻烦!

短信宝实现短信发送要求:
1.平台绑定手机号    必须和   营业执照法人一致
2.平台个人实名认证 必须和  营业执照法人一致
3.平台需要上传营业执照
4.平台需要上传手持身份证
5.平台需要网站备案信息 备案信息 必须和 营业执照法人一致

第五步完成后 我不知道还需要提交什么才能通过认证实现短信发送 ,因为我看不到平台这个洞底

 

UniSMS

介绍:

认证要求, 平台有 个人认证 和 企业认证 

  • 个人认证实名通过后 创建签名 添加对应信息提交等待
  • 企业认证提交 三合一营业执照  创建签名 添加对应信息提交等待
     

说一下 unisms 我这里是怎么通过的 
提交三合一营业执照 创建签名 签名名称 添加 机构名即可
相比其他平台 unisms 会快捷容易

实现短信验证码发送

unisms有实现短信发送两种方式分别是 SDK 和 API接口 


sdk实现:

注意:这里把 .js 后缀 替换为 .cjs 为什么要替换? 这样 Node.js 会将该文件视为 CommonJS 模块。

安装第三方库:

npm install unisms or yarn add unisms

const express = require('express')
const router = express.Router()
const UniSMS = require('unisms').default

const client = new UniSMS({
  accessKeyId: 'your access key id'
})
const SmsSignature = 'your SMS signature name'

async function sendSMS (mobile, template, codeNumber) {
  try {
    const data = await client.send({
      to: mobile,  //手机号
      signature: SmsSignature, //你的短信签名 名称
      templateId: template,  //你的短信签名模版
      templateData: { code: codeNumber } //短信验证码
    })

    console.log('SMS Sent:', data)
    return data
  } catch (error) {
    console.error('Error sending SMS:', error)
    throw error
  }
}
// 生成指定位数的随机数
function generateRandomCode (length) {
  const min = Math.pow(10, length - 1)
  const max = Math.pow(10, length) - 1
  return Math.floor(Math.random() * (max - min + 1) + min).toString()
}

router.get('/uniSdk/MobileCodeNumber', async (req, res) => {
  console.log(`这是用户传递来的信息`, req.query)
  if (!req.query.mobile)
    return res.status(400).json({ code: 400, message: 'mobile参数必传' })
  try {
    const mobileNumber = req.query.mobile // 替换为实际的手机号码
    const verificationCode = generateRandomCode(6) // 生成或获取实际的验证码
    console.log(`验证码`, verificationCode)
    const TemplateContent = 'your sms template variate'
    await sendSMS(mobileNumber, TemplateContent, verificationCode)
    res.status(200).json({ code: 200, message: 'SMS sent successfully!' })
  } catch (error) {
    console.error(error)
    res.status(400).json({ code: 400, message: 'Failed to send SMS.' })
  }
})

module.exports = router

API接口实现

import express from 'express'
const router = express.Router()
import axios from 'axios'
// 替换为你的API账号信息

const apiAccessKeyId = 'your access key id'
const apiUrl = 'https://uni.apistd.com/'
const apiAction = 'sms.message.send'
const SmsSignature = 'your sms signature name'
// 'pub_verif_basic'
// 发送短信验证码的函数
async function sendSMS (mobile, template, codeNumber) {
  try {
    // url: apiUrl + '?action=' + apiAction + '&accessKeyId=' + apiAccessKeyId
    const { data } = await axios({
      url: apiUrl + '?action=' + apiAction + '&accessKeyId=' + apiAccessKeyId,
      method: 'post',
      data: {
        to: mobile, //手机号
        signature: SmsSignature, //短信签名 名称
        templateId: template, // 短信内容
        templateData: { code: codeNumber } //验证码
      } 
    })

    // 处理响应,根据需要进行进一步的处理
    console.log('SMS Sent:', data)
    return data
  } catch (error) {
    console.error('Error sending SMS:', error)
    throw error
  }
}
// 生成指定位数的随机数
function generateRandomCode (length) {
  const min = Math.pow(10, length - 1)
  const max = Math.pow(10, length) - 1
  return Math.floor(Math.random() * (max - min + 1) + min).toString()
}

router.get('/Test/MobileCodeNumber', async (req, res) => {
  console.log(`这是用户传递来的信息`, req.query)
  if (!req.query.mobile)
    return res.status(400).json({ code: 400, message: 'mobile参数必传' })
  try {
    const mobileNumber = req.query.mobile // 替换为实际的手机号码
    const verificationCode = generateRandomCode(6) // 生成或获取实际的验证码
    console.log(`验证码`, verificationCode)
    const TemplateContent = 'your sms Template variable'
    await sendSMS(mobileNumber, TemplateContent, verificationCode)
    res.status(200).json({ code: 200, message: 'SMS sent successfully!' })
  } catch (error) {
    console.error(error)
    res.status(400).json({ code: 400, message: 'Failed to send SMS.' })
  }
})
export default router

猜你喜欢

转载自blog.csdn.net/m0_64494670/article/details/134903708
今日推荐