vip_service.dart 16 KB

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