ChoosePay.dart 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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:url_launcher/url_launcher.dart';
  12. class ChoosePay extends StatefulWidget {
  13. ChoosePay(this.type, this.chooseProduct, this.payWay);
  14. int type;
  15. ProductInfo chooseProduct;
  16. int payWay;
  17. @override
  18. _ChoosePayState createState() => _ChoosePayState();
  19. }
  20. class _ChoosePayState extends State<ChoosePay> with WidgetsBindingObserver {
  21. String chooseKey = '';
  22. bool isAlipay = false;
  23. bool isWechat = false;
  24. bool isCloud_flash = false;
  25. bool isUnion_pay = false;
  26. int userId = 0;
  27. Future<void> getUserInfo() async {
  28. Result res = await HttpManager.get('userInfo/getUserInfo');
  29. if (res.success) {
  30. print(res.data);
  31. StoreProvider.of<AppState>(context).dispatch(UpdateUserAction(UserInfo.fromJson(res.data)));
  32. userId = res.data['id'];
  33. } else {}
  34. }
  35. Future<void> getInfo() async {
  36. Result res = await HttpManager.get('payCodeStock/allUsedPayType?money=${widget.chooseProduct.money}');
  37. if (res.success) {
  38. for (var item in res.data) {
  39. setState(() {
  40. if (item['payType'] == 'wechat') {
  41. isWechat = true;
  42. } else if (item['payType'] == 'alipay') {
  43. isAlipay = true;
  44. } else if (item['payType'] == 'cloud_flash') {
  45. isCloud_flash = true;
  46. } else if (item['payType'] == 'union_pay') {
  47. isUnion_pay = true;
  48. }
  49. if (chooseKey == '') {
  50. chooseKey = item['payType'];
  51. }
  52. });
  53. }
  54. }
  55. }
  56. @override
  57. void initState() {
  58. super.initState();
  59. // fluwx.register(appId: "wx62f8a9c1d3d70245");
  60. Future.delayed(Duration.zero, () {
  61. getUserInfo();
  62. if (widget.payWay == 1) {
  63. getInfo();
  64. } else {
  65. print('aaaaaaa');
  66. setState(() {
  67. isWechat = true;
  68. isAlipay = true;
  69. chooseKey = 'wechat';
  70. });
  71. }
  72. });
  73. }
  74. @override
  75. Widget build(BuildContext context) {
  76. return Scaffold(
  77. appBar: AppBar(
  78. title: Text('选择支付方式'),
  79. centerTitle: true,
  80. ),
  81. body: Container(
  82. color: Color(0xFF2E3049),
  83. child: Column(
  84. children: <Widget>[
  85. Container(
  86. height: 142,
  87. child: Row(
  88. mainAxisAlignment: MainAxisAlignment.center,
  89. crossAxisAlignment: CrossAxisAlignment.center,
  90. children: <Widget>[
  91. Text(
  92. '需支付',
  93. style: TextStyle(color: Theme.of(context).primaryColor, fontSize: 12),
  94. ),
  95. Container(
  96. width: 10,
  97. ),
  98. Text('¥${widget.chooseProduct.money}', style: TextStyle(color: Theme.of(context).primaryColor, fontSize: 38))
  99. ],
  100. ),
  101. ),
  102. isAlipay
  103. ? PayContent('支付宝', '支持有支付宝,网银的用户使用', 'images/icon_zhifubao.png', 'alipay', chooseKey, () {
  104. setState(() {
  105. chooseKey = 'alipay';
  106. });
  107. })
  108. : Container(),
  109. isWechat
  110. ? PayContent('微信支付', '使用微信支付,方便快捷', 'images/icon_weixin.png', 'wechat', chooseKey, () {
  111. setState(() {
  112. chooseKey = 'wechat';
  113. });
  114. })
  115. : Container(),
  116. isCloud_flash
  117. ? PayContent('云闪付APP支付', '支持各大银联银行支付', 'images/icon_zhifu_yunshanfu.png', 'cloud_flash', chooseKey, () {
  118. setState(() {
  119. chooseKey = 'cloud_flash';
  120. });
  121. })
  122. : Container(),
  123. isUnion_pay
  124. ? PayContent('银联支付', '支持各大银联银行支付', 'images/pay1.png', 'union_pay', chooseKey, () {
  125. setState(() {
  126. chooseKey = 'union_pay';
  127. });
  128. })
  129. : Container()
  130. ],
  131. ),
  132. ),
  133. floatingActionButton: Container(
  134. width: double.infinity,
  135. padding: EdgeInsets.symmetric(horizontal: 15),
  136. height: 48,
  137. child: FlatButton(
  138. color: Theme.of(context).primaryColor,
  139. textColor: Colors.white,
  140. child: Text('立即支付'),
  141. onPressed: () async {
  142. if (widget.payWay == 2) {
  143. String url = HttpManager.baseUrl +
  144. '/apay/unifiedOrder?userId=' +
  145. userId.toString() +
  146. '&productId=' +
  147. widget.chooseProduct.id.toString() +
  148. '&bankcode=' +
  149. (chooseKey == 'wechat' ? '901' : '904');
  150. if (await canLaunch(url)) {
  151. await launch(url);
  152. } else {
  153. throw 'Could not launch $url';
  154. }
  155. } else {
  156. int stockId = 0;
  157. Toast.show(context, '加载中', -1, 'loading');
  158. print(widget.chooseProduct.money);
  159. print(chooseKey);
  160. final Result res1 = await HttpManager.get('payCodeStock/randomStock', data: {'money': widget.chooseProduct.money, 'payType': chooseKey});
  161. print(res1.data);
  162. if (res1.success) {
  163. stockId = res1.data['id'];
  164. } else {
  165. Toast.hide();
  166. }
  167. final Result res =
  168. await HttpManager.post('payCodeOrder/submit', data: {'userId': userId, 'stockId': stockId, 'productId': widget.chooseProduct.id});
  169. Toast.hide();
  170. if (res.success) {
  171. String url = HttpManager.baseUrl + 'phone/#/pay?id=${res.data}';
  172. if (await canLaunch(url)) {
  173. await launch(url);
  174. } else {
  175. throw 'Could not launch $url';
  176. }
  177. } else {
  178. Toast.show(context, res.error, 1500, 'info');
  179. }
  180. }
  181. // return;
  182. // String ipAddress = await GetIp.ipAddress;
  183. // print(ipAddress);
  184. // final Result res = await HttpManager.get('lyf/appUnifiedOrder', data: {
  185. // 'userId': StoreProvider.of<AppState>(context).state.userInfo.id,
  186. // // 'typeFlag': widget.type,
  187. // 'ip': ipAddress,
  188. // 'productId': widget.chooseProduct.id
  189. // });
  190. // print(StoreProvider.of<AppState>(context).state.userInfo.id);
  191. // if (res.success) {
  192. // fluwx.launchMiniProgram(username: "gh_a6a98cd9b3cd", path: "page/component/confirm/index?data=" + res.data["data"]).then((res) {
  193. // print(res);
  194. // });
  195. // MyDialog.showDialog(context, '已经成功调起微信支付', submitText: '支付完成', isCancel: true, onsubmit: () async {
  196. // final Result res2 = await HttpManager.get('wechatTemp/getwechatTemp', data: {'id': res.data["id"]});
  197. // if (res2.success && res2.data != new Map()) {
  198. // if (res2.data['resultCode'] == 'NOT_PAY') {
  199. // Toast.show(context, '支付取消', 1500, 'info');
  200. // } else {
  201. // Toast.show(context, '购买成功', 1500, 'success');
  202. // getUserInfo();
  203. // Navigator.of(context).pop();
  204. // }
  205. // }
  206. // });
  207. // }
  208. // Toast.show(context, '加载中', -1, 'loading');
  209. // final Result res = await HttpManager.post('productInfo/buy', data: {
  210. // 'userId': StoreProvider.of<AppState>(context).state.userInfo.id,
  211. // 'typeFlag': widget.type,
  212. // 'money': widget.chooseProduct.money,
  213. // 'id': widget.chooseProduct.id
  214. // });
  215. // Toast.hide();
  216. // if (res.success) {
  217. // Toast.show(context, '购买成功', 1500, 'success');
  218. // getUserInfo();
  219. // Navigator.of(context).pop();
  220. // } else {
  221. // Toast.show(context, res.error, 1500, 'info');
  222. // }
  223. },
  224. ),
  225. ),
  226. floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
  227. );
  228. }
  229. }
  230. typedef void OnTapMenu();
  231. class PayContent extends StatelessWidget {
  232. PayContent(this.name, this.subName, this.image, this.keyVal, this.chooseKey, this.tapMenu);
  233. String name;
  234. String subName;
  235. String image;
  236. String keyVal;
  237. String chooseKey;
  238. OnTapMenu tapMenu;
  239. List<String> chooseIcon = ['images/icon_weixuanzhong.png', 'images/icon_xuanzhong2.png'];
  240. @override
  241. Widget build(BuildContext context) {
  242. return Material(
  243. color: Colors.transparent,
  244. child: InkWell(
  245. onTap: tapMenu,
  246. child: Container(
  247. height: 86,
  248. padding: EdgeInsets.fromLTRB(30, 18, 30, 18),
  249. decoration: BoxDecoration(
  250. border: Border(
  251. top: BorderSide(width: 1, color: Color(0xFF25273C)),
  252. )),
  253. child: Row(
  254. children: <Widget>[
  255. Image.asset(image),
  256. Container(
  257. width: 10,
  258. ),
  259. Expanded(
  260. flex: 1,
  261. child: Column(
  262. crossAxisAlignment: CrossAxisAlignment.start,
  263. mainAxisAlignment: MainAxisAlignment.center,
  264. children: <Widget>[
  265. Text(
  266. name,
  267. style: TextStyle(color: Colors.white, fontSize: 14),
  268. ),
  269. Container(
  270. height: 6,
  271. ),
  272. Text(
  273. subName,
  274. style: TextStyle(color: Color(0xFF727785), fontSize: 13),
  275. )
  276. ],
  277. ),
  278. ),
  279. Image.asset(chooseIcon[keyVal == chooseKey ? 1 : 0])
  280. ],
  281. ),
  282. )));
  283. }
  284. }