question_pay.dart 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  1. import 'dart:math';
  2. import 'package:flustars/flustars.dart' as FlutterStars;
  3. import 'package:flutter/material.dart';
  4. import 'package:flutter/services.dart';
  5. import 'package:flutter_alipay/flutter_alipay.dart';
  6. import 'package:flutter_screenutil/flutter_screenutil.dart';
  7. import 'package:flutter_spinkit/flutter_spinkit.dart';
  8. import 'package:fluwx/fluwx.dart' as fluwx;
  9. import 'package:liftmanager/common/common.dart';
  10. import 'package:liftmanager/internal/wode/model/table_dicts_model.dart';
  11. import 'package:liftmanager/net/api_service.dart';
  12. import 'package:liftmanager/res/resources.dart';
  13. import 'package:liftmanager/routers/fluro_navigator.dart';
  14. import 'package:liftmanager/utils/fast_notification.dart';
  15. import 'package:liftmanager/utils/theme_utils.dart';
  16. import 'package:liftmanager/utils/toast.dart';
  17. import 'package:liftmanager/widgets/app_bar.dart';
  18. import 'package:liftmanager/widgets/load_image.dart';
  19. import 'package:liftmanager/internal/bbs/model/expert_model.dart' as expertM;
  20. import 'package:umeng_common_sdk/umeng_common_sdk.dart';
  21. class QuestionPay extends StatefulWidget {
  22. QuestionPay(this.id, this.type, this.name, this.userHeadUrl,
  23. {this.expert, this.title});
  24. final String id;
  25. final String expert;
  26. final String name;
  27. final String type;
  28. final String userHeadUrl;
  29. final String title;
  30. @override
  31. State<StatefulWidget> createState() {
  32. return QuestionPayState();
  33. }
  34. }
  35. class QuestionPayState extends State<QuestionPay> {
  36. static const payType = [
  37. {
  38. "icon": "tab_first/pay_yue",
  39. "title": "账户余额",
  40. },
  41. {
  42. "icon": "tab_first/pay_zhifubao",
  43. "title": "支付宝",
  44. },
  45. {
  46. "icon": "tab_first/pay_winxin",
  47. "title": "微信",
  48. },
  49. ];
  50. String checkFalse = "tab_first/check_false";
  51. String checkTrue = "tab_first/check_true";
  52. int indexNow = -1;
  53. String isRed = "";
  54. String moneyKey;
  55. List<TableDictsModel> moneyList;
  56. String payTypeString = "";
  57. int rewardType;
  58. String title = "";
  59. dynamic _wxPay;
  60. expertM.Records userInfo;
  61. @override
  62. void initState() {
  63. super.initState();
  64. UmengCommonSdk.onPageStart("案例打赏");
  65. SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle.light);
  66. setSchema();
  67. if (widget.type == "question") {
  68. title = "问题";
  69. payTypeString = "QUESTION";
  70. rewardType = 1;
  71. } else if (widget.type == "video") {
  72. title = "视频";
  73. payTypeString = "VIDEO";
  74. rewardType = 2;
  75. }
  76. getData();
  77. }
  78. @override
  79. void dispose() {
  80. UmengCommonSdk.onPageEnd("案例打赏");
  81. super.dispose();
  82. }
  83. getData() async {
  84. await getRewardSecond();
  85. await getbalance();
  86. await getMoney();
  87. await getUserInfo();
  88. setState(() {});
  89. }
  90. getUserInfo() async {
  91. await NewApiService().getExpertDetail(widget.expert, onSuccess: (res) {
  92. userInfo = res;
  93. });
  94. }
  95. void setSchema() async {
  96. // await FlutterAlipay.setIosUrlSchema("tytylift");
  97. await FlutterAlipay.setIosUrlSchema("com.tyty.lift.managers");
  98. }
  99. Future getMoney() async {
  100. await NewApiService().queryConstant("reward_setting", "reward",
  101. onSuccess: (res) {
  102. moneyList = res;
  103. setState(() {});
  104. }, onError: (code, msg) {
  105. toasts(msg);
  106. });
  107. }
  108. String balance = '0';
  109. Future getbalance() async {
  110. await NewApiService().getUserCoupons(onSuccess: (res) {
  111. setState(() {
  112. balance = res["balance"] != null ? '${res["balance"]}' : '0';
  113. });
  114. }, onError: (code, msg) {
  115. toasts(msg);
  116. });
  117. }
  118. String rewardSecond = '0';
  119. Future getRewardSecond() async {
  120. await NewApiService().getRewardSecond(
  121. id: widget.expert,
  122. onSuccess: (res) {
  123. if (mounted) {
  124. setState(() {
  125. rewardSecond = res != null ? '$res' : '0';
  126. });
  127. }
  128. },
  129. onError: (code, msg) {
  130. toasts(msg);
  131. });
  132. }
  133. randomInt(int min, int max) {
  134. return new Random().nextInt(max) % (max - min + 1) + min;
  135. }
  136. Future lastMoneyPay() async {
  137. await NewApiService().getAlipayBalanceApp({
  138. "orders": widget.id,
  139. "payType": payTypeString,
  140. "rewardType": rewardType,
  141. "reawrdMoney": moneyKey,
  142. "userId": FlutterStars.SpUtil.getString(Constant.userId),
  143. }, onSuccess: (res) {
  144. // moneyList = res;
  145. toasts("打赏成功");
  146. UmengCommonSdk.onEvent(
  147. "ym_reward_paySuccess",
  148. {
  149. "formUserName": widget.name,
  150. "formUserId": widget.expert,
  151. "title": widget.title,
  152. "typeName": widget.type,
  153. "id": widget.id,
  154. "money": moneyKey,
  155. "payment": "余额支付"
  156. },
  157. );
  158. String initThisUserMoney = randomInt(1111, 9999).toString() +
  159. DateTime.now().millisecondsSinceEpoch.toString();
  160. FastNotification.push("initUserMoney", initThisUserMoney);
  161. FastNotification.push("initIsPay", initThisUserMoney);
  162. Navigator.of(context).pop();
  163. }, onError: (code, msg) {
  164. toasts(msg);
  165. });
  166. }
  167. // 确认充值
  168. void confirmReward() async {
  169. if (moneyKey == null) {
  170. toasts("请选择打赏金额");
  171. return;
  172. }
  173. if (indexNow == -1) {
  174. toasts("请选择打赏支付方式");
  175. return;
  176. }
  177. String payway;
  178. if (indexNow == 1) {
  179. payway = 'alipay';
  180. await NewApiService().alipayReward(
  181. orders: widget.id,
  182. payway: payway,
  183. rewardType: rewardType,
  184. reawrdMoney: moneyKey,
  185. onSuccess: (res) {
  186. callAlipay(res);
  187. },
  188. onError: (code, msg) {
  189. toasts(msg);
  190. },
  191. );
  192. } else if (indexNow == 0) {
  193. showAlert(
  194. context,
  195. "提示",
  196. "确定支付?",
  197. "确定",
  198. () {
  199. lastMoneyPay();
  200. NavigatorUtils.goBack(context);
  201. },
  202. txt2: "取消",
  203. onPre2: () {
  204. NavigatorUtils.goBack(context);
  205. },
  206. );
  207. } else if (indexNow == 2) {
  208. payway = 'wx';
  209. await NewApiService().wxReward(
  210. orders: widget.id,
  211. payway: payway,
  212. rewardType: rewardType,
  213. reawrdMoney: moneyKey,
  214. onSuccess: (res) {
  215. wxMethod(res);
  216. },
  217. onError: (code, msg) {
  218. toasts(msg);
  219. },
  220. );
  221. } else {
  222. toasts("暂不支持该支付方式");
  223. return;
  224. }
  225. }
  226. wxMethod(_payInfo) {
  227. print(_payInfo["appid"]);
  228. fluwx
  229. .payWithWeChat(
  230. appId: _payInfo["appid"],
  231. partnerId: _payInfo["partnerid"],
  232. prepayId: _payInfo["prepayid"],
  233. packageValue: _payInfo["package"],
  234. nonceStr: _payInfo["noncestr"],
  235. timeStamp: int.parse(_payInfo["timestamp"]),
  236. sign: _payInfo["sign"])
  237. .then((data) {
  238. print(data);
  239. });
  240. _wxPay?.cancel();
  241. _wxPay = fluwx.responseFromPayment
  242. .listen((fluwx.WeChatPaymentResponse response) async {
  243. print("WeChatPaymentResponse" + response.errCode.toString());
  244. if (response.errCode == -2) {
  245. //支付取消
  246. toasts("支付取消");
  247. } else if (response.errCode == -1) {
  248. //支付失败
  249. toasts("支付失败");
  250. } else if (response.errCode == 0) {
  251. //支付成功
  252. toasts("打赏成功");
  253. UmengCommonSdk.onEvent(
  254. "ym_reward_paySuccess",
  255. {
  256. "formUserName": widget.name,
  257. "formUserId": widget.expert,
  258. "title": widget.title,
  259. "typeName": widget.type,
  260. "id": widget.id,
  261. "money": moneyKey,
  262. "payment": "微信"
  263. },
  264. );
  265. String initThisUserMoney = randomInt(1111, 9999).toString() +
  266. DateTime.now().millisecondsSinceEpoch.toString();
  267. FastNotification.push("initUserMoney", initThisUserMoney);
  268. FastNotification.push("initIsPay", initThisUserMoney);
  269. Navigator.pop(context);
  270. }
  271. if (!mounted) return;
  272. setState(() {});
  273. });
  274. }
  275. // 调用支付宝
  276. void callAlipay(String _payInfo) async {
  277. AlipayResult payResult;
  278. try {
  279. print("The pay info is : " + _payInfo);
  280. payResult = await FlutterAlipay.pay(_payInfo);
  281. } on Exception catch (e) {
  282. payResult = null;
  283. }
  284. print(payResult);
  285. if (payResult != null) {
  286. if (payResult.resultStatus == "9000") {
  287. toasts("打赏成功");
  288. UmengCommonSdk.onEvent(
  289. "ym_reward_paySuccess",
  290. {
  291. "formUserName": widget.name,
  292. "formUserId": widget.expert,
  293. "title": widget.title,
  294. "typeName": widget.type,
  295. "id": widget.id,
  296. "money": moneyKey,
  297. "payment": "支付宝"
  298. },
  299. );
  300. String initThisUserMoney = randomInt(1111, 9999).toString() +
  301. DateTime.now().millisecondsSinceEpoch.toString();
  302. FastNotification.push("initUserMoney", initThisUserMoney);
  303. FastNotification.push("initIsPay", initThisUserMoney);
  304. Navigator.pop(context);
  305. } else {
  306. toasts(payResult.memo);
  307. }
  308. } else {
  309. toasts("支付异常");
  310. }
  311. }
  312. Widget loadCircle() {
  313. return Container(
  314. padding: EdgeInsets.only(top: 10, bottom: 10),
  315. color: ThemeUtils.getTabsBg(context),
  316. child: Center(
  317. child: SpinKitFadingCircle(
  318. color: Colors.blueAccent,
  319. size: 30.0,
  320. ),
  321. ),
  322. );
  323. }
  324. @override
  325. Widget build(BuildContext context) {
  326. double width = MediaQuery.of(context).size.width;
  327. double height = MediaQuery.of(context).size.height;
  328. return Container(
  329. child: Scaffold(
  330. appBar: MyAppBar(
  331. statubarStyle: SystemUiOverlayStyle.light,
  332. centerTitle: "打赏",
  333. titleColor: Colors.white,
  334. bgColors: [Color(0xff5887FF), Color(0xff00ACFF)],
  335. ),
  336. body: Container(
  337. child: Stack(
  338. children: <Widget>[
  339. Container(
  340. color: Colors.white,
  341. // padding: EdgeInsets.only(top:70),
  342. child: ListView(
  343. children: <Widget>[
  344. Container(
  345. height: ScreenUtil().setHeight(200),
  346. // color: bgColors == null ? Colors.white : null,
  347. decoration: BoxDecoration(
  348. gradient: LinearGradient(
  349. colors: [Color(0xff5887FF), Color(0xff00ACFF)],
  350. ),
  351. ),
  352. child: Column(
  353. children: [
  354. SizedBox(
  355. height: ScreenUtil().setHeight(20),
  356. ),
  357. ClipRRect(
  358. borderRadius: BorderRadius.circular(
  359. ScreenUtil().setWidth(35)),
  360. child: Container(
  361. child: LoadNetworkImage(
  362. widget.userHeadUrl,
  363. // item.avatarUrl,
  364. // fit: BoxFit.fitWidth,
  365. width: ScreenUtil().setWidth(70),
  366. height: ScreenUtil().setWidth(70),
  367. ),
  368. )),
  369. SizedBox(
  370. height: ScreenUtil().setHeight(10),
  371. ),
  372. Row(
  373. crossAxisAlignment: CrossAxisAlignment.center,
  374. mainAxisAlignment: MainAxisAlignment.center,
  375. children: [
  376. Text(
  377. '${widget.name} ',
  378. style: TextStyle(
  379. fontSize: ScreenUtil().setSp(16),
  380. color: Colors.white),
  381. ),
  382. (widget.type == "video" ||
  383. userInfo?.expertLevel == null)
  384. ? Container()
  385. : Container(
  386. padding: EdgeInsets.fromLTRB(5, 1, 5, 1),
  387. decoration: BoxDecoration(
  388. //背景
  389. color: Colors.red,
  390. //设置四周圆角 角度
  391. borderRadius: BorderRadius.all(
  392. Radius.circular(4.0)),
  393. ),
  394. child: Text(
  395. 'Lv${userInfo?.expertLevel}',
  396. style: TextStyle(color: Colors.white),
  397. ),
  398. )
  399. ],
  400. ),
  401. Text(
  402. '收到打赏次数 $rewardSecond',
  403. style: TextStyle(
  404. fontSize: ScreenUtil().setSp(11),
  405. color: Colors.white),
  406. ),
  407. SizedBox(
  408. height: ScreenUtil().setHeight(30),
  409. ),
  410. Text(
  411. '客官,如果您觉得答案不错,给TA一点鼓励吧~',
  412. style: TextStyle(
  413. fontSize: ScreenUtil().setSp(13),
  414. color: Colors.white),
  415. )
  416. ],
  417. ),
  418. ),
  419. Container(
  420. padding: EdgeInsets.only(
  421. // left: ScreenUtil().setWidth(20),
  422. right: ScreenUtil().setWidth(15),
  423. top: ScreenUtil().setWidth(15),
  424. bottom: ScreenUtil().setWidth(15)),
  425. decoration: BoxDecoration(
  426. // border: Border(
  427. // bottom:
  428. // BorderSide(width: 5, color: Color(0xfff5f5f5)),
  429. // ),
  430. ),
  431. child: Row(
  432. // mainAxisAlignment: MainAxisAlignment.spaceBetween,
  433. children: <Widget>[
  434. Container(
  435. // margin: EdgeInsets.only(
  436. // top: ScreenUtil().setHeight(10)),
  437. width: ScreenUtil().setWidth(2),
  438. height: ScreenUtil().setHeight(13),
  439. color: Color(0xFF568AFF),
  440. ),
  441. SizedBox(width: ScreenUtil().setWidth(10)),
  442. Text(
  443. "请选择打赏金额",
  444. style: TextStyle(
  445. color: Color(0xff222222),
  446. fontSize: ScreenUtil().setSp(16)),
  447. textAlign: TextAlign.start,
  448. ),
  449. ]),
  450. ),
  451. moneyList != null && moneyList != []
  452. ? Container(
  453. padding: EdgeInsets.only(
  454. left: ScreenUtil().setWidth(10)),
  455. child: Wrap(
  456. alignment: WrapAlignment.start,
  457. crossAxisAlignment: WrapCrossAlignment.center,
  458. children: moneyList.asMap().keys.map((index) {
  459. return InkWell(
  460. onTap: () {
  461. setState(() {
  462. moneyKey = moneyList[index].key;
  463. isRed = moneyList[index].key;
  464. print(isRed);
  465. print(moneyList[index].key);
  466. });
  467. },
  468. child: Container(
  469. alignment: Alignment.center,
  470. width: width / 4 - 15,
  471. height: ScreenUtil().setHeight(50),
  472. // padding: EdgeInsets.only(
  473. // bottom: ScreenUtil().setHeight(26),
  474. // top: ScreenUtil().setHeight(26)),
  475. margin: EdgeInsets.only(
  476. left: 5, right: 5, bottom: 5, top: 5),
  477. decoration: BoxDecoration(
  478. color: isRed == moneyList[index].key
  479. ? Color(0x215589FF)
  480. : Colors.white,
  481. border: Border.all(
  482. width: 1,
  483. color: isRed == moneyList[index].key
  484. ? Colours.blue_app_main
  485. : Color(0xff999999))),
  486. child: Text(
  487. (moneyList[index].value ?? "") + "元",
  488. style: TextStyle(
  489. color: isRed == moneyList[index].key
  490. ? Colours.blue_app_main
  491. : Color(0xff666666),
  492. fontSize: ScreenUtil().setSp(15)),
  493. textAlign: TextAlign.center,
  494. ),
  495. ),
  496. );
  497. }).toList(),
  498. ))
  499. : loadCircle(),
  500. SizedBox(
  501. height: ScreenUtil().setHeight(10),
  502. ),
  503. // Color(0xffF9F9F9)
  504. Container(
  505. height: ScreenUtil().setHeight(10),
  506. color: Color(0xffF9F9F9),
  507. width: double.infinity,
  508. ),
  509. Container(
  510. padding: EdgeInsets.only(
  511. // left: ScreenUtil().setWidth(20),
  512. right: ScreenUtil().setWidth(15),
  513. top: ScreenUtil().setWidth(25),
  514. bottom: ScreenUtil().setWidth(10)),
  515. decoration: BoxDecoration(
  516. border: Border(
  517. bottom:
  518. BorderSide(width: 0.5, color: Color(0xfff5f5f5)),
  519. ),
  520. ),
  521. child: Row(
  522. // mainAxisAlignment: MainAxisAlignment.spaceBetween,
  523. children: <Widget>[
  524. Container(
  525. // margin: EdgeInsets.only(
  526. // top: ScreenUtil().setHeight(10)),
  527. width: ScreenUtil().setWidth(2),
  528. height: ScreenUtil().setHeight(13),
  529. color: Color(0xFF568AFF),
  530. ),
  531. SizedBox(width: ScreenUtil().setWidth(10)),
  532. Text(
  533. "选择支付方式",
  534. style: TextStyle(
  535. color: Color(0xff000000),
  536. fontSize: ScreenUtil().setSp(16)),
  537. textAlign: TextAlign.start,
  538. ),
  539. ]),
  540. ),
  541. Container(
  542. padding: EdgeInsets.only(
  543. left: ScreenUtil().setWidth(15),
  544. bottom: ScreenUtil().setWidth(80),
  545. right: ScreenUtil().setWidth(15)),
  546. child: Column(
  547. children: payType.asMap().keys.map((i) {
  548. return Container(
  549. decoration: BoxDecoration(
  550. border: Border(
  551. bottom: BorderSide(
  552. width: 0.5, color: Color(0xfff5f5f5)),
  553. ),
  554. ),
  555. height: ScreenUtil().setWidth(60),
  556. child: Row(
  557. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  558. children: <Widget>[
  559. Row(
  560. children: <Widget>[
  561. LoadAssetImage(
  562. // image: AssetImage(i['img']),
  563. payType[i]["icon"],
  564. width: ScreenUtil().setWidth(26),
  565. height: ScreenUtil().setWidth(26),
  566. color:
  567. i == 0 ? Colours.blue_app_main : null,
  568. // alignment: Alignment.centerLeft,
  569. ),
  570. Container(width: 5, child: null),
  571. Text(
  572. payType[i]["title"],
  573. style: TextStyle(
  574. color: Color(0xff000000),
  575. fontSize: ScreenUtil().setSp(16)),
  576. textAlign: TextAlign.start,
  577. ),
  578. i == 0
  579. ? Text(
  580. '¥$balance',
  581. style: TextStyle(
  582. color: Color(0xffCCCCCC),
  583. fontSize: 14),
  584. )
  585. : Container()
  586. ],
  587. ),
  588. InkWell(
  589. child: Container(
  590. // padding: EdgeInsets.only(top:10,left:5,right:5,bottom:10),
  591. child: LoadAssetImage(
  592. // image: AssetImage(i['img']),
  593. indexNow == i ? checkTrue : checkFalse,
  594. width: ScreenUtil().setWidth(20),
  595. height: ScreenUtil().setWidth(20),
  596. // alignment: Alignment.centerLeft,
  597. ),
  598. ),
  599. onTap: () {
  600. setState(() {
  601. indexNow = i;
  602. print(indexNow);
  603. });
  604. },
  605. ),
  606. ],
  607. ),
  608. );
  609. }).toList(),
  610. ),
  611. )
  612. ],
  613. ),
  614. ),
  615. Positioned(
  616. bottom: 0,
  617. left: 0,
  618. child: Container(
  619. width: width,
  620. padding: EdgeInsets.only(
  621. top: ScreenUtil().setWidth(15),
  622. bottom: ScreenUtil().setWidth(15),
  623. left: ScreenUtil().setWidth(25),
  624. right: ScreenUtil().setWidth(25),
  625. ),
  626. color: Colors.white,
  627. child: Container(
  628. height: ScreenUtil().setWidth(44),
  629. decoration: BoxDecoration(
  630. borderRadius: BorderRadius.circular(22.0),
  631. color: Colours.blue_app_main,
  632. // gradient: const LinearGradient(
  633. // colors: [Color(0xFF00D9FF), Color(0xFF0287FF)],
  634. // ),
  635. ),
  636. child: FlatButton(
  637. // padding: EdgeInsets.all(15.0),
  638. child: Text("确认打赏"),
  639. textColor: Colors.white,
  640. onPressed: () {
  641. confirmReward();
  642. },
  643. ),
  644. ),
  645. ),
  646. )
  647. ],
  648. ),
  649. ),
  650. ),
  651. );
  652. }
  653. }