新增 mobile/:畅联手机端 Flutter 工程(B-58)
- 登录:工号+密码走 account-service /api/login,自动登录、被踢下线回登录页 - 消息:会话列表(未读角标/免打扰/时间)、单聊群聊、文字/语音/图片/文件消息、失败重发、历史分页 - 通讯录:新的同事/我的群聊入口、按部门分组(好友 ex 字段)、搜索 - 通话:一对一语音通话(信令走 OpenIM 自定义消息 + LiveKit,token 走 /api/rtc_token) - 界面按确认效果图 m1/m2/m3 实现,主色 #3B87F5,四态视图齐全,无英文残留
This commit is contained in:
@@ -0,0 +1,171 @@
|
||||
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);
|
||||
// 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),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user