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 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 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 createState() => _LoginScreenState(); } class _LoginScreenState extends State { 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 _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), ), ); } }