import 'dart:math'; import 'package:flustars/flustars.dart' as FlutterStars; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_alipay/flutter_alipay.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart'; import 'package:flutter_spinkit/flutter_spinkit.dart'; import 'package:fluwx/fluwx.dart' as fluwx; import 'package:liftmanager/common/common.dart'; import 'package:liftmanager/internal/wode/model/table_dicts_model.dart'; import 'package:liftmanager/net/api_service.dart'; import 'package:liftmanager/res/resources.dart'; import 'package:liftmanager/routers/fluro_navigator.dart'; import 'package:liftmanager/utils/fast_notification.dart'; import 'package:liftmanager/utils/theme_utils.dart'; import 'package:liftmanager/utils/toast.dart'; import 'package:liftmanager/widgets/app_bar.dart'; import 'package:liftmanager/widgets/load_image.dart'; import 'package:liftmanager/internal/bbs/model/expert_model.dart' as expertM; class QuestionPay extends StatefulWidget { QuestionPay(this.id, this.type, this.name, this.userHeadUrl, {this.expert}); final String id; final String expert; final String name; final String type; final String userHeadUrl; @override State createState() { return QuestionPayState(); } } class QuestionPayState extends State { static const payType = [ { "icon": "tab_first/pay_yue", "title": "账户余额", }, { "icon": "tab_first/pay_zhifubao", "title": "支付宝", }, { "icon": "tab_first/pay_winxin", "title": "微信", }, ]; String checkFalse = "tab_first/check_false"; String checkTrue = "tab_first/check_true"; int indexNow = -1; String isRed = ""; String moneyKey; List moneyList; String payTypeString = ""; int rewardType; String title = ""; dynamic _wxPay; expertM.Records userInfo; @override void initState() { super.initState(); SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle.light); setSchema(); if (widget.type == "question") { title = "问题"; payTypeString = "QUESTION"; rewardType = 1; } else if (widget.type == "video") { title = "视频"; payTypeString = "VIDEO"; rewardType = 2; } getData(); } getData() async { await getRewardSecond(); await getbalance(); await getMoney(); await getUserInfo(); setState(() {}); } getUserInfo() async { await NewApiService().getExpertDetail(widget.expert, onSuccess: (res) { userInfo = res; }); } void setSchema() async { // await FlutterAlipay.setIosUrlSchema("tytylift"); await FlutterAlipay.setIosUrlSchema("com.tyty.lift.managers"); } Future getMoney() async { await NewApiService().queryConstant("reward_setting", "reward", onSuccess: (res) { moneyList = res; setState(() {}); }, onError: (code, msg) { toasts(msg); }); } String balance = '0'; Future getbalance() async { await NewApiService().getUserCoupons(onSuccess: (res) { setState(() { balance = res["balance"] != null ? '${res["balance"]}' : '0'; }); }, onError: (code, msg) { toasts(msg); }); } String rewardSecond = '0'; Future getRewardSecond() async { await NewApiService().getRewardSecond( id: widget.expert, onSuccess: (res) { if (mounted) { setState(() { rewardSecond = res != null ? '$res' : '0'; }); } }, onError: (code, msg) { toasts(msg); }); } randomInt(int min, int max) { return new Random().nextInt(max) % (max - min + 1) + min; } Future lastMoneyPay() async { await NewApiService().getAlipayBalanceApp({ "orders": widget.id, "payType": payTypeString, "rewardType": rewardType, "reawrdMoney": moneyKey, "userId": FlutterStars.SpUtil.getString(Constant.userId), }, onSuccess: (res) { // moneyList = res; toasts("打赏成功"); String initThisUserMoney = randomInt(1111, 9999).toString() + DateTime.now().millisecondsSinceEpoch.toString(); FastNotification.push("initUserMoney", initThisUserMoney); FastNotification.push("initIsPay", initThisUserMoney); Navigator.of(context).pop(); }, onError: (code, msg) { toasts(msg); }); } // 确认充值 void confirmReward() async { if (moneyKey == null) { toasts("请选择打赏金额"); return; } if (indexNow == -1) { toasts("请选择打赏支付方式"); return; } String payway; if (indexNow == 1) { payway = 'alipay'; await NewApiService().alipayReward( orders: widget.id, payway: payway, rewardType: rewardType, reawrdMoney: moneyKey, onSuccess: (res) { callAlipay(res); }, onError: (code, msg) { toasts(msg); }, ); } else if (indexNow == 0) { showAlert( context, "提示", "确定支付?", "确定", () { lastMoneyPay(); NavigatorUtils.goBack(context); }, txt2: "取消", onPre2: () { NavigatorUtils.goBack(context); }, ); } else if (indexNow == 2) { payway = 'wx'; await NewApiService().wxReward( orders: widget.id, payway: payway, rewardType: rewardType, reawrdMoney: moneyKey, onSuccess: (res) { wxMethod(res); }, onError: (code, msg) { toasts(msg); }, ); } else { toasts("暂不支持该支付方式"); return; } } wxMethod(_payInfo) { print(_payInfo["appid"]); fluwx .payWithWeChat( appId: _payInfo["appid"], partnerId: _payInfo["partnerid"], prepayId: _payInfo["prepayid"], packageValue: _payInfo["package"], nonceStr: _payInfo["noncestr"], timeStamp: int.parse(_payInfo["timestamp"]), sign: _payInfo["sign"]) .then((data) { print(data); }); _wxPay?.cancel(); _wxPay = fluwx.responseFromPayment .listen((fluwx.WeChatPaymentResponse response) async { print("WeChatPaymentResponse" + response.errCode.toString()); if (response.errCode == -2) { //支付取消 toasts("支付取消"); } else if (response.errCode == -1) { //支付失败 toasts("支付失败"); } else if (response.errCode == 0) { //支付成功 toasts("打赏成功"); String initThisUserMoney = randomInt(1111, 9999).toString() + DateTime.now().millisecondsSinceEpoch.toString(); FastNotification.push("initUserMoney", initThisUserMoney); FastNotification.push("initIsPay", initThisUserMoney); Navigator.pop(context); } if (!mounted) return; setState(() {}); }); } // 调用支付宝 void callAlipay(String _payInfo) async { AlipayResult payResult; try { print("The pay info is : " + _payInfo); payResult = await FlutterAlipay.pay(_payInfo); } on Exception catch (e) { payResult = null; } print(payResult); if (payResult != null) { if (payResult.resultStatus == "9000") { toasts("打赏成功"); String initThisUserMoney = randomInt(1111, 9999).toString() + DateTime.now().millisecondsSinceEpoch.toString(); FastNotification.push("initUserMoney", initThisUserMoney); FastNotification.push("initIsPay", initThisUserMoney); Navigator.pop(context); } else { toasts(payResult.memo); } } else { toasts("支付异常"); } } Widget loadCircle() { return Container( padding: EdgeInsets.only(top: 10, bottom: 10), color: ThemeUtils.getTabsBg(context), child: Center( child: SpinKitFadingCircle( color: Colors.blueAccent, size: 30.0, ), ), ); } @override Widget build(BuildContext context) { double width = MediaQuery.of(context).size.width; double height = MediaQuery.of(context).size.height; return Container( child: Scaffold( appBar: MyAppBar( statubarStyle: SystemUiOverlayStyle.light, centerTitle: "打赏", titleColor: Colors.white, bgColors: [Color(0xff5887FF), Color(0xff00ACFF)], ), body: Container( child: Stack( children: [ Container( color: Colors.white, // padding: EdgeInsets.only(top:70), child: ListView( children: [ Container( height: ScreenUtil().setHeight(200), // color: bgColors == null ? Colors.white : null, decoration: BoxDecoration( gradient: LinearGradient( colors: [Color(0xff5887FF), Color(0xff00ACFF)], ), ), child: Column( children: [ SizedBox( height: ScreenUtil().setHeight(20), ), ClipRRect( borderRadius: BorderRadius.circular( ScreenUtil().setWidth(35)), child: Container( child: LoadNetworkImage( widget.userHeadUrl, // item.avatarUrl, // fit: BoxFit.fitWidth, width: ScreenUtil().setWidth(70), height: ScreenUtil().setWidth(70), ), )), SizedBox( height: ScreenUtil().setHeight(10), ), Row( crossAxisAlignment: CrossAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.center, children: [ Text( '${widget.name} ', style: TextStyle( fontSize: ScreenUtil().setSp(16), color: Colors.white), ), (widget.type == "video" || userInfo?.expertLevel == null) ? Container() : Container( padding: EdgeInsets.fromLTRB(5, 1, 5, 1), decoration: BoxDecoration( //背景 color: Colors.red, //设置四周圆角 角度 borderRadius: BorderRadius.all( Radius.circular(4.0)), ), child: Text( 'Lv${userInfo?.expertLevel}', style: TextStyle(color: Colors.white), ), ) ], ), Text( '收到打赏次数 $rewardSecond', style: TextStyle( fontSize: ScreenUtil().setSp(11), color: Colors.white), ), SizedBox( height: ScreenUtil().setHeight(30), ), Text( '客官,如果您觉得答案不错,给TA一点鼓励吧~', style: TextStyle( fontSize: ScreenUtil().setSp(13), color: Colors.white), ) ], ), ), Container( padding: EdgeInsets.only( // left: ScreenUtil().setWidth(20), right: ScreenUtil().setWidth(15), top: ScreenUtil().setWidth(15), bottom: ScreenUtil().setWidth(15)), decoration: BoxDecoration( // border: Border( // bottom: // BorderSide(width: 5, color: Color(0xfff5f5f5)), // ), ), child: Row( // mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Container( // margin: EdgeInsets.only( // top: ScreenUtil().setHeight(10)), width: ScreenUtil().setWidth(2), height: ScreenUtil().setHeight(13), color: Color(0xFF568AFF), ), SizedBox(width: ScreenUtil().setWidth(10)), Text( "请选择打赏金额", style: TextStyle( color: Color(0xff222222), fontSize: ScreenUtil().setSp(16)), textAlign: TextAlign.start, ), ]), ), moneyList != null && moneyList != [] ? Container( padding: EdgeInsets.only( left: ScreenUtil().setWidth(10)), child: Wrap( alignment: WrapAlignment.start, crossAxisAlignment: WrapCrossAlignment.center, children: moneyList.asMap().keys.map((index) { return InkWell( onTap: () { setState(() { moneyKey = moneyList[index].key; isRed = moneyList[index].key; print(isRed); print(moneyList[index].key); }); }, child: Container( alignment: Alignment.center, width: width / 4 - 15, height: ScreenUtil().setHeight(50), // padding: EdgeInsets.only( // bottom: ScreenUtil().setHeight(26), // top: ScreenUtil().setHeight(26)), margin: EdgeInsets.only( left: 5, right: 5, bottom: 5, top: 5), decoration: BoxDecoration( color: isRed == moneyList[index].key ? Color(0x215589FF) : Colors.white, border: Border.all( width: 1, color: isRed == moneyList[index].key ? Colours.blue_app_main : Color(0xff999999))), child: Text( (moneyList[index].value ?? "") + "元", style: TextStyle( color: isRed == moneyList[index].key ? Colours.blue_app_main : Color(0xff666666), fontSize: ScreenUtil().setSp(15)), textAlign: TextAlign.center, ), ), ); }).toList(), )) : loadCircle(), SizedBox( height: ScreenUtil().setHeight(10), ), // Color(0xffF9F9F9) Container( height: ScreenUtil().setHeight(10), color: Color(0xffF9F9F9), width: double.infinity, ), Container( padding: EdgeInsets.only( // left: ScreenUtil().setWidth(20), right: ScreenUtil().setWidth(15), top: ScreenUtil().setWidth(25), bottom: ScreenUtil().setWidth(10)), decoration: BoxDecoration( border: Border( bottom: BorderSide(width: 0.5, color: Color(0xfff5f5f5)), ), ), child: Row( // mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Container( // margin: EdgeInsets.only( // top: ScreenUtil().setHeight(10)), width: ScreenUtil().setWidth(2), height: ScreenUtil().setHeight(13), color: Color(0xFF568AFF), ), SizedBox(width: ScreenUtil().setWidth(10)), Text( "选择支付方式", style: TextStyle( color: Color(0xff000000), fontSize: ScreenUtil().setSp(16)), textAlign: TextAlign.start, ), ]), ), Container( padding: EdgeInsets.only( left: ScreenUtil().setWidth(15), bottom: ScreenUtil().setWidth(80), right: ScreenUtil().setWidth(15)), child: Column( children: payType.asMap().keys.map((i) { return Container( decoration: BoxDecoration( border: Border( bottom: BorderSide( width: 0.5, color: Color(0xfff5f5f5)), ), ), height: ScreenUtil().setWidth(60), child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Row( children: [ LoadAssetImage( // image: AssetImage(i['img']), payType[i]["icon"], width: ScreenUtil().setWidth(26), height: ScreenUtil().setWidth(26), color: i == 0 ? Colours.blue_app_main : null, // alignment: Alignment.centerLeft, ), Container(width: 5, child: null), Text( payType[i]["title"], style: TextStyle( color: Color(0xff000000), fontSize: ScreenUtil().setSp(16)), textAlign: TextAlign.start, ), i == 0 ? Text( '¥$balance', style: TextStyle( color: Color(0xffCCCCCC), fontSize: 14), ) : Container() ], ), InkWell( child: Container( // padding: EdgeInsets.only(top:10,left:5,right:5,bottom:10), child: LoadAssetImage( // image: AssetImage(i['img']), indexNow == i ? checkTrue : checkFalse, width: ScreenUtil().setWidth(20), height: ScreenUtil().setWidth(20), // alignment: Alignment.centerLeft, ), ), onTap: () { setState(() { indexNow = i; print(indexNow); }); }, ), ], ), ); }).toList(), ), ) ], ), ), Positioned( bottom: 0, left: 0, child: Container( width: width, padding: EdgeInsets.only( top: ScreenUtil().setWidth(15), bottom: ScreenUtil().setWidth(15), left: ScreenUtil().setWidth(25), right: ScreenUtil().setWidth(25), ), color: Colors.white, child: Container( height: ScreenUtil().setWidth(44), decoration: BoxDecoration( borderRadius: BorderRadius.circular(22.0), color: Colours.blue_app_main, // gradient: const LinearGradient( // colors: [Color(0xFF00D9FF), Color(0xFF0287FF)], // ), ), child: FlatButton( // padding: EdgeInsets.all(15.0), child: Text("确认打赏"), textColor: Colors.white, onPressed: () { confirmReward(); }, ), ), ), ) ], ), ), ), ); } }