vip_service.dart 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. import 'dart:math';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter_alipay/flutter_alipay.dart';
  4. import 'package:flutter_screenutil/flutter_screenutil.dart';
  5. import 'package:flutter_spinkit/flutter_spinkit.dart';
  6. import 'package:fluwx/fluwx.dart' as fluwx;
  7. import 'package:liftmanager/common/user_db.dart';
  8. import 'package:liftmanager/internal/wode/model/vipfee_model.dart';
  9. import 'package:liftmanager/net/api_service.dart';
  10. import 'package:liftmanager/res/colors.dart';
  11. import 'package:liftmanager/utils/fast_notification.dart';
  12. import 'package:liftmanager/utils/theme_utils.dart';
  13. import 'package:liftmanager/utils/toast.dart';
  14. import 'package:liftmanager/widgets/app_bar.dart';
  15. import 'package:liftmanager/widgets/load_image.dart';
  16. class VipService extends StatefulWidget {
  17. VipService(this.id, this.selectId);
  18. final String id;
  19. ///月卡季卡id
  20. final String selectId;
  21. @override
  22. State<StatefulWidget> createState() {
  23. return VipServiceState();
  24. }
  25. }
  26. class VipServiceState extends State<VipService> {
  27. static const payType = [
  28. {
  29. "icon": "tab_first/pay_yue",
  30. "title": "账户余额",
  31. },
  32. {
  33. "icon": "tab_first/pay_zhifubao",
  34. "title": "支付宝",
  35. },
  36. {
  37. "icon": "tab_first/pay_winxin",
  38. "title": "微信",
  39. },
  40. ];
  41. int indexNow = -1;
  42. String checkFalse = "tab_first/check_false";
  43. String checkTrue = "tab_first/check_true";
  44. List<Records> moneyList;
  45. String moneyKey;
  46. String isRed = "";
  47. String title = "";
  48. int rewardType;
  49. @override
  50. void initState() {
  51. super.initState();
  52. moneyKey = widget.selectId;
  53. getVipFeeList();
  54. setSchema();
  55. _initFluwx();
  56. }
  57. _initFluwx() async {
  58. await fluwx.registerWxApi(
  59. appId: "wx0f10e6386fb9969e",
  60. doOnAndroid: true,
  61. doOnIOS: true,
  62. universalLink: "https://www.edtyun.com/");
  63. var result = await fluwx.isWeChatInstalled();
  64. setState(() {});
  65. }
  66. void setSchema() async {
  67. // await FlutterAlipay.setIosUrlSchema("tytylift");
  68. await FlutterAlipay.setIosUrlSchema("com.tyty.lift.managers");
  69. }
  70. Future getVipFeeList() async {
  71. await NewApiService().getVipFeeList(widget.id, onSuccess: (res) {
  72. moneyList = res.records;
  73. setState(() {});
  74. }, onError: (code, msg) {
  75. toasts(msg);
  76. });
  77. }
  78. randomInt(int min, int max) {
  79. return new Random().nextInt(max) % (max - min + 1) + min;
  80. }
  81. // 确认充值
  82. void confirmReward() async {
  83. if (moneyKey == null) {
  84. toasts("请选择一项");
  85. return;
  86. }
  87. if (indexNow == -1) {
  88. toasts("请选择支付方式");
  89. return;
  90. }
  91. String payway;
  92. if (indexNow == 1) {
  93. payway = 'alipay';
  94. await NewApiService().getRechargeVIP(
  95. payway: payway,
  96. orders: moneyKey,
  97. onSuccess: (res) {
  98. callAlipay(res);
  99. },
  100. onError: (code, msg) {
  101. toasts(msg);
  102. },
  103. );
  104. } else if (indexNow == 2) {
  105. payway = 'wx';
  106. await NewApiService().getRechargeVIPwx(
  107. payway: payway,
  108. orders: moneyKey,
  109. onSuccess: (res) {
  110. wxMethod(res);
  111. },
  112. onError: (code, msg) {
  113. toasts(msg);
  114. },
  115. );
  116. } else {
  117. toasts("暂不支持该支付方式");
  118. return;
  119. }
  120. }
  121. dynamic _wxPay;
  122. wxMethod(_payInfo) {
  123. print(_payInfo["appid"]);
  124. fluwx
  125. .payWithWeChat(
  126. appId: _payInfo["appid"],
  127. partnerId: _payInfo["partnerid"],
  128. prepayId: _payInfo["prepayid"],
  129. packageValue: _payInfo["package"],
  130. nonceStr: _payInfo["noncestr"],
  131. timeStamp: int.parse(_payInfo["timestamp"]),
  132. sign: _payInfo["sign"])
  133. .then((data) {
  134. print(data);
  135. });
  136. _wxPay?.cancel();
  137. _wxPay = fluwx.responseFromPayment
  138. .listen((fluwx.WeChatPaymentResponse response) async {
  139. print("WeChatPaymentResponse" + response.errCode.toString());
  140. if (response.errCode == -2) {
  141. //支付取消
  142. toasts("支付取消");
  143. } else if (response.errCode == -1) {
  144. //支付失败
  145. toasts("支付失败");
  146. } else if (response.errCode == 0) {
  147. //支付成功
  148. toasts("支付成功");
  149. upDataUserLogin();
  150. String initThisUserMoney = randomInt(1111, 9999).toString() +
  151. DateTime.now().millisecondsSinceEpoch.toString();
  152. FastNotification.push("initUserMoney", initThisUserMoney);
  153. Navigator.pop(context);
  154. }
  155. });
  156. }
  157. //更新用户信息
  158. Future upDataUserLogin() async {
  159. await NewApiService().upDataUserLogin(onSuccess: (res) {
  160. User().setCurrentUser(res, type: "upDataAppMenus");
  161. String initThisUserMoney = randomInt(1111, 9999).toString() +
  162. DateTime.now().millisecondsSinceEpoch.toString();
  163. FastNotification.push("initUserMoney", initThisUserMoney);
  164. String initThisUserInfomation = randomInt(1111, 9999).toString() +
  165. DateTime.now().millisecondsSinceEpoch.toString();
  166. FastNotification.push("initUserInfomation", initThisUserInfomation);
  167. }, onError: (code, msg) {
  168. toasts(msg);
  169. });
  170. }
  171. // 调用支付宝
  172. void callAlipay(String _payInfo) async {
  173. AlipayResult payResult;
  174. try {
  175. print("The pay info is : " + _payInfo);
  176. payResult = await FlutterAlipay.pay(_payInfo);
  177. } on Exception catch (e) {
  178. payResult = null;
  179. }
  180. print(payResult);
  181. if (payResult != null) {
  182. if (payResult.resultStatus == "9000") {
  183. toasts("支付成功");
  184. upDataUserLogin();
  185. String initThisUserMoney = randomInt(1111, 9999).toString() +
  186. DateTime.now().millisecondsSinceEpoch.toString();
  187. FastNotification.push("initUserMoney", initThisUserMoney);
  188. Navigator.pop(context);
  189. } else {
  190. toasts(payResult.memo);
  191. }
  192. } else {
  193. toasts("支付异常");
  194. }
  195. }
  196. @override
  197. Widget build(BuildContext context) {
  198. double width = MediaQuery.of(context).size.width;
  199. double height = MediaQuery.of(context).size.height;
  200. return Container(
  201. child: Scaffold(
  202. appBar: MyAppBar(
  203. centerTitle: "开通会员",
  204. ),
  205. body: Container(
  206. child: Stack(
  207. children: <Widget>[
  208. Container(
  209. // padding: EdgeInsets.only(top:70),
  210. child: ListView(
  211. children: <Widget>[
  212. // Container(
  213. // padding: EdgeInsets.only(
  214. // left: ScreenUtil().setWidth(20),
  215. // right: ScreenUtil().setWidth(15),
  216. // top: ScreenUtil().setWidth(15),
  217. // bottom: ScreenUtil().setWidth(15)),
  218. // decoration: BoxDecoration(
  219. // // border: Border(
  220. // // bottom:
  221. // // BorderSide(width: 5, color: Color(0xfff5f5f5)),
  222. // // ),
  223. // ),
  224. // child: Row(
  225. // mainAxisAlignment: MainAxisAlignment.spaceBetween,
  226. // children: <Widget>[
  227. // Text(
  228. // "请选择一项",
  229. // style:
  230. // TextStyle(fontSize: ScreenUtil().setSp(16)),
  231. // textAlign: TextAlign.start,
  232. // ),
  233. // ]),
  234. // ),
  235. // moneyList != null && moneyList != []
  236. // ? Container(
  237. // padding: EdgeInsets.only(
  238. // left: ScreenUtil().setWidth(10)),
  239. // child: Wrap(
  240. // alignment: WrapAlignment.start,
  241. // crossAxisAlignment: WrapCrossAlignment.center,
  242. // children: moneyList.asMap().keys.map((index) {
  243. // return InkWell(
  244. // onTap: () {
  245. // setState(() {
  246. // moneyKey = moneyList[index].id.toString();
  247. // isRed = moneyList[index].id.toString();
  248. // });
  249. // },
  250. // child: Container(
  251. // width: width / 4 - 15,
  252. // padding: EdgeInsets.only(
  253. // bottom: ScreenUtil().setWidth(16),
  254. // top: ScreenUtil().setWidth(16)),
  255. // margin: EdgeInsets.only(
  256. // left: 5, right: 5, bottom: 5, top: 5),
  257. // decoration: BoxDecoration(
  258. // color: ThemeUtils.getTabsBg(context),
  259. // borderRadius: BorderRadius.circular(5),
  260. // boxShadow: [
  261. // BoxShadow(
  262. // offset: Offset(0, 0), //x,y轴
  263. // color: Colors.grey[300], //投影颜色
  264. // blurRadius: 5, //投影距离
  265. // )
  266. // ],
  267. // ),
  268. // child: Column(
  269. // children: <Widget>[
  270. // Text(
  271. // moneyList[index].name ?? "",
  272. // style: TextStyle(
  273. // color: isRed ==
  274. // moneyList[index]
  275. // .id
  276. // .toString()
  277. // ? Color(0xffff0000)
  278. // : Color(0xff666666),
  279. // fontSize: ScreenUtil().setSp(15)),
  280. // textAlign: TextAlign.center,
  281. // ),
  282. // Text(
  283. // (moneyList[index].price != null
  284. // ? moneyList[index]
  285. // .price
  286. // .toString()
  287. // : "0.0") +
  288. // "元",
  289. // style: TextStyle(
  290. // color: isRed ==
  291. // moneyList[index]
  292. // .id
  293. // .toString()
  294. // ? Color(0xffff0000)
  295. // : Color(0xff666666),
  296. // fontSize: ScreenUtil().setSp(15)),
  297. // textAlign: TextAlign.center,
  298. // )
  299. // ],
  300. // ),
  301. // ),
  302. // );
  303. // }).toList(),
  304. // ))
  305. // : loadCircle(),
  306. Container(
  307. padding: EdgeInsets.only(
  308. left: ScreenUtil().setWidth(20),
  309. right: ScreenUtil().setWidth(15),
  310. // top: ScreenUtil().setWidth(50),
  311. bottom: ScreenUtil().setWidth(10)),
  312. decoration: BoxDecoration(
  313. border: Border(
  314. bottom:
  315. BorderSide(width: 0.5, color: Color(0xfff5f5f5)),
  316. ),
  317. ),
  318. child: Row(
  319. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  320. children: <Widget>[
  321. Text(
  322. "选择支付方式",
  323. style:
  324. TextStyle(fontSize: ScreenUtil().setSp(16)),
  325. textAlign: TextAlign.start,
  326. ),
  327. ]),
  328. ),
  329. Container(
  330. padding: EdgeInsets.only(
  331. left: ScreenUtil().setWidth(15),
  332. bottom: ScreenUtil().setWidth(80),
  333. right: ScreenUtil().setWidth(15)),
  334. child: Column(
  335. children: payType.asMap().keys.map((i) {
  336. return i != 0
  337. ? Container(
  338. decoration: BoxDecoration(
  339. border: Border(
  340. bottom: BorderSide(
  341. width: 0.5, color: Color(0xfff5f5f5)),
  342. ),
  343. ),
  344. height: ScreenUtil().setWidth(60),
  345. child: Row(
  346. mainAxisAlignment:
  347. MainAxisAlignment.spaceBetween,
  348. children: <Widget>[
  349. Row(
  350. children: <Widget>[
  351. LoadAssetImage(
  352. // image: AssetImage(i['img']),
  353. payType[i]["icon"],
  354. width: ScreenUtil().setWidth(26),
  355. height: ScreenUtil().setWidth(26),
  356. // alignment: Alignment.centerLeft,
  357. ),
  358. Container(width: 5, child: null),
  359. Text(
  360. payType[i]["title"],
  361. style: TextStyle(
  362. fontSize:
  363. ScreenUtil().setSp(16)),
  364. textAlign: TextAlign.start,
  365. ),
  366. ],
  367. ),
  368. InkWell(
  369. child: Container(
  370. // padding: EdgeInsets.only(top:10,left:5,right:5,bottom:10),
  371. child: LoadAssetImage(
  372. // image: AssetImage(i['img']),
  373. indexNow == i
  374. ? checkTrue
  375. : checkFalse,
  376. width: ScreenUtil().setWidth(20),
  377. height: ScreenUtil().setWidth(20),
  378. // alignment: Alignment.centerLeft,
  379. ),
  380. ),
  381. onTap: () {
  382. setState(() {
  383. indexNow = i;
  384. });
  385. },
  386. ),
  387. ],
  388. ),
  389. )
  390. : Container(child: null);
  391. }).toList(),
  392. ),
  393. )
  394. ],
  395. ),
  396. ),
  397. Positioned(
  398. bottom: 0,
  399. left: 0,
  400. child: Container(
  401. width: width,
  402. padding: EdgeInsets.only(
  403. top: ScreenUtil().setWidth(15),
  404. bottom: ScreenUtil().setWidth(15),
  405. left: ScreenUtil().setWidth(25),
  406. right: ScreenUtil().setWidth(25),
  407. ),
  408. color: ThemeUtils.getTabsBg(context),
  409. child: Container(
  410. height: ScreenUtil().setWidth(44),
  411. decoration: BoxDecoration(
  412. color: Colours.blue_app_main,
  413. borderRadius: BorderRadius.circular(22.0),
  414. // gradient: const LinearGradient(
  415. // colors: [Color(0xFF00D9FF), Color(0xFF0287FF)],
  416. // ),
  417. ),
  418. child: FlatButton(
  419. // padding: EdgeInsets.all(15.0),
  420. child: Text("立即支付"),
  421. textColor: Colors.white,
  422. onPressed: () {
  423. // upDataUserLogin();
  424. confirmReward();
  425. },
  426. ),
  427. ),
  428. ),
  429. )
  430. ],
  431. ),
  432. ),
  433. ),
  434. );
  435. }
  436. Widget loadCircle() {
  437. return Container(
  438. padding: EdgeInsets.only(top: 10, bottom: 10),
  439. color: ThemeUtils.getTabsBg(context),
  440. child: Center(
  441. child: SpinKitFadingCircle(
  442. color: Colors.blueAccent,
  443. size: 30.0,
  444. ),
  445. ),
  446. );
  447. }
  448. }