import 'dart:io';
import 'package:base_app/models/common/banner_model.dart';
import 'package:image_picker/image_picker.dart';
import 'package:base_app/common/common.dart';
import 'package:base_app/data/api/apis.dart';
import 'package:base_app/data/net/dio_util.dart';
import 'package:base_app/data/protocol/base_resp.dart';
import 'package:dio/dio.dart';

class CommonRepository {
  // 上传图片
  Future<String> uploadImage(PickedFile image) async {
    String path = image.path;
    var name = path.substring(path.lastIndexOf("/") + 1, path.length);
    FormData formData =
        new FormData.from({"file": new UploadFileInfo(new File(path), name)});

    BaseResp baseResp = await DioUtil().request(
        Method.post, AppApi.getPath(path: AppApi.IMAGE_UPLOAD),
        data: formData);
    if (baseResp.code != Constant.status_success) {
      return Future.error(baseResp.message);
    } else {
      return baseResp.data;
    }
  }

  // 获取banner
  Future<List<BannerModel>> getBanner() async {
    BaseResp<List> baseResp = await DioUtil()
        .request<List>(Method.get, AppApi.getPath(path: AppApi.BANNER));
    List<BannerModel> bannerList;
    if (baseResp.code != Constant.status_success) {
      return new Future.error(baseResp.message);
    }
    if (baseResp.data != null) {
      bannerList = baseResp.data.map<BannerModel>((value) {
        return BannerModel.fromJson(value);
      }).toList();
    }
    return bannerList;
  }
}
