Use nodejs to access and encapsulate third-party SMS verification code tool classes

Introduction: Use nodejs to access and encapsulate third-party SMS verification code tool classes

  • Third-party SMS operator access

    • install axios
      yarn add axios@1.1.3
      
  • configuration

    • Create a new file in the config folder of the project, and name it the name of the access SMS verification code platform, such as aliyunMessage.js. The specific configuration is as follows
      	const axios = require('axios')
      	const sendMsgCode = (phone,randomCode) => {
              
              
      		return axios({
              
              
         		 	method:'post',
         			url:`http(s)://jmsms.market.alicloudapi.com/sms/send`,  //短信验证码平台接入的url
          		data:{
              
              
              		appid:'ATB3w4hJl7xqs9P2ty',   //appid可以在开通的短信验证码平台控制台即可查到
             			appSecret:'J5ByBB287o1Cipvc01U2eOgV0diXYuT8',  // appSecret可以在开通的短信验证码平台控制台即可查到
              		code:randomCode,  //要发送的随机数,需要自己定义
              		phoneNum:phone,  // 接收短信验证码的手机号
              		templateCode: "SMS_168781429"  // 额外的参数,需要结合短信验证码平台来编写,如不需要去掉即可
          		}
      		})
      	}
      	// 测试调试发送短信
      	(async()=>{
              
              
      		console.log((await sendMsgCode(13800138000,'5201')).data);
      	})()
      	// 进行导出
      	module.exports = sendMsgCode;
      
  • Randomly generate four-digit packages (path: utils/randomTool.js)

    class RandomTool{
          
          
    	static randomCode(){
          
          
        	return Math.floor(Math.random()*(9999-1000)+1000)
    	}
    }
    module.exports = RandomTool
    
  • Data layer code logic

    • Optimization scheme (Scheme 2)
      • The atomicity of two redis operations must be guaranteed: the atomicity of a transaction means that the database executes multiple operations in the transaction as a whole, and the server either executes all the operations in the transaction, or executes none of them .
      • while reducing namespace waste
            // 获取手机验证码逻辑处理
        sendCode: async (phone,captcha,type,key,randomCode) =>{
                  
                  
            // 60秒内不能重新获取
            // ------>方案一
            // if(await redisConfig.exists(`${type}:over:`+phone)) {
                  
                  
            //     return { code: -1, msg: '60秒内不能重复获取'}
            // }
        
            // ------->方案二
            if(await redisConfig.exists(`${
                    
                    type}:code:`+phone)){
                  
                  
                let dateRedis = dayjs(Number((await redisConfig.get(`${
                    
                    type}:code:`+phone)).split('_')[0]))
                if(dayjs(Date.now()).diff(dateRedis,'second')<=60) {
                  
                  
                    return {
                  
                  code:-1,msg:'60秒内不能重复获取'}
                }
            }
        
            // 是否已经填写图形验证码
            if (!(await redisConfig.exists(`${
                    
                    type}:captcha:`+key))) {
                  
                  
                return {
                  
                   code: -1, msg: '请发送图形验证码' }
              }
        
            // 对比用户的图形验证码和redis缓存中的是否一致
            let captchaRedis = await redisConfig.get(`${
                    
                    type}:captcha:`+key)
            if(!(captcha.toLowerCase() === captchaRedis.toLowerCase())) {
                  
                  
                return {
                  
                   code: -1,msg:'图形验证码填写错误'}
            }
        
            // 发送手机验证码
            let CodeRes = (await aliyunMessage(phone,randomCode)).data
            
            // ------->方案一
            // // 验证码存入redis
            // redisConfig.set(`${type}:code`+phone,randomCode,600)
        
            // // 存60秒判断
            // redisConfig.set(`${type}:over:`+phone,'1',60)
        
            // ------->方案二
            // 获取当前时间拼接验证码
            let randomCodeTime = `${
                    
                    Date.now()}_${
                    
                    randomCode}`
            redisConfig.set(`${
                    
                    type}:code:`+phone,randomCodeTime,600)
        
            // 删除图形验证码
            redisConfig.del(`${
                    
                    type}:captcha:`+key)
        
            if(CodeRes.code == 0) return {
                  
                  code:0,msg:'验证码发送成功'}
            else {
                  
                  return {
                  
                  code:-1,msg:'验证码发送失败'}}
        }
        
  • Encrypt the user's ip and device with md5, file path: controller/NotifyController.js

    const NotifyService = require('../service/NotifyService')
    const GetUserInfoTool = require('../utils/GetUserInfoTool')
    const SecretTool = require('../utils/SecretTool')
    const randomTool = require('../utils/randomTool')
    
    const NotifyController = {
          
          
        sendCode: async(req,res)=>{
          
          
            let {
          
          phone,captcha,type}  = req.body;
            // 用户的ip+设备md5加密
            let _key = SecretTool.md5(GetUserInfoTool.getIp(req)+GetUserInfoTool.getUserAgent(req))
            let handleRes = await NotifyService.sendCode(phone,captcha,type,_key,randomTool.randomCode())
            res.send(handleRes)
        }
    }
    
    module.exports = NotifyController
    
  • Write the interface path in the router folder and the method to call

    const express = require('express')
    const router = express.Router()
    const NotifyController = require('../controller/NotifyController')
    
    // 图形验证码接口
    router.get('/captcha',NotifyController.captcha)
    
    // 手机验证码接口
    router.post('/sendCode',NotifyController.sendCode)
    
    module.exports = router
    
  • You're done, you can call it on apifox, you need to request the graphic verification code interface first, how to write the graphic verification code can be seen in my previous articles
    insert image description here

  • You will also receive a corresponding SMS verification code on your mobile phone.
    insert image description here

Guess you like

Origin blog.csdn.net/u011313034/article/details/131040808