二维码跳转登录

This commit is contained in:
orangebabu
2024-08-18 15:30:05 +08:00
parent 8d683965a5
commit bab15aee23
18 changed files with 870 additions and 163 deletions

View File

@@ -61,6 +61,8 @@ export class UserLoginComponent implements OnInit, OnDestroy {
state = '';
count = 0;
interval$: any;
//二维码内容
ticket = '';
constructor(
fb: FormBuilder,
@@ -298,25 +300,63 @@ export class UserLoginComponent implements OnInit, OnDestroy {
});
}
/**
* 获取二维码
*/
getLoginQrCode() {
this.qrexpire = false;
this.qrCodeService.getLoginQrCode().subscribe(res => {
if (res.code === 0 && res.data.rqCode) { // 使用返回的 rqCode
const qrImageElement = document.getElementById('div_qrcodelogin');
this.ticket = res.data.ticket;
if (qrImageElement) {
qrImageElement.innerHTML = `<img src="${res.data.rqCode}" alt="QR Code" style="width: 200px; height: 200px;">`;
}
// 设置分钟后 qrexpire 为 false
/* // 设置5分钟后 qrexpire 为 false
setTimeout(() => {
this.qrexpire = true;
this.cdr.detectChanges(); // 更新视图
}, 3 * 60 * 1000); // 180000 毫秒 = 3 分钟
}, 5 * 60 * 1000); // 5 分钟*/
this.loginByQrCode();
}
});
}
/**
* 二维码轮询登录
*/
loginByQrCode() {
const interval = setInterval(() => {
this.qrCodeService.loginByQrCode({
authType: 'scancode',
code: this.ticket,
}).subscribe(res => {
if (res.code === 0) {
this.qrexpire = true;
// 清空路由复用信息
this.reuseTabService.clear();
// 设置用户Token信息
this.authnService.auth(res.data);
this.authnService.navigate({});
} else if (res.code === 20004) {
this.qrexpire = true;
}
// Handle response here
// If you need to stop the interval after a certain condition is met,
// you can clear the interval like this:
if (this.qrexpire) {
clearInterval(interval);
}
this.cdr.detectChanges(); // 更新视图
});
}, 5 * 1000); // 5 seconds
}
getQrCode(): void {

View File

@@ -15,7 +15,7 @@
*/
import { Injectable } from '@angular/core';
import { SettingsService, _HttpClient, User } from '@delon/theme';
import { _HttpClient } from '@delon/theme';
@Injectable({
providedIn: 'root'
@@ -26,4 +26,8 @@ export class QrCodeService {
getLoginQrCode() {
return this.http.get('/login/genScanCode');
}
loginByQrCode(authParam: any) {
return this.http.post('/login/sign/qrcode', authParam);
}
}