123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391 |
- import 'dart:convert';
- import 'dart:isolate';
- import 'dart:typed_data';
- import 'package:dio/dio.dart';
- import 'package:encrypt/encrypt.dart';
- import 'package:flutter/cupertino.dart';
- import 'package:flutter/foundation.dart';
- import 'package:flutter/material.dart';
- import 'package:ysairplane/code/YSBill.dart';
- import 'package:ysairplane/code/YSLogin.dart';
- import 'package:fluttertoast/fluttertoast.dart';
- import 'package:shared_preferences/shared_preferences.dart';
- import 'package:ysairplane/code/YSSetPayPass.dart';
- import 'YSTools.dart';
- //http://aviation.ytby0402.top
- //http://192.168.1.77:8082
- String base = 'http://aviation.ytby0402.top';
- int outTime = 20000;
- enum requestType{
- put,
- delete,
- get,
- post
- }
- typedef widgetCallback = Widget Function(dynamic value);
- ysRequestHttp(BuildContext context,{requestType type,String api,var parameter,bool isLoading,VoidCallback refresh,bool isToken}) async {
- FocusScope.of(context).unfocus();
- if(isLoading==true)showDialog(
- context: context,
- barrierDismissible: false,
- builder: (BuildContext context) {
- return LoadingDialog();
- }
- );
- try {
- SharedPreferences prefs = await SharedPreferences.getInstance();
- String token = prefs.getString('token');
- Map<String, dynamic> httpHeaders = {
- 'Accept': 'application/json,*/*',
- 'Content-Type': 'application/json',
- if(isToken==true)'Authentication': token??''
- };
- String url = base+api;
- print('请求网址:$url');
- if(isToken==true)print('token:$token');
- print('请求参数:$parameter');
- Response response;
- if(type==requestType.put){
- response = await Dio().put(url,data: parameter,options: Options(receiveTimeout: outTime,headers: httpHeaders));
- }else if(type==requestType.post){
- response = await Dio().post(url,data: parameter,options: Options(receiveTimeout: outTime,headers: httpHeaders));
- }else if(type==requestType.delete){
- response = await Dio().delete(url,data: parameter,options: Options(receiveTimeout: outTime,headers: httpHeaders));
- }else{
- response = await Dio().get(url,queryParameters: Map<String, dynamic>.from(parameter),options: Options(receiveTimeout: outTime,headers: httpHeaders));
- }
- if(isLoading==true)Navigator.of(context,rootNavigator: true).pop();
- LogUtil.d('请求结果:${response.data}');
- Map dict = response.data;
- if(dict['code']==200){
- ysFlutterToast(context,'操作成功');
- return dict;
- }else if(dict['code']==500){
- ysFlutterToast(context,'网络错误');
- }else if(dict['code']==404){
- ysFlutterToast(context,'接口请求错误');
- }else if(dict['code']==401){
- ysFlutterToast(context,'登陆已过期');
- Navigator.of(context,rootNavigator: true).push(
- CupertinoPageRoute(
- builder: (context){
- Future<SharedPreferences> _prefer = SharedPreferences.getInstance();
- _prefer.then((value){
- value.remove('token');
- });
- return YSLogin();
- }
- )
- ).then((value){
- refresh();
- });
- }else if(dict['code']==400){
- ysFlutterToast(context,'没有检测到手机号');
- }else if(dict['code']==403){
- ysFlutterToast(context,'没有相关权限');
- }else if(dict['code']==405){
- ysFlutterToast(context,'没有注册,请注册');
- if(isToken==true)Navigator.of(context).push(
- CupertinoPageRoute(
- builder: (context){
- Future<SharedPreferences> _prefer = SharedPreferences.getInstance();
- _prefer.then((value){
- value.remove('token');
- });
- return YSLogin();
- }
- )
- ).then((value){
- refresh();
- });
- }else if(dict['code']==406){
- ysFlutterToast(context,'密码错误');
- }else if(dict['code']==407){
- ysFlutterToast(context,'注册成功,需要新建账号');
- Navigator.of(context).push(
- CupertinoPageRoute(
- builder: (context){
- return YSBill();
- }
- )
- );
- }else if(dict['code']==408){
- ysFlutterToast(context,'用户名重复');
- }
- } catch (error) {
- if(isLoading==true)Navigator.of(context,rootNavigator: true).pop();
- print('网络错误:$error');
- ysFlutterToast(context,'网络错误');
- }
- }
- ysRequestHttpEncrypt(BuildContext context,{requestType type,String api,var parameter,bool isLoading,bool isToken}) async {
- FocusScope.of(context).unfocus();
- if(isLoading==true)showDialog(
- context: context,
- barrierDismissible: false,
- builder: (BuildContext context) {
- return LoadingDialog();
- }
- );
- try {
- SharedPreferences prefs = await SharedPreferences.getInstance();
- String token = prefs.getString('token');
- Map<String, dynamic> httpHeaders = {
- 'Accept': 'application/json,*/*',
- 'Content-Type': 'application/json',
- if(isToken==true)'Authentication': token??''
- };
- String url = base+api;
- print('请求网址:$url');
- if(isToken==true)print('token:$token');
- print('请求参数:$parameter');
- var requestStr = await rsaEncryption(json.encode(parameter));
- print('加密参数:${{'parmar':requestStr}}');
- Response response;
- if(type==requestType.put){
- response = await Dio().put(url,data: {'parmar':requestStr},options: Options(receiveTimeout: outTime,headers: httpHeaders));
- }else if(type==requestType.post){
- response = await Dio().post(url,data: {'parmar':requestStr},options: Options(receiveTimeout: outTime,headers: httpHeaders));
- }else if(type==requestType.delete){
- response = await Dio().delete(url,data: {'parmar':requestStr},options: Options(receiveTimeout: outTime,headers: httpHeaders));
- }else{
- response = await Dio().get(url,queryParameters: Map<String, dynamic>.from({'parmar':requestStr}),
- options: Options(receiveTimeout: outTime,headers: httpHeaders));
- }
- if(isLoading==true)Navigator.of(context,rootNavigator: true).pop();
- print('请求结果:${response.data}');
- Map dict = response.data;
- if(dict['code']==200){
- ysFlutterToast(context,'操作成功');
- if(response.data['data']!=null){
- String encrypt = response.data['data'];
- String dictStr = await rsaPrivateDecrypt(encrypt);
- LogUtil.d('解密后:$dictStr');
- return json.decode(dictStr);
- }else{
- return '';
- }
- }else if(dict['code']==500){
- ysFlutterToast(context,'网络错误');
- }else if(dict['code']==404){
- ysFlutterToast(context,'接口请求错误');
- }else if(dict['code']==401){
- ysFlutterToast(context,'登陆已过期');
- if(isToken==true)Navigator.of(context).push(
- CupertinoPageRoute(
- builder: (context){
- Future<SharedPreferences> _prefer = SharedPreferences.getInstance();
- _prefer.then((value){
- value.remove('token');
- });
- return YSLogin();
- }
- )
- );
- }else if(dict['code']==400){
- ysFlutterToast(context,'没有检测到手机号');
- }else if(dict['code']==403){
- ysFlutterToast(context,'没有相关权限');
- }else if(dict['code']==405){
- ysFlutterToast(context,'没有注册,请注册');
- if(isToken==true)Navigator.of(context).push(
- CupertinoPageRoute(
- builder: (context){
- Future<SharedPreferences> _prefer = SharedPreferences.getInstance();
- _prefer.then((value){
- value.remove('token');
- });
- return YSLogin();
- }
- )
- ).then((value){
- });
- }else if(dict['code']==406){
- ysFlutterToast(context,'密码错误');
- }else if(dict['code']==407){
- ysFlutterToast(context,'注册成功,需要新建账号');
- String encrypt = response.data['data'];
- var dictStr = await rsaPrivateDecrypt(encrypt);
- print('解密后:$dictStr');
- Navigator.of(context).push(
- CupertinoPageRoute(
- builder: (context){
- return YSBill();
- }
- )
- );
- }else if(dict['code']==408){
- ysFlutterToast(context,'用户名重复');
- }else if(dict['code']==804){
- return {'code':804};
- }else if(dict['code']==801){
- return {'code':801};
- }
- } catch (error) {
- if(isLoading==true)Navigator.of(context,rootNavigator: false).pop();
- print('网络错误:$error');
- ysFlutterToast(context,'网络错误');
- }
- }
- class LoadingDialog extends Dialog {
- @override
- Widget build(BuildContext context) {
- return Material(
- type: MaterialType.transparency,
- child: Center(
- child: SizedBox(
- width: 120.0,
- height: 120.0,
- child: Container(
- decoration: ShapeDecoration(
- color: Colors.black87,
- shape: RoundedRectangleBorder(
- borderRadius: BorderRadius.all(
- Radius.circular(8.0),
- ),
- ),
- ),
- child: Column(
- mainAxisAlignment: MainAxisAlignment.center,
- crossAxisAlignment: CrossAxisAlignment.center,
- children: <Widget>[
- CircularProgressIndicator(valueColor: AlwaysStoppedAnimation<Color>(Colors.white),),
- Padding(
- padding: EdgeInsets.only(top: 20.0,),
- child: Text('正在请求',style: TextStyle(color: Colors.white),),
- ),
- ],
- ),
- ),
- ),
- ),
- );
- }
- }
- void ysFlutterToast(BuildContext context,String msg){
- FlutterToast(context).showToast(
- child: Container(
- padding: EdgeInsets.only(left: 20,right: 20,top: 5,bottom: 5),
- decoration: BoxDecoration(
- color: Colors.black87,
- borderRadius: BorderRadius.all(Radius.circular(25))
- ),
- child: Text(msg,style: TextStyle(fontSize: 14,color: Colors.white),),
- ),
- gravity: ToastGravity.BOTTOM,
- toastDuration: Duration(seconds: 2),
- );
- }
- //加密
- Future<String> rsaEncryption(String data) async {
- // 原始json转成字节数组
- List<int> sourceBytes = utf8.encode(data);
- final parser = RSAKeyParser();
- String key = '-----BEGIN PUBLIC KEY-----\nMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCit9lK3Y4jrSBAWbbH4OYN4UzMLx7Q+zy0qKzyzMszeEitI8xLP9E4HanriT'
- 'X6iKFrdKJLb55DWga79trUozlzeI7XXu67S5VRr+YRLOXHbbTGrJVV63RnolgGd9jkzDd74wjVJIn8gylqlN+WG5/4daMHeEVQ8bGSq03aotzxrwIDAQAB';
- final publicKey = parser.parse(key);
- final encrypter = Encrypter(RSA(publicKey: publicKey));
- // 数据长度
- int inputLen = sourceBytes.length;
- // 加密最大长度
- int maxLen = 117;
- // 存放加密后的字节数组
- List<int> totalBytes = List();
- // 分段加密 步长为117
- for (var i = 0; i < inputLen; i += maxLen) {
- // 还剩多少字节长度
- int endLen = inputLen - i;
- List<int> item;
- if (endLen > maxLen) {
- item = sourceBytes.sublist(i, i + maxLen);
- }else{
- item = sourceBytes.sublist(i, i + endLen);
- }
- // 加密后的对象转换成字节数组再存放到容器
- totalBytes.addAll(encrypter.encryptBytes(item).bytes);
- }
- return base64.encode(totalBytes);
- }
- //解密
- Future<String> rsaPrivateDecrypt(String data) async {
- Uint8List sourceBytes = base64.decode(data);
- final parser = RSAKeyParser();
- String key = '-----BEGIN PRIVATE KEY-----\nMIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAKK32UrdjiOtIEBZtsfg5g3hTMwvHtD7PLSorPLMyzN4SK0jzEs/0TgdqeuJNfqIoWt0oktvnkNaBrv22tSjOXN4jtde7rtLlVGv5hEs5c'
- 'dttMaslVXrdGeiWAZ32OTMN3vjCNUkifyDKWqU35Ybn/h1owd4RVDxsZKrTdqi3PGvAgMBAAECgYBQ/OanD40Ojr3NqZmC9JoscGXT/uP8qf91/7pNAsdkr8qkenvVPEc7AfCv7dQzBUwqepvIph6EcUuGxH/4c+FEn5X'
- 'QS/dNhdDHZtZ6E+DQzXHvrz7wVfDSecyYEKHRKw0WZmoCy6bfeDEf5zShmMJtmtakGq/+ES6U0DtLN4SWkQJBANCadJQm2ZSnHc0lCHkdkcSXsS+nhtRZ5EyKg3Xr81BClQXPtswYhPcNq9QSC/CBiaVNeC0TOOZEfaVEH'
- 'wc+bAkCQQDHsHPzMbnyZtPdv3MG1xukTgIuFsmg/LPM+hkuSNDUNR63PBNeVv59DwnFd+bgQTyZWQ97wm6Nx4ZQSfN3BE33AkB4RbWVfdjRZpE+KG38NtpGuRdF3JdWdAW3Q92L7eC5k8oMMbi5cCGpt84sVcNgha9xCXuSs'
- 'ZSK3056LQ6exJTRAkBSPmqDD0f2fkNkYSWO+6l20mozcU857tpe4eLdHUBlJjuwXB3eDRZji34KxodgdX3v6q5l2n6OBk9bYhnUOwGhAkB7+gFxeARigkNJl8CCVtpJ9oAped5NT8ebDS8KCAU2qu6Y9mz04bk2NbHAf6TmIm'
- 'oLCa/ORgP0Tn4HefEOfBKl';//根据存放位置更改
- final privateKey = parser.parse(key);
- final encrypter = Encrypter(RSA(privateKey: privateKey));
- // 数据长度
- int inputLen = sourceBytes.length;
- // 解密最大长度
- int maxLen = 128;
- // 存放加密后的字节数组
- List<int> totalBytes = List();
- // 分段解密 步长为128
- for (var i = 0; i < inputLen; i += maxLen) {
- // 还剩多少字节长度
- int endLen = inputLen - i;
- Uint8List item;
- if (endLen > maxLen) {
- item = sourceBytes.sublist(i, i + maxLen);
- }else{
- item = sourceBytes.sublist(i, i + endLen);
- }
- // 解密后的对象转换成字节数组再存放到容器
- totalBytes.addAll(encrypter.decryptBytes(Encrypted(item)));
- }
- return utf8.decode(totalBytes);
- }
- ysFutureWidge({widgetCallback data,VoidCallback net}){
- return FutureBuilder(
- future: net(),
- builder: (context, snapshot) {
- var widget;
- if (snapshot.connectionState == ConnectionState.done) {
- if (snapshot.hasError) {
- widget = Icon(
- Icons.error,
- color: Colors.red,
- size: 48,
- );
- }else if(snapshot.data==null||snapshot.data.isEmpty||snapshot.data==[]){
- widget = Container();
- } else {
- widget = data(snapshot);
- }
- } else {
- widget = Padding(
- padding: EdgeInsets.all(20),
- child: CircularProgressIndicator(),
- );
- }
- return Center(
- child: Container(
- child: widget,
- ),
- );
- },
- );
- }
|