member_controller.dart 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  1. import 'dart:async';
  2. import 'dart:io';
  3. import 'dart:math';
  4. import 'dart:ui';
  5. import 'package:agile_pay/flutter_pay.dart';
  6. import 'package:flutter/material.dart';
  7. import 'package:flutter_screenutil/flutter_screenutil.dart';
  8. import 'package:get/get.dart';
  9. import 'package:injectable/injectable.dart';
  10. import 'package:location/base/base_controller.dart';
  11. import 'package:location/data/bean/goods_bean.dart';
  12. import 'package:location/data/bean/pay_item_bean.dart';
  13. import 'package:location/data/consts/channel_id.dart';
  14. import 'package:location/data/repositories/account_repository.dart';
  15. import 'package:location/data/repositories/member_repository.dart';
  16. import 'package:location/dialog/alipay_qr_code_dialog.dart';
  17. import 'package:location/handler/error_handler.dart';
  18. import 'package:location/module/login/login_page.dart';
  19. import 'package:location/resource/assets.gen.dart';
  20. import 'package:location/utils/async_util.dart';
  21. import 'package:location/utils/common_expand.dart';
  22. import 'package:location/utils/toast_util.dart';
  23. import '../../data/api/response/subscription_check_response.dart';
  24. import '../../data/bean/goods_evaluate_info.dart';
  25. import '../../data/bean/member_status_info.dart';
  26. import '../../data/bean/wechat_payment_sign_bean.dart';
  27. import '../../data/consts/error_code.dart';
  28. import '../../data/consts/payment_type.dart';
  29. import '../../data/consts/web_url.dart';
  30. import '../../device/atmob_platform_info.dart';
  31. import '../../dialog/common_confirm_dialog_impl.dart';
  32. import '../../dialog/loading_dialog.dart';
  33. import '../../dialog/member_retain_dialog.dart';
  34. import '../../dialog/wechat_qr_code_dialog.dart';
  35. import '../../resource/string.gen.dart';
  36. import '../../sdk/wechat/wechat_share_util.dart';
  37. import '../../utils/app_review_service.dart';
  38. import '../../utils/http_handler.dart';
  39. import '../../utils/payment_status_manager.dart';
  40. import '../../widget/animated_switcher_widget.dart';
  41. import '../browser/browser_view.dart';
  42. import 'member_evaluation_pop_up_dialog.dart';
  43. import 'member_first_week_discount_dialog.dart';
  44. import 'member_fun_bean.dart';
  45. import 'package:apple_pay/apple_pay.dart';
  46. import 'member_page.dart';
  47. import 'member_payment_completed_dialog.dart';
  48. @injectable
  49. class MemberController extends BaseController implements PaymentStatusCallback {
  50. final AccountRepository accountRepository;
  51. final MemberRepository memberRepository;
  52. final PaymentStatusManager paymentStatusManager;
  53. final RxBool _isShowMemberSubscribeTxt = false.obs;
  54. bool get isShowMemberSubscribeTxt => _isShowMemberSubscribeTxt.value;
  55. final switcherController = SwitcherController();
  56. final ScrollController scrollController = ScrollController();
  57. StreamController? _changeStreamController;
  58. final random = Random();
  59. final RxDouble _toolBarOpacity = 0.0.obs;
  60. double get toolBarOpacity => _toolBarOpacity.value;
  61. final List<String> _storeTypes = ['终身会员', '年度会员', '月度会员'];
  62. String? get phone => accountRepository.loginPhoneNum.value;
  63. bool get isLogin => accountRepository.isLogin.value;
  64. MemberStatusInfo? get memberStatusInfo =>
  65. accountRepository.memberStatusInfo.value;
  66. final RxList<GoodsBean> goodsList = <GoodsBean>[].obs;
  67. final Rxn<GoodsBean> _selectedGoods = Rxn<GoodsBean>();
  68. GoodsBean? get selectedGoods => _selectedGoods.value;
  69. final Rxn<PayItemBean> _selectedPayWay = Rxn<PayItemBean>();
  70. PayItemBean? get selectedPayWay => _selectedPayWay.value;
  71. final RxList<PayItemBean> payItemList = <PayItemBean>[].obs;
  72. ///评论
  73. final RxList<GoodsEvaluateInfo> evaluateList = <GoodsEvaluateInfo>[].obs;
  74. CancelableFuture? _memberDataFuture;
  75. Duration? get activityDuration => memberRepository.activityDuration.value;
  76. ///检查续订的状态
  77. final Rx<SubscriptionCheckResponse> _checkResponse =
  78. Rx(SubscriptionCheckResponse(outTradeNo: ""));
  79. SubscriptionCheckResponse? get requestCheckResponse => _checkResponse.value;
  80. bool _isShowPayFailedDialog = false; // 是否已经展示过支付失败弹窗
  81. final List<MemberFunBean> funList = [
  82. MemberFunBean(1, Assets.images.iconMemberFun1.path,
  83. StringName.memberFunName1, StringName.memberFunName1Desc),
  84. MemberFunBean(2, Assets.images.iconMemberFun2.path,
  85. StringName.memberFunName2, StringName.memberFunName2Desc),
  86. // MemberFunBean(3, Assets.images.iconMemberFun3.path,
  87. // StringName.memberFunName3, StringName.memberFunName3Desc),
  88. // MemberFunBean(4, Assets.images.iconMemberFun4.path,
  89. // StringName.memberFunName4, StringName.memberFunName4Desc), //该功能还未开发
  90. MemberFunBean(5, Assets.images.iconMemberFun5.path,
  91. StringName.memberFunName5, StringName.memberFunName5Desc),
  92. MemberFunBean(6, Assets.images.iconMemberFun6.path,
  93. StringName.memberFunName6, StringName.memberFunName6Desc),
  94. ];
  95. /*final List<MemberEvaluateBean> evaluateList = [
  96. MemberEvaluateBean(1, Assets.images.iconEvaluate1.path, '用户189****7913',
  97. "上班没时间,远程遛娃,非常方便很好用。"),
  98. MemberEvaluateBean(2, Assets.images.iconEvaluate2.path, '用户177****2345',
  99. "这个功能太棒了!尤其是夜间出行时,一键报警让我感觉特别安心。"),
  100. MemberEvaluateBean(3, Assets.images.iconEvaluate3.path, '用户138****6789',
  101. "强烈推荐!我和家人经常用这个功能来共享位置,尤其是旅游时,走散了也不怕。"),
  102. MemberEvaluateBean(4, Assets.images.iconEvaluate4.path, '用户159****3456',
  103. "实时定位非常精准,用来监控孩子的行踪特别方便,再也不用担心他们乱跑了。"),
  104. MemberEvaluateBean(5, Assets.images.iconEvaluate5.path, '用户182****9012',
  105. "用来遛狗也很方便,再也不用担心狗狗跑丢了,真是个好工具!"),
  106. ];*/
  107. MemberController(
  108. this.accountRepository, this.memberRepository, this.paymentStatusManager);
  109. @override
  110. void onReady() async {
  111. super.onReady();
  112. _isTherePopUpPrompt();
  113. _startAnimationSwitcher();
  114. scrollController.addListener(() {
  115. double offset = scrollController.offset;
  116. double opacity = offset / 100;
  117. if (opacity > 1) {
  118. opacity = 1;
  119. } else if (opacity < 0) {
  120. opacity = 0;
  121. }
  122. _toolBarOpacity.value = opacity;
  123. });
  124. refreshMemberData();
  125. precacheImage(
  126. Assets.images.iconMemberSpecialProductsSelect.provider(), Get.context!);
  127. precacheImage(
  128. Assets.images.iconMemberSpecialProductsNormal.provider(), Get.context!);
  129. }
  130. ///是否弹窗提示
  131. void _isTherePopUpPrompt() {
  132. MemberPageType pageType =
  133. Get.arguments ?? MemberPageType.universalAccessEnter;
  134. if (pageType == MemberPageType.afeterTrialMemberEnter) {
  135. MemberFirstWeekDiscountDialog.show(confirmOnTap: () {
  136. onBuyClick();
  137. }, onPrivacyPolicyClick: () {
  138. onPrivacyPolicyClick();
  139. }, onTermOfServiceClick: () {
  140. onTermOfServiceClick();
  141. });
  142. }
  143. }
  144. void _startAnimationSwitcher() {
  145. _changeStreamController = AsyncUtil.interval(
  146. (time) => changeSwitcherContent(), Duration(seconds: 3), -1);
  147. }
  148. Future<void> changeSwitcherContent() async {
  149. int userId = random.nextInt(10000);
  150. String userIdStr = userId.toString();
  151. int padLength = 4 - userIdStr.length;
  152. for (int i = 0; i < padLength; i++) {
  153. userIdStr = random.nextInt(10).toString() + userIdStr;
  154. }
  155. bool isHour = random.nextBool();
  156. String secondsOrHour;
  157. if (isHour) {
  158. secondsOrHour = "${1 + random.nextInt(8)}小时";
  159. } else {
  160. secondsOrHour = "${1 + random.nextInt(59)}分钟";
  161. }
  162. int index = random.nextInt(_storeTypes.length);
  163. switcherController.updateWidget(Row(
  164. mainAxisAlignment: MainAxisAlignment.center,
  165. children: [
  166. RichText(
  167. text: TextSpan(
  168. style: TextStyle(
  169. fontSize: 11.sp,
  170. color: Colors.white,
  171. fontWeight: FontWeight.w400),
  172. children: [
  173. TextSpan(text: userIdStr),
  174. TextSpan(text: '用户 '),
  175. TextSpan(text: secondsOrHour),
  176. TextSpan(text: '前购买了'),
  177. TextSpan(
  178. text: _storeTypes[index],
  179. style: TextStyle(color: '#FFC95D'.color)),
  180. ]))
  181. ],
  182. ));
  183. }
  184. String getUserName(String phone) {
  185. if (phone.length > 4) {
  186. phone = phone.substring(phone.length - 4);
  187. }
  188. return '${StringName.mineAccountLoggedDesc}$phone';
  189. }
  190. void back() {
  191. Get.back();
  192. }
  193. void onLoginClick() {
  194. if (accountRepository.isLogin.value) {
  195. return;
  196. }
  197. LoginPage.start();
  198. }
  199. void refreshMemberData() {
  200. _memberDataFuture?.cancel();
  201. _memberDataFuture =
  202. AsyncUtil.retryWithExponentialBackoff(() => _requestMemberData(), 4);
  203. _memberDataFuture?.catchError((error) {
  204. ErrorHandler.toastError(error);
  205. });
  206. }
  207. Future<void> _requestMemberData() {
  208. return memberRepository
  209. .getMemberList(itemListType: Platform.isIOS ? 2 : 0)
  210. .then((response) {
  211. goodsList.clear();
  212. payItemList.clear();
  213. evaluateList.clear();
  214. _selectedGoods.value = null;
  215. _checkMemberHasSubscribe(response.goodsList);
  216. if (response.goodsList?.isNotEmpty == true) {
  217. goodsList.addAll(response.goodsList!);
  218. _selectedGoods.value = goodsList.first;
  219. }
  220. if (response.payInfoList?.isNotEmpty == true) {
  221. payItemList.addAll(response.payInfoList!);
  222. _selectedPayWay.value = payItemList.first;
  223. }
  224. if (response.evaluateList?.isNotEmpty == true) {
  225. evaluateList.addAll(response.evaluateList!);
  226. }
  227. });
  228. }
  229. void _checkMemberHasSubscribe(List<GoodsBean>? goodsList) {
  230. if (goodsList == null) {
  231. _isShowMemberSubscribeTxt.value = false;
  232. return;
  233. }
  234. for (var goods in goodsList) {
  235. if (goods.subscribable == 1) {
  236. _isShowMemberSubscribeTxt.value = true;
  237. return;
  238. }
  239. }
  240. _isShowMemberSubscribeTxt.value = false;
  241. }
  242. void onGoodsItemClick(GoodsBean item) {
  243. _selectedGoods.value = item;
  244. onBuyClick();
  245. }
  246. void onPrivacyPolicyClick() {
  247. BrowserPage.start(WebUrl.privacyPolicy);
  248. }
  249. void onTermOfServiceClick() {
  250. BrowserPage.start(WebUrl.userAgreement);
  251. }
  252. void onRenewalAgreementClick() {
  253. BrowserPage.start(WebUrl.renewalAgreement);
  254. }
  255. void onPayWayItemClick(PayItemBean item) {
  256. _selectedPayWay.value = item;
  257. }
  258. void onPopBack({bool iosIsBack = true}) {
  259. if (Platform.isIOS) {
  260. if (iosIsBack) {
  261. back();
  262. return;
  263. }
  264. if (accountRepository.memberIsExpired()) {
  265. FocusScope.of(Get.context!).unfocus();
  266. userCancelsPaymentDisplay();
  267. }
  268. } else {
  269. if (accountRepository.memberIsExpired()) {
  270. userCancelsPaymentDisplay();
  271. } else {
  272. back();
  273. }
  274. }
  275. }
  276. ///用户取消支付展示
  277. void userCancelsPaymentDisplay() {
  278. if (_isShowPayFailedDialog) {
  279. if (Platform.isAndroid) back();
  280. return;
  281. }
  282. if (Platform.isAndroid) {
  283. showRetainDialog(isBack: true);
  284. } else {
  285. Future.delayed(Duration(milliseconds: 500), () {
  286. showRetainDialog(isBack: true);
  287. });
  288. }
  289. }
  290. ///支付错误的时候调用
  291. void showRetainDialog({bool isBack = false, int? errno}) {
  292. _isShowPayFailedDialog = true;
  293. MemberRetainDialog.show(
  294. payClick: () {
  295. onBuyClick();
  296. },
  297. cancelClick: () {
  298. errorPayToast(errno);
  299. if (Platform.isAndroid && isBack) back();
  300. },
  301. isShowRecommendView: isBack);
  302. }
  303. void onBuyClick() async {
  304. final buyGoods = selectedGoods;
  305. final buyPayWay = selectedPayWay;
  306. if (buyGoods == null) {
  307. ToastUtil.show(StringName.memberPleaseChoiceGoods);
  308. return;
  309. }
  310. if (buyPayWay == null) {
  311. ToastUtil.show(StringName.memberPleaseChoicePayment);
  312. return;
  313. }
  314. memberRepository.setLastSelectedMember(buyGoods, buyPayWay);
  315. //增加渠道登录判断
  316. if (Platform.isAndroid && atmobPlatformInfo.tgPlatform == ChannelId.sd) {
  317. if (!accountRepository.isLogin.value) {
  318. ToastUtil.show(StringName.accountNoLogin);
  319. final isLogin = await LoginPage.start();
  320. if (!isLogin) {
  321. return;
  322. }
  323. }
  324. }
  325. int goodsId = buyGoods.id;
  326. int payPlatform = buyPayWay.payPlatform;
  327. int payMethod = buyPayWay.payMethod;
  328. int payWayType =
  329. getPayWayType(payMethod: payMethod, payPlatform: payPlatform);
  330. LoadingDialog.show(StringName.payLoading,
  331. backDismiss: true, clickMaskDismiss: true);
  332. memberRepository
  333. .submitAndRequestPay(
  334. goodsId: goodsId, payPlatform: payPlatform, payMethod: payMethod)
  335. .then((response) {
  336. if (payWayType == PayWayType.paymentWayWechat) {
  337. _onWeChatPay(response.outTradeNo, response.wechatPayPrepayJson!,
  338. payMethod, buyGoods, buyPayWay);
  339. } else if (payWayType == PayWayType.paymentWayWechatScan) {
  340. _onWechatScanPay(response.outTradeNo, response.wechatPayQrcodeUrl!,
  341. buyPayWay, buyGoods);
  342. } else if (payWayType == PayWayType.paymentWayAlipay) {
  343. _onAliPay(response.outTradeNo, response.alipayOrderString!, payMethod,
  344. buyGoods, buyPayWay);
  345. } else if (payWayType == PayWayType.paymentWayAlipayScan) {
  346. _onAliScanPay(response.outTradeNo, response.alipayQrcodeHtml!,
  347. buyPayWay, buyGoods);
  348. } else if (payWayType == PayWayType.paymentWayApple) {
  349. _onApplePay(response.outTradeNo, response.appAccountToken ?? "",
  350. buyPayWay, buyGoods);
  351. } else {
  352. LoadingDialog.hide();
  353. ToastUtil.show(StringName.payNotSupport);
  354. }
  355. }).catchError((error) {
  356. LoadingDialog.hide();
  357. //处理购买失败
  358. showRetainDialog();
  359. if (error is ServerErrorException) {
  360. if (error.code == ErrorCode.payOrderError) {
  361. refreshMemberData();
  362. ToastUtil.show(error.message);
  363. } else if (error.code == ErrorCode.noLoginError) {
  364. ToastUtil.show(StringName.accountNoLogin);
  365. LoginPage.start();
  366. } else {
  367. ToastUtil.show(error.message);
  368. }
  369. } else {
  370. ToastUtil.show(StringName.memberPaymentFailed);
  371. }
  372. });
  373. }
  374. ///发起购买请求
  375. Future<void> _onApplePay(
  376. String outTradeNo,
  377. String appAccountToken,
  378. PayItemBean payWayInfo,
  379. GoodsBean goodsInfo,
  380. ) async {
  381. final result = await ApplePay().purchase(
  382. productId: goodsInfo.appleGoodsId ?? "",
  383. appAccountToken: appAccountToken,
  384. );
  385. if (result["success"] == true) {
  386. var receipt = result['receipt'];
  387. print('购买成功: ${result['receipt']}');
  388. checkPaymentStatus(
  389. outTradeNo,
  390. payWayInfo,
  391. goodsInfo,
  392. receiptData: receipt,
  393. );
  394. } else {
  395. LoadingDialog.hide();
  396. ToastUtil.show("支付失败,请稍后重试");
  397. print('购买失败: ${result['error']}');
  398. showRetainDialog();
  399. }
  400. }
  401. void _onAliScanPay(String outTradeNo, String qrHtml, PayItemBean paymentWay,
  402. GoodsBean goodsBean) {
  403. LoadingDialog.hide();
  404. AlipayQrCodeDialog.show(
  405. qrCodeHtml: qrHtml,
  406. loadSuccessCallback: () {
  407. checkPaymentStatus(outTradeNo, paymentWay, goodsBean);
  408. },
  409. onCloseCallback: () async {
  410. //关闭后再持续查询几秒
  411. CustomLoadingDialog.show();
  412. await Future.delayed(Duration(seconds: 4));
  413. paymentStatusManager.removePollingSubscription(outTradeNo);
  414. CustomLoadingDialog.hide();
  415. });
  416. }
  417. void checkPaymentStatus(
  418. String orderNo, PayItemBean paymentWay, GoodsBean goodsBean,
  419. {String? receiptData}) {
  420. LoadingDialog.show(StringName.payQuerypayState);
  421. paymentStatusManager.registerPaymentSuccessCallback(orderNo, this);
  422. paymentStatusManager.checkPaymentStatus(orderNo, paymentWay, goodsBean,
  423. receiptData: receiptData);
  424. }
  425. void _onWeChatPay(String outTradeNo, String payJson, int payMethod,
  426. GoodsBean buyGoods, PayItemBean buyPayWay) {
  427. final bean = WechatPaymentSignBean.stringToBean(payJson);
  428. final payInfo = WechatPayInfo(
  429. appId: bean.appId,
  430. partnerId: bean.partnerId,
  431. prepayId: bean.prepayId,
  432. package: bean.package,
  433. noncestr: bean.nonceStr,
  434. timestamp: bean.timeStamp,
  435. sign: bean.sign);
  436. requestSdkPay(payInfo, outTradeNo, payMethod, buyGoods, buyPayWay);
  437. }
  438. void _onAliPay(String outTradeNo, String payJson, int payMethod,
  439. GoodsBean buyGoods, PayItemBean buyPayWay) {
  440. final payInfo = AliPayInfo(payJson);
  441. requestSdkPay(payInfo, outTradeNo, payMethod, buyGoods, buyPayWay);
  442. }
  443. void _onWechatScanPay(String outTradeNo, String qrCodeUrl,
  444. PayItemBean paymentWay, GoodsBean goodsBean) {
  445. LoadingDialog.hide();
  446. WechatQrCodeDialog.show(
  447. qrCodeUrl: qrCodeUrl,
  448. loadSuccessCallback: () {
  449. checkPaymentStatus(outTradeNo, paymentWay, goodsBean);
  450. },
  451. onCloseCallback: () async {
  452. //关闭后再持续查询几秒
  453. CustomLoadingDialog.show();
  454. await Future.delayed(Duration(seconds: 4));
  455. paymentStatusManager.removePollingSubscription(outTradeNo);
  456. CustomLoadingDialog.hide();
  457. });
  458. }
  459. ///查询订阅状态
  460. Future<void> _requestCheckRestoreStatus(String? receiptData) async {
  461. memberRepository.subscriptionCheck(3, receiptData ?? "").then((value) {
  462. _checkResponse.value = value;
  463. }).catchError((error) {});
  464. }
  465. /// 点击了恢复订阅
  466. Future<void> clickRecoverSubscribe() async {
  467. PayItemBean? paymentWay = _selectedPayWay.value;
  468. if (paymentWay == null) {
  469. return;
  470. }
  471. int payPlatform = paymentWay.payPlatform;
  472. int payMethod = paymentWay.payMethod;
  473. CustomLoadingDialog.show();
  474. Future.delayed(const Duration(seconds: 20), () {
  475. CustomLoadingDialog.hide();
  476. //ToastUtil.show("没有发现可恢复的记录");
  477. });
  478. final result = await ApplePay().restore();
  479. if (result["success"] == true) {
  480. // CustomLoadingDialog.hide();
  481. var receipt = result['receipt'];
  482. print('查找恢复记录成功: ${result['receipt']}');
  483. checkRestoreStatus(receipt);
  484. } else {
  485. CustomLoadingDialog.hide();
  486. ToastUtil.show("恢复失败");
  487. print('恢复失败: ${result['error']}');
  488. }
  489. // 显示恢复订阅弹窗
  490. // RecoverSubscribeDialog.show("周会员2025年3月6日到期。", () {
  491. // AtmobLog.d(tag, "恢复订阅弹窗 => 点击确认");
  492. // });
  493. }
  494. /// 检查恢复订阅结果
  495. Future<void> checkRestoreStatus(String? receiptData) async {
  496. PayItemBean? paymentWay = _selectedPayWay.value;
  497. if (paymentWay == null) {
  498. // ToastUtil.showToast(StringName.storeChoicePayment.tr);
  499. return;
  500. }
  501. if (receiptData == null) {
  502. return;
  503. }
  504. int payPlatform = paymentWay.payPlatform;
  505. int payMethod = paymentWay.payMethod;
  506. // var code = await storeRepository.resume(payPlatform, payMethod, receiptData);
  507. memberRepository.subscriptionResume(3, receiptData).then((data) async {
  508. CustomLoadingDialog.hide();
  509. ToastUtil.show("恢复成功");
  510. await AccountRepository.getInstance().getMemberStatus();
  511. //accountRepository.refreshMemberStatus();
  512. Get.back();
  513. }).catchError((error) {
  514. CustomLoadingDialog.hide();
  515. ToastUtil.show("恢复失败");
  516. });
  517. // if (code == 0) {
  518. // CustomLoadingDialog.hide();
  519. // ToastUtil.show("Restore success");
  520. // userRepository.getUserInfo();
  521. // Get.back();
  522. // } else {
  523. // CustomLoadingDialog.hide();
  524. // ToastUtil.show("Restore fail");
  525. // }
  526. }
  527. void requestSdkPay(dynamic payInfo, String outTradeNo, int payMethod,
  528. GoodsBean buyGoods, PayItemBean buyPayWay) {
  529. AgilePay.startPay(payInfo, success: (String? result) {
  530. checkPaymentStatus(outTradeNo, buyPayWay, buyGoods, receiptData: result);
  531. }, payError: (int errno, String? errorMessage) {
  532. LoadingDialog.hide();
  533. debugPrint('zk---payError: $errno, $errorMessage');
  534. errorEventReport(payMethod);
  535. showRetainDialog(errno: errno);
  536. }, error: (int errno, String? error) {
  537. LoadingDialog.hide();
  538. debugPrint('zk---error: $errno, $error');
  539. errorEventReport(payMethod);
  540. showRetainDialog(errno: errno);
  541. });
  542. }
  543. void errorEventReport(int payMethod) {
  544. if (payMethod == PayMethod.wechat) {
  545. // EventHandler.report();
  546. } else if (payMethod == PayMethod.alipay) {
  547. // EventHandler.report();
  548. } else if (payMethod == PayMethod.apple) {
  549. // EventHandler.report();
  550. }
  551. }
  552. void errorPayToast(int? errno) {
  553. if (errno == null) {
  554. return;
  555. }
  556. if (errno == AgilePayCode.payCodeNotSupport) {
  557. ToastUtil.show(StringName.payNotSupport);
  558. } else if (errno == AgilePayCode.payCodeCancelError) {
  559. ToastUtil.show(StringName.payUserCancel);
  560. } else if (errno == AgilePayCode.payCodeWxEnvError) {
  561. ToastUtil.show(StringName.payWxEvnError);
  562. } else if (errno == AgilePayCode.payCodeNotConnectStore) {
  563. ToastUtil.show(StringName.payNotConnectStore);
  564. } else {
  565. ToastUtil.show(StringName.payError);
  566. }
  567. }
  568. @override
  569. void onClose() {
  570. super.onClose();
  571. _changeStreamController?.close();
  572. _memberDataFuture?.cancel();
  573. scrollController.dispose();
  574. paymentStatusManager.unregisterPaymentSuccessCallback(this);
  575. }
  576. @override
  577. void onPaymentSuccess(
  578. String orderNo, PayItemBean paymentWay, GoodsBean storeItemBean) {
  579. ///购买成功消失
  580. LoadingDialog.hide();
  581. ///购买成功之后弹出
  582. afterTheFirstPurchasePromptSharingBoxPops();
  583. }
  584. @override
  585. void onPaymentFail() {
  586. ///购买成功消失
  587. LoadingDialog.hide();
  588. }
  589. ///第一次购买之后弹出提示分享框
  590. void afterTheFirstPurchasePromptSharingBoxPops() {
  591. paymentStatusManager.onOrderFirstCheck().then((checkReponse) {
  592. //是否展示邀请弹框
  593. if (checkReponse.showInvite || checkReponse.showEvaluate) {
  594. MemberPageType pageType =
  595. Get.arguments ?? MemberPageType.universalAccessEnter;
  596. //去分享
  597. if (checkReponse.showInvite &&
  598. pageType == MemberPageType.addFriendToEnter) {
  599. MemberPaymentCompletedDialog.show(confirmOnTap: () {
  600. WechatShareUtil.shareWebPage().catchError((error) {
  601. ToastUtil.show(error);
  602. });
  603. if (checkReponse.showEvaluate) {
  604. ///是否展示好评领会员弹框
  605. MemberEvaluationPopUpDialog.show(
  606. confirmOnTap: () {
  607. AppReviewService.requestAppReview(Get.context!);
  608. onPaySucessShow();
  609. },
  610. cancelOnTap: () {
  611. onPaySucessShow();
  612. },
  613. days: 1);
  614. } else {
  615. onPaySucessShow();
  616. }
  617. }, cancelOnTap: () {
  618. if (checkReponse.showEvaluate) {
  619. ///是否展示好评领会员弹框
  620. MemberEvaluationPopUpDialog.show(
  621. confirmOnTap: () {
  622. AppReviewService.requestAppReview(Get.context!);
  623. ///领取会员后请求
  624. memberRepository
  625. .memberEvaluate()
  626. .then((_) {})
  627. .catchError((error) {});
  628. ; //memberEvaluate
  629. onPaySucessShow();
  630. },
  631. cancelOnTap: () {
  632. onPaySucessShow();
  633. },
  634. days: 1);
  635. } else {
  636. onPaySucessShow();
  637. }
  638. });
  639. } else if (checkReponse.showEvaluate) {
  640. ///是否展示好评领会员弹框
  641. MemberEvaluationPopUpDialog.show(
  642. confirmOnTap: () {
  643. AppReviewService.requestAppReview(Get.context!);
  644. onPaySucessShow();
  645. },
  646. cancelOnTap: () {
  647. onPaySucessShow();
  648. },
  649. days: 1);
  650. }
  651. } else {
  652. onPaySucessShow();
  653. }
  654. }).catchError((erro) {
  655. onPaySucessShow();
  656. });
  657. }
  658. ///支付成功之后展示
  659. void onPaySucessShow() {
  660. try {
  661. WechatQrCodeDialog.dismiss();
  662. AlipayQrCodeDialog.dismiss();
  663. CustomLoadingDialog.hide();
  664. LoadingDialog.hide();
  665. } catch (e) {
  666. debugPrint('zk---onPaymentSuccess error: $e');
  667. }
  668. showPaymentSuccessDialog(onConfirm: back, onCancel: back);
  669. }
  670. }