feat: web端扫码登录

This commit is contained in:
guozhigq
2024-06-08 03:14:37 +08:00
parent 41e491daef
commit 976f0c7bec
6 changed files with 190 additions and 23 deletions

View File

@ -42,6 +42,10 @@ class LoginPageController extends GetxController {
late int tel;
late int webSmsCode;
RxInt validSeconds = 180.obs;
late Timer validTimer;
late String qrcodeKey;
// 监听pageView切换
void onPageChange(int index) {
currentIndex.value = index;
@ -298,4 +302,35 @@ class LoginPageController extends GetxController {
}
});
}
// 获取登录二维码
Future getWebQrcode() async {
var res = await LoginHttp.getWebQrcode();
validSeconds.value = 180;
if (res['status']) {
qrcodeKey = res['data']['qrcode_key'];
validTimer = Timer.periodic(const Duration(seconds: 1), (validTimer) {
if (validSeconds.value > 0) {
validSeconds.value--;
queryWebQrcodeStatus();
} else {
getWebQrcode();
validTimer.cancel();
}
});
return res;
} else {
SmartDialog.showToast(res['msg']);
}
}
// 轮询二维码登录状态
Future queryWebQrcodeStatus() async {
var res = await LoginHttp.queryWebQrcodeStatus(qrcodeKey);
if (res['status']) {
await LoginUtils.confirmLogin('', null);
validTimer.cancel();
Get.back();
}
}
}

View File

@ -1,5 +1,6 @@
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:qr_flutter/qr_flutter.dart';
import 'controller.dart';
@ -37,6 +38,105 @@ class _LoginPageState extends State<LoginPage> {
icon: const Icon(Icons.arrow_back),
),
),
actions: [
IconButton(
tooltip: '浏览器打开',
onPressed: () {
Get.offNamed(
'/webview',
parameters: {
'url': 'https://passport.bilibili.com/h5-app/passport/login',
'type': 'login',
'pageTitle': '登录bilibili',
},
);
},
icon: const Icon(Icons.language),
),
IconButton(
tooltip: '二维码登录',
onPressed: () {
showDialog(
context: context,
builder: (context) {
return StatefulBuilder(
builder: (context, StateSetter setState) {
return AlertDialog(
title: Row(
children: [
const Text('扫码登录'),
IconButton(
onPressed: () {
setState(() {});
},
icon: const Icon(Icons.refresh),
),
],
),
contentPadding: const EdgeInsets.fromLTRB(0, 0, 0, 4),
content: AspectRatio(
aspectRatio: 1,
child: Container(
width: 200,
padding: const EdgeInsets.all(12),
child: FutureBuilder(
future: _loginPageCtr.getWebQrcode(),
builder: (context, snapshot) {
if (snapshot.connectionState ==
ConnectionState.done) {
if (snapshot.data == null) {
return const SizedBox();
}
Map data = snapshot.data as Map;
return QrImageView(
data: data['data']['url'],
backgroundColor: Colors.transparent,
);
} else {
return const Center(
child: SizedBox(
width: 40,
height: 40,
child: CircularProgressIndicator(),
),
);
}
},
),
),
),
actions: [
TextButton(
onPressed: () {},
child: Obx(() {
return Text(
'有效期: ${_loginPageCtr.validSeconds.value}s',
style: Theme.of(context).textTheme.titleMedium,
);
}),
),
TextButton(
onPressed: () {},
child: Text(
'检查登录状态',
style: TextStyle(
fontSize: Theme.of(context)
.textTheme
.titleMedium!
.fontSize,
),
),
)
],
);
});
},
);
},
icon: const Icon(Icons.qr_code),
),
const SizedBox(width: 22),
],
),
body: PageView(
physics: const NeverScrollableScrollPhysics(),
@ -99,27 +199,6 @@ class _LoginPageState extends State<LoginPage> {
},
),
),
GestureDetector(
onTap: () {
Get.offNamed(
'/webview',
parameters: {
'url':
'https://passport.bilibili.com/h5-app/passport/login',
'type': 'login',
'pageTitle': '登录bilibili',
},
);
},
child: Padding(
padding: const EdgeInsets.only(left: 2),
child: Text(
'使用网页端登录',
style: TextStyle(
color: Theme.of(context).colorScheme.primary),
),
),
),
const Spacer(),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,