//api_service.dart import 'package:dio/dio.dart';

class ApiService { static Dio dio = Dio();

static Future getJwcLogin() async { return dio.get( 'https://nepuback-nepu-restart-xbbhhovrls.cn-beijing.fcapp.run/jwc_login', options: Options(responseType: ResponseType.bytes), ); }

static Future checkLoginInfo( String username, String password, String verifyCode, String jwcJsessionId, String jwcWebvpnKey, String jwcWebvpnUsername, ) async { return dio.get( 'https://nepu-node-login-nepu-restart-togqejjknk.cn-beijing.fcapp.run/course', options: Options(), queryParameters: { 'account': username, 'password': password, 'verifycode': verifyCode, 'JSESSIONID': jwcJsessionId, '_webvpn_key': jwcWebvpnKey, 'webvpn_username': jwcWebvpnUsername, }, ); } }

//原代码中调用getJwcLogin()和checkLoginInfo()的部分需要修改为调用ApiService中的方法。

//无感知登录 Future No_perception_login() async { Response response = await ApiService.getJwcLogin().then((value) async { for (var item in value.headers .value('Set-Cookie') .toString() .replaceAll('{', '') .replaceAll('}', '') .replaceAll("'", '') .replaceAll(' ', '') .split(',')) { if (item.length < 50 && item.length > 10) { jwc_jsessionid = item; } if (item.length > 100) { jwc_webvpn_key = item; } if (item.length > 50 && item.length < 100) { jwc_webvpn_username = item; } if (item.length == 4) { jwc_verifycode = item; } if (item.length == 5) { No_perception_login(); } } Response response1 = await ApiService.checkLoginInfo( Global.jwc_xuehao, Global.jwc_password, Global.jwc_verifycode, Global.jwc_jsessionid, Global.jwc_webvpn_key, Global.jwc_webvpn_username, ).then((value1) async { if (value1.data['message'].toString() == '登录成功') { print('无感登陆成功了'); print(await Global().getLoginInfo()); } return value1; });

return value;

}); }

//登录校验 Future getLoginstatus( String username, String password, String verifyCode, setState, context, ) async { Response response = await ApiService.checkLoginInfo( username, password, verifyCode, Global.jwc_jsessionid, Global.jwc_webvpn_key, Global.jwc_webvpn_username, );

//持久化存储登录信息 saveString() { Global().storelogininfo(username, password); }

//切换到HomePage页面 print(response.data.toString()); if (response.data['message'].toString() == '登录成功') { saveString(); _pureyzm = true; Global.getVerifyCode(context, setState); setState(() {}); return ''; } else { setState(() {}); }

return response.data['message'].toString() + ',请等待新的验证码刷新或手动点击更新';

Flutter 代码优化:抽取公共部分并整合网络请求

原文地址: https://www.cveoy.top/t/topic/oj4i 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录