import 'dart:async'; import 'dart:convert'; import 'package:dio/dio.dart'; import 'package:dio/src/form_data.dart' as f; import 'package:dio/src/response.dart' as r; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:flutter_smart_dialog/flutter_smart_dialog.dart'; import 'package:get/get.dart'; import 'package:huixiang/constant.dart'; import 'package:huixiang/data/activity_act_record_details.dart'; import 'package:huixiang/data/activity_area_list.dart'; import 'package:huixiang/data/activity_details.dart'; import 'package:huixiang/data/activity_order.dart'; import 'package:huixiang/data/address.dart'; import 'package:huixiang/data/base_data.dart'; import 'package:huixiang/data/base_list_data.dart'; import 'package:huixiang/data/find_mini_group.dart'; import 'package:huixiang/data/home_recommend_list.dart'; import 'package:huixiang/data/launch_join_act.dart'; import 'package:huixiang/data/member_recharge.dart'; import 'package:huixiang/data/min_order_info.dart'; import 'package:huixiang/data/mini_detail.dart'; import 'package:huixiang/data/settle_order_info.dart'; import 'package:huixiang/data/shoping_home_config.dart'; import 'package:huixiang/data/shopping_cart.dart'; import 'package:huixiang/data/store_info.dart'; import 'package:huixiang/data/wx_pay.dart'; import 'package:huixiang/generated/l10n.dart'; import 'package:huixiang/utils/flutter_utils.dart'; import 'package:huixiang/view_widget/login_tips_dialog.dart'; import 'package:retrofit/retrofit.dart'; part 'min_api.g.dart'; ///调用小程序的接口 @RestApi(baseUrl: kReleaseMode ? serviceMiniBaseUrl : localMiniBaseUrl) abstract class MinApiService { factory MinApiService( Dio dio, { String? baseUrl, required BuildContext context, String? token, String? tenant, String? storeId, bool showLoading = false, ParseErrorLogger? errorLogger, }) { Map headers = (token == null || token == "") ? {} : {'token': "Bearer $token"}; if (tenant != null && tenant != "") { headers["tenant"] = tenant; } if (storeId != null && storeId != "") { headers["store-id"] = storeId; } baseUrl = kReleaseMode ? serviceMiniBaseUrl : localMiniBaseUrl; headers["Environment"] = "app"; dio.options = BaseOptions( connectTimeout: 36000.milliseconds, receiveTimeout: 36000.milliseconds, headers: headers, responseType: ResponseType.json, ); dio.interceptors.add( InterceptorsWrapper(onRequest: ( RequestOptions options, RequestInterceptorHandler handler, ) { debugPrint("\n======================= Min_请求数据 ======================="); debugPrint("method = ${options.method.toString()}"); debugPrint("url = ${options.uri.toString()}"); debugPrint("headers = ${options.headers}"); if (showLoading && !SmartDialog.checkExist(dialogTypes: {SmartAllDialogType.loading})) { AppUtils.cancelCloseLoading(); //是否显示loading S.current.zhengzaijiazai.loading; } if (options.data is f.FormData) { debugPrint("params data = FormData"); } else { debugPrint("params data = ${jsonEncode(options.data)}"); } debugPrint("params queryParameters = ${options.queryParameters}"); handler.next(options); }, onResponse: ( r.Response response, ResponseInterceptorHandler handler, ) { debugPrint("\n======================= Min_响应数据开始 ======================="); if (SmartDialog.checkExist(dialogTypes: {SmartAllDialogType.loading})) { AppUtils.timerCloseLoading(); } debugPrint("code = ${response.statusCode}"); p(jsonEncode(response.data)); Map map = response.data; if (map["code"] == 40005 || map["code"] == 40001) { LoginTipsDialog.instance.show(context); } debugPrint("======================= Min_响应数据结束 =======================\n"); if (map["code"] == 0) { handler.next(response); } else { throw DioException( requestOptions: response.requestOptions, type: DioExceptionType.unknown, response: response, message: "${map["msg"] ?? map["message"] ?? map["repMsg"]}", ); } }, onError: ( DioException e, ErrorInterceptorHandler handler, ) { if (SmartDialog.checkExist(dialogTypes: {SmartAllDialogType.loading})) { AppUtils.closeLoading(); } debugPrint("\n======================= Min_错误响应数据 ======================="); debugPrint("type = ${e.type}"); debugPrint("message = ${e.message}"); debugPrint("\n"); handler.next(e); }), ); if (kReleaseMode) { baseUrl = serviceMiniBaseUrl; } return _MinApiService(dio, baseUrl: baseUrl, errorLogger: errorLogger); } static void p(String msg) { int maxStrLength = 900; while (msg.length > maxStrLength) { debugPrint(msg.substring(0, maxStrLength), wrapWidth: maxStrLength); msg = msg.substring(maxStrLength); } debugPrint(msg, wrapWidth: maxStrLength); } /// 会员信息 @GET("/member/info") Future memberInfo(); ///小程序查询分组及商品列表 @POST("product/findMiNiGroupList") Future> findMiNiGroupList( @Body() Map param); ///小程序查询商品详情/規格选择 @GET("/product/queryMiNiProductDetail?id={id}") Future> miNiDetail(@Path("id") String id); ///添加购物车 @POST("shoppingcart") Future> addShoppingCart( @Body() Map param); ///获取购物车商品 ///?tableId={tableId} @GET("shoppingcart") Future> getShoppingCart( @Query("tableId") int tableId); ///清空购物车商品 // ?storeId={storeId} @GET("shoppingcart/delCart") Future> clearShoppingCart(@Query("storeId") num storeId); ///修改购物车 @PUT("shoppingcart") Future> shoppingCart1( @Body() Map param); ///修改购物车商品数量 @PUT("shoppingcart/single") Future> shoppingCartSingle( @Body() Map param); ///订单结算信息 @POST("order/getOrderInfo") Future> getOrderInfo( @Body() Map param); ///查看订单详情 @POST("order/getOrderDetail") Future> getOrderDetails( @Body() Map param); ///查询用户所有收货地址 @GET("address/queryMemberAddress") Future> queryAddress(); ///小程序下单 @POST("order/placeOrderFirst") Future> placeOrderFirst(@Body() Map param); ///小程序下单加菜 @POST("order/addOrder") Future> addOrder(@Body() Map param); /// 结算 @POST("order/settlement") Future> settlementWx(@Body() Map param); /// 结算 @POST("order/settlement") Future settlementApi(@Body() Map param); ///查询店铺商家详情 @POST("store/getStore") Future> queryStoreInfo1(@Body() Map param); /// 父订单信息 @GET("order/getParentInfo?tableId={tableId}") Future getParentInfo(@Path("tableId") String tableId); /// 小程序商品推荐/为你推荐 @GET("product/recommendList") Future> recommendList(); ///获取门店首页配置 @GET("store/homeConfig") Future> homeConfig(); ///活动专区,秒杀,砍价,拼团 @GET("actTemplate/findActListByType?allDay={allDay}&type={type}") Future> findActListByType( @Path("allDay") bool allDay, @Path("type") String type); /// APP、小程序点击查看活动列表的某一个活动详情 @GET("actTemplate/viewProduct?actProductId={actProductId}&type={type}") Future> viewProduct( @Path("actProductId") String actProductId, @Path("type") String type); /// 发起活动 @POST("actTemplate/launchAct") Future> launchAct(@Body() Map param); /// 参与活动 @POST("actTemplate/joinAct") Future> joinAct(@Body() Map param); /// 活动付款 @POST("actTemplate/actPay") Future?> actPay(@Body() Map param); /// 查看我的拼团、砍价、列表 @POST("actTemplate/showMyActList") Future> showMyActList( @Body() Map param); /// 查看某一个发起的拼团、砍价详情 @GET("actTemplate/showOneAct?actRecordId={actRecordId}") Future> showOneAct( @Path("actRecordId") String actRecordId); /// 小程序取消优惠券 @GET("promotion/cancelMemberCoupon?orderId={orderId}") Future cancelMemberCoupon(@Path("orderId") String orderId); /// 小程序使用该优惠券 @POST("promotion/useMemberCoupon") Future useMemberCoupon(@Body() Map param); ///商户充值 @POST("/member/recharge") Future> memberRecharge(@Body() Map param); ///充值列表 @GET("/store/getRechargePreferential") Future> memberRechargeList(); }