http_utils.dart 4.06 KB
import 'package:flutter/cupertino.dart';
import 'package:flutter_mes2/utils/Constant.dart';
import 'package:flutter_mes2/utils/ToastUtil.dart';
import 'package:flutter_mes2/view/progress_dialog.dart';

/**
 *  http_utils.dart
 *
 *  Created by iotjin on 2020/07/07.
 *  description:  项目接口请求 管理类
 */


import 'apis.dart';
import 'dio_utils.dart';
import 'log_utils.dart';

typedef Success<T> = Function(T data);
typedef Fail = Function(int code);

class HttpUtils {

  static ProgressDialog pr;

  //登录
  static void login<T>(
      BuildContext context,
    parameters, {
    Success success,
    Fail fail,
  }) {
    post(context, HttpConstant.LOGIN, parameters, success: success, fail: fail);
  }


  static void pdaWeld<T>(
      BuildContext context,
      parameters, {
        Success success,
        Fail fail,
      }) {
    post(context, HttpConstant.PDAWELD, parameters, success: success, fail: fail);
  }

  static void pdaStation<T>(
      BuildContext context,
      parameters, {
        Success success,
        Fail fail,
      }) {
    post(context, HttpConstant.PDAStation, parameters, success: success, fail: fail);
  }

  static void pdaError<T>(
      BuildContext context,
      parameters, {
        Success success,
        Fail fail,
      }) {
    post(context, HttpConstant.PDAError, parameters, success: success, fail: fail);
  }

  static void pdaWeidHandle<T>(
      BuildContext context,
      parameters, {
        Success success,
        Fail fail,
      }) {
    post(context, HttpConstant.PDAWeidHandle, parameters, success: success, fail: fail);
  }


  //分页加载数据
  static void getNewPageList<T>(
      BuildContext context,
    parameters, {
    Success success,
    Fail fail,
  }) {
    post(context, APIs.getPage, parameters, success: success, fail: fail);
  }

  //分页加载分组数据
  static void getNewPageGroupList<T>(
      BuildContext context,
      parameters, {
        Success success,
        Fail fail,
      }) {
    post(context, APIs.getGroupPage, parameters, success: success, fail: fail);
  }

  //GET
  static void GetRequest<T>(
      BuildContext context,
    APIType apiType,
    parameters, {
    Success success,
    Fail fail,
  }) {
    post(context, APITypeValues[apiType], parameters, success: success, fail: fail);
  }

  //POST
  static void PostRequest<T>(
      BuildContext context,
    APIType apiType,
    parameters, {
    Success success,
    Fail fail,
  }) {
    post(context, APITypeValues[apiType], parameters, success: success, fail: fail);
  }

  /********************************* 分割线 ********************************/

  //get 请求
  static void get<T>(
      BuildContext context,
    String url,
    parameters, {
    Success success,
    Fail fail,
  }) {
    _request(context, Method.GET, url, parameters, success: success, fail: fail);
  }

  //post 请求
  static void post<T>(
      BuildContext context,
    String url,
    parameters, {
    Success success,
    Fail fail,
  }) {
    _request(context, Method.POST, url, parameters, success: success, fail: fail);
  }

  //_request 请求
  static void _request<T>(
      BuildContext context,
    Method method,
    String url,
    parameters, {
    Success success,
    Fail fail,
  }) {
    pr = new ProgressDialog(context, ProgressDialogType.Normal);
//    pr.setMessage('正在加载信息...');
//    pr.show();
    /// restful 请求处理
    /// /base/search/hist/:user_id        user_id=27
    /// 最终生成 url 为     /base/search/hist/27
    parameters.forEach((key, value) {
      if (url.indexOf(key) != -1) {
        url = url.replaceAll(':$key', value.toString());
      }
    });

    DioUtils.request(method, url, parameters, success: (result) {

      if (result['code'] == 200) {
        if (success != null) {
          success(result);
        }
      } else {
        //其他状态,弹出错误提示信息
        ToastUtil.showText(result['msg']);
      }
    }, fail: (code, msg) {
      if(pr.isShowing()) {
        pr.hide();
      }

      ToastUtil.showText(msg);
      if (fail != null) {
        fail(code);
      }
    });
  }
}