optimize auto login

main
htylight 2024-03-17 01:49:08 +08:00
parent 83b02b3e60
commit 9983222f4c
4 changed files with 69 additions and 73 deletions

View File

@ -31,27 +31,9 @@ final GoRouter router = GoRouter(
GoRoute(
path: '/welcome',
name: 'Welcome',
builder: (BuildContext context, GoRouterState state) =>
const WelcomeScreen(),
redirect: (context, state) async {
await getIt.get<Token>().init();
if (getIt.get<Token>().token.isNotEmpty) {
print(1111111);
try {
Map<String, dynamic> res = await signinByToken();
if (res['code'] == 10200) {
await getIt.get<Token>().updateToken(res['token']);
getIt.get<UserAccount>().init(res['data']);
await HiveDatabase.init();
return '/chat';
} else {
return null;
}
} catch (e) {
return null;
}
}
return null;
builder: (BuildContext context, GoRouterState state) {
String? isLogout = state.uri.queryParameters['isLogout'];
return WelcomeScreen(isLogout: isLogout);
},
),
GoRoute(
@ -63,10 +45,12 @@ final GoRouter router = GoRouter(
child: const SignupScreen(),
transitionDuration: const Duration(milliseconds: 200),
reverseTransitionDuration: const Duration(milliseconds: 200),
transitionsBuilder: (BuildContext context,
transitionsBuilder: (
BuildContext context,
Animation<double> animation,
Animation<double> secondaryAnimation,
Widget child) {
Widget child,
) {
const begin = Offset(1.0, 0.0);
const end = Offset.zero;
final tween = Tween(begin: begin, end: end);

View File

@ -69,7 +69,7 @@ class _ChatScreenState extends State<ChatScreen> with GetItStateMixin {
await _getUnreceivedMsg(userId);
}
return Future(() => true);
return true;
}
Future<void> _getUnreceivedMsg(String userId) async {

View File

@ -36,7 +36,7 @@ class SettingScreen extends StatelessWidget {
onPressed: () async {
await getIt.get<Token>().clear();
// ignore: use_build_context_synchronously
context.go('/welcome');
context.goNamed('Welcome', queryParameters: {'isLogout': '1'});
// ignore: use_build_context_synchronously
Timer(
const Duration(milliseconds: 200),

View File

@ -1,3 +1,5 @@
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
@ -12,7 +14,12 @@ import '../../models/user_model.dart';
import '../../request/signup_signin.dart';
class WelcomeScreen extends StatefulWidget {
const WelcomeScreen({super.key});
const WelcomeScreen({
super.key,
this.isLogout,
});
final String? isLogout;
@override
State<WelcomeScreen> createState() => _WelcomeScreenState();
@ -20,8 +27,12 @@ class WelcomeScreen extends StatefulWidget {
class _WelcomeScreenState extends State<WelcomeScreen> {
Future<int> _tryLoginUseToken() async {
if (widget.isLogout != null) {
return 200;
}
await getIt.get<Token>().init();
if (getIt.get<Token>().token.isNotEmpty) {
EasyLoading.showInfo('自动登录中...');
try {
Map<String, dynamic> res = await signinByToken();
if (res['code'] == 10200) {
@ -42,54 +53,55 @@ class _WelcomeScreenState extends State<WelcomeScreen> {
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: FutureBuilder<int>(
future: _tryLoginUseToken(),
builder: (BuildContext context, AsyncSnapshot<int> snapshot) {
if (snapshot.hasData) {
int? data = snapshot.data;
if (data == 10200) {
void initState() {
super.initState();
_tryLoginUseToken().then((value) {
if (value == 10200) {
EasyLoading.showSuccess(
'登录成功!',
duration: const Duration(milliseconds: 500),
duration: const Duration(seconds: 3),
dismissOnTap: true,
);
context.pushNamed('Chat');
} else if (data == 9999) {
} else if (value == 9999) {
EasyLoading.showInfo(
'登录状态已过期,请重新登录!',
duration: const Duration(milliseconds: 500),
duration: const Duration(seconds: 3),
dismissOnTap: true,
);
} else if (data == 500) {
} else if (value == 500) {
EasyLoading.showError(
'连接服务器失败,请稍后再试!',
duration: const Duration(milliseconds: 500),
duration: const Duration(seconds: 3),
dismissOnTap: true,
);
} else {
EasyLoading.dismiss();
}
} else {
EasyLoading.show(status: '自动登录中...');
});
}
return Column(
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Column(
children: [
const SizedBox(
height: kDefaultPadding,
),
Image.asset('assets/images/welcome_image.png'),
CommonElevatedButton(
onPressed: () => context.push('/signin'),
onPressed: () => context.pushNamed('SignIn'),
text: '登录',
),
CommonElevatedButton(
onPressed: () => context.push('/signup'),
onPressed: () => context.pushNamed('SignUp'),
text: '注册',
color: kSecondaryColor,
),
],
);
}),
),
),
);
}