import 'package:flutter/material.dart'; import 'package:flutter/cupertino.dart'; import '../net/HttpManager.dart'; import '../net/Result.dart'; import '../styles/totast.dart'; import 'package:flutter_redux/flutter_redux.dart'; import '../redux/AppState.dart'; import '../model/ProductInfo.dart'; import '../redux/UserRedux.dart'; import '../model/UserInfo.dart'; import 'package:url_launcher/url_launcher.dart'; class ChoosePay extends StatefulWidget { ChoosePay(this.type, this.chooseProduct, this.payWay); int type; ProductInfo chooseProduct; int payWay; @override _ChoosePayState createState() => _ChoosePayState(); } class _ChoosePayState extends State with WidgetsBindingObserver { String chooseKey = ''; bool isAlipay = false; bool isWechat = false; bool isCloud_flash = false; bool isUnion_pay = false; int userId = 0; Future getUserInfo() async { Result res = await HttpManager.get('userInfo/getUserInfo'); if (res.success) { print(res.data); StoreProvider.of(context).dispatch(UpdateUserAction(UserInfo.fromJson(res.data))); userId = res.data['id']; } else {} } Future getInfo() async { Result res = await HttpManager.get('payCodeStock/allUsedPayType?money=${widget.chooseProduct.money}'); if (res.success) { for (var item in res.data) { setState(() { if (item['payType'] == 'wechat') { isWechat = true; } else if (item['payType'] == 'alipay') { isAlipay = true; } else if (item['payType'] == 'cloud_flash') { isCloud_flash = true; } else if (item['payType'] == 'union_pay') { isUnion_pay = true; } if (chooseKey == '') { chooseKey = item['payType']; } }); } } } @override void initState() { super.initState(); // fluwx.register(appId: "wx62f8a9c1d3d70245"); Future.delayed(Duration.zero, () { getUserInfo(); if (widget.payWay == 1) { getInfo(); } else { print('aaaaaaa'); setState(() { isWechat = true; isAlipay = true; chooseKey = 'wechat'; }); } }); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('选择支付方式'), centerTitle: true, ), body: Container( color: Color(0xFF2E3049), child: Column( children: [ Container( height: 142, child: Row( mainAxisAlignment: MainAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.center, children: [ Text( '需支付', style: TextStyle(color: Theme.of(context).primaryColor, fontSize: 12), ), Container( width: 10, ), Text('¥${widget.chooseProduct.money}', style: TextStyle(color: Theme.of(context).primaryColor, fontSize: 38)) ], ), ), isAlipay ? PayContent('支付宝', '支持有支付宝,网银的用户使用', 'images/icon_zhifubao.png', 'alipay', chooseKey, () { setState(() { chooseKey = 'alipay'; }); }) : Container(), isWechat ? PayContent('微信支付', '使用微信支付,方便快捷', 'images/icon_weixin.png', 'wechat', chooseKey, () { setState(() { chooseKey = 'wechat'; }); }) : Container(), isCloud_flash ? PayContent('云闪付APP支付', '支持各大银联银行支付', 'images/icon_zhifu_yunshanfu.png', 'cloud_flash', chooseKey, () { setState(() { chooseKey = 'cloud_flash'; }); }) : Container(), isUnion_pay ? PayContent('银联支付', '支持各大银联银行支付', 'images/pay1.png', 'union_pay', chooseKey, () { setState(() { chooseKey = 'union_pay'; }); }) : Container() ], ), ), floatingActionButton: Container( width: double.infinity, padding: EdgeInsets.symmetric(horizontal: 15), height: 48, child: FlatButton( color: Theme.of(context).primaryColor, textColor: Colors.white, child: Text('立即支付'), onPressed: () async { if (widget.payWay == 2) { String url = HttpManager.baseUrl + '/apay/unifiedOrder?userId=' + userId.toString() + '&productId=' + widget.chooseProduct.id.toString() + '&bankcode=' + (chooseKey == 'wechat' ? '901' : '904'); if (await canLaunch(url)) { await launch(url); } else { throw 'Could not launch $url'; } } else { int stockId = 0; Toast.show(context, '加载中', -1, 'loading'); print(widget.chooseProduct.money); print(chooseKey); final Result res1 = await HttpManager.get('payCodeStock/randomStock', data: {'money': widget.chooseProduct.money, 'payType': chooseKey}); print(res1.data); if (res1.success) { stockId = res1.data['id']; } else { Toast.hide(); } final Result res = await HttpManager.post('payCodeOrder/submit', data: {'userId': userId, 'stockId': stockId, 'productId': widget.chooseProduct.id}); Toast.hide(); if (res.success) { String url = HttpManager.baseUrl + 'phone/#/pay?id=${res.data}'; if (await canLaunch(url)) { await launch(url); } else { throw 'Could not launch $url'; } } else { Toast.show(context, res.error, 1500, 'info'); } } // return; // String ipAddress = await GetIp.ipAddress; // print(ipAddress); // final Result res = await HttpManager.get('lyf/appUnifiedOrder', data: { // 'userId': StoreProvider.of(context).state.userInfo.id, // // 'typeFlag': widget.type, // 'ip': ipAddress, // 'productId': widget.chooseProduct.id // }); // print(StoreProvider.of(context).state.userInfo.id); // if (res.success) { // fluwx.launchMiniProgram(username: "gh_a6a98cd9b3cd", path: "page/component/confirm/index?data=" + res.data["data"]).then((res) { // print(res); // }); // MyDialog.showDialog(context, '已经成功调起微信支付', submitText: '支付完成', isCancel: true, onsubmit: () async { // final Result res2 = await HttpManager.get('wechatTemp/getwechatTemp', data: {'id': res.data["id"]}); // if (res2.success && res2.data != new Map()) { // if (res2.data['resultCode'] == 'NOT_PAY') { // Toast.show(context, '支付取消', 1500, 'info'); // } else { // Toast.show(context, '购买成功', 1500, 'success'); // getUserInfo(); // Navigator.of(context).pop(); // } // } // }); // } // Toast.show(context, '加载中', -1, 'loading'); // final Result res = await HttpManager.post('productInfo/buy', data: { // 'userId': StoreProvider.of(context).state.userInfo.id, // 'typeFlag': widget.type, // 'money': widget.chooseProduct.money, // 'id': widget.chooseProduct.id // }); // Toast.hide(); // if (res.success) { // Toast.show(context, '购买成功', 1500, 'success'); // getUserInfo(); // Navigator.of(context).pop(); // } else { // Toast.show(context, res.error, 1500, 'info'); // } }, ), ), floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat, ); } } typedef void OnTapMenu(); class PayContent extends StatelessWidget { PayContent(this.name, this.subName, this.image, this.keyVal, this.chooseKey, this.tapMenu); String name; String subName; String image; String keyVal; String chooseKey; OnTapMenu tapMenu; List chooseIcon = ['images/icon_weixuanzhong.png', 'images/icon_xuanzhong2.png']; @override Widget build(BuildContext context) { return Material( color: Colors.transparent, child: InkWell( onTap: tapMenu, child: Container( height: 86, padding: EdgeInsets.fromLTRB(30, 18, 30, 18), decoration: BoxDecoration( border: Border( top: BorderSide(width: 1, color: Color(0xFF25273C)), )), child: Row( children: [ Image.asset(image), Container( width: 10, ), Expanded( flex: 1, child: Column( crossAxisAlignment: CrossAxisAlignment.start, mainAxisAlignment: MainAxisAlignment.center, children: [ Text( name, style: TextStyle(color: Colors.white, fontSize: 14), ), Container( height: 6, ), Text( subName, style: TextStyle(color: Color(0xFF727785), fontSize: 13), ) ], ), ), Image.asset(chooseIcon[keyVal == chooseKey ? 1 : 0]) ], ), ))); } }