MyDio.dart 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. import 'dart:convert';
  2. import 'dart:io';
  3. import 'package:bbyyy/beans/simple_bean.dart';
  4. import 'package:bbyyy/my_tools/easy_loading/easy_loading.dart';
  5. import 'package:bbyyy/my_tools/global.dart';
  6. import 'package:bbyyy/my_tools/my_cookie.dart';
  7. import 'package:bbyyy/paegs/root_page/root_page_view.dart';
  8. import 'package:dio/dio.dart';
  9. import 'package:flutter/cupertino.dart';
  10. import 'package:flutter/material.dart';
  11. import 'package:simple_logger/simple_logger.dart';
  12. ///http请求成功回调
  13. typedef SCallBack<T> = void Function(Response response, bool hasError);
  14. ///失败回调
  15. typedef FCallBack = void Function(DioError error);
  16. final logger = SimpleLogger();
  17. class MyDio {
  18. //私有构造函数
  19. MyDio._internal() {
  20. initDio();
  21. }
  22. //保存单例
  23. static MyDio _singleton = MyDio._internal();
  24. //工厂构造函数
  25. factory MyDio() => _singleton;
  26. Dio dio; // 使用默认配置
  27. BaseOptions options;
  28. initDio() {
  29. // 或者通过传递一个 `options`来创建dio实例
  30. options = BaseOptions(
  31. baseUrl: MyCookie().getServer(),
  32. connectTimeout: 30000,
  33. receiveTimeout: 3000,
  34. );
  35. if (MyCookie().loginInformation != null) {
  36. options.headers = {
  37. '__token__': MyCookie().getToken(),
  38. '__user__': MyCookie().getUser(),
  39. 'Content-Type': 'application/json'
  40. };
  41. }
  42. dio = Dio(options);
  43. }
  44. post(String url, data, SCallBack sCallBack, FCallBack fCallBack) async {
  45. logger.info(MyCookie().getServer() + url);
  46. try {
  47. logger.info(json.encode(data));
  48. } catch (e) {}
  49. try {
  50. Response re = await dio.post(url, data: data);
  51. print(dio.options.headers);
  52. logger.info(MyCookie().getServer() + url);
  53. try {
  54. logger.info(json.encode(data));
  55. } catch (e) {}
  56. logger.info(re.data);
  57. if (await checkingReturnParameter(re.data)) {
  58. sCallBack(re, false);
  59. // EasyLoading.dismiss();
  60. } else {
  61. sCallBack(re, true);
  62. }
  63. } on DioError catch (e) {
  64. switch (e.type) {
  65. case DioErrorType.cancel:
  66. fCallBack(e);
  67. EasyLoading.showToast('请求已取消');
  68. break;
  69. case DioErrorType.connectTimeout:
  70. fCallBack(e);
  71. EasyLoading.showToast('链接超时');
  72. break;
  73. case DioErrorType.other:
  74. fCallBack(e);
  75. EasyLoading.showToast('网络错误');
  76. break;
  77. case DioErrorType.receiveTimeout:
  78. fCallBack(e);
  79. EasyLoading.showToast('接受超时');
  80. break;
  81. case DioErrorType.response:
  82. fCallBack(e);
  83. EasyLoading.showToast('服务器异常');
  84. break;
  85. case DioErrorType.sendTimeout:
  86. fCallBack(e);
  87. EasyLoading.showToast('网络不佳');
  88. break;
  89. default:
  90. fCallBack(e);
  91. EasyLoading.showToast('网络错误');
  92. }
  93. }
  94. }
  95. get(String url, SCallBack sCallBack, FCallBack fCallBack) async {
  96. try {
  97. Response re = await dio.get(url);
  98. logger.info(MyCookie().getServer() + url);
  99. logger.info(re.data);
  100. if (await checkingReturnParameter(re.data)) {
  101. sCallBack(re, false);
  102. // EasyLoading.dismiss();
  103. } else {
  104. sCallBack(re, true);
  105. }
  106. } on DioError catch (e) {
  107. switch (e.type) {
  108. case DioErrorType.cancel:
  109. fCallBack(e);
  110. EasyLoading.showToast('请求已取消');
  111. break;
  112. case DioErrorType.connectTimeout:
  113. fCallBack(e);
  114. EasyLoading.showToast('您的网络不稳定,请稍后重试');
  115. break;
  116. case DioErrorType.other:
  117. fCallBack(e);
  118. EasyLoading.showToast('您的网络不稳定,请稍后重试');
  119. break;
  120. case DioErrorType.receiveTimeout:
  121. fCallBack(e);
  122. EasyLoading.showToast('您的网络不稳定,请稍后重试');
  123. break;
  124. case DioErrorType.response:
  125. fCallBack(e);
  126. EasyLoading.showToast('您的网络不稳定,请稍后重试');
  127. break;
  128. case DioErrorType.sendTimeout:
  129. fCallBack(e);
  130. EasyLoading.showToast('您的网络不稳定,请稍后重试');
  131. break;
  132. default:
  133. fCallBack(e);
  134. EasyLoading.showToast('您的网络不稳定,请稍后重试');
  135. }
  136. }
  137. }
  138. query(data, SCallBack sCallBack, FCallBack fCallBack) {
  139. post('/data/query', data, sCallBack, fCallBack);
  140. }
  141. save(data, SCallBack sCallBack, FCallBack fCallBack) {
  142. post('/data/save', data, sCallBack, fCallBack);
  143. }
  144. del(data, SCallBack sCallBack, FCallBack fCallBack) {
  145. post('/data/delete', data, sCallBack, fCallBack);
  146. }
  147. update(data, SCallBack sCallBack, FCallBack fCallBack) {
  148. post('/data/update', data, sCallBack, fCallBack);
  149. }
  150. updateM(data, SCallBack sCallBack, FCallBack fCallBack) {
  151. post('/data/updateM', data, sCallBack, fCallBack);
  152. }
  153. loginUpload(File _image, String user, String token, SCallBack sCallBack,
  154. FCallBack fCallBack, BuildContext context) async {
  155. var s = '/file/upload?token=$token&user=$user';
  156. logger.info(MyCookie().getServer() + s);
  157. var name = _image.path
  158. .substring(_image.path.lastIndexOf("/") + 1, _image.path.length);
  159. print(name);
  160. FormData formData = new FormData.fromMap({
  161. '__files__': await MultipartFile.fromFile(
  162. _image.path,
  163. filename: name,
  164. ),
  165. });
  166. post(s, formData, sCallBack, fCallBack);
  167. }
  168. upload(File _image, SCallBack sCallBack, FCallBack fCallBack,
  169. BuildContext context) async {
  170. var s = '/file/upload?${MyCookie().getUT()}';
  171. logger.info(MyCookie().getServer() + s);
  172. var name = _image.path
  173. .substring(_image.path.lastIndexOf("/") + 1, _image.path.length);
  174. print(name);
  175. FormData formData = new FormData.fromMap({
  176. '__files__': await MultipartFile.fromFile(
  177. _image.path,
  178. filename: name,
  179. ),
  180. });
  181. post(s, formData, sCallBack, fCallBack);
  182. }
  183. loginUpdate(data, String user, String token, SCallBack sCallBack,
  184. FCallBack fCallBack, BuildContext context) {
  185. String s = '/rdm/update?token=$token&user=$user';
  186. logger.info(MyCookie().getServer() + s);
  187. post(s, data, sCallBack, fCallBack);
  188. }
  189. Future<bool> checkingReturnParameter(data) async {
  190. SimpleBean simpleBean;
  191. try {
  192. simpleBean = SimpleBean.fromJson(json.decode(data));
  193. } catch (e) {
  194. print(e);
  195. }
  196. if (simpleBean != null) {
  197. if (simpleBean.error == null || simpleBean.error.length == 0) {
  198. return true;
  199. } else {
  200. EasyLoading.showToast(simpleBean.error);
  201. if (simpleBean.error.contains('token') ||
  202. simpleBean.error.contains('会话过期,请重新登录') ||
  203. simpleBean.error.contains('登录失效,请重新登录')) {
  204. MyCookie().clean();
  205. RootPageView().bNIndex = 0;
  206. navigatorKey.currentState.pushNamedAndRemoveUntil(
  207. '/loginPage', ModalRoute.withName("/loginPage"));
  208. }
  209. return false;
  210. }
  211. } else {
  212. return false;
  213. }
  214. }
  215. }