Flutter 一卡通 API 服务类和调用示例
以下是 ApiService 类中的网络请求代码:
import 'dart:convert';
import 'package:dio/dio.dart';
class ApiService {
Dio dio = Dio();
//获取一卡通近期流水
Future<List<Map<String, String>>> getRecentlyTransactions(String jwc_xuehao) async {
List<Map<String, String>> yikatong_recent = [];
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=' +
jwc_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
Future<String> getAccno(String jwc_xuehao) async {
Response response = await dio.post(
'http://wxy.hrbxyz.cn/api/Apixyk/getcardinfo?schoolname=东北石油大学&account=' +
jwc_xuehao +
'&password=112233');
var data = json.decode(response.toString());
return data['data']['obj']['AccNo'];
}
//获取一卡通的二维码
Future<String> getQr(String jwc_xuehao) async {
String accno = await getAccno(jwc_xuehao);
Response response = await dio.post(
'http://wxy.hrbxyz.cn/api/Apixyk/getval?schoolname=东北石油大学&account=' +
jwc_xuehao +
'&accno=' +
accno +
'&password=' +
Global.password.substring(Global.password.length - 6, Global.password.length));
var data = json.decode(response.toString());
try {
return data['data'];
} catch (e) {
return '一卡通系统错误';
}
}
}
下面是调用上述网络请求的代码:
class SomeClass {
ApiService apiService = ApiService();
void someMethod(BuildContext context) async {
List<Map<String, String>> yikatong_recent = await apiService.getRecentlyTransactions(Global.jwc_xuehao);
//创建流水表格
Widget yikatong_recently = DataTable(
columns: <DataColumn>[
DataColumn(
label: Container(
//屏幕适配
width: MediaQuery.of(context).size.width * 0.1,
child: Text(
'时间',
style: TextStyle(fontStyle: FontStyle.italic),
),
),
),
DataColumn(
label: Container(
width: MediaQuery.of(context).size.width * 0.1,
child: Text(
'类型',
style: TextStyle(fontStyle: FontStyle.italic),
),
),
),
DataColumn(
label: Container(
width: MediaQuery.of(context).size.width * 0.1,
child: Text(
'金额',
style: TextStyle(fontStyle: FontStyle.italic),
),
),
)
],
rows: yikatong_recent
.map(
(recent) => DataRow(
cells: <DataCell>[
DataCell(Text(recent['Trading_time'])),
DataCell(Text(recent['TranName'])),
DataCell(Text(recent['Transaction_amount'])),
],
),
)
.toList(),
);
//弹出流水窗口
showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: const Text('一卡通近30天流水'),
content: yikatong_recently,
actions: <Widget>[
TextButton(
child: const Text('关闭'),
onPressed: () {
Navigator.of(context).pop();
},
),
],
);
});
String qrcode = await apiService.getQr(Global.jwc_xuehao);
print(qrcode);
}
}
原文地址: https://www.cveoy.top/t/topic/oj26 著作权归作者所有。请勿转载和采集!