基于轮询或长连接的扫码功能实现

本文介绍了一种基于轮询和websocket长连接的扫码功能实现。用户A生成二维码后,用户B扫码并等待A授权。通过轮询方式,双方持续查询码状态,而websocket提供实时通讯。文中详细阐述了两种方法的具体实现流程,并提供了模拟测试的效果展示。
摘要由CSDN通过智能技术生成

需求:用户A进入小程序生成一个二维码,用户B用手机扫用户A的二维码,扫码成功后用户B需要等待用户A进行确认授权,用户A确认授权后,用户B的页面出现用户A的二维码的信息。

解决方案:1、轮询方式;2、websocket长连接方式(有些浏览器不兼容websocket)

一、轮询方式

流程图
轮询扫码流程
具体实现

亮码者生成二维码后,增加轮询调用查询码状态接口

    //小程序端轮询查看码状态
    client() {
      var _this = this;
      var code = this.$route.query.code;
      axios
        .get("/personal/info/codeStatus?code=" + code)
        .then(function (response) {
          console.log(response.data.data);
          setTimeout(function () {
            if (response.data.data == 1) {//状态为1说明还没有被扫描
              _this.client();
            } else {
              axios
                .get("/personal/info/doScan?code=" + code)
                .then(function (response) {
                  console.log(response.data.data);
                })
                .catch(function (error) {
                  console.log(error);
                });
            }
          }, 1500);
        })
        .catch(function (error) {
          console.log(error);
        });
    },

扫码者扫码之后也增加轮询调用查询码状态接口

    //扫码端轮询查看码状态
    server() {
      var _this = this;
      var code = this.$route.query.code;
      axios
        .get("/personal/info/codeStatus?code=" + code)
        .then(function (response) {
          console.log(response.data.data);
          setTimeout(function () {
            if (response.data.data == 2) {//状态为2说明小程序端还没有确认
              _this.server();
            } else {
              axios
                .get("/personal/info/doScan?code=" + code)
                .then(function (response) {
                  console.log(response.data.data);
                })
                .catch(function (error) {
                  console.log(error);
                });
            }
          }, 1500);
        })
        .catch(function (error) {
          console.log(error);
        });
    },

生成码接口

	@CrossOrigin(origins ="*") //解决跨域问题,可以用全局配置
	@ApiOperation(value = "用户亮码")
    @PostMapping("/createCode")
    public String createCode(@RequestBody CreateCodeVo createCodeVo) throws IOException {
   
        if (ObjectUtils.allNotNull(createCodeVo)) {
   
            String encrypt = AESUtil.encrypt(String.valueOf(createCodeVo.getUserId()));
            System.out.println(encrypt);
            final BufferedImage bufferedImage = QRCodeUtil.createCodeWithLogo("http://192.168.1.104:8080/#/?code="+encrypt, "", -1, -1, -1);
            if (bufferedImage != null) {
   
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                ImageIO.write(bufferedImage, "png", baos);
                byte[] bytes = baos.toByteArray();
                BASE64Encoder encoder = new BASE64Encoder();
                String pngBase64 = encoder.encodeBuffer(bytes);
                baos.close();
                pngBase64 = pngBase64.replaceAll("\n", "").replaceAll("\r", "")
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值