question_pay.dart 24 KB

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