| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236 |
- import 'dart:convert';
- import 'dart:io';
- import 'package:bbyyy/beans/simple_bean.dart';
- import 'package:bbyyy/my_tools/easy_loading/easy_loading.dart';
- import 'package:bbyyy/my_tools/global.dart';
- import 'package:bbyyy/my_tools/my_cookie.dart';
- import 'package:bbyyy/paegs/root_page/root_page_view.dart';
- import 'package:dio/dio.dart';
- import 'package:flutter/cupertino.dart';
- import 'package:flutter/material.dart';
- import 'package:simple_logger/simple_logger.dart';
- ///http请求成功回调
- typedef SCallBack<T> = void Function(Response response, bool hasError);
- ///失败回调
- typedef FCallBack = void Function(DioError error);
- final logger = SimpleLogger();
- class MyDio {
- //私有构造函数
- MyDio._internal() {
- initDio();
- }
- //保存单例
- static MyDio _singleton = MyDio._internal();
- //工厂构造函数
- factory MyDio() => _singleton;
- Dio dio; // 使用默认配置
- BaseOptions options;
- initDio() {
- // 或者通过传递一个 `options`来创建dio实例
- options = BaseOptions(
- baseUrl: MyCookie().getServer(),
- connectTimeout: 30000,
- receiveTimeout: 3000,
- );
- if (MyCookie().loginInformation != null) {
- options.headers = {
- '__token__': MyCookie().getToken(),
- '__user__': MyCookie().getUser(),
- 'Content-Type': 'application/json'
- };
- }
- dio = Dio(options);
- }
- post(String url, data, SCallBack sCallBack, FCallBack fCallBack) async {
- logger.info(MyCookie().getServer() + url);
- try {
- logger.info(json.encode(data));
- } catch (e) {}
- try {
- Response re = await dio.post(url, data: data);
- print(dio.options.headers);
- logger.info(MyCookie().getServer() + url);
- try {
- logger.info(json.encode(data));
- } catch (e) {}
- logger.info(re.data);
- if (await checkingReturnParameter(re.data)) {
- sCallBack(re, false);
- // EasyLoading.dismiss();
- } else {
- sCallBack(re, true);
- }
- } on DioError catch (e) {
- switch (e.type) {
- case DioErrorType.cancel:
- fCallBack(e);
- EasyLoading.showToast('请求已取消');
- break;
- case DioErrorType.connectTimeout:
- fCallBack(e);
- EasyLoading.showToast('链接超时');
- break;
- case DioErrorType.other:
- fCallBack(e);
- EasyLoading.showToast('网络错误');
- break;
- case DioErrorType.receiveTimeout:
- fCallBack(e);
- EasyLoading.showToast('接受超时');
- break;
- case DioErrorType.response:
- fCallBack(e);
- EasyLoading.showToast('服务器异常');
- break;
- case DioErrorType.sendTimeout:
- fCallBack(e);
- EasyLoading.showToast('网络不佳');
- break;
- default:
- fCallBack(e);
- EasyLoading.showToast('网络错误');
- }
- }
- }
- get(String url, SCallBack sCallBack, FCallBack fCallBack) async {
- try {
- Response re = await dio.get(url);
- logger.info(MyCookie().getServer() + url);
- logger.info(re.data);
- if (await checkingReturnParameter(re.data)) {
- sCallBack(re, false);
- // EasyLoading.dismiss();
- } else {
- sCallBack(re, true);
- }
- } on DioError catch (e) {
- switch (e.type) {
- case DioErrorType.cancel:
- fCallBack(e);
- EasyLoading.showToast('请求已取消');
- break;
- case DioErrorType.connectTimeout:
- fCallBack(e);
- EasyLoading.showToast('您的网络不稳定,请稍后重试');
- break;
- case DioErrorType.other:
- fCallBack(e);
- EasyLoading.showToast('您的网络不稳定,请稍后重试');
- break;
- case DioErrorType.receiveTimeout:
- fCallBack(e);
- EasyLoading.showToast('您的网络不稳定,请稍后重试');
- break;
- case DioErrorType.response:
- fCallBack(e);
- EasyLoading.showToast('您的网络不稳定,请稍后重试');
- break;
- case DioErrorType.sendTimeout:
- fCallBack(e);
- EasyLoading.showToast('您的网络不稳定,请稍后重试');
- break;
- default:
- fCallBack(e);
- EasyLoading.showToast('您的网络不稳定,请稍后重试');
- }
- }
- }
- query(data, SCallBack sCallBack, FCallBack fCallBack) {
- post('/data/query', data, sCallBack, fCallBack);
- }
- save(data, SCallBack sCallBack, FCallBack fCallBack) {
- post('/data/save', data, sCallBack, fCallBack);
- }
- del(data, SCallBack sCallBack, FCallBack fCallBack) {
- post('/data/delete', data, sCallBack, fCallBack);
- }
- update(data, SCallBack sCallBack, FCallBack fCallBack) {
- post('/data/update', data, sCallBack, fCallBack);
- }
- updateM(data, SCallBack sCallBack, FCallBack fCallBack) {
- post('/data/updateM', data, sCallBack, fCallBack);
- }
- loginUpload(File _image, String user, String token, SCallBack sCallBack,
- FCallBack fCallBack, BuildContext context) async {
- var s = '/file/upload?token=$token&user=$user';
- logger.info(MyCookie().getServer() + s);
- var name = _image.path
- .substring(_image.path.lastIndexOf("/") + 1, _image.path.length);
- print(name);
- FormData formData = new FormData.fromMap({
- '__files__': await MultipartFile.fromFile(
- _image.path,
- filename: name,
- ),
- });
- post(s, formData, sCallBack, fCallBack);
- }
- upload(File _image, SCallBack sCallBack, FCallBack fCallBack,
- BuildContext context) async {
- var s = '/file/upload?${MyCookie().getUT()}';
- logger.info(MyCookie().getServer() + s);
- var name = _image.path
- .substring(_image.path.lastIndexOf("/") + 1, _image.path.length);
- print(name);
- FormData formData = new FormData.fromMap({
- '__files__': await MultipartFile.fromFile(
- _image.path,
- filename: name,
- ),
- });
- post(s, formData, sCallBack, fCallBack);
- }
- loginUpdate(data, String user, String token, SCallBack sCallBack,
- FCallBack fCallBack, BuildContext context) {
- String s = '/rdm/update?token=$token&user=$user';
- logger.info(MyCookie().getServer() + s);
- post(s, data, sCallBack, fCallBack);
- }
- Future<bool> checkingReturnParameter(data) async {
- SimpleBean simpleBean;
- try {
- simpleBean = SimpleBean.fromJson(json.decode(data));
- } catch (e) {
- print(e);
- }
- if (simpleBean != null) {
- if (simpleBean.error == null || simpleBean.error.length == 0) {
- return true;
- } else {
- EasyLoading.showToast(simpleBean.error);
- if (simpleBean.error.contains('token') ||
- simpleBean.error.contains('会话过期,请重新登录') ||
- simpleBean.error.contains('登录失效,请重新登录')) {
- MyCookie().clean();
- RootPageView().bNIndex = 0;
- navigatorKey.currentState.pushNamedAndRemoveUntil(
- '/loginPage', ModalRoute.withName("/loginPage"));
- }
- return false;
- }
- } else {
- return false;
- }
- }
- }
|