member_controller.dart 23 KB

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