Use nodejs+redis+mysql to develop WeChat QR code polling interface

Introduction: How does the client know when the user scans the code? Develop polling interface

  • process
    insert image description here
  • Create polling interface
    http://127.0.0.1:8081/api/wx_login/v1/checkScan
    
    请求方法:get
    请求参数:ticket
    
    • Add an interface path to the wxLogin.js file in the router file of the project
      const wxLoginController = require('../controller/WxLoginController')
      // 轮询用户是否扫码
      router.get('/checkScan',wxLoginController.checkScan)
      
    • Receive the data passed by the front-end call interface at the control layer, and return it to the front-end after processing
      const WxLoginService = require('../service/WxLoginService')
      
        checkScan: async (req,res)=>{
              
              
          let handleRes = await WxLoginService.checkScan(req)
          res.send(handleRes)
        }
      }
      
      • In the data layer, query whether the isScan field of the redis cache is changed to yes. If so, it means that the user has already scanned the code, and then returns the token to the front end for storage. If not, it prompts the front end to wait for the user to scan the code.
      const redisConfig = require("../config/redisConfig");
      const BackCode = require("../utils/BackCode");
      const CodeEnum = require('../utils/CodeEnum')
      
      checkScan: async (req) => {
              
              
        let {
              
              ticket} = req.query
        let key = `wechat:ticket:${
                
                ticket}`
        let redisData = JSON.parse(await redisConfig.get(key))
        if(redisData && redisData.isScan == 'yes') {
              
              
          let {
              
              token} = redisData
          return BackCode.buildSuccessAndData({
              
              data:`Bearer${
                
                token}`})
        }else {
              
              
          return BackCode.buildResult(CodeEnum.WECHAT_WAIT_SCAN)
        }
      },
      
  • Thanks to the high level, you can test the interface on APIFOX, the interface address: /api/wxLogin/v1/checkScan
    insert image description here

Guess you like

Origin blog.csdn.net/u011313034/article/details/131189983
Recommended