question_pay.dart 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. import 'dart:convert';
  2. import 'package:flutter/material.dart';
  3. import 'package:liftmanager/res/gaps.dart';
  4. import 'package:liftmanager/net/api_service.dart';
  5. import 'package:liftmanager/utils/toast.dart';
  6. import 'package:liftmanager/widgets/app_bar.dart';
  7. import 'package:liftmanager/internal/search/search_router.dart';
  8. import 'package:liftmanager/widgets/app_search_bar.dart';
  9. import 'package:liftmanager/res/resources.dart';
  10. import 'package:liftmanager/routers/fluro_navigator.dart';
  11. import 'package:liftmanager/widgets/load_image.dart';
  12. import 'package:liftmanager/internal/bbs/bbs_router.dart';
  13. import 'package:flutter_screenutil/flutter_screenutil.dart';
  14. import 'package:flutter_alipay/flutter_alipay.dart';
  15. import 'package:liftmanager/internal/wode/model/table_dicts_model.dart';
  16. import 'package:flutter_spinkit/flutter_spinkit.dart';
  17. import 'package:flustars/flustars.dart' as FlutterStars;
  18. import 'package:liftmanager/common/common.dart';
  19. import 'package:liftmanager/utils/fast_notification.dart';
  20. import 'dart:math';
  21. import 'package:fluwx/fluwx.dart' as fluwx;
  22. import 'package:liftmanager/utils/theme_utils.dart';
  23. class QuestionPay extends StatefulWidget {
  24. QuestionPay(this.id, this.type, this.name);
  25. final String id;
  26. final String type;
  27. final String name;
  28. @override
  29. State<StatefulWidget> createState() {
  30. return QuestionPayState();
  31. }
  32. }
  33. class QuestionPayState extends State<QuestionPay> {
  34. static const payType = [
  35. {
  36. "icon": "tab_first/pay_yue",
  37. "title": "账户余额支付",
  38. },
  39. {
  40. "icon": "tab_first/pay_zhifubao",
  41. "title": "支付宝支付",
  42. },
  43. {
  44. "icon": "tab_first/pay_winxin",
  45. "title": "微信支付",
  46. },
  47. ];
  48. int indexNow = -1;
  49. String checkFalse = "tab_first/check_false";
  50. String checkTrue = "tab_first/check_true";
  51. List<TableDictsModel> moneyList;
  52. String moneyKey;
  53. String isRed = "";
  54. String title = "";
  55. String payTypeString = "";
  56. int rewardType;
  57. dynamic _wxPay;
  58. @override
  59. void initState() {
  60. super.initState();
  61. setSchema();
  62. if (widget.type == "question") {
  63. title = "问题";
  64. payTypeString = "QUESTION";
  65. rewardType = 1;
  66. } else if (widget.type == "video") {
  67. title = "视频";
  68. payTypeString = "VIDEO";
  69. rewardType = 2;
  70. }
  71. getMoney();
  72. }
  73. void setSchema() async{
  74. // await FlutterAlipay.setIosUrlSchema("tytylift");
  75. await FlutterAlipay.setIosUrlSchema("com.tyty.lift.managers");
  76. }
  77. Future getMoney() async {
  78. await NewApiService().queryConstant("reward_setting", "reward",
  79. onSuccess: (res) {
  80. moneyList = res;
  81. setState(() {});
  82. }, onError: (code, msg) {
  83. toasts(msg);
  84. });
  85. }
  86. randomInt(int min, int max) {
  87. return new Random().nextInt(max) % (max - min + 1) + min;
  88. }
  89. Future lastMoneyPay() async {
  90. await NewApiService().getAlipayBalanceApp({
  91. "orders": widget.id,
  92. "payType": payTypeString,
  93. "rewardType": rewardType,
  94. "reawrdMoney": moneyKey,
  95. "userId": FlutterStars.SpUtil.getString(Constant.userId),
  96. }, onSuccess: (res) {
  97. // moneyList = res;
  98. toasts("打赏成功");
  99. String initThisUserMoney = randomInt(1111, 9999).toString() +
  100. DateTime.now().millisecondsSinceEpoch.toString();
  101. FastNotification.push("initUserMoney", initThisUserMoney);
  102. FastNotification.push("initIsPay", initThisUserMoney);
  103. Navigator.of(context).pop();
  104. }, onError: (code, msg) {
  105. toasts(msg);
  106. });
  107. }
  108. // 确认充值
  109. void confirmReward() async {
  110. if (moneyKey == null) {
  111. toasts("请选择打赏金额");
  112. return;
  113. }
  114. if (indexNow == -1) {
  115. toasts("请选择打赏支付方式");
  116. return;
  117. }
  118. String payway;
  119. if (indexNow == 1) {
  120. payway = 'alipay';
  121. await NewApiService().alipayReward(
  122. orders: widget.id,
  123. payway: payway,
  124. rewardType: rewardType,
  125. reawrdMoney: moneyKey,
  126. onSuccess: (res) {
  127. callAlipay(res);
  128. },
  129. onError: (code, msg) {
  130. toasts(msg);
  131. },
  132. );
  133. } else if (indexNow == 0) {
  134. showAlert(
  135. context,
  136. "提示",
  137. "确定支付?",
  138. "确定",
  139. () {
  140. lastMoneyPay();
  141. NavigatorUtils.goBack(context);
  142. },
  143. txt2: "取消",
  144. onPre2: () {
  145. NavigatorUtils.goBack(context);
  146. },
  147. );
  148. } else if (indexNow == 2){
  149. payway = 'wx';
  150. await NewApiService().wxReward(
  151. orders: widget.id,
  152. payway: payway,
  153. rewardType: rewardType,
  154. reawrdMoney: moneyKey,
  155. onSuccess: (res) {
  156. wxMethod(res);
  157. },
  158. onError: (code, msg) {
  159. toasts(msg);
  160. },
  161. );
  162. } else {
  163. toasts("暂不支持该支付方式");
  164. return;
  165. }
  166. }
  167. wxMethod(_payInfo) {
  168. print(_payInfo["appid"]);
  169. fluwx.payWithWeChat(
  170. appId: _payInfo["appid"],
  171. partnerId: _payInfo["partnerid"],
  172. prepayId: _payInfo["prepayid"],
  173. packageValue: _payInfo["package"],
  174. nonceStr: _payInfo["noncestr"],
  175. timeStamp: int.parse(_payInfo["timestamp"]),
  176. sign: _payInfo["sign"]
  177. ).then((data) {
  178. print(data);
  179. });
  180. _wxPay?.cancel();
  181. _wxPay = fluwx.responseFromPayment.listen((fluwx.WeChatPaymentResponse response) async {
  182. print("WeChatPaymentResponse"+response.errCode.toString());
  183. if (response.errCode == -2) {
  184. //支付取消
  185. toasts("支付取消");
  186. } else if (response.errCode == -1) {
  187. //支付失败
  188. toasts("支付失败");
  189. } else if (response.errCode == 0) {
  190. //支付成功
  191. toasts("打赏成功");
  192. String initThisUserMoney = randomInt(1111, 9999).toString() +
  193. DateTime.now().millisecondsSinceEpoch.toString();
  194. FastNotification.push("initUserMoney", initThisUserMoney);
  195. FastNotification.push("initIsPay", initThisUserMoney);
  196. Navigator.pop(context);
  197. }
  198. if (!mounted) return;
  199. setState(() {});
  200. });
  201. }
  202. // 调用支付宝
  203. void callAlipay(String _payInfo) async {
  204. AlipayResult payResult;
  205. try {
  206. print("The pay info is : " + _payInfo);
  207. payResult = await FlutterAlipay.pay(_payInfo);
  208. } on Exception catch (e) {
  209. payResult = null;
  210. }
  211. print(payResult);
  212. if (payResult != null) {
  213. if (payResult.resultStatus == "9000") {
  214. toasts("打赏成功");
  215. String initThisUserMoney = randomInt(1111, 9999).toString() +
  216. DateTime.now().millisecondsSinceEpoch.toString();
  217. FastNotification.push("initUserMoney", initThisUserMoney);
  218. FastNotification.push("initIsPay", initThisUserMoney);
  219. Navigator.pop(context);
  220. } else {
  221. toasts(payResult.memo);
  222. }
  223. } else {
  224. toasts("支付异常");
  225. }
  226. }
  227. @override
  228. Widget build(BuildContext context) {
  229. double width = MediaQuery.of(context).size.width;
  230. double height = MediaQuery.of(context).size.height;
  231. return Container(
  232. child: Scaffold(
  233. appBar: MyAppBar(
  234. centerTitle: "打赏",
  235. ),
  236. body: Container(
  237. child: Stack(
  238. children: <Widget>[
  239. Container(
  240. // padding: EdgeInsets.only(top:70),
  241. child: ListView(
  242. children: <Widget>[
  243. // Container(
  244. // padding: EdgeInsets.only(
  245. // left: ScreenUtil().setWidth(20),
  246. // right: ScreenUtil().setWidth(15),
  247. // top: ScreenUtil().setWidth(15),
  248. // bottom: ScreenUtil().setWidth(15),
  249. // ),
  250. // decoration: BoxDecoration(
  251. // border: Border(
  252. // bottom:
  253. // BorderSide(width: 5, color: Color(0xfff5f5f5)),
  254. // ),
  255. // ),
  256. // child: Row(
  257. // mainAxisAlignment: MainAxisAlignment.spaceBetween,
  258. // children: <Widget>[
  259. // Row(
  260. // crossAxisAlignment: CrossAxisAlignment.center,
  261. // children: <Widget>[
  262. // Text(
  263. // "$title打赏",
  264. // style: TextStyle(
  265. // color: Color(0xff333333),
  266. // fontSize: ScreenUtil().setSp(16)),
  267. // textAlign: TextAlign.start,
  268. // ),
  269. // Container(
  270. // padding: EdgeInsets.only(
  271. // left: ScreenUtil().setWidth(10)),
  272. // child: Text(
  273. // widget.name,
  274. // style: TextStyle(
  275. // color: Color(0xff666666),
  276. // fontSize: ScreenUtil().setSp(16)),
  277. // textAlign: TextAlign.start,
  278. // ),
  279. // )
  280. // ]),
  281. // Container(
  282. // child: Text(
  283. // "¥" + totalMoney,
  284. // style: TextStyle(
  285. // color: Colors.red,
  286. // fontSize: ScreenUtil().setSp(16)),
  287. // textAlign: TextAlign.end,
  288. // ),
  289. // )
  290. // ])
  291. // ),
  292. // Container(
  293. // padding: EdgeInsets.only(
  294. // left: ScreenUtil().setWidth(20),
  295. // right: ScreenUtil().setWidth(15),
  296. // top: ScreenUtil().setWidth(15),
  297. // bottom: ScreenUtil().setWidth(15)),
  298. // decoration: BoxDecoration(
  299. // border: Border(
  300. // bottom:
  301. // BorderSide(width: 5, color: Color(0xfff5f5f5)),
  302. // ),
  303. // ),
  304. // child: Row(
  305. // mainAxisAlignment: MainAxisAlignment.spaceBetween,
  306. // children: <Widget>[
  307. // Text(
  308. // "共需支付",
  309. // style: TextStyle(
  310. // color: Color(0xff222222),
  311. // fontSize: ScreenUtil().setSp(16)),
  312. // textAlign: TextAlign.start,
  313. // ),
  314. // Text(
  315. // "¥" + totalMoney,
  316. // style: TextStyle(
  317. // color: Colors.red,
  318. // fontSize: ScreenUtil().setSp(16)),
  319. // textAlign: TextAlign.start,
  320. // ),
  321. // ]),
  322. // ),
  323. Container(
  324. padding: EdgeInsets.only(
  325. left: ScreenUtil().setWidth(20),
  326. right: ScreenUtil().setWidth(15),
  327. top: ScreenUtil().setWidth(15),
  328. bottom: ScreenUtil().setWidth(15)),
  329. decoration: BoxDecoration(
  330. // border: Border(
  331. // bottom:
  332. // BorderSide(width: 5, color: Color(0xfff5f5f5)),
  333. // ),
  334. ),
  335. child: Row(
  336. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  337. children: <Widget>[
  338. Text(
  339. "请选择打赏金额",
  340. style: TextStyle(
  341. color: Color(0xff222222),
  342. fontSize: ScreenUtil().setSp(16)),
  343. textAlign: TextAlign.start,
  344. ),
  345. ]),
  346. ),
  347. moneyList != null && moneyList != []
  348. ? Container(
  349. padding: EdgeInsets.only(
  350. left: ScreenUtil().setWidth(10)),
  351. child: Wrap(
  352. alignment: WrapAlignment.start,
  353. crossAxisAlignment: WrapCrossAlignment.center,
  354. children: moneyList.asMap().keys.map((index) {
  355. return InkWell(
  356. onTap: () {
  357. setState(() {
  358. moneyKey = moneyList[index].key;
  359. isRed = moneyList[index].key;
  360. print(isRed);
  361. print(moneyList[index].key);
  362. });
  363. },
  364. child: Container(
  365. width: width / 4 - 15,
  366. padding: EdgeInsets.only(
  367. bottom: ScreenUtil().setWidth(26),
  368. top: ScreenUtil().setWidth(26)),
  369. margin: EdgeInsets.only(
  370. left: 5, right: 5, bottom: 5, top: 5),
  371. decoration:
  372. BoxDecoration(color: Color(0xfff5f5f5)),
  373. child: Text(
  374. (moneyList[index].value ?? "") + "元",
  375. style: TextStyle(
  376. color: isRed == moneyList[index].key
  377. ? Color(0xffff0000)
  378. : Color(0xff666666),
  379. fontSize: ScreenUtil().setSp(15)),
  380. textAlign: TextAlign.center,
  381. ),
  382. ),
  383. );
  384. }).toList(),
  385. ))
  386. : loadCircle(),
  387. Container(
  388. padding: EdgeInsets.only(
  389. left: ScreenUtil().setWidth(20),
  390. right: ScreenUtil().setWidth(15),
  391. top: ScreenUtil().setWidth(50),
  392. bottom: ScreenUtil().setWidth(10)),
  393. decoration: BoxDecoration(
  394. border: Border(
  395. bottom:
  396. BorderSide(width: 0.5, color: Color(0xfff5f5f5)),
  397. ),
  398. ),
  399. child: Row(
  400. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  401. children: <Widget>[
  402. Text(
  403. "选择支付方式",
  404. style: TextStyle(
  405. color: Color(0xff000000),
  406. fontSize: ScreenUtil().setSp(16)),
  407. textAlign: TextAlign.start,
  408. ),
  409. ]),
  410. ),
  411. Container(
  412. padding: EdgeInsets.only(
  413. left: ScreenUtil().setWidth(15),
  414. bottom: ScreenUtil().setWidth(80),
  415. right: ScreenUtil().setWidth(15)),
  416. child: Column(
  417. children: payType.asMap().keys.map((i) {
  418. return Container(
  419. decoration: BoxDecoration(
  420. border: Border(
  421. bottom: BorderSide(
  422. width: 0.5, color: Color(0xfff5f5f5)),
  423. ),
  424. ),
  425. height: ScreenUtil().setWidth(60),
  426. child: Row(
  427. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  428. children: <Widget>[
  429. Row(
  430. children: <Widget>[
  431. LoadAssetImage(
  432. // image: AssetImage(i['img']),
  433. payType[i]["icon"],
  434. width: ScreenUtil().setWidth(26),
  435. height: ScreenUtil().setWidth(26),
  436. // alignment: Alignment.centerLeft,
  437. ),
  438. Container(width: 5, child: null),
  439. Text(
  440. payType[i]["title"],
  441. style: TextStyle(
  442. color: Color(0xff000000),
  443. fontSize: ScreenUtil().setSp(16)),
  444. textAlign: TextAlign.start,
  445. ),
  446. ],
  447. ),
  448. InkWell(
  449. child: Container(
  450. // padding: EdgeInsets.only(top:10,left:5,right:5,bottom:10),
  451. child: LoadAssetImage(
  452. // image: AssetImage(i['img']),
  453. indexNow == i ? checkTrue : checkFalse,
  454. width: ScreenUtil().setWidth(20),
  455. height: ScreenUtil().setWidth(20),
  456. // alignment: Alignment.centerLeft,
  457. ),
  458. ),
  459. onTap: () {
  460. setState(() {
  461. indexNow = i;
  462. print(indexNow);
  463. });
  464. },
  465. ),
  466. ],
  467. ),
  468. );
  469. }).toList(),
  470. ),
  471. )
  472. ],
  473. ),
  474. ),
  475. Positioned(
  476. bottom: 0,
  477. left: 0,
  478. child: Container(
  479. width: width,
  480. padding: EdgeInsets.only(
  481. top: ScreenUtil().setWidth(15),
  482. bottom: ScreenUtil().setWidth(15),
  483. left: ScreenUtil().setWidth(25),
  484. right: ScreenUtil().setWidth(25),
  485. ),
  486. color: Colors.white,
  487. child: Container(
  488. height: ScreenUtil().setWidth(44),
  489. decoration: BoxDecoration(
  490. borderRadius: BorderRadius.circular(22.0),
  491. gradient: const LinearGradient(
  492. colors: [Color(0xFF00D9FF), Color(0xFF0287FF)],
  493. ),
  494. ),
  495. child: FlatButton(
  496. // padding: EdgeInsets.all(15.0),
  497. child: Text("立即支付"),
  498. textColor: Colors.white,
  499. onPressed: () {
  500. confirmReward();
  501. },
  502. ),
  503. ),
  504. ),
  505. )
  506. ],
  507. ),
  508. ),
  509. ),
  510. );
  511. }
  512. Widget loadCircle() {
  513. return Container(
  514. padding: EdgeInsets.only(top: 10, bottom: 10),
  515. color: ThemeUtils.getTabsBg(context),
  516. child: Center(
  517. child: SpinKitFadingCircle(
  518. color: Colors.blueAccent,
  519. size: 30.0,
  520. ),
  521. ),
  522. );
  523. }
  524. }