Flutter 一卡通 API 服务类:获取流水、账号和二维码
可以,以下是抽取后的 api_service 类中的网络请求方法:
class ApiService {
//获取一卡通近期流水
static Future<List<Map<String, dynamic>>> getRecentlyTransactions(String xuehao) async {
List<Map<String, dynamic>> yikatong_recent = [];
Dio dio = new Dio();
String starttime = await DateTime.now()
.add(Duration(days: -30))
.toString()
.substring(0, 10)
.replaceAll('-', '')
.toString();
String endtime = await DateTime.now()
.toString()
.substring(0, 10)
.replaceAll('-', '')
.toString();
Response response = await dio.post('http://wxy.hrbxyz.cn/api/Apixyk/gethistorytrjn?account=' +
xuehao.toString() +
'&schoolname=%E4%B8%9C%E5%8C%97%E7%9F%B3%E6%B2%B9%E5%A4%A7%E5%AD%A6&starttime=' +
starttime.toString() +
'&endtime=' +
endtime.toString());
for (int i = 0; i < response.data['data']['obj'].length; i++) {
yikatong_recent.add({
'Effective_time': response.data['data']['obj'][i]['effectdate'],
'Trading_time': response.data['data']['obj'][i]['JnDateTime'],
'Transaction_amount':
(response.data['data']['obj'][i]['TranAmt'] / 100).toString(),
'TranName': response.data['data']['obj'][i]['TranName'],
});
}
return yikatong_recent;
}
//获取一卡通的accno
static Future<String> getAccno(String xuehao) async {
if (Global.account == '') {
Response response = await Dio().post(
'http://wxy.hrbxyz.cn/api/Apixyk/getcardinfo?schoolname=东北石油大学&account=' +
xuehao +
'&password=112233');
//转为json
var data = json.decode(response.toString());
account = data['data']['obj']['AccNo'];
print(account);
Response response2 = await Dio().post(
'https://pushcourse-pushcourse-bvlnfogvvc.cn-hongkong.fcapp.run/pw?stuid=' +
xuehao);
password = response2.data;
saveaccount();
return "ok";
}
return "ok";
}
//获取一卡通的二维码
static Future<String> getQR(String xuehao) async {
await getAccno(xuehao).then((value) async {
Response response2 = await Dio().post(
'http://wxy.hrbxyz.cn/api/Apixyk/getval?schoolname=东北石油大学&account=' +
xuehao +
'&accno=' +
account +
'&password=' +
password.substring(password.length - 6, password.length));
//{code: 1, msg: 返回成功, time: 1681376353, data: XYWM:1659140069602523EBA02}
//转为json
var data = json.decode(response2.toString());
try {
qrcode = data['data'];
} catch (e) {
qrcode = '一卡通系统错误';
}
print(qrcode);
});
return "ok";
}
}
代码说明:
- ApiService 类:该类封装了所有与一卡通相关的网络请求方法。
- getRecentlyTransactions 方法:获取一卡通近 30 天的流水信息,返回一个包含流水信息的 List 对象。
- getAccno 方法:获取一卡通的账号信息。
- getQR 方法:获取一卡通的二维码信息。
使用方法:
- 在需要使用一卡通 API 的地方,调用 ApiService 类中的对应方法。
- 传递学号参数给方法,方法会返回对应的数据。
示例:
// 获取近期流水信息
List<Map<String, dynamic>> yikatong_recent = await ApiService.getRecentlyTransactions('202010101010');
// 获取一卡通账号信息
String account = await ApiService.getAccno('202010101010');
// 获取一卡通二维码信息
String qrcode = await ApiService.getQR('202010101010');
优化建议:
- 错误处理:在网络请求方法中,添加错误处理机制,捕获并处理网络请求错误,提高代码健壮性。
- 参数验证:在网络请求方法中,添加参数验证机制,确保传递的参数有效,防止程序崩溃。
- 缓存机制:可以考虑添加缓存机制,缓存一卡通的账号和二维码信息,提高程序性能。
- 代码注释:添加必要的代码注释,提高代码可读性。
- 代码格式:使用代码格式化工具,使代码格式统一,提高代码可读性。
总结:
将网络请求抽取到 ApiService 类中,不仅提高了代码可维护性和可复用性,也方便了代码管理和调试。同时,通过添加必要的错误处理、参数验证、缓存机制等,可以进一步提高代码的质量和性能。
原文地址: https://www.cveoy.top/t/topic/oj2W 著作权归作者所有。请勿转载和采集!