import 'package:dio/dio.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_baidu_mapapi_base/flutter_baidu_mapapi_base.dart'; import 'package:flutter_baidu_mapapi_utils/flutter_baidu_mapapi_utils.dart'; import 'package:flutter_smart_dialog/flutter_smart_dialog.dart'; import 'package:fluwx/fluwx.dart'; import 'package:huixiang/generated/l10n.dart'; import 'package:huixiang/retrofit/data/address.dart'; import 'package:huixiang/retrofit/data/base_data.dart'; import 'package:huixiang/retrofit/data/down_order.dart'; import 'package:huixiang/retrofit/data/min_order_info.dart'; import 'package:huixiang/retrofit/data/placeOrderFirst.dart'; import 'package:huixiang/retrofit/data/settleOrderInfo.dart'; import 'package:huixiang/retrofit/data/store_info.dart'; import 'package:huixiang/retrofit/data/wx_pay.dart'; import 'package:huixiang/retrofit/min_api.dart'; import 'package:huixiang/settlement/settlement_view/activity_coupon_remarks.dart'; import 'package:huixiang/settlement/settlement_view/distribution.dart'; import 'package:huixiang/settlement/settlement_view/edit_phone.dart'; import 'package:huixiang/settlement/settlement_view/pay_method.dart'; import 'package:huixiang/settlement/settlement_view/settlement_activity.dart'; import 'package:huixiang/settlement/settlement_view/settlement_coupon.dart'; import 'package:huixiang/settlement/settlement_view/settlement_order_commodity.dart'; import 'package:huixiang/utils/font_weight.dart'; import 'package:huixiang/utils/min.dart'; import 'package:huixiang/view_widget/my_appbar.dart'; import 'package:huixiang/view_widget/round_button.dart'; import 'package:huixiang/view_widget/tips_dialog.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart'; class Settlement extends StatefulWidget { final arguments; Settlement({this.arguments}); @override State createState() { return _Settlement(); } } class _Settlement extends State { MinApiService minService; Function(int type) callback; StoreInfo storeInfo; // ShoppingCart shopCarGoods; SettleOrderInfo settleOrderInfo; MinOrderInfo minOrderInfo; int selectedBtn = 0; String remakers = "口味、面包硬度等"; String parentCode = ""; String parentId = ""; int payChannel = 4; int tableId = 0; int numberOfPeople = 0; Address address; CouponListBean couponListBean; PromotionInfoListBean promotion; String mobile; bool placeOrder = false; @override void initState() { super.initState(); tableId = widget.arguments["tableId"] ?? 0; parentCode = widget.arguments["parentCode"] ?? ""; parentId = widget.arguments["parentId"] ?? ""; storeInfo = widget.arguments["storeInfo"]; numberOfPeople = widget.arguments["numberOfPeople"] ?? 0; if (tableId == 0) { placeOrder = true; } SharedPreferences.getInstance().then((value) { String minToken = value.getString("minToken"); String tenant = value.getString("tenant"); String storeId = value.getString("storeId"); minService = MinApiService( Dio(), context: context, token: minToken, tenant: tenant, storeId: storeId, ); queryOrderInfo(null, selectedBtn, null, 0, null); }); } ///订单结算信息 queryOrderInfo( addressId, isTake, memberCouponId, orderId, promotionId, ) async { BaseData baseData = await minService.getOrderInfo({ "addressId": addressId, "isTake": isTake, "memberCouponId": memberCouponId, "orderId": orderId, "promotionId": promotionId }).catchError((error) {}); if (baseData != null && baseData.isSuccess) { setState(() { settleOrderInfo = baseData.data; if (settleOrderInfo.orderProductList == null || settleOrderInfo.orderProductList.length == 0) { placeOrder = true; queryOrderDetails(parentId); } }); } } ///查看订单详情 queryOrderDetails(id) async { print("ididididididid: $id"); BaseData baseData = await minService.getOrderDetails({ "id": id, }).catchError((error) {}); if (baseData != null && baseData.isSuccess) { setState(() { minOrderInfo = baseData.data; }); } } queryAddress(int selectedBtn) async { this.selectedBtn = selectedBtn; if (address != null) { queryOrderInfo(address.id, selectedBtn, null, 0, null); return; } BaseData> baseDate = await minService.queryAddress().catchError((error) {}); BMFCoordinate bmfCoordinate = BMFCoordinate( double.tryParse(storeInfo.latitude), double.tryParse(storeInfo.longitude), ); if (baseDate != null && baseDate.isSuccess) { address = baseDate.data[0]; for (int i = 1; i < baseDate.data.length; i++) { Address address1 = baseDate.data[i]; BMFCoordinate coordinate = await BMFCalculateUtils.coordConvert( coordinate: BMFCoordinate( double.tryParse(address.longitude), double.tryParse(address.latitude), ), fromType: BMF_COORD_TYPE.COMMON, toType: BMF_COORD_TYPE.BD09LL); BMFCoordinate coordinate1 = await BMFCalculateUtils.coordConvert( coordinate: BMFCoordinate( double.tryParse(address1.longitude), double.tryParse(address1.latitude), ), fromType: BMF_COORD_TYPE.COMMON, toType: BMF_COORD_TYPE.BD09LL); double mi = await BMFCalculateUtils.getLocationDistance( bmfCoordinate, coordinate); double mi1 = await BMFCalculateUtils.getLocationDistance( bmfCoordinate, coordinate1); if (mi1 < mi) { address = address1; } } queryOrderInfo(address.id, selectedBtn, null, 0, null); } } PlaceOrderFirst placeOrderFirst; DownOrder downOrder; ///小程序下单 queryPlaceOrderFirst() async { for (int i = 0; i < settleOrderInfo.orderProductList.length; i++) { settleOrderInfo.orderProductList.forEach((element1) { if (settleOrderInfo.orderProductList[i].productId == element1.productId) { settleOrderInfo.orderProductList[i].skuId = element1.skuId; } }); } placeOrderFirst = PlaceOrderFirst(); placeOrderFirst.cartNum = settleOrderInfo.orderNum; placeOrderFirst.cartSum = settleOrderInfo.price; placeOrderFirst.addressId = address == null ? null : address.id; placeOrderFirst.isSubscribe = false; placeOrderFirst.isTakeOut = selectedBtn; placeOrderFirst.notes = remakers; placeOrderFirst.numberOfPeople = "$numberOfPeople"; placeOrderFirst.orderSource = 2; placeOrderFirst.orderType = "0"; placeOrderFirst.orderTypeId = 0; placeOrderFirst.parentCode = parentCode; // 火锅加菜 placeOrderFirst.parentId = parentId; // 火锅加菜 placeOrderFirst.payChannel = payChannel; placeOrderFirst.promotionInfoDTO = PromotionInfoDTOBean(); placeOrderFirst.promotionInfoDTO.promotionId = promotion != null ? promotion.id : ""; placeOrderFirst.promotionInfoDTO.couponId = couponListBean != null ? couponListBean.id : ""; placeOrderFirst.recMobile = (mobile == null || mobile == "") ? mobile : storeInfo.headMobile; placeOrderFirst.shoppingCartSkuItemList = settleOrderInfo.orderProductList; placeOrderFirst.skuItemDTOList = []; placeOrderFirst.source = 1; placeOrderFirst.storeId = storeInfo.id; placeOrderFirst.subcribeTime = null; placeOrderFirst.tableId = "$tableId"; if (tableId == 0) { ///正常的商店下单 BaseData baseData = await minService .placeOrderFirst(placeOrderFirst.toJson()) .catchError((error) { print("error: $error"); }); if (baseData != null && baseData.isSuccess) { placeOrder = true; this.downOrder = baseData.data; querySettlement(); } } else { ///火锅店下单, 等待结算 BaseData baseData; if (parentId == "") { // 下单 if (placeOrder) { // 结算 querySettlement(); } else { // 下单 baseData = await minService .placeOrderFirst(placeOrderFirst.toJson()) .catchError((error) { print("error: $error"); }); } } else { if (placeOrder) { // 结算 querySettlement(); } else { //加菜 baseData = await minService .addOrder(placeOrderFirst.toJson()) .catchError((error) { print("error: $error"); }); } } if (baseData != null && baseData.isSuccess) { placeOrder = true; this.downOrder = baseData.data; queryOrderDetails( (parentId == null || parentId == "") ? baseData.data.id : parentId); setState(() {}); } } } ///结算 querySettlement() async { placeOrderFirst.id = parentId != "" ? parentId : downOrder.id; placeOrderFirst.orderProductVOList = downOrder == null ? minOrderInfo.orderProductVOList : downOrder.orderProductVOList; if (placeOrderFirst.payChannel == 1) { if (!(await Min.isInitialize())) { // 小程序的微信支付和app的充值支付使用同一个WXPayEntryActivity回调, // 然而充值时小程序未初始化会导致回调内部代码调用getPackage空指针, // 故而在此初始化一下 await Min.initialize(); } BaseData baseData = await minService .settlementWx(placeOrderFirst.toJson()) .catchError((error) {}); if (baseData != null && baseData.isSuccess) { WxPay wxPay = baseData.data; await registerWxApi( appId: wxPay.appId, doOnAndroid: true, universalLink: "https://hx.lotus-wallet.com/app/", ); payWithWeChat( appId: wxPay.appId, partnerId: wxPay.partnerId, prepayId: wxPay.prepayId, packageValue: wxPay.packageValue, nonceStr: wxPay.nonceStr, timeStamp: int.tryParse(wxPay.timeStamp), sign: wxPay.sign, ); weChatResponseEventHandler.listen((event) async { print("payCallback: ${event.errCode}"); toOrderDetails(placeOrderFirst.id); }); } } else { BaseData baseData = await minService .settlementApi(placeOrderFirst.toJson()) .catchError((error) {}); if (baseData != null && baseData.isSuccess) { toOrderDetails(placeOrderFirst.id); } else { SmartDialog.show(widget: Tips((){}, text: "${baseData.msg}",)); } } } toOrderDetails(String orderId) { Navigator.of(context).popAndPushNamed( '/router/order_details', arguments: { "id": orderId, }, ); } @override Widget build(BuildContext context) { return Stack( children: [ Positioned( top: 0, left: 0, right: 0, child: Container( height: 175.h, color: Color(0xFF3A405A), width: MediaQuery.of(context).size.width, ), ), Positioned( child: Scaffold( backgroundColor: Colors.transparent, appBar: MyAppBar( background: Color(0xFF3A405A), leadingColor: Colors.white, title: S.of(context).dingdanjiesuan, titleColor: Colors.white, brightness: Brightness.dark, titleSize: 18.sp, ), body: Container( child: Column( children: [ Expanded( child: Container( child: SingleChildScrollView( physics: BouncingScrollPhysics(), child: Container( child: Column( children: [ DistributionMode( queryOrderInfo, queryAddress, storeInfo, address, ), SizedBox( height: 16, ), /// 预留的手机号, 可修改 EditPhoneWidget( storeInfo, mobileChange, ), ///订单商品 SettlementOrderCommodity( selectedBtn, settleOrderInfo, minOrderInfo, ), ///优惠券/备注 ActivityCouponRemarks( couponCart, activityCart, settleOrderInfo, coupons, promotions, couponCount(), remakers, (){ Navigator.of(context).pushNamed( '/router/edit_remarks_page', arguments: {}, ).then((value) => {setState(() { if(value != null) remakers = value; })}); } ), if (placeOrder) ///支付方式 PayMethod(payChannelCheck), ], ), ), ), ), flex: 1, ), Container( height: 54.h, color: Colors.white, child: Row( crossAxisAlignment: CrossAxisAlignment.center, children: [ Spacer(), Text( S.of(context).heji, style: TextStyle( fontSize: 12.sp, fontWeight: MyFontWeight.regular, color: Colors.black, ), ), Text( minOrderInfo != null ? "¥${minOrderInfo.orderSumPrice}" : "¥${settleOrderInfo == null ? "0" : settleOrderInfo.price}", style: TextStyle( fontSize: 20.sp, fontWeight: MyFontWeight.semi_bold, color: Color(0xFF32A060), ), ), SizedBox( width: 28, ), RoundButton( width: 103.w, height: 54.h, text: placeOrder ? S.current.jiesuan : "下单", textColor: Colors.white, fontWeight: MyFontWeight.regular, backgroup: Color(0xFF32A060), fontSize: 16.sp, padding: EdgeInsets.symmetric( vertical: 5.h, ), callback: () { queryPlaceOrderFirst(); }, ), ], ), ), ], ), ), ), left: 0, right: 0, top: 0, bottom: 0, ), ], ); } payChannelCheck(int payChannel) { this.payChannel = payChannel; } mobileChange(String mobile) { this.mobile = mobile; } int couponCount() { if (settleOrderInfo == null || settleOrderInfo.couponList == null || settleOrderInfo.couponList.length == 0) return 0; int count = 0; settleOrderInfo.couponList.forEach((element) { if (element.tenantCode == "") { count++; } }); return count; } String coupons = ""; String promotions = ""; ///优惠券弹窗 couponCart() async { CouponListBean couponBean = await showModalBottomSheet( context: context, backgroundColor: Colors.transparent, builder: (context) { return SettlementCoupon(settleOrderInfo, storeInfo, couponBean: couponListBean); }, ); if (couponBean != null) { this.couponListBean = couponBean; coupons = couponListBean.promotionName; promotions = ""; queryOrderInfo( address != null ? address.id : null, selectedBtn, couponListBean != null ? couponListBean.id : null, 0, null, ); } } ///活动弹窗 activityCart() async { // if (couponListBean != null) return; PromotionInfoListBean pro = await showModalBottomSheet( context: context, backgroundColor: Colors.transparent, builder: (context) { return SettlementActivity(settleOrderInfo, storeInfo); }, ); if (pro != null) { this.promotion = pro; promotions = promotion.name; coupons = ""; queryOrderInfo( address != null ? address.id : null, selectedBtn, null, 0, promotion != null ? promotion.id : null, ); } } }