import 'dart:convert'; import 'package:bbyyy/beans/pay_way_bean_entity.dart'; import 'package:bbyyy/beans/user_bean_entity.dart'; import 'package:bbyyy/beans/withdraw_pricing_bean_entity.dart'; import 'package:bbyyy/https/MyDio.dart'; import 'package:bbyyy/my_tools/dims.dart'; import 'package:bbyyy/my_tools/easy_loading/easy_loading.dart'; import 'package:bbyyy/my_tools/event_bus.dart'; import 'package:bbyyy/my_tools/my_apis.dart'; import 'package:bbyyy/my_tools/my_colors.dart'; import 'package:bbyyy/my_tools/my_cookie.dart'; import 'package:bbyyy/my_tools/my_tools.dart'; import 'package:bbyyy/my_tools/my_views.dart'; import 'package:bbyyy/paegs/mine_page/bind_alipay_page/bind_alipay_page.dart'; import 'package:bbyyy/pay/pay_tools.dart'; import 'package:flustars/flustars.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/gestures.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; class WithdrawPage extends StatefulWidget { @override _WithdrawPageState createState() => _WithdrawPageState(); } class _WithdrawPageState extends State { TextEditingController _controller = TextEditingController(); int withdrawalStatus = 1; //0--未提现 1--提现中 2--提现完成 double percentFee = 0.0; double extraFee = 0.0; double threshold = 0.0; double maxPerHand = 0.0; double minPerHand = 0.0; int withdrawalPath = -1; // 1---支付宝 2---微信 List payWays; bool showAlipay = false; bool showWeChat = false; bool showHandlingFee = false; double handlingFee = 0.0; @override void initState() { // TODO: implement initState super.initState(); queryPersonalInformation(); queryWithdrawalInstructions(); Future.delayed(Duration.zero, () { checkPaymentMethod((response, hasError) { if (!hasError) { PayWayBeanEntity entity = PayWayBeanEntity().fromJson(json.decode(response.toString())); payWays = entity.data; showWeChat = payWays[payWays.indexWhere((element) => element.way == 2)] .withdraw; showAlipay = payWays[payWays.indexWhere((element) => element.way == 1)] .withdraw; if (MyCookie().userBean.wxOpenid.isNotEmpty && payWays[payWays.indexWhere((element) => element.way == 2)] .withdraw) { withdrawalPath = 2; } else if (MyCookie().userBean.aliPayAccount.isNotEmpty && payWays[payWays.indexWhere((element) => element.way == 1)] .withdraw) { withdrawalPath = 1; } setState(() {}); } }, (e) {}); }); } @override Widget build(BuildContext context) { return GestureDetector( onTap: () { MyTools().hideKeyboard(context); }, behavior: HitTestBehavior.translucent, child: Scaffold( backgroundColor: MyColors.cF7F7F7, body: Column( children: [ MyViews().myAppBar('申请提现', context, [ GestureDetector( onTap: () { MyTools().toPage(context, BindAlipayPage(), (then) { setState(() {}); }); }, behavior: HitTestBehavior.translucent, child: Container( padding: EdgeInsets.symmetric(vertical: 8, horizontal: 16), child: Text( '修改绑定', style: TextStyle(color: MyColors.c333333, fontSize: 16), ), ), ), ]), Expanded( child: SingleChildScrollView( child: Column( children: [ if (showAlipay) GestureDetector( onTap: () { if (!payWays[payWays .indexWhere((element) => element.way == 1)] .withdraw) { showToast('支付宝提现暂时维护中'); return; } if (MyCookie().userBean.aliPayAccount.isEmpty) { showToast('未绑定支付宝'); } else { setState(() { withdrawalPath = 1; }); } }, behavior: HitTestBehavior.translucent, child: Container( margin: EdgeInsets.fromLTRB(17, 24, 17, 0), child: Row( children: [ Container( decoration: BoxDecoration( color: withdrawalPath == 1 ? MyColors.cFF4233 : MyColors.cD0D0D0, borderRadius: BorderRadius.circular(9)), height: 18, width: 18, child: Icon( Icons.check, color: Colors.white, size: 14, ), alignment: Alignment.center, margin: EdgeInsets.only(right: 10), ), MyViews().myText( '提现到支付宝:${MyCookie().userBean.aliPayAccount.isEmpty ? '未绑定支付宝' : MyCookie().userBean.aliPayAccount}', MyColors.c666666, 15), ], ), ), ), if (showWeChat) GestureDetector( onTap: () { // if (!payWays[payWays // .indexWhere((element) => element.way == 2)] // .withdraw) { // showToast('微信提现暂时维护中'); // return; // } if (MyCookie().userBean.wxOpenid.isEmpty) { showToast('未绑定微信'); } else { setState(() { withdrawalPath = 2; }); } }, behavior: HitTestBehavior.translucent, child: Container( margin: EdgeInsets.fromLTRB(17, 24, 17, 0), child: Row( children: [ Container( decoration: BoxDecoration( color: withdrawalPath == 2 ? MyColors.cFF4233 : MyColors.cD0D0D0, borderRadius: BorderRadius.circular(9)), height: 18, width: 18, child: Icon( Icons.check, color: Colors.white, size: 14, ), alignment: Alignment.center, margin: EdgeInsets.only(right: 10), ), MyViews().myText('提现到微信:', MyColors.c666666, 15), MyCookie().userBean.wxOpenid.isEmpty ? MyViews() .myText('未绑定微信', MyColors.c666666, 15) : Row( children: [ Container( margin: EdgeInsets.only(right: 10), child: ClipRRect( borderRadius: BorderRadius.circular(12), child: MyViews().netImg( MyCookie().userBean.wxPicture, 24, 24), ), ), MyViews().myText( MyCookie().userBean.wxName, MyColors.c666666, 15) ], ) ], ), ), ), Container( decoration: BoxDecoration( color: Colors.white, borderRadius: BorderRadius.circular(4), ), margin: EdgeInsets.symmetric(horizontal: 17, vertical: 14), padding: EdgeInsets.symmetric(horizontal: 20, vertical: 15), child: Column( children: [ MyViews().myText('提现金额', MyColors.c333333, 16), Container( margin: EdgeInsets.only(top: 15), child: Row( children: [ Text( '¥', style: TextStyle( color: MyColors.c333333, fontSize: 30), ), Expanded( child: TextField( inputFormatters: [ FilteringTextInputFormatter.allow( RegExp("[0-9.]")), MoneyTextInputFormatter() ], controller: _controller, cursorColor: MyColors.cFF4233, cursorWidth: 1.0, decoration: InputDecoration( border: InputBorder.none, disabledBorder: InputBorder.none, enabledBorder: InputBorder.none, focusedBorder: InputBorder.none, hintText: '请输入金额', hintStyle: TextStyle( color: MyColors.c999999, fontSize: 16), isDense: true, contentPadding: const EdgeInsets.fromLTRB( 14, 4.5, 8, 4.5)), maxLines: 1, style: TextStyle( color: MyColors.c333333, fontSize: 30, fontWeight: FontWeight.bold, height: 1.3, letterSpacing: 0.2), keyboardType: TextInputType.number, onChanged: (t) { try { showH(); double amount = double.parse(t); if (amount > 0) { withdrawalStatus = 0; setState(() {}); } } catch (e) { withdrawalStatus = 1; } setState(() {}); }, ), ) ], ), ), Container( margin: EdgeInsets.only(top: 4, bottom: 15), color: MyColors.cE7E7E7, height: 0.5, ), RichText( text: TextSpan( text: '当前零钱余额', style: TextStyle( color: MyColors.c999999, fontSize: 15), children: [ TextSpan( text: '${MyCookie().userBean.balance.toStringAsFixed(2)}', style: TextStyle( color: MyColors.cFF4233, fontSize: 15), ), TextSpan( text: '元,', style: TextStyle( color: MyColors.c999999, fontSize: 15), ), TextSpan( text: '全部提现', style: TextStyle( color: MyColors.cFF4233, fontSize: 15), recognizer: TapGestureRecognizer() ..onTap = () { _controller.text = MyCookie() .userBean .balance .toStringAsFixed(2); showH(); setState(() { withdrawalStatus = 0; }); }), ]), ), if (showHandlingFee) RichText( text: TextSpan( text: '手续费', style: TextStyle( color: MyColors.c999999, fontSize: 15), children: [ TextSpan( text: '${handlingFee.toStringAsFixed(2)}', style: TextStyle( color: MyColors.cFF4233, fontSize: 15), ), ]), ), if (showHandlingFee) RichText( text: TextSpan( text: '实际到账', style: TextStyle( color: MyColors.c999999, fontSize: 15), children: [ TextSpan( text: '${(double.parse(_controller.text.toString()) - handlingFee).toStringAsFixed(2)}', style: TextStyle( color: MyColors.cFF4233, fontSize: 15), ), ]), ), GestureDetector( onTap: () { if (withdrawalPath == -1) { showToast('请选择提现方式'); return; } if (MyCookie().userBean.forbidWithdraw) { showSimpleDialog('你已经被冻结提现,请联系群主或客服。', context, () { Navigator.pop(context); }); return; } if (withdrawalStatus == 0) { withdrawalStatus = 1; withdraw(); } else {} }, behavior: HitTestBehavior.translucent, child: Container( decoration: BoxDecoration( color: withdrawalStatus == 0 ? MyColors.cFF4233 : MyColors.cEFEFEF, borderRadius: BorderRadius.circular(20)), child: MyViews().myText('提现', Colors.white, 14), alignment: Alignment.center, height: 40, margin: EdgeInsets.only(top: 20, bottom: 15), ), ), Container( margin: EdgeInsets.only(bottom: 8), child: RichText( text: TextSpan( text: '您的提现手续费为:', style: TextStyle( color: MyColors.c999999, fontSize: 14), children: [ TextSpan( text: '${NumUtil.multiply(percentFee, 100)}%${extraFee == 0 ? '' : '+$extraFee'}', style: TextStyle( color: MyColors.cE2A62D, fontSize: 14), ), // TextSpan( // text: '元', // style: TextStyle( // color: MyColors.c999999, // fontSize: 14), // ), ]), ), ), Visibility( visible: maxPerHand != 0, child: Container( margin: EdgeInsets.only(bottom: 8), child: RichText( text: TextSpan( text: '单笔最高限额:', style: TextStyle( color: MyColors.c999999, fontSize: 14), children: [ TextSpan( text: '$maxPerHand', style: TextStyle( color: MyColors.cE2A62D, fontSize: 14), ), TextSpan( text: '元', style: TextStyle( color: MyColors.c999999, fontSize: 14), ), ]), ), ), ), Visibility( visible: minPerHand != 0, child: Container( margin: EdgeInsets.only(bottom: 8), child: RichText( text: TextSpan( text: '单笔最低限额:', style: TextStyle( color: MyColors.c999999, fontSize: 14), children: [ TextSpan( text: '$minPerHand', style: TextStyle( color: MyColors.cE2A62D, fontSize: 14), ), TextSpan( text: '元', style: TextStyle( color: MyColors.c999999, fontSize: 14), ), ]), ), ), ), Visibility( visible: threshold != 0, child: RichText( text: TextSpan( text: '当日免手续费提现额度:', style: TextStyle( color: MyColors.c999999, fontSize: 14), children: [ TextSpan( text: '$threshold', style: TextStyle( color: MyColors.cE2A62D, fontSize: 14), ), TextSpan( text: '元', style: TextStyle( color: MyColors.c999999, fontSize: 14), ), ]), ), ), ], crossAxisAlignment: CrossAxisAlignment.start, ), ) ], ), ), ), ], ), ), ); } void withdraw() { try { double amount = double.parse(_controller.text.toString()); if (amount <= MyCookie().userBean.balance && amount >= minPerHand) { var j = { 'user_uid': MyCookie().getUID(), 'way': withdrawalPath, 'amount': amount }; if (withdrawalPath == 2) { j['wx_openid'] = MyCookie().userBean.wxOpenid; } print(j.toString()); MyDio().post(MyApis.getApi('withdraw'), j, (response, hasError) { if (!hasError) { showToast('提现成功'); withdrawalStatus = 2; queryPersonalInformation(); } else { setState(() { withdrawalStatus = 0; }); } }, (error) { setState(() { withdrawalStatus = 0; }); }); } else if (amount > MyCookie().userBean.balance) { setState(() { withdrawalStatus = 0; }); showSimpleDialog('您的余额不足。', context, () { Navigator.pop(context); }); } else { setState(() { withdrawalStatus = 0; }); showSimpleDialog('提现失败:提现金额不能低于$minPerHand元。', context, () { Navigator.pop(context); }); } } catch (e) { setState(() { withdrawalStatus = 0; EasyLoading.dismiss(); }); showSimpleDialog('输入内容有误,请重新输入', context, () { Navigator.pop(context); }); } } void queryPersonalInformation() { MyDio().query({ "key": "user", "filters": { "conditions": ["uid == ${MyCookie().getUID()}"] }, "dims": userDims, "paging": [1, 20] }, (response, hasError) { if (!hasError) { UserBeanEntity entity = UserBeanEntity().fromJson(json.decode(response.data.toString())); MyCookie().userBean = entity.data.data[0]; EventBus().emit('userChange'); setState(() {}); if (withdrawalStatus == 2) { Navigator.pop(context); } } }, (error) {}); } void queryWithdrawalInstructions() { MyDio().query({ "key": "withdraw_pricing", "filters": {}, "dims": withdrawPricingDims, "paging": [1, 20] }, (response, hasError) { if (!hasError) { WithdrawPricingBeanEntity entity = WithdrawPricingBeanEntity() .fromJson(json.decode(response.data.toString())); if (entity.data.data.isNotEmpty) { percentFee = entity.data.data[0].percentFee; extraFee = entity.data.data[0].extraFee; threshold = entity.data.data[0].threshold; maxPerHand = entity.data.data[0].maxPerHand; minPerHand = entity.data.data[0].minPerHand; } } setState(() {}); }, (error) {}); } showH() { try { double amount = double.parse(_controller.text.toString()); if (amount >= minPerHand) { handlingFee = double.parse((double.parse(_controller.text.toString()) * percentFee * 100) .toStringAsFixed(0)) / 100 + extraFee; setState(() { showHandlingFee = true; }); } else { setState(() { showHandlingFee = false; }); } } catch (e) { setState(() { showHandlingFee = false; }); } } } class MoneyTextInputFormatter extends TextInputFormatter { @override TextEditingValue formatEditUpdate( TextEditingValue oldValue, TextEditingValue newValue) { // TODO: implement formatEditUpdate String newvalueText = newValue.text; if (newvalueText == ".") { //第一个数为. newvalueText = "0."; } else if (newvalueText.contains(".")) { if (newvalueText.lastIndexOf(".") != newvalueText.indexOf(".")) { //输入了2个小数点 newvalueText = newvalueText.substring(0, newvalueText.lastIndexOf('.')); } else if (newvalueText.length - 1 - newvalueText.indexOf(".") > 2) { //输入了1个小数点 小数点后两位 newvalueText = newvalueText.substring(0, newvalueText.indexOf(".") + 3); } } return TextEditingValue( text: newvalueText, selection: new TextSelection.collapsed(offset: newvalueText.length), ); } }