Use node+redis to develop registration interface through SMS verification code

Introduction: Use node+redis to develop registration interface development through mobile phone SMS verification code

  • Receive the mobile phone number and verification code from the control layer at the data layer for logical processing. The specific code is as follows:
    // service/UserService.js
    const UserService = {
          
          
        register:async (phone,code)=>{
          
          
            // 手机号注册查重
            let existPhone = await DB.Account.findAll({
          
          where:{
          
          phone}})
            if(existPhone.length>0) {
          
          
                return {
          
          code:-1,msg:'手机号已经注册'}
            }
            // 获取redis中的验证码和用户传入的对比是否一致
            if(await redisConfig.exists('register:code:'+phone)){
          
          
                let codeRes = (await redisConfig.get('register:code:'+phone)).split('_')[1]
                if(!(code==codeRes)) {
          
          
                    return {
          
          code:-1,msg:'短信验证码不正确'}
                }
            } else {
          
          
                return {
          
          code:-1,msg:'请先获取短信验证码'}
            }
    
            // 随机生成头像和昵称
            let avatar = RandomTool.randomAvatar()
            let name = RandomTool.randomName()
    
            // 生成token,7天后过期
            let user = {
          
          avatar,name,phone}
            let token = SecretTool.jwtSign(user,'168h')
    
            // 将用户信息插入数据库
            await DB.Account.create({
          
          username:name,head_img:avatar,phone})
            return {
          
          code:0,data:`Bearer${
            
            token}`}
        }
    }
    
  • Define a function to randomly generate avatars and names in utils
    	// utils/randomTool.js
    	// 随机生成头像
    static randomAvatar() {
          
          
    let imgList = [
      "https://xd-video-pc-img.oss-cn-beijing.aliyuncs.com/xdclass_pro/default/head_img/10.jpeg",
      "https://xd-video-pc-img.oss-cn-beijing.aliyuncs.com/xdclass_pro/default/head_img/11.jpeg",
      "https://xd-video-pc-img.oss-cn-beijing.aliyuncs.com/xdclass_pro/default/head_img/12.jpeg",
      "https://xd-video-pc-img.oss-cn-beijing.aliyuncs.com/xdclass_pro/default/head_img/13.jpeg",
      "https://xd-video-pc-img.oss-cn-beijing.aliyuncs.com/xdclass_pro/default/head_img/14.jpeg",
      "https://xd-video-pc-img.oss-cn-beijing.aliyuncs.com/xdclass_pro/default/head_img/15.jpeg",
      "https://xd-video-pc-img.oss-cn-beijing.aliyuncs.com/xdclass_pro/default/head_img/16.jpeg",
      "https://xd-video-pc-img.oss-cn-beijing.aliyuncs.com/xdclass_pro/default/head_img/17.jpeg",
      "https://xd-video-pc-img.oss-cn-beijing.aliyuncs.com/xdclass_pro/default/head_img/18.jpeg",
      "https://xd-video-pc-img.oss-cn-beijing.aliyuncs.com/xdclass_pro/default/head_img/19.jpeg",
    ];
    let num = Math.floor(Math.random() * 10);
    return imgList[num];
    }
    
    // 随机生成昵称
    static randomName() {
          
          
    let name = [
      "编程小白23423",
      "编程小白94352",
      "编程小白46597",
      "编程小白46236",
      "编程小白73453",
      "编程小白07848",
      "编程小白44462",
      "编程小白36688",
      "编程小白23665",
      "编程小白84562",
    ];
    let num = Math.floor(Math.random() * 10);
    return name[num];
    }
    
  • The control layer receives the mobile phone number and verification code passed by the front end and calls the method of the data layer to pass it over for logical processing
    	const UserService = require('../service/UserService.js')
    	
    	const UserController = {
          
          
    	    register:async (req,res)=>{
          
          
    	        let {
          
          phone,code} = req.body 
    	        let handleRes =  await UserService.register(phone,code)
    	        res.send(handleRes)
    	    }
    	}
    	
    	module.exports = UserController
    
  • You're done, you can test it on APIFOX, or call it to the front end. You need to pay attention to the following points:
    • The front-end page needs to fill in the mobile phone number and verification code first
    • The validity period of the verification code is 120 seconds, which can be defined by yourself
    • A mobile phone that has already been registered cannot be registered again
    • The following is a screenshot of successful registration, for reference only, please feel free to private message me if you have any questions!
      insert image description here

Guess you like

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