member_controller.dart 25 KB

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