fix: 修复手机连不上服务器时启动页无限转圈(B-58)
- 启动页先读本地凭证:无凭证(首次打开)直接进登录页,不再先等 SDK 初始化 - SDK init/login 加 20s 超时;init 用共享 Future 防止超时后重复触发 initSDK - 登录页对 IM 登录超时给出明确提示(确认手机连着公司内网 WiFi) - 修复 flutter create 模板遗留的 widget_test(引用不存在的 MyApp),改为登录页渲染冒烟测试 - 版本号 1.0.1+2
This commit is contained in:
+16
-10
@@ -77,18 +77,24 @@ class _SplashGateState extends State<SplashGate> {
|
||||
|
||||
Future<void> _boot() async {
|
||||
final im = IMService.instance;
|
||||
// 先读本地凭证:没有凭证(首次打开)直接去登录页,
|
||||
// 不在启动页等 SDK 初始化——服务器连不上时 initSDK 可能长时间不返回,
|
||||
// 不能让启动页一直转圈。
|
||||
final credential = await LoginScreen.readCredential();
|
||||
if (credential == null) {
|
||||
if (!mounted) return;
|
||||
Navigator.of(context).pushReplacement(MaterialPageRoute(builder: (_) => const LoginScreen()));
|
||||
return;
|
||||
}
|
||||
try {
|
||||
await im.init();
|
||||
final credential = await LoginScreen.readCredential();
|
||||
if (credential != null) {
|
||||
await im.login(userID: credential.$1, token: credential.$2);
|
||||
CallService.instance.start();
|
||||
if (!mounted) return;
|
||||
Navigator.of(context).pushReplacement(MaterialPageRoute(builder: (_) => const HomeScreen()));
|
||||
return;
|
||||
}
|
||||
await im.init().timeout(const Duration(seconds: 20));
|
||||
await im.login(userID: credential.$1, token: credential.$2).timeout(const Duration(seconds: 20));
|
||||
CallService.instance.start();
|
||||
if (!mounted) return;
|
||||
Navigator.of(context).pushReplacement(MaterialPageRoute(builder: (_) => const HomeScreen()));
|
||||
return;
|
||||
} catch (_) {
|
||||
// 自动登录失败(token 失效等):清凭证回登录页
|
||||
// 自动登录失败(token 失效、服务器连不上等):清凭证回登录页
|
||||
await LoginScreen.clearCredential();
|
||||
}
|
||||
if (!mounted) return;
|
||||
|
||||
@@ -78,8 +78,11 @@ class _LoginScreenState extends State<LoginScreen> {
|
||||
password: password,
|
||||
platformID: IMService.instance.platformID,
|
||||
);
|
||||
// 2. 登录 OpenIM SDK
|
||||
await IMService.instance.login(userID: result.userID, token: result.token);
|
||||
// 2. 登录 OpenIM SDK(服务器连不上时这一步可能长时间不返回,加超时兜底)
|
||||
await IMService.instance.login(userID: result.userID, token: result.token).timeout(
|
||||
const Duration(seconds: 20),
|
||||
onTimeout: () => throw AuthException('连不上消息服务器,请确认手机连着公司内网 WiFi'),
|
||||
);
|
||||
// 3. 启动通话信令监听
|
||||
CallService.instance.start();
|
||||
// 4. 存凭证用于下次自动登录
|
||||
|
||||
@@ -75,9 +75,24 @@ class IMService extends ChangeNotifier {
|
||||
|
||||
int get platformID => Platform.isIOS ? IMPlatform.ios : IMPlatform.android;
|
||||
|
||||
/// App 启动时初始化 SDK(只做一次)
|
||||
Future<void> init() async {
|
||||
if (sdkReady) return;
|
||||
/// App 启动时初始化 SDK(只做一次)。
|
||||
/// 用共享 Future 防止超时后重复触发 initSDK:第一次调用还在跑时,
|
||||
/// 后续调用直接复用同一个 Future;失败了则允许下次重试。
|
||||
Future<void>? _initFuture;
|
||||
|
||||
Future<void> init() {
|
||||
if (sdkReady) return Future.value();
|
||||
final inFlight = _initFuture;
|
||||
if (inFlight != null) return inFlight;
|
||||
final future = _doInit();
|
||||
_initFuture = future;
|
||||
return future.whenComplete(() {
|
||||
// 失败的初始化允许重试;成功的保留 sdkReady 标记即可
|
||||
if (!sdkReady && identical(_initFuture, future)) _initFuture = null;
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> _doInit() async {
|
||||
final dir = await getApplicationDocumentsDirectory();
|
||||
final dataDir = '${dir.path}/openim';
|
||||
await Directory(dataDir).create(recursive: true);
|
||||
|
||||
Reference in New Issue
Block a user