import { ElButton, ElIcon, ElLink } from 'element-plus' import QrcodeVue from 'qrcode.vue' import { defineComponent } from 'vue' import styles from './index.module.less' import Cookies from 'js-cookie' import request from '@/helpers/request' import { state } from '@/state' import { getCodeBaseUrl, setAuth } from '@/helpers/utils' import { CircleCheck } from '@element-plus/icons-vue' import logoIco from '@/common/images/logo.png' export const getAssetsHomeFile = (fileName: string) => { const path = `../../images/${fileName}` const modules = import.meta.globEager('../../images/*') return modules[path].default } export default defineComponent({ name: 'qrCode', props: { loginType: { type: String }, onChange: { type: Function, default: (type: string) => {} }, onClose: { type: Function, default: () => {} } }, data() { return { qrCode: '', isScan: false, // 是否扫码 scanCode: '', codeTimerOut: false, // 是否超时或登录过期 // 状态 no_scan 未扫码 scanned 已扫码 succeed 登录成功 filed 登录失败 codeStatus: 'no_scan' as 'no_scan' | 'scanned' | 'succeed' | 'filed' // 扫码状态 } }, async mounted() { try { const scanCode = sessionStorage.getItem('scanCode') await this.getCode(scanCode) const str = this.loginType === 'TEACHER' ? 'teacher' : 'student' this.qrCode = `${getCodeBaseUrl(`/${str}`)}/#/scanLogin?code=${ this.scanCode }` console.log(this.qrCode) state.loginPopupTimer = setInterval(async () => { await this.getList() }, 5000) } catch { // } }, methods: { async getCode(code?: string) { try { const res = await request.get('/api-auth/getQRLoginCode', { params: { clientId: 'website', clientSecret: 'website', code } }) this.scanCode = res.data.code this.codeStatus = res.data.codeStatus sessionStorage.setItem('scanCode', res.data.code) } catch {} }, async getList() { try { console.log(this.scanCode) const res = await request.get('/api-auth/pollingQRLoginCode', { params: { code: this.scanCode } }) console.log(res, 'getlist') const result = res.data // 判断是否有数据,如果没有数据则认定为超时 if (!result) { this.codeTimerOut = true this.removeTimer() return } this.codeStatus = result.codeStatus if (result.codeStatus === 'succeed') { this.removeTimer() const { authentication, userType } = result const token = authentication.token_type + ' ' + authentication.access_token setAuth( JSON.stringify({ token, loginUserType: userType }) ) this.onClose() window.location.reload() } else if (result.codeStatus === 'filed') { this.removeTimer() } else if (result.codeStatus === 'scanned') { this.isScan = true } } catch { console.log('error') this.codeTimerOut = true this.removeTimer() } }, removeTimer() { // 取消定时器 this.codeStatus = 'no_scan' this.isScan = false clearInterval(state.loginPopupTimer) } }, render() { return (
扫码成功
请在手机上根据提示确认登录
二维码已失效
打开酷乐秀学生端APP
扫一扫登录