base_info_provider.dart 7.83 KB
import 'dart:async';
import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:one_key/constant/constant.dart';
import 'package:one_key/constant/error.dart';
import 'package:one_key/constant/state.dart';
import 'package:one_key/models/base_info_model.dart';
import 'package:one_key/models/seller_phone_model.dart';
import 'package:one_key/models/table_photo_model.dart';
import 'package:one_key/models/user_history_entity.dart';
import 'package:one_key/net/base_resp.dart';
import 'package:one_key/net/dio_util.dart';
import 'package:one_key/net/http_url.dart';
import 'package:one_key/provider/user_model.dart';
import 'package:one_key/utils/utils.dart';
import 'package:provider/provider.dart';

import '../app.dart';
import '../models/user.dart';

class BaseIndoProvider with ChangeNotifier {
  BaseInfoSunModel? baseInfoSunModel; //客户经理基础信息
  Map<String, List<String?>> photoList = {}; //商家图片信息

  PageState pageState = PageState.Loading;

  TextEditingController phoneController = TextEditingController();
  TextEditingController nameController = TextEditingController();
  TextEditingController pnumController = TextEditingController();

  late List sexList;
  List<UserHistoryEntity> userHistorys = []; //历史录入
  BaseIndoProvider() {
    initSexList();
  }

  ///称谓
  initSexList() {
    String sexStr = '1';
    sexList = [
      {
        'title': '先生',
        'select': sexStr == '1' ? true : false,
        'code': '1',
        'hasLeftBorder': true
      },
      {
        'title': '女士',
        'select': sexStr == '2' ? true : false,
        'code': '2',
        'hasLeftBorder': false
      },
      {
        'title': '公司',
        'select': sexStr == '3' ? true : false,
        'code': '3',
        'hasLeftBorder': false
      },
      {
        'title': '贵宾',
        'select': sexStr == '4' ? true : false,
        'code': '4',
        'hasLeftBorder': false
      }
    ];
  }

  //获取历史录入
  getUserHistorys(BuildContext context, Map<String, String?> params) async {
    try {
      BaseResp<List?> baseResp = await DioUtil().request<List>(
          context, Method.post, HttpUrl.get_reserve_user,
          data: params, showLoading: false);
      if (baseResp != null && baseResp.code == Constant.status_success) {
        userHistorys = baseResp.data!.map<UserHistoryEntity>((value) {
          return UserHistoryEntity.fromJson(value);
        }).toList();
        notifyListeners();
      }
    } catch (error) {
      print(error.toString());
    }
  }

  ///客户经理添加/修改基本信息
  Future<dynamic> setCustomerActivityBaseinfo(
      BuildContext context, Map<String, String?> params,
      {isShowLoading = true}) async {
    try {
      BaseResp<dynamic> baseResp = await DioUtil().request<dynamic>(
          context, Method.post, HttpUrl.set_customer_activity_baseinfo,
          data: params, showLoading: isShowLoading);
      if (baseResp.code == Constant.status_success) {
        if (baseResp.data != null) {
          // coupons = baseResp.data['aid'].split('');
          return true;
        } else {
          // Utils.showToast(baseResp.msg ?? "修改失败");
          return false;
        }
      } else {
        // if (baseResp != null) {
        // Utils.showToast(baseResp.msg ?? "");
        // }
        return false;
      }
    } catch (error) {
      print(error.toString());
      if (error is MyError) {}
      return false;
    }
  }

  ///客户经理获取基本信息
  Future<bool> getCustomerActivityBaseinfo(
      BuildContext context, Map<String, String?> params,
      {isShowLoading = true}) async {
    try {
      BaseResp<dynamic> baseResp = await DioUtil().request<dynamic>(
          context, Method.post, HttpUrl.get_customer_activity_baseinfo,
          data: params, showLoading: isShowLoading);
      if (baseResp.code == Constant.status_success) {
        if (baseResp.data == null || baseResp.data == false) {
          return false;
        } else {
          baseInfoSunModel = BaseInfoModel.fromJson(
                  baseResp.responseData as Map<String, dynamic>)
              .data;
          // for (int i = 0; i < couponListSelect.length; i++) {
          //   coupons.add(couponListSelect[i].id);
          //   // couponsShow.add(couponTemp[i].id);
          // }
          // hasModify = true;
          if (App.sp!.containsKey('user')) {
            String s = App.sp!.getString('user')!;
            User _user = User.fromJson(json.decode(s));
            _user.userphoneMyPage = baseInfoSunModel!.phone;

            await UserModel().saveUserInfo(_user);
          }
          notifyListeners();

          return true;
        }
      } else {
        // if (baseResp != null) {
          Utils.showToast(baseResp.msg ?? "");
        // }
        return false;
      }
    } catch (error) {
      print(error.toString());
      Map<String, String?> params = {
        'sellerid':
            Provider.of<UserModel>(context, listen: false).user!.sellerid,
        "uid": Provider.of<UserModel>(context, listen: false).user!.openid,
      };
      getCustomerActivityBaseinfo(context, params, isShowLoading: false);
      if (error is MyError) {}
    
    }
      return false;
  }

  // ///根据商家ID获取对应类型图片
  // Future<bool> getSellerPhotoById(
  //     BuildContext context, Map<String, String> params,
  //     {isShowLoading = true}) async {
  //   BaseResp<dynamic> baseResp = await DioUtil()
  //       .request<dynamic>(context, Method.post, HttpUrl.get_seller_photo_by_id,
  //           data: params, showLoading: isShowLoading)
  //       !!.catchError((error) {
  //     print(error.toString());
  //     if (error is MyError) {}
  //   });
  //   if (baseResp.code == Constant.status_success) {
  //     if (baseResp.data == null || baseResp.data == false) {
  //       return false;
  //     } else {
  //       photoList = SellerPhoneModel.fromJson(baseResp.responseData).data;
  //       // for (int i = 0; i < couponListSelect.length; i++) {
  //       //   coupons.add(couponListSelect[i].id);
  //       //   // couponsShow.add(couponTemp[i].id);
  //       // }
  //       // hasModify = true;
  //       notifyListeners();
  //       return true;
  //     }
  //   } else {
  //     if (baseResp != null) {
  //       Utils.showToast(baseResp.msg ?? "");
  //     }
  //     return false;
  //   }
  // }

  ///App获取多个桌位关联图片
  Future<dynamic> getTablesPhoto(
      BuildContext context, Map<String, String?> params,
      {isShowLoading = true}) async {
        try {
          BaseResp<dynamic> baseResp = await DioUtil().request<dynamic>(
            context, Method.post, HttpUrl.get_tables_photo,
            data: params, showLoading: isShowLoading); 
             if (baseResp.code == Constant.status_success) {
      if (baseResp.data == null) {
      } else {
        List<TablePhotoSunModel?>? tablePhotoList = TablePhotoModel.fromJson(
                baseResp.responseData as Map<String, dynamic>)
            .data;
        String tid = params['tableids']!;
        List<String> tids = tid.split(',');
        Map<String, List<String?>> photos = {};
        for (String tid in tids) {
          photos[tid] = [];
        }
        for (String tid in tids) {
          tablePhotoList!.forEach((item) {
            // for (TablePhotoSunModel item in tablePhotoList!) {
            if (tid == item!.tableid) {
              List<String?> list = photos.putIfAbsent(tid, () => []);
              list.add(item.url);
              photos[tid] = list;
            }
          });
        }
        print(photos);
        photoList = photos;
        // photoList.add(tablePhotoList[i].url);
        notifyListeners();
      }
    } else {
      // if (baseResp != null) {
        Utils.showToast(baseResp.msg ?? "");
      // }
      return [];
    }
        } catch (error) {
      print(error.toString());
      if (error is MyError) {}
    }
   
  }
}