修复用户端令牌失效后的登录恢复
This commit is contained in:
@@ -3,14 +3,22 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
|
||||
import '../../../app/auth_navigation.dart';
|
||||
import '../../../app/dependencies.dart';
|
||||
import '../../../data/services/api_client.dart';
|
||||
|
||||
/// 用户端手机号密码登录页面。
|
||||
class LoginPage extends StatefulWidget {
|
||||
const LoginPage({required this.session, super.key});
|
||||
const LoginPage({
|
||||
required this.session,
|
||||
this.redirectTarget,
|
||||
this.showSessionExpiredMessage = false,
|
||||
super.key,
|
||||
});
|
||||
|
||||
final UserSession session;
|
||||
final String? redirectTarget;
|
||||
final bool showSessionExpiredMessage;
|
||||
|
||||
@override
|
||||
State<LoginPage> createState() => _LoginPageState();
|
||||
@@ -27,6 +35,14 @@ class _LoginPageState extends State<LoginPage> {
|
||||
String? _phoneError;
|
||||
bool _obscurePassword = true;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
if (widget.showSessionExpiredMessage) {
|
||||
_error = '登录状态已失效,请重新登录';
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_phone.dispose();
|
||||
@@ -174,7 +190,13 @@ class _LoginPageState extends State<LoginPage> {
|
||||
: const Text('安全登录'),
|
||||
),
|
||||
TextButton(
|
||||
onPressed: () => context.push('/register'),
|
||||
onPressed: () => context.push(
|
||||
buildAuthLocation(
|
||||
'/register',
|
||||
redirectTarget: widget.redirectTarget,
|
||||
sessionExpired: widget.showSessionExpiredMessage,
|
||||
),
|
||||
),
|
||||
child: const Text('首次使用?注册账号'),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
|
||||
@@ -1,12 +1,24 @@
|
||||
// 功能描述:提供用户注册流程,并在注册后保留原登录目标页。
|
||||
// 版本:1.1.0
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
|
||||
import '../../../app/auth_navigation.dart';
|
||||
import '../../../app/dependencies.dart';
|
||||
import '../../../data/services/api_client.dart';
|
||||
|
||||
/// 用户端账号注册页面。
|
||||
class RegisterPage extends StatefulWidget {
|
||||
const RegisterPage({required this.session, super.key});
|
||||
const RegisterPage({
|
||||
required this.session,
|
||||
this.redirectTarget,
|
||||
this.showSessionExpiredMessage = false,
|
||||
super.key,
|
||||
});
|
||||
|
||||
final UserSession session;
|
||||
final String? redirectTarget;
|
||||
final bool showSessionExpiredMessage;
|
||||
|
||||
@override
|
||||
State<RegisterPage> createState() => _RegisterPageState();
|
||||
@@ -51,7 +63,19 @@ class _RegisterPageState extends State<RegisterPage> {
|
||||
'request_identity': _requestIdentity,
|
||||
},
|
||||
);
|
||||
if (mounted) Navigator.of(context).pop();
|
||||
if (mounted) {
|
||||
if (Navigator.of(context).canPop()) {
|
||||
Navigator.of(context).pop();
|
||||
} else {
|
||||
context.go(
|
||||
buildAuthLocation(
|
||||
'/login',
|
||||
redirectTarget: widget.redirectTarget,
|
||||
sessionExpired: widget.showSessionExpiredMessage,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
if (mounted) setState(() => _message = error.toString());
|
||||
} finally {
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../../../data/repositories/client_repository.dart';
|
||||
import '../../../data/services/api_client.dart';
|
||||
import '../../../domain/models/client_models.dart';
|
||||
import '../../core/widgets.dart';
|
||||
import 'service_relation_card.dart';
|
||||
@@ -47,6 +48,9 @@ class _HomePageState extends State<HomePage> {
|
||||
return const Center(child: CircularProgressIndicator());
|
||||
}
|
||||
if (snapshot.hasError) {
|
||||
if (snapshot.error is SessionExpiredException) {
|
||||
return const SizedBox.shrink();
|
||||
}
|
||||
return EmptyState(
|
||||
title: '首页加载失败',
|
||||
description: snapshot.error.toString(),
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
// 功能描述:展示用户订单、退款和工单,并处理显式确认后的业务操作。
|
||||
// 版本:1.1.0
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:uuid/uuid.dart';
|
||||
|
||||
import '../../../data/repositories/client_repository.dart';
|
||||
import '../../../data/services/api_client.dart';
|
||||
import '../../../data/services/payment_launcher.dart';
|
||||
import '../../../domain/models/client_models.dart';
|
||||
import '../shared/record_list_page.dart';
|
||||
@@ -93,6 +96,7 @@ class OrdersPage extends StatelessWidget {
|
||||
);
|
||||
}
|
||||
} catch (error) {
|
||||
if (error is SessionExpiredException) return;
|
||||
if (context.mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text(error.toString())),
|
||||
|
||||
@@ -8,6 +8,7 @@ import 'package:uuid/uuid.dart';
|
||||
|
||||
import '../../../app/dependencies.dart';
|
||||
import '../../../data/repositories/client_repository.dart';
|
||||
import '../../../data/services/api_client.dart';
|
||||
import '../../../domain/models/client_models.dart';
|
||||
import '../../core/widgets.dart';
|
||||
import 'profile_avatar.dart';
|
||||
@@ -65,7 +66,11 @@ class _ProfilePageState extends State<ProfilePage> {
|
||||
);
|
||||
controller.dispose();
|
||||
if (address == null || address.isEmpty) return;
|
||||
await widget.repository.addAddress(address, isDefault: true);
|
||||
try {
|
||||
await widget.repository.addAddress(address, isDefault: true);
|
||||
} on SessionExpiredException {
|
||||
return;
|
||||
}
|
||||
if (mounted) ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text('地址已保存')));
|
||||
}
|
||||
|
||||
@@ -91,11 +96,15 @@ class _ProfilePageState extends State<ProfilePage> {
|
||||
);
|
||||
controller.dispose();
|
||||
if (description == null || description.isEmpty) return;
|
||||
await widget.repository.createTicket(
|
||||
requestNo: const Uuid().v7(),
|
||||
category: 'repair',
|
||||
description: description,
|
||||
);
|
||||
try {
|
||||
await widget.repository.createTicket(
|
||||
requestNo: const Uuid().v7(),
|
||||
category: 'repair',
|
||||
description: description,
|
||||
);
|
||||
} on SessionExpiredException {
|
||||
return;
|
||||
}
|
||||
if (mounted) ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text('工单已提交')));
|
||||
}
|
||||
|
||||
@@ -107,6 +116,9 @@ class _ProfilePageState extends State<ProfilePage> {
|
||||
builder: (context, snapshot) {
|
||||
if (!snapshot.hasData) {
|
||||
if (snapshot.hasError) {
|
||||
if (snapshot.error is SessionExpiredException) {
|
||||
return const SizedBox.shrink();
|
||||
}
|
||||
return EmptyState(
|
||||
title: '个人信息加载失败',
|
||||
description: '请检查网络后重新进入本页',
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
// 功能描述:管理通用记录列表的加载、数据和非鉴权错误状态。
|
||||
// 版本:1.1.0
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
||||
import '../../../data/services/api_client.dart';
|
||||
import '../../../domain/models/client_models.dart';
|
||||
|
||||
typedef RecordLoader = Future<List<ClientRecord>> Function();
|
||||
@@ -23,7 +26,8 @@ class RecordListViewModel extends ChangeNotifier {
|
||||
try {
|
||||
_records = await _loader();
|
||||
} catch (error) {
|
||||
_error = error;
|
||||
// 会话失效由统一路由接管,不在列表中重复展示网络错误。
|
||||
if (error is! SessionExpiredException) _error = error;
|
||||
} finally {
|
||||
_loading = false;
|
||||
notifyListeners();
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
// 功能描述:展示商城商品并处理用户确认后的下单流程。
|
||||
// 版本:1.1.0
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:uuid/uuid.dart';
|
||||
|
||||
import '../../../data/repositories/client_repository.dart';
|
||||
import '../../../data/services/api_client.dart';
|
||||
import '../../../domain/models/client_models.dart';
|
||||
import '../../core/widgets.dart';
|
||||
|
||||
@@ -80,6 +83,7 @@ class _ShopPageState extends State<ShopPage> {
|
||||
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text('订单已创建,请前往订单页支付')));
|
||||
}
|
||||
} catch (error) {
|
||||
if (error is SessionExpiredException) return;
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text(error.toString())));
|
||||
}
|
||||
@@ -96,6 +100,9 @@ class _ShopPageState extends State<ShopPage> {
|
||||
return const Center(child: CircularProgressIndicator());
|
||||
}
|
||||
if (snapshot.hasError) {
|
||||
if (snapshot.error is SessionExpiredException) {
|
||||
return const SizedBox.shrink();
|
||||
}
|
||||
return EmptyState(title: '商品加载失败', description: snapshot.error.toString());
|
||||
}
|
||||
final products = snapshot.data ?? const [];
|
||||
|
||||
Reference in New Issue
Block a user