vue项目引用QRCode生成二维码扫码实现app登录

vue项目引用QRCode生成二维码扫码实现app登录

效果如下
在这里插入图片描述

在这里插入图片描述

1、安装

npm install --save qrcode

2、template

<template>
    <div class="login-content_code">
        <h1>打开XXAPP扫描二维码登录</h1>
        <div class="qr-code-area">
            <img class="login-qr-code" :src='qrCodeData.loginQrCode'> // 放置二维码的地方
            <div class="cover" v-if="qrCodeData.loginQRCodeStatus != 'normal'"> // 二维码登录状态不正常时
                <div
                    v-if="qrCodeData.loginQRCodeStatus == 'timeout'" // 二维码超时
                    style="color:#333333;cursor:pointer"
                    @click="handle_refreshLoginQrCode"
                   >
                     二维码已过期
                    <br>
                     请点击刷新
                </div>
            </div>
        </div>
    </div>
</template>

2、step函数中

import QRCode from 'qrcode'
const qrCodeData = reactive({
    loginQRCodeStatus: 'notGet',    // 二维码登录状态 | normal 正常 | timeout 超时 | success 登录成功
    qrToken: "", 
    loginQrCode: "", // 二维码图片
    loginQrCodeTimer: 0, // 有效时间
    checkLoginTimer: 0, // 轮询时间
})

onMounted(()=>{
   getLoginQrCode()
}) // 初始二维码

async function getLoginQrCode() {

    let res = await axios.post({
       .....
    }) // 获取app扫码登录的二维码标识
    qrCodeData.qrToken = res.data // 将返回的data里的字符串生成二维码
    QRCode.toDataURL(JSON.stringify({
        content: qrCodeData.qrToken,
        copyright: "app",
        function: "pc_login",
        time: new Date().getTime()
    }))
    .then(url => {
        qrCodeData.loginQrCode = url // 生成的二维码
    })
    .catch(err => {
        console.error(err)
    })
    qrCodeData.loginQRCodeStatus = 'normal' // 将状态设置为正常
    qrCodeData.loginQrCodeTimer = setTimeout(() => {
        qrCodeData.loginQRCodeStatus = 'timeout'
        clearInterval(qrCodeData.loginQrCodeTimer)
	}, 1000 * 60 * 5) // 有效期5分钟 
	checkAppLogin()
}
function checkAppLogin () {
    if ( qrCodeData.loginQRCodeStatus === 'normal') {
        // 开启检测
        qrCodeData.checkLoginTimer = setTimeout(async () => { // 轮询
            // 发起扫app二维码登录的接口
           let res = axios.post({
                ......
            })
            ....// 根据返回的状态做相应的操作
            if (res.code === 211) {
                // 二维码已过期,请刷新
                qrCodeData.loginQRCodeStatus = 'timeout'
                return
            }
            if (res.code === 210) {
                // 继续检查是否被使用
                checkAppLogin()
                return
            }
            if (res.code === 200) {
                // 登录成功
                afterLogin(res)
                return
            }
            ....
            clearInterval(qrCodeData.checkLoginTimer)
        }, 1000) 
    }
    else {
        clearInterval(qrCodeData.checkLoginTimer)
    }
}
function handle_refreshLoginQrCode () {
	getLoginQrCode()
}
function afterLogin() {
..... // 登录成功后操作
}
  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Smile_zxx

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值