Files
tongxunruanjian/mobile/lib/screens/login_screen.dart
T
KIMI 3b5c76cac4 fix: 修复手机连不上服务器时启动页无限转圈(B-58)
- 启动页先读本地凭证:无凭证(首次打开)直接进登录页,不再先等 SDK 初始化
- SDK init/login 加 20s 超时;init 用共享 Future 防止超时后重复触发 initSDK
- 登录页对 IM 登录超时给出明确提示(确认手机连着公司内网 WiFi)
- 修复 flutter create 模板遗留的 widget_test(引用不存在的 MyApp),改为登录页渲染冒烟测试
- 版本号 1.0.1+2
2026-08-15 14:54:52 +08:00

175 lines
6.1 KiB
Dart

import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import '../services/auth_api.dart';
import '../services/call_service.dart';
import '../services/im_service.dart';
import '../theme.dart';
import 'home_screen.dart';
/// 登录页:工号 + 密码,登录中按钮转菊花,失败在按钮下方红字提示。
class LoginScreen extends StatefulWidget {
const LoginScreen({super.key});
/// 本地保存登录凭证的键(自动登录用)
static const String keyUserID = 'login_userID';
static const String keyToken = 'login_token';
static const String keyNickname = 'login_nickname';
/// 保存登录凭证
static Future<void> saveCredential(String userID, String token, String nickname) async {
final prefs = await SharedPreferences.getInstance();
await prefs.setString(keyUserID, userID);
await prefs.setString(keyToken, token);
await prefs.setString(keyNickname, nickname);
}
/// 清除登录凭证(退出登录 / token 失效时)
static Future<void> clearCredential() async {
final prefs = await SharedPreferences.getInstance();
await prefs.remove(keyUserID);
await prefs.remove(keyToken);
await prefs.remove(keyNickname);
}
/// 读取已保存的凭证
static Future<(String, String)?> readCredential() async {
final prefs = await SharedPreferences.getInstance();
final userID = prefs.getString(keyUserID);
final token = prefs.getString(keyToken);
if (userID == null || userID.isEmpty || token == null || token.isEmpty) return null;
return (userID, token);
}
@override
State<LoginScreen> createState() => _LoginScreenState();
}
class _LoginScreenState extends State<LoginScreen> {
final TextEditingController _idCtrl = TextEditingController();
final TextEditingController _pwdCtrl = TextEditingController();
final AuthApi _authApi = AuthApi();
bool _logging = false;
String? _error;
@override
void dispose() {
_idCtrl.dispose();
_pwdCtrl.dispose();
super.dispose();
}
Future<void> _login() async {
final staffNo = _idCtrl.text.trim();
final password = _pwdCtrl.text;
if (staffNo.isEmpty || password.isEmpty) {
setState(() => _error = '请输入工号和密码');
return;
}
setState(() {
_logging = true;
_error = null;
});
try {
// 1. 公司账号登录,拿 OpenIM 的 userID/imToken
final result = await _authApi.login(
staffNo: staffNo,
password: password,
platformID: IMService.instance.platformID,
);
// 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. 存凭证用于下次自动登录
await LoginScreen.saveCredential(result.userID, result.token, result.nickname);
if (!mounted) return;
Navigator.of(context).pushReplacement(
MaterialPageRoute(builder: (_) => const HomeScreen()),
);
} on AuthException catch (e) {
setState(() => _error = e.message);
} catch (_) {
setState(() => _error = '登录出错了,请稍后再试');
} finally {
if (mounted) setState(() => _logging = false);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: AppColors.pageBg,
body: SafeArea(
child: SingleChildScrollView(
padding: const EdgeInsets.symmetric(horizontal: AppGap.x8),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
const SizedBox(height: 96),
const Text(
'畅联',
textAlign: TextAlign.center,
style: TextStyle(fontSize: 32, fontWeight: FontWeight.w600, color: AppColors.primary),
),
const SizedBox(height: AppGap.x12),
_input(_idCtrl, '请输入工号', Icons.person_outline, false),
const SizedBox(height: AppGap.x4),
_input(_pwdCtrl, '请输入密码', Icons.lock_outline, true),
const SizedBox(height: AppGap.x6),
SizedBox(
height: 48,
child: FilledButton(
onPressed: _logging ? null : _login,
style: FilledButton.styleFrom(
backgroundColor: AppColors.primary,
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(8)),
),
child: _logging
? const SizedBox(
width: 20,
height: 20,
child: CircularProgressIndicator(strokeWidth: 2, color: Colors.white),
)
: const Text('登录', style: TextStyle(fontSize: AppFont.body)),
),
),
if (_error != null) ...[
const SizedBox(height: AppGap.x3),
Text(
_error!,
textAlign: TextAlign.center,
style: const TextStyle(fontSize: AppFont.sub, color: AppColors.danger),
),
],
],
),
),
),
);
}
Widget _input(TextEditingController ctrl, String hint, IconData icon, bool obscure) {
return TextField(
controller: ctrl,
obscureText: obscure,
decoration: InputDecoration(
hintText: hint,
hintStyle: const TextStyle(color: AppColors.textSecondary, fontSize: AppFont.body),
prefixIcon: Icon(icon, color: AppColors.textSecondary),
filled: true,
fillColor: AppColors.searchBg,
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(8),
borderSide: BorderSide.none,
),
contentPadding: const EdgeInsets.symmetric(vertical: AppGap.x4),
),
);
}
}