base_page_presenter.dart 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import 'dart:io';
  2. import 'package:dio/dio.dart';
  3. import 'package:liftmanager/net/api_service.dart';
  4. import 'package:liftmanager/net/dio_utils.dart';
  5. import 'package:liftmanager/net/error_handle.dart';
  6. import 'package:meta/meta.dart';
  7. import 'mvps.dart';
  8. class BasePagePresenter<V extends IMvpView> extends IPresenter {
  9. V view;
  10. CancelToken _cancelToken;
  11. BasePagePresenter(){
  12. _cancelToken = CancelToken();
  13. }
  14. @override
  15. void deactivate() {}
  16. @override
  17. void didChangeDependencies() {}
  18. @override
  19. void didUpdateWidgets<W>(W oldWidget) {}
  20. @override
  21. void dispose() {
  22. /// 销毁时,将请求取消
  23. if (!_cancelToken.isCancelled){
  24. _cancelToken.cancel();
  25. }
  26. }
  27. @override
  28. void initState() {}
  29. /// 返回Future 适用于刷新,加载更多
  30. Future requestNetwork<T>(Method method, {@required String url, bool isShow : true, bool isClose: true, Function(T t) onSuccess,
  31. Function(List<T> list) onSuccessList, Function(int code, String msg) onError, dynamic params,
  32. Map<String, dynamic> queryParameters, CancelToken cancelToken, Options options, bool isList : false}) async {
  33. if (isShow) view.showProgress();
  34. await DioUtils.instance.requestNetwork<T>(method, url,
  35. params: params,
  36. queryParameters: queryParameters,
  37. options: options,
  38. cancelToken: cancelToken?? _cancelToken,
  39. onSuccess: (data){
  40. if (isClose) view.closeProgress();
  41. if (onSuccess != null) {
  42. onSuccess(data);
  43. }
  44. },
  45. onSuccessList: (data){
  46. if (isClose) view.closeProgress();
  47. if (onSuccessList != null) {
  48. onSuccessList(data);
  49. }
  50. },
  51. onError: (code, msg){
  52. _onError(code, msg, onError);
  53. }
  54. );
  55. }
  56. void asyncRequestNetwork<T>(Method method, {@required String url, bool isShow : true, bool isClose: true, Function(T t) onSuccess, Function(List<T> list) onSuccessList, Function(int code, String msg) onError,
  57. dynamic params, Map<String, dynamic> queryParameters, CancelToken cancelToken, Options options, bool isList : false}){
  58. if (isShow) view.showProgress();
  59. DioUtils.instance.asyncRequestNetwork<T>(method, url,
  60. params: params,
  61. queryParameters: queryParameters,
  62. options: options,
  63. cancelToken: cancelToken?? _cancelToken,
  64. isList: isList,
  65. onSuccess: (data){
  66. if (isClose) view.closeProgress();
  67. if (onSuccess != null) {
  68. onSuccess(data);
  69. }
  70. },
  71. onSuccessList: (data){
  72. if (isClose) view.closeProgress();
  73. if (onSuccessList != null) {
  74. onSuccessList(data);
  75. }
  76. },
  77. onError: (code, msg){
  78. _onError(code, msg, onError);
  79. }
  80. );
  81. }
  82. /// 上传图片实现
  83. Future<String> uploadImg(File image) async{
  84. String imgPath = "";
  85. try{
  86. String path = image.path;
  87. var name = path.substring(path.lastIndexOf("/") + 1);
  88. FormData formData = FormData.fromMap({
  89. "uploadIcon": await MultipartFile.fromFile(path, filename: name)
  90. });
  91. await requestNetwork<String>(Method.post,
  92. url: ApiUrl.account_smscode,
  93. params: formData,
  94. onSuccess: (data){
  95. imgPath = data;
  96. }
  97. );
  98. }catch(e){
  99. view.showToast("图片上传失败!");
  100. }
  101. return imgPath;
  102. }
  103. _onError(int code, String msg, Function(int code, String msg) onError){
  104. /// 异常时直接关闭加载圈,不受isClose影响
  105. view.closeProgress();
  106. if (code != ExceptionHandle.cancel_error){
  107. view.showToast(msg);
  108. }
  109. /// 页面如果dispose,则不回调onError
  110. if (onError != null && view.getContext() != null) {
  111. onError(code, msg);
  112. }
  113. }
  114. }