customer_service_provider.dart 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_riverpod/flutter_riverpod.dart';
  3. import 'package:go_router/go_router.dart';
  4. import '../core/config/app_config.dart';
  5. import '../core/l10n/app_localizations.dart';
  6. import '../data/repositories/auth_repository.dart';
  7. import '../presentation/screens/customer_service/customer_service_webview.dart';
  8. import 'auth_provider.dart';
  9. import 'profile_provider.dart';
  10. /// 打开客服聊天(H5 WebView 方式),未登录时跳转登录页
  11. Future<void> openCustomerService(BuildContext context, WidgetRef ref) async {
  12. if (!AppConfig.customerServiceEnabled) {
  13. return;
  14. }
  15. final isLoggedIn = ref.read(isLoggedInProvider);
  16. if (!isLoggedIn) {
  17. context.push('/login');
  18. return;
  19. }
  20. // 提前读取 l10n,避免跨异步使用 BuildContext
  21. final l10n = AppLocalizations.of(context)!;
  22. // 等待 profile 加载完成,避免用占位 uid 打开客服
  23. var profileState = ref.read(profileProvider);
  24. if (profileState.isLoadingProfile) {
  25. int waited = 0;
  26. while (ref.read(profileProvider).isLoadingProfile && waited < 30) {
  27. await Future.delayed(const Duration(milliseconds: 100));
  28. waited++;
  29. }
  30. profileState = ref.read(profileProvider);
  31. }
  32. final profile = profileState.user;
  33. final userName = profile.rawEmail.isNotEmpty ? profile.rawEmail : l10n.user;
  34. // uid 优先用 profile,为空时从登录时写入的 secure storage 兜底
  35. String userId = profile.uid;
  36. if (userId.isEmpty) {
  37. userId = await ref.read(authRepositoryProvider).getSavedUid() ?? '';
  38. }
  39. if (!context.mounted) return;
  40. // 以 Modal BottomSheet 形式展示七陌 H5 客服
  41. showModalBottomSheet<void>(
  42. context: context,
  43. useRootNavigator: true,
  44. isScrollControlled: true,
  45. enableDrag: false,
  46. backgroundColor: Colors.transparent,
  47. barrierColor: Colors.black.withAlpha(102), // 50% 透明度
  48. builder: (BuildContext bottomSheetContext) {
  49. return SizedBox(
  50. height: MediaQuery.of(bottomSheetContext).size.height * 0.85,
  51. child: CustomerServiceWebView(
  52. userId: userId,
  53. userName: userName,
  54. ),
  55. );
  56. },
  57. );
  58. }