import 'dart:io';

import 'package:dio/dio.dart';
import 'package:netrain_flutter_app/douwen/custom/http/base/ApiException.dart';
import 'package:netrain_flutter_app/douwen/custom/http/base/HttpCode.dart';
import 'package:netrain_flutter_app/douwen/custom/http/base/HttpUtil.dart';

import 'IEntity.dart';

mixin IService {
  String getServicePath();

  ///获取请求对象
  Dio _getRequest() {
    return HttpUtil().getDio(HttpUtil.baseUrl + getServicePath() ?? "");
  }

  Future<IEntity> post(String path, data) => _getRequest()
      .post(path, data: data)
      .then((value) => _analysisData(value))
      .catchError((error) => _analysisError(error));

  IEntity _analysisData(Response<IEntity> value) {
    IEntity iEntity = value.data;
    return iEntity;
  }

  ///这里处理公共逻辑异常
  _analysisApiException(IEntity iEntity) {

  }

  IEntity _analysisError(var error) {
    IEntity iEntity;
    if (error is DioError) {
      dynamic e = error.error;
      if (e is ApiException) {
        iEntity = e.iEntity;
        _analysisApiException(iEntity);
      }else{
        iEntity = new IEntity(code: HttpCode.NORMAL_ERROR,msg:e.toString());
      }
    }
    return iEntity;
  }
}
