ChoosePay.dart 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter/cupertino.dart';
  3. import '../net/HttpManager.dart';
  4. import '../net/Result.dart';
  5. import '../styles/totast.dart';
  6. import 'package:flutter_redux/flutter_redux.dart';
  7. import '../redux/AppState.dart';
  8. import '../model/ProductInfo.dart';
  9. import '../redux/UserRedux.dart';
  10. import '../model/UserInfo.dart';
  11. import 'package:fluwx/fluwx.dart' as fluwx;
  12. import 'package:get_ip/get_ip.dart';
  13. import 'package:dio/dio.dart';
  14. import 'dart:convert';
  15. import '../widget/Dialog.dart';
  16. class ChoosePay extends StatefulWidget {
  17. ChoosePay(this.type, this.chooseProduct);
  18. int type;
  19. ProductInfo chooseProduct;
  20. @override
  21. _ChoosePayState createState() => _ChoosePayState();
  22. }
  23. class _ChoosePayState extends State<ChoosePay> with WidgetsBindingObserver {
  24. String chooseKey = 'wxpay';
  25. Future<void> getUserInfo() async {
  26. Result res = await HttpManager.get('userInfo/getUserInfo');
  27. if (res.success) {
  28. print(res.data);
  29. StoreProvider.of<AppState>(context).dispatch(UpdateUserAction(UserInfo.fromJson(res.data)));
  30. } else {}
  31. }
  32. @override
  33. void initState() {
  34. super.initState();
  35. fluwx.register(appId: "wx62f8a9c1d3d70245");
  36. Future.delayed(Duration.zero, () => getUserInfo());
  37. }
  38. @override
  39. Widget build(BuildContext context) {
  40. return Scaffold(
  41. appBar: AppBar(
  42. title: Text('选择支付方式'),
  43. centerTitle: true,
  44. ),
  45. body: Container(
  46. color: Color(0xFF2E3049),
  47. child: Column(
  48. children: <Widget>[
  49. Container(
  50. height: 142,
  51. child: Row(
  52. mainAxisAlignment: MainAxisAlignment.center,
  53. crossAxisAlignment: CrossAxisAlignment.center,
  54. children: <Widget>[
  55. Text(
  56. '需支付',
  57. style: TextStyle(color: Theme.of(context).primaryColor, fontSize: 12),
  58. ),
  59. Container(
  60. width: 10,
  61. ),
  62. Text('¥${widget.chooseProduct.money}', style: TextStyle(color: Theme.of(context).primaryColor, fontSize: 38))
  63. ],
  64. ),
  65. ),
  66. // PayContent('支付宝', '支持有支付宝,网银的用户使用', 'images/icon_zhifubao.png', 'alipay', chooseKey, () {
  67. // setState(() {
  68. // chooseKey = 'alipay';
  69. // });
  70. // }),
  71. PayContent('微信支付', '使用微信支付,方便快捷', 'images/icon_weixin.png', 'wxpay', chooseKey, () {
  72. setState(() {
  73. chooseKey = 'wxpay';
  74. });
  75. })
  76. ],
  77. ),
  78. ),
  79. floatingActionButton: Container(
  80. width: double.infinity,
  81. padding: EdgeInsets.symmetric(horizontal: 15),
  82. height: 48,
  83. child: FlatButton(
  84. color: Theme.of(context).primaryColor,
  85. textColor: Colors.white,
  86. child: Text('立即支付'),
  87. onPressed: () async {
  88. // return;
  89. String ipAddress = await GetIp.ipAddress;
  90. print(ipAddress);
  91. final Result res = await HttpManager.get('lyf/appUnifiedOrder', data: {
  92. 'userId': StoreProvider.of<AppState>(context).state.userInfo.id,
  93. // 'typeFlag': widget.type,
  94. 'ip': ipAddress,
  95. 'productId': widget.chooseProduct.id
  96. });
  97. print(StoreProvider.of<AppState>(context).state.userInfo.id);
  98. if (res.success) {
  99. fluwx.launchMiniProgram(username: "gh_a6a98cd9b3cd", path: "page/component/confirm/index?data=" + res.data["data"]).then((res) {
  100. print(res);
  101. });
  102. MyDialog.showDialog(context, '已经成功调起微信支付', submitText: '支付完成', isCancel: true, onsubmit: () async {
  103. final Result res2 = await HttpManager.get('wxpayTemp/getWxpayTemp', data: {'id': res.data["id"]});
  104. if (res2.success && res2.data != new Map()) {
  105. if (res2.data['resultCode'] == 'NOT_PAY') {
  106. Toast.show(context, '支付取消', 1500, 'info');
  107. } else {
  108. Toast.show(context, '购买成功', 1500, 'success');
  109. getUserInfo();
  110. Navigator.of(context).pop();
  111. }
  112. }
  113. });
  114. }
  115. // String str= 'eyJhbW91bnQiOiIxIiwiYX
  116. // String str= 'eyJhbW91bnQiOiIxIiwiYXR0YWNoIjoie1widG90YWxcIjoxLFwicHJvZHVjdElkXCI6XCIxXCIsXCJ1c2VySWRcIjpcIjg0NzA1XCIsXCJjYXNoXCI6MTB9IiwiYm9keSI6IjHpl6jnpagiLCJtY2hfaWQiOiIxMDAwMDAwODM1NDEiLCJub25jZV9zdHIiOiI1ZmM3NWVlZmY0MjA0MGEwYmEwMWQwZDE2NGJhYmU2YyIsIm5vdGlmeV91cmwiOiJodHRwOi8vMTIzLjU4LjI0MC4xMzg6OTAwMC8iLCJvdXRfdHJhZGVfbm8iOiIxNTY0OTkzMjYwNDc0NjQxNTcyNyIsInBheW1lbnRfdHlwZSI6InRyYWRlLndlaXhpbi5hcHAiLCJyZXR1cm5fdXJsIjoiaHR0cDovLzEyMy41OC4yNDAuMTM4OjkwMDAvbHlmL25vdGlmeSIsInNpZ24iOiIxYjg0NWE4ZjNmYWI0OTA1NTViYWE5MGZkYzFkODc1MyIsInNpZ25fdHlwZSI6Ik1ENSIsInNwYmlsbF9jcmVhdGVfaXAiOiIxMTkuMTM3LjU1LjE4OSJ9'; fluwx.launchMiniProgram(username: "gh_a6a98cd9b3cd",path:"page/component/confirm/index?data="+str);
  117. // fluwx.launchMiniProgram(username: "gh_a6a98cd9b3cd",path:"page/component/confirm/index?data="+str);
  118. // Toast.show(context, '加载中', -1, 'loading');
  119. // final Result res = await HttpManager.post('productInfo/buy', data: {
  120. // 'userId': StoreProvider.of<AppState>(context).state.userInfo.id,
  121. // 'typeFlag': widget.type,
  122. // 'money': widget.chooseProduct.money,
  123. // 'id': widget.chooseProduct.id
  124. // });
  125. // Toast.hide();
  126. // if (res.success) {
  127. // Toast.show(context, '购买成功', 1500, 'success');
  128. // getUserInfo();
  129. // Navigator.of(context).pop();
  130. // } else {
  131. // Toast.show(context, res.error, 1500, 'info');
  132. // }
  133. },
  134. ),
  135. ),
  136. floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
  137. );
  138. }
  139. }
  140. typedef void OnTapMenu();
  141. class PayContent extends StatelessWidget {
  142. PayContent(this.name, this.subName, this.image, this.keyVal, this.chooseKey, this.tapMenu);
  143. String name;
  144. String subName;
  145. String image;
  146. String keyVal;
  147. String chooseKey;
  148. OnTapMenu tapMenu;
  149. List<String> chooseIcon = ['images/icon_weixuanzhong.png', 'images/icon_xuanzhong2.png'];
  150. @override
  151. Widget build(BuildContext context) {
  152. return Material(
  153. color: Colors.transparent,
  154. child: InkWell(
  155. onTap: tapMenu,
  156. child: Container(
  157. height: 86,
  158. padding: EdgeInsets.fromLTRB(30, 18, 30, 18),
  159. decoration: BoxDecoration(
  160. border: Border(
  161. top: BorderSide(width: 1, color: Color(0xFF25273C)),
  162. )),
  163. child: Row(
  164. children: <Widget>[
  165. Image.asset(image),
  166. Container(
  167. width: 10,
  168. ),
  169. Expanded(
  170. flex: 1,
  171. child: Column(
  172. crossAxisAlignment: CrossAxisAlignment.start,
  173. mainAxisAlignment: MainAxisAlignment.center,
  174. children: <Widget>[
  175. Text(
  176. name,
  177. style: TextStyle(color: Colors.white, fontSize: 14),
  178. ),
  179. Container(
  180. height: 6,
  181. ),
  182. Text(
  183. subName,
  184. style: TextStyle(color: Color(0xFF727785), fontSize: 13),
  185. )
  186. ],
  187. ),
  188. ),
  189. Image.asset(chooseIcon[keyVal == chooseKey ? 1 : 0])
  190. ],
  191. ),
  192. )));
  193. }
  194. }