member_controller.dart 24 KB

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