diff --git a/marking_app/lib/pages/login/index.dart b/marking_app/lib/pages/login/index.dart index c70bee5..5cf1c6e 100644 --- a/marking_app/lib/pages/login/index.dart +++ b/marking_app/lib/pages/login/index.dart @@ -50,10 +50,8 @@ class _TheLoginState extends ConsumerState with CommonMixin { //添加证书 setHttpsPEM() async { - (dio.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate = - (client) { - client.badCertificateCallback = - (X509Certificate cert, String host, int port) { + (dio.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate = (client) { + client.badCertificateCallback = (X509Certificate cert, String host, int port) { return true; }; }; @@ -91,18 +89,13 @@ class _TheLoginState extends ConsumerState with CommonMixin { }); getShowRegister(); super.initState(); - dio = Dio(BaseOptions( - contentType: "application/json", - connectTimeout: 8000, - receiveTimeout: 8000)); - dio.interceptors - .add(LogInterceptor(responseBody: true, requestBody: true)); //添加日志 + dio = Dio(BaseOptions(contentType: "application/json", connectTimeout: 8000, receiveTimeout: 8000)); + dio.interceptors.add(LogInterceptor(responseBody: true, requestBody: true)); //添加日志 setHttpsPEM(); client = RestClient(dio, baseUrl: RequestConfig().loginBaseUrl); - _userNameController = TextEditingController() - ..addListener(userNameListener); + _userNameController = TextEditingController()..addListener(userNameListener); _passwordController = TextEditingController(); _pwdFocus = FocusNode(); _theFocus = FocusNode(); @@ -121,8 +114,11 @@ class _TheLoginState extends ConsumerState with CommonMixin { } void getShowRegister() async { - BaseStructureResult resultData = await client.showRegister(); - if (resultData.success) setState(() => showRegister = resultData.data); + try { + var client = await getClient(); + BaseStructureResult resultData = await client.showRegister(); + if (resultData.success) setState(() => showRegister = resultData.data); + } catch (e) {} } // 获取用户学校数据 @@ -138,9 +134,7 @@ class _TheLoginState extends ConsumerState with CommonMixin { if (res.success) { schools = res.data ?? []; UseLoginSchool? schoolElement; - if (lastTimeSchoolId != null) - schoolElement = - schools.firstWhereOrNull((e) => e.schoolId == lastTimeSchoolId); + if (lastTimeSchoolId != null) schoolElement = schools.firstWhereOrNull((e) => e.schoolId == lastTimeSchoolId); if (schoolElement != null) schoolId = schoolElement.schoolId; else @@ -162,8 +156,7 @@ class _TheLoginState extends ConsumerState with CommonMixin { int useNameLength = userName.length; bool hasNameValNew = useNameLength > 0; if (!hasNameValNew) lastTimeSchoolId = null; - if (hasNameValNew != hasNameVal) - toUpState(setState, () => hasNameVal = hasNameValNew, mounted); + if (hasNameValNew != hasNameVal) toUpState(setState, () => hasNameVal = hasNameValNew, mounted); const isProd = bool.fromEnvironment('dart.vm.product'); getSchoolData(userName); // 获取用户学校数据 if (!isProd && useNameLength == 11) { @@ -219,15 +212,12 @@ class _TheLoginState extends ConsumerState with CommonMixin { child: SizedBox( height: 86.w, width: 86.w, - child: Image.asset('assets/images/logo.png', - fit: BoxFit.cover), + child: Image.asset('assets/images/logo.png', fit: BoxFit.cover), ), ), Container( - margin: EdgeInsets.symmetric( - horizontal: 32.w, vertical: 24.h), - padding: EdgeInsets.only( - top: 34.h, bottom: 16.h, left: 22.w, right: 22.w), + margin: EdgeInsets.symmetric(horizontal: 32.w, vertical: 24.h), + padding: EdgeInsets.only(top: 34.h, bottom: 16.h, left: 22.w, right: 22.w), decoration: BoxDecoration( color: Colors.white, border: Border.all(width: 1.w, color: Colors.white), @@ -256,18 +246,13 @@ class _TheLoginState extends ConsumerState with CommonMixin { ), decoration: InputDecoration( hintText: "请输入账号", - hintStyle: TextStyle( - fontSize: 16.sp, - color: const Color.fromRGBO(153, 153, 153, 1)), + hintStyle: TextStyle(fontSize: 16.sp, color: const Color.fromRGBO(153, 153, 153, 1)), labelText: "账号", - labelStyle: TextStyle( - fontSize: 16.sp, - color: const Color.fromRGBO(148, 163, 182, 1)), + labelStyle: TextStyle(fontSize: 16.sp, color: const Color.fromRGBO(148, 163, 182, 1)), suffixIcon: !hasNameVal ? null : Transform.translate( - offset: - Offset(10, 10), // 根据原始组件的padding值来设置偏移量 + offset: Offset(10, 10), // 根据原始组件的padding值来设置偏移量 child: IconButton( alignment: Alignment.center, padding: EdgeInsets.zero, @@ -303,19 +288,13 @@ class _TheLoginState extends ConsumerState with CommonMixin { onTap: _showPassword, child: Icon( Icons.remove_red_eye, - color: !_isShowPwd - ? Theme.of(context).primaryColor - : Colors.grey, + color: !_isShowPwd ? Theme.of(context).primaryColor : Colors.grey, ), ), - hintStyle: TextStyle( - fontSize: 16.sp, - color: const Color.fromRGBO(153, 153, 153, 1)), + hintStyle: TextStyle(fontSize: 16.sp, color: const Color.fromRGBO(153, 153, 153, 1)), labelText: "密码", isDense: true, - labelStyle: TextStyle( - fontSize: 16.sp, - color: const Color.fromRGBO(148, 163, 182, 1)), + labelStyle: TextStyle(fontSize: 16.sp, color: const Color.fromRGBO(148, 163, 182, 1)), ), ), $SchoolDataDropDown(schools, schoolId, (int? id) { @@ -333,10 +312,8 @@ class _TheLoginState extends ConsumerState with CommonMixin { checkColor: Colors.white, value: keepPwd, onChanged: (value) { - FocusScope.of(context) - .requestFocus(_pwdFocus); - FocusScope.of(context) - .requestFocus(_theFocus); + FocusScope.of(context).requestFocus(_pwdFocus); + FocusScope.of(context).requestFocus(_theFocus); setState(() { keepPwd = value ?? false; }); @@ -349,24 +326,15 @@ class _TheLoginState extends ConsumerState with CommonMixin { FocusScope.of(context).requestFocus(_theFocus); setState(() => keepPwd = !keepPwd); }, - child: Text('记住密码', - style: TextStyle( - fontSize: 14.sp, - color: const Color.fromRGBO( - 148, 163, 182, 1))), + child: Text('记住密码', style: TextStyle(fontSize: 14.sp, color: const Color.fromRGBO(148, 163, 182, 1))), ), Spacer(), if (showRegister == true) InkWell( onTap: () { - RouterManager.router.navigateTo( - context, RouterManager.registerPath); + RouterManager.router.navigateTo(context, RouterManager.registerPath); }, - child: Text('注册', - style: TextStyle( - fontSize: 14.sp, - color: const Color.fromRGBO( - 148, 163, 182, 1)))), + child: Text('注册', style: TextStyle(fontSize: 14.sp, color: const Color.fromRGBO(148, 163, 182, 1)))), ], ), InkWell( @@ -374,9 +342,7 @@ class _TheLoginState extends ConsumerState with CommonMixin { child: Container( margin: EdgeInsets.symmetric(vertical: 10.h), decoration: BoxDecoration( - color: canLogin - ? const Color.fromRGBO(9, 105, 246, 1) - : Colors.grey, + color: canLogin ? const Color.fromRGBO(9, 105, 246, 1) : Colors.grey, boxShadow: [ BoxShadow( color: const Color.fromRGBO(46, 91, 255, 0.5), @@ -385,16 +351,14 @@ class _TheLoginState extends ConsumerState with CommonMixin { spreadRadius: 0.5, //阴影扩散程度 ) ], - borderRadius: - BorderRadius.all(Radius.circular(8.w)), + borderRadius: BorderRadius.all(Radius.circular(8.w)), ), alignment: Alignment.center, width: double.infinity, height: 50.h, child: Text( '登 录', - style: TextStyle( - fontSize: 16.sp, color: Colors.white), + style: TextStyle(fontSize: 16.sp, color: Colors.white), ), ), ), @@ -408,10 +372,8 @@ class _TheLoginState extends ConsumerState with CommonMixin { checkColor: Colors.white, value: readAgreement, onChanged: (value) { - FocusScope.of(context) - .requestFocus(_pwdFocus); - FocusScope.of(context) - .requestFocus(_theFocus); + FocusScope.of(context).requestFocus(_pwdFocus); + FocusScope.of(context).requestFocus(_theFocus); setState(() { readAgreement = value ?? false; }); @@ -487,29 +449,22 @@ class _TheLoginState extends ConsumerState with CommonMixin { barrierDismissible: false, builder: (BuildContext context) { return CupertinoAlertDialog( - title: quickText('用户协议及隐私协议', - size: 14.sp, color: Color.fromARGB(255, 53, 52, 52)), + title: quickText('用户协议及隐私协议', size: 14.sp, color: Color.fromARGB(255, 53, 52, 52)), content: SingleChildScrollView( padding: EdgeInsets.only(top: 4.h), child: RichText( text: TextSpan( text: '为了更好地保障您的合法权益,请您阅读并同意以下协议', - style: TextStyle( - color: Color.fromARGB(255, 137, 138, 139), - fontSize: 11.sp), + style: TextStyle(color: Color.fromARGB(255, 137, 138, 139), fontSize: 11.sp), children: [ - TextSpan( - text: '《用户协议》《隐式协议》', - style: TextStyle( - color: Colors.deepOrangeAccent, fontSize: 13.sp)), + TextSpan(text: '《用户协议》《隐式协议》', style: TextStyle(color: Colors.deepOrangeAccent, fontSize: 13.sp)), ], ), ), ), actions: [ CupertinoDialogAction( - child: Text("取消", - style: TextStyle(color: Color.fromARGB(255, 58, 58, 58))), + child: Text("取消", style: TextStyle(color: Color.fromARGB(255, 58, 58, 58))), onPressed: () => Navigator.of(context).pop(false), ), CupertinoDialogAction( @@ -535,22 +490,16 @@ class _TheLoginState extends ConsumerState with CommonMixin { print('userPwdMd5=$userPwdMd5'); EasyLoading.show(status: 'loading...'); - BaseStructureResult resultData = await client - .toLogin(UserLoginParams(userName, userPwdMd5, schoolId!)); - UserLogin? userData = resultData.code == 200 && resultData.data != null - ? UserLogin.fromJson(resultData.data) - : null; - if (resultData.code != 200 || - userData?.accessToken == null || - userData?.accessToken == '') { + BaseStructureResult resultData = await client.toLogin(UserLoginParams(userName, userPwdMd5, schoolId!)); + UserLogin? userData = resultData.code == 200 && resultData.data != null ? UserLogin.fromJson(resultData.data) : null; + if (resultData.code != 200 || userData?.accessToken == null || userData?.accessToken == '') { return toMsg(resultData.message ?? '登录失败,请重试'); } FastData fastData = FastData.getInstance(); fastData.setToken(userData!.accessToken); - BaseStructureResult userRes = - await client.getUserInfo('Bearer ${userData.accessToken}'); + BaseStructureResult userRes = await client.getUserInfo('Bearer ${userData.accessToken}'); if (userRes.code != 200 || userRes.data == null) { throw Exception('登录失败,请重试'); @@ -559,19 +508,14 @@ class _TheLoginState extends ConsumerState with CommonMixin { fastData.setUser(userRes.data!).then((value) { // 记住密码 if (keepPwd) { - fastData.setUserPwdAndAccount({ - 'pwd': userPwd, - 'account': userName, - 'schoolId': schoolId.toString() - }); + fastData.setUserPwdAndAccount({'pwd': userPwd, 'account': userName, 'schoolId': schoolId.toString()}); } // 更新 ref.read(userProvider.notifier).initUserInfo(); ref.read(userTokenProvider.notifier).initToken(); // 跳转登录页 - RouterManager.router.navigateTo(context, RouterManager.root, - clearStack: true, transition: getTransition()); + RouterManager.router.navigateTo(context, RouterManager.root, clearStack: true, transition: getTransition()); }); } catch (e) { toPrint(val: e.toString()); @@ -609,25 +553,19 @@ class _TheLoginState extends ConsumerState with CommonMixin { } @hwidget -Widget $schoolDataDropDown(BuildContext context, List schools, - int? schoolId, void Function(int? id) call) { +Widget $schoolDataDropDown(BuildContext context, List schools, int? schoolId, void Function(int? id) call) { var useSchool = useState(schools); var useSchoolId = useState(schoolId); - useValueChanged, void>( - schools, (_, __) => useSchool.value = schools); - useValueChanged( - schoolId, (_, __) => useSchoolId.value = schoolId); + useValueChanged, void>(schools, (_, __) => useSchool.value = schools); + useValueChanged(schoolId, (_, __) => useSchoolId.value = schoolId); if (useSchool.value.isEmpty) return SizedBox(); return Padding( padding: EdgeInsets.only(top: 24.h), child: Column( children: [ - Container( - alignment: Alignment.topLeft, - child: quickText('登录学校', - size: 12.sp, color: const Color.fromRGBO(148, 163, 182, 1))), + Container(alignment: Alignment.topLeft, child: quickText('登录学校', size: 12.sp, color: const Color.fromRGBO(148, 163, 182, 1))), Container( height: 40.h, width: double.infinity, @@ -639,17 +577,12 @@ Widget $schoolDataDropDown(BuildContext context, List schools, padding: EdgeInsets.only(right: 4.w), icon: const Icon(Icons.keyboard_arrow_down_rounded), style: TextStyle(color: Colors.deepPurple), - underline: Container( - height: 0.6.h, color: const Color.fromRGBO(80, 87, 103, 1)), + underline: Container(height: 0.6.h, color: const Color.fromRGBO(80, 87, 103, 1)), onChanged: (int? id) { call(id); useSchoolId.value = schoolId; }, - items: useSchool.value - .map((e) => DropdownMenuItem( - value: e.schoolId, - child: quickText(e.schoolName, size: 12.sp))) - .toList(), + items: useSchool.value.map((e) => DropdownMenuItem(value: e.schoolId, child: quickText(e.schoolName, size: 12.sp))).toList(), ), ) ],