comment_manage_provider.dart 4.47 KB
import 'dart:async';

import 'package:flutter/material.dart';
import 'package:one_key/constant/state.dart';
import 'package:video_compress/video_compress.dart';

import '../constant/constant.dart';
import '../constant/error.dart';
import '../models/comments_label_model.dart';
import '../models/comments_model.dart';
import '../net/base_resp.dart';
import '../net/dio_util.dart';
import '../net/http_url.dart';
import '../utils/utils.dart';

class CommentManageProvider extends ChangeNotifier {
  PageState pageState = PageState.Loading;

  List<CommentsSunModel>? commentsData = [];
  List<CommentsLabelSunModel>? commentsLabeldata = [];

  ///回复商家评价
  Future<dynamic> setComment(
      BuildContext context, Map<String, String?> params,
      {isShowLoading = true}) async {
    try {
      BaseResp<dynamic> baseResp = await DioUtil().request<dynamic>(
          context, Method.post, HttpUrl.set_comment,
          data: params, showLoading: isShowLoading);
      if (baseResp.code == Constant.status_success) {
        notifyListeners();
        return true;
        // } else {
        //   return false;
        // }
      } else {
        if (baseResp != null) {
          Utils.showToast(baseResp.msg ?? "");
        }

        notifyListeners();
        return false;
      }
    } catch (error) {
      print(error.toString());
      if (error is MyError) {
        // switch (error.type) {
        //   case 1:
        //     pageState = PageState.ConnectivityError;
        //     break;
        //   case 2:
        //     pageState = PageState.ConnectivityTimeOut;
        //     break;
        //   default:
        //     pageState = PageState.ConnectivityError;
        //     break;
        // }
      }
      // notifyListeners();
      return false;
    }
  }

  ///获取标签
  Future<dynamic> getCountCommentLabel(
      BuildContext context, Map<String, String> params,
      {isShowLoading = true}) async {
    try {
      BaseResp<dynamic> baseResp = await DioUtil().request<dynamic>(
          context, Method.post, HttpUrl.get_count_comment_label,
          data: params, showLoading: isShowLoading);
      if (baseResp.code == Constant.status_success) {
        commentsLabeldata = CommentsLabelModel.fromJson(
                baseResp.responseData as Map<String, dynamic>)
            .data;

        notifyListeners();
        return true;
        // } else {
        //   return false;
        // }
      } else {
        // if (baseResp != null) {
        Utils.showToast(baseResp.msg ?? "");
        // }

        notifyListeners();
        return false;
      }
    } catch (error) {
      print(error.toString());
      if (error is MyError) {
        // switch (error.type) {
        //   case 1:
        //     pageState = PageState.ConnectivityError;
        //     break;
        //   case 2:
        //     pageState = PageState.ConnectivityTimeOut;
        //     break;
        //   default:
        //     pageState = PageState.ConnectivityError;
        //     break;
        // }
      }
      // notifyListeners();
      return false;
    }
  }

  ///获取评价列表
  Future<dynamic> getSellerComments(
      BuildContext context, Map<String, String?> params,
      {isShowLoading = true}) async {
    try {
      BaseResp<dynamic> baseResp = await DioUtil().request<dynamic>(
          context, Method.post, HttpUrl.get_seller_comments,
          data: params, showLoading: isShowLoading);
      if (baseResp.code == Constant.status_success) {
        commentsData = CommentsModel.fromJson(
                baseResp.responseData as Map<String, dynamic>)
            .data;

        if (commentsData!.length == 0) {
          pageState = PageState.Empty;
        } else {
          pageState = PageState.Success;
        }

        notifyListeners();
        return true;
        // } else {
        //   return false;
        // }
      } else {
        // if (baseResp != null) {
          Utils.showToast(baseResp.msg ?? "");
        // }
        pageState = PageState.ConnectivityError;
        notifyListeners();
        return false;
      }
    } catch (error) {
      print(error.toString());
      if (error is MyError) {
        switch (error.type) {
          case 1:
            pageState = PageState.ConnectivityError;
            break;
          case 2:
            pageState = PageState.ConnectivityTimeOut;
            break;
          default:
            pageState = PageState.ConnectivityError;
            break;
        }
      }
      notifyListeners();
      return false;
    }
  }
}