member_controller.dart 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  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 '../../data/repositories/phone_event_repository.dart';
  29. import '../../dialog/common_confirm_dialog_impl.dart';
  30. import '../../dialog/loading_dialog.dart';
  31. import '../../dialog/member_retain_dialog.dart';
  32. import '../../dialog/wechat_qr_code_dialog.dart';
  33. import '../../resource/string.gen.dart';
  34. import '../../sdk/wechat/wechat_share_util.dart';
  35. import '../../utils/app_review_service.dart';
  36. import '../../utils/http_handler.dart';
  37. import '../../utils/mmkv_util.dart';
  38. import '../../utils/payment_status_manager.dart';
  39. import '../../widget/animated_switcher_widget.dart';
  40. import '../browser/browser_view.dart';
  41. import '../mine/mine_controller.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. import 'member_user_cancel_pay_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 switcherController = SwitcherController();
  55. final ScrollController scrollController = ScrollController();
  56. StreamController? _changeStreamController;
  57. final random = Random();
  58. final RxDouble _toolBarOpacity = 0.0.obs;
  59. double get toolBarOpacity => _toolBarOpacity.value;
  60. final List<String> _storeTypes = ['终身会员', '年度会员', '月度会员'];
  61. String? get phone => accountRepository.loginPhoneNum.value;
  62. bool get isLogin => accountRepository.isLogin.value;
  63. MemberStatusInfo? get memberStatusInfo =>
  64. accountRepository.memberStatusInfo.value;
  65. final RxList<GoodsBean> goodsList = <GoodsBean>[].obs;
  66. final Rxn<GoodsBean> _selectedGoods = Rxn<GoodsBean>();
  67. GoodsBean? get selectedGoods => _selectedGoods.value;
  68. final Rxn<PayItemBean> _selectedPayWay = Rxn<PayItemBean>();
  69. PayItemBean? get selectedPayWay => _selectedPayWay.value;
  70. final RxList<PayItemBean> payItemList = <PayItemBean>[].obs;
  71. ///评论
  72. final RxList<GoodsEvaluateInfo> evaluateList = <GoodsEvaluateInfo>[].obs;
  73. final RxBool isShowCount = RxBool(true);
  74. CancelableFuture? _memberDataFuture;
  75. ///广告弹窗防抖
  76. bool _isPopBackInProgress = false; // 操作进行中标志
  77. ///检查续订的状态
  78. final Rx<SubscriptionCheckResponse> _checkResponse =
  79. Rx(SubscriptionCheckResponse(outTradeNo: ""));
  80. SubscriptionCheckResponse? get requestCheckResponse => _checkResponse.value;
  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. static const String _keMemberPageShowKey = "member_page_show_key";
  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. }
  127. ///是否弹窗提示
  128. void _isTherePopUpPrompt() {
  129. MemberPageType pageType = Get.arguments ?? MemberPageType.universalAccessEnter;
  130. if (pageType == MemberPageType.afeterTrialMemberEnter) {
  131. MemberFirstWeekDiscountDialog.show(
  132. confirmOnTap: () {
  133. onBuyClick();
  134. },
  135. onPrivacyPolicyClick: () {
  136. onPrivacyPolicyClick();
  137. },
  138. onTermOfServiceClick: () {
  139. onTermOfServiceClick();
  140. }
  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(fontSize: 11.sp, color: Colors.white,fontWeight: FontWeight.w400),
  169. children: [
  170. TextSpan(text: userIdStr),
  171. TextSpan(text: '用户 '),
  172. TextSpan(text: secondsOrHour),
  173. TextSpan(text: '前购买了'),
  174. TextSpan(
  175. text: _storeTypes[index],
  176. style: TextStyle(color: '#FFC95D'.color)),
  177. ]))
  178. ],
  179. ));
  180. }
  181. String getUserName(String phone) {
  182. if (phone.length > 4) {
  183. phone = phone.substring(phone.length - 4);
  184. }
  185. return '${StringName.mineAccountLoggedDesc}$phone';
  186. }
  187. void back() {
  188. Get.back();
  189. }
  190. void onLoginClick() {
  191. if (accountRepository.isLogin.value) {
  192. return;
  193. }
  194. LoginPage.start();
  195. }
  196. void refreshMemberData() {
  197. _memberDataFuture?.cancel();
  198. _memberDataFuture =
  199. AsyncUtil.retryWithExponentialBackoff(() => _requestMemberData(), 4);
  200. _memberDataFuture?.catchError((error) {
  201. ErrorHandler.toastError(error);
  202. });
  203. }
  204. Future<void> _requestMemberData() {
  205. return memberRepository
  206. .getMemberList(itemListType: Platform.isIOS ? 2 : 0)
  207. .then((response) {
  208. goodsList.clear();
  209. payItemList.clear();
  210. evaluateList.clear();
  211. _selectedGoods.value = null;
  212. if (response.goodsList?.isNotEmpty == true) {
  213. goodsList.addAll(response.goodsList!);
  214. _selectedGoods.value = goodsList.first;
  215. }
  216. if (response.payInfoList?.isNotEmpty == true) {
  217. payItemList.addAll(response.payInfoList!);
  218. _selectedPayWay.value = payItemList.first;
  219. }
  220. if (response.evaluateList?.isNotEmpty == true) {
  221. evaluateList.addAll(response.evaluateList!);
  222. }
  223. });
  224. }
  225. void onGoodsItemClick(GoodsBean item) {
  226. _selectedGoods.value = item;
  227. }
  228. void onPrivacyPolicyClick() {
  229. BrowserPage.start(WebUrl.privacyPolicy);
  230. }
  231. void onTermOfServiceClick() {
  232. BrowserPage.start(WebUrl.userAgreement);
  233. }
  234. void onPayWayItemClick(PayItemBean item) {
  235. _selectedPayWay.value = item;
  236. }
  237. void onPopBack() {
  238. if (Platform.isIOS) {
  239. back();
  240. if (accountRepository.memberIsExpired()) {
  241. FocusScope.of(Get.context!).unfocus();
  242. userCancelsPaymentDisplay();
  243. }
  244. } else {
  245. if (accountRepository.memberIsExpired()) {
  246. userCancelsPaymentDisplay();
  247. } else {
  248. back();
  249. }
  250. }
  251. }
  252. ///用户取消支付展示
  253. void userCancelsPaymentDisplay() {
  254. if (_isPopBackInProgress) {
  255. // 如果正在处理返回逻辑,直接跳过
  256. return;
  257. }
  258. _isPopBackInProgress = true; // 锁定状态
  259. //_keMemberPageShowKey
  260. String? memberPageKeyStr = KVUtil.getString(_keMemberPageShowKey, '');
  261. if ((memberPageKeyStr ?? '').isEmpty && (accountRepository.memberStatusInfo.value?.trialed == false)) {
  262. ///永久化存储
  263. KVUtil.putString(_keMemberPageShowKey, _keMemberPageShowKey);
  264. MemberUserCancelPayDialog.show(payClick: () {
  265. //获取会员
  266. MineController attributionController = Get.find<MineController>();
  267. attributionController.onMemberTryOutClick();
  268. _isPopBackInProgress = false;
  269. }, cancelClick: () {
  270. _isPopBackInProgress = false;
  271. if (!Platform.isIOS) {
  272. back();
  273. }
  274. });
  275. } else {
  276. _isPopBackInProgress = false;
  277. }
  278. }
  279. ///支付错误的时候调用
  280. void showRetainDialog() {
  281. if (_isPopBackInProgress) {
  282. // 如果正在处理返回逻辑,直接跳过
  283. return;
  284. }
  285. _isPopBackInProgress = true; // 锁定状态
  286. MemberRetainDialog.show(payClick: () {
  287. onBuyClick();
  288. _isPopBackInProgress = false;
  289. }, cancelClick: () {
  290. _isPopBackInProgress = false;
  291. if (!Platform.isIOS) {
  292. back();
  293. }
  294. });
  295. }
  296. void onBuyClick() {
  297. if (selectedGoods == null) {
  298. ToastUtil.show(StringName.memberPleaseChoiceGoods);
  299. return;
  300. }
  301. if (selectedPayWay == null && !Platform.isIOS) {
  302. ToastUtil.show(StringName.memberPleaseChoicePayment);
  303. return;
  304. }
  305. final buyGoods = selectedGoods!;
  306. final buyPayWay = selectedPayWay!;
  307. int goodsId = buyGoods.id;
  308. int payPlatform = buyPayWay.payPlatform;
  309. int payMethod = buyPayWay.payMethod;
  310. int payWayType =
  311. getPayWayType(payMethod: payMethod, payPlatform: payPlatform);
  312. LoadingDialog.show(StringName.payLoading);
  313. memberRepository
  314. .submitAndRequestPay(
  315. goodsId: goodsId, payPlatform: payPlatform, payMethod: payMethod)
  316. .then((response) {
  317. if (payWayType == PayWayType.paymentWayWechat) {
  318. _onWeChatPay(response.outTradeNo, response.wechatPayPrepayJson!,
  319. payMethod, buyGoods, buyPayWay);
  320. } else if (payWayType == PayWayType.paymentWayWechatScan) {
  321. _onWechatScanPay(response.outTradeNo, response.wechatPayQrcodeUrl!,
  322. buyPayWay, buyGoods);
  323. } else if (payWayType == PayWayType.paymentWayAlipay) {
  324. _onAliPay(response.outTradeNo, response.alipayOrderString!, payMethod,
  325. buyGoods, buyPayWay);
  326. } else if (payWayType == PayWayType.paymentWayAlipayScan) {
  327. _onAliScanPay(response.outTradeNo, response.alipayQrcodeHtml!,
  328. buyPayWay, buyGoods);
  329. } else if (payWayType == PayWayType.paymentWayApple) {
  330. _onApplePay(response.outTradeNo, response.appAccountToken ?? "",
  331. buyPayWay, buyGoods);
  332. } else {
  333. ToastUtil.show(StringName.payNotSupport);
  334. }
  335. }).catchError((error) {
  336. //处理购买失败
  337. showRetainDialog();
  338. if (error is ServerErrorException) {
  339. if (error.code == ErrorCode.payOrderError) {
  340. refreshMemberData();
  341. ToastUtil.show(error.message);
  342. } else if (error.code == ErrorCode.noLoginError) {
  343. ToastUtil.show(StringName.accountNoLogin);
  344. LoginPage.start();
  345. } else {
  346. ToastUtil.show(error.message);
  347. }
  348. } else {
  349. ToastUtil.show(StringName.memberPaymentFailed);
  350. }
  351. }).whenComplete(() {
  352. LoadingDialog.hide();
  353. });
  354. }
  355. ///发起购买请求
  356. Future<void> _onApplePay(String outTradeNo,
  357. String appAccountToken,
  358. PayItemBean payWayInfo,
  359. GoodsBean goodsInfo,) async {
  360. final result = await ApplePay().purchase(
  361. productId: goodsInfo.appleGoodsId ?? "",
  362. appAccountToken: appAccountToken,
  363. );
  364. if (result["success"] == true) {
  365. var receipt = result['receipt'];
  366. print('购买成功: ${result['receipt']}');
  367. checkPaymentStatus(
  368. outTradeNo,
  369. payWayInfo,
  370. goodsInfo,
  371. receiptData: receipt,
  372. );
  373. } else {
  374. LoadingDialog.hide();
  375. ToastUtil.show("支付失败,请稍后重试");
  376. print('购买失败: ${result['error']}');
  377. }
  378. }
  379. void _onAliScanPay(String outTradeNo, String qrHtml, PayItemBean paymentWay,
  380. GoodsBean goodsBean) {
  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. paymentStatusManager.registerPaymentSuccessCallback(orderNo, this);
  398. paymentStatusManager.checkPaymentStatus(orderNo, paymentWay, goodsBean,
  399. receiptData: receiptData);
  400. }
  401. void _onWeChatPay(String outTradeNo, String payJson, int payMethod,
  402. GoodsBean buyGoods, PayItemBean buyPayWay) {
  403. final bean = WechatPaymentSignBean.stringToBean(payJson);
  404. final payInfo = WechatPayInfo(
  405. appId: bean.appId,
  406. partnerId: bean.partnerId,
  407. prepayId: bean.prepayId,
  408. package: bean.package,
  409. noncestr: bean.nonceStr,
  410. timestamp: bean.timeStamp,
  411. sign: bean.sign);
  412. requestSdkPay(payInfo, outTradeNo, payMethod, buyGoods, buyPayWay);
  413. }
  414. void _onAliPay(String outTradeNo, String payJson, int payMethod,
  415. GoodsBean buyGoods, PayItemBean buyPayWay) {
  416. final payInfo = AliPayInfo(payJson);
  417. requestSdkPay(payInfo, outTradeNo, payMethod, buyGoods, buyPayWay);
  418. }
  419. void _onWechatScanPay(String outTradeNo, String qrCodeUrl,
  420. PayItemBean paymentWay, GoodsBean goodsBean) {
  421. WechatQrCodeDialog.show(
  422. qrCodeUrl: qrCodeUrl,
  423. loadSuccessCallback: () {
  424. checkPaymentStatus(outTradeNo, paymentWay, goodsBean);
  425. },
  426. onCloseCallback: () async {
  427. //关闭后再持续查询几秒
  428. CustomLoadingDialog.show();
  429. await Future.delayed(Duration(seconds: 4));
  430. paymentStatusManager.removePollingSubscription(outTradeNo);
  431. CustomLoadingDialog.hide();
  432. });
  433. }
  434. ///查询订阅状态
  435. Future<void> _requestCheckRestoreStatus(String? receiptData) async {
  436. memberRepository.subscriptionCheck(3, receiptData ?? "").then((value) {
  437. _checkResponse.value = value;
  438. }).catchError((error) {});
  439. }
  440. /// 点击了恢复订阅
  441. Future<void> clickRecoverSubscribe() async {
  442. PayItemBean? paymentWay = _selectedPayWay.value;
  443. if (paymentWay == null) {
  444. return;
  445. }
  446. int payPlatform = paymentWay.payPlatform;
  447. int payMethod = paymentWay.payMethod;
  448. CustomLoadingDialog.show();
  449. Future.delayed(const Duration(seconds: 20), () {
  450. CustomLoadingDialog.hide();
  451. //ToastUtil.show("没有发现可恢复的记录");
  452. });
  453. final result = await ApplePay().restore();
  454. if (result["success"] == true) {
  455. // CustomLoadingDialog.hide();
  456. var receipt = result['receipt'];
  457. print('查找恢复记录成功: ${result['receipt']}');
  458. checkRestoreStatus(receipt);
  459. } else {
  460. CustomLoadingDialog.hide();
  461. ToastUtil.show("恢复失败");
  462. print('恢复失败: ${result['error']}');
  463. }
  464. // 显示恢复订阅弹窗
  465. // RecoverSubscribeDialog.show("周会员2025年3月6日到期。", () {
  466. // AtmobLog.d(tag, "恢复订阅弹窗 => 点击确认");
  467. // });
  468. }
  469. /// 检查恢复订阅结果
  470. Future<void> checkRestoreStatus(String? receiptData) async {
  471. PayItemBean? paymentWay = _selectedPayWay.value;
  472. if (paymentWay == null) {
  473. // ToastUtil.showToast(StringName.storeChoicePayment.tr);
  474. return;
  475. }
  476. if (receiptData == null) {
  477. return;
  478. }
  479. int payPlatform = paymentWay.payPlatform;
  480. int payMethod = paymentWay.payMethod;
  481. // var code = await storeRepository.resume(payPlatform, payMethod, receiptData);
  482. memberRepository.subscriptionResume(3, receiptData).then((data) async {
  483. CustomLoadingDialog.hide();
  484. ToastUtil.show("恢复成功");
  485. await AccountRepository.getInstance().getMemberStatus();
  486. //accountRepository.refreshMemberStatus();
  487. Get.back();
  488. }).catchError((error) {
  489. CustomLoadingDialog.hide();
  490. ToastUtil.show("恢复失败");
  491. });
  492. // if (code == 0) {
  493. // CustomLoadingDialog.hide();
  494. // ToastUtil.show("Restore success");
  495. // userRepository.getUserInfo();
  496. // Get.back();
  497. // } else {
  498. // CustomLoadingDialog.hide();
  499. // ToastUtil.show("Restore fail");
  500. // }
  501. }
  502. void requestSdkPay(dynamic payInfo, String outTradeNo, int payMethod,
  503. GoodsBean buyGoods, PayItemBean buyPayWay) {
  504. AgilePay.startPay(payInfo, success: (String? result) {
  505. LoadingDialog.show(StringName.payQuerypayState);
  506. checkPaymentStatus(outTradeNo, buyPayWay, buyGoods, receiptData: result);
  507. }, payError: (int error, String? errorMessage) {
  508. debugPrint('zk---payError: $error, $errorMessage');
  509. errorPayToast(error);
  510. errorEventReport(payMethod);
  511. }, error: (int errno, String? error) {
  512. debugPrint('zk---error: $errno, $error');
  513. errorPayToast(errno);
  514. errorEventReport(payMethod);
  515. });
  516. }
  517. void errorEventReport(int payMethod) {
  518. if (payMethod == PayMethod.wechat) {
  519. // EventHandler.report();
  520. } else if (payMethod == PayMethod.alipay) {
  521. // EventHandler.report();
  522. } else if (payMethod == PayMethod.apple) {
  523. // EventHandler.report();
  524. }
  525. }
  526. void errorPayToast(int errno) {
  527. if (errno == AgilePayCode.payCodeNotSupport) {
  528. ToastUtil.show(StringName.payNotSupport);
  529. } else if (errno == AgilePayCode.payCodeCancelError) {
  530. ToastUtil.show(StringName.payUserCancel);
  531. } else if (errno == AgilePayCode.payCodeWxEnvError) {
  532. ToastUtil.show(StringName.payWxEvnError);
  533. } else if (errno == AgilePayCode.payCodeNotConnectStore) {
  534. ToastUtil.show(StringName.payNotConnectStore);
  535. } else {
  536. ToastUtil.show(StringName.payError);
  537. }
  538. }
  539. @override
  540. void onClose() {
  541. super.onClose();
  542. _changeStreamController?.close();
  543. _memberDataFuture?.cancel();
  544. scrollController.dispose();
  545. paymentStatusManager.unregisterPaymentSuccessCallback(this);
  546. }
  547. @override
  548. void onPaymentSuccess(
  549. String orderNo, PayItemBean paymentWay, GoodsBean storeItemBean) {
  550. ///购买成功之后弹出
  551. afterTheFirstPurchasePromptSharingBoxPops();
  552. }
  553. ///第一次购买之后弹出提示分享框
  554. void afterTheFirstPurchasePromptSharingBoxPops() {
  555. paymentStatusManager.onOrderFirstCheck().then((checkReponse) {
  556. //是否展示邀请弹框
  557. if (checkReponse.showInvite || checkReponse.showEvaluate) {
  558. MemberPageType pageType = Get.arguments ?? MemberPageType.universalAccessEnter;
  559. //去分享
  560. if (checkReponse.showInvite && pageType == MemberPageType.addFriendToEnter) {
  561. MemberPaymentCompletedDialog.show(confirmOnTap: () {
  562. WechatShareUtil.shareWebPage().catchError((error) {
  563. ToastUtil.show(error);
  564. });
  565. if (checkReponse.showEvaluate) {
  566. ///是否展示好评领会员弹框
  567. MemberEvaluationPopUpDialog.show(confirmOnTap: () {
  568. AppReviewService.requestAppReview(Get.context!);
  569. onPaySucessShow();
  570. },cancelOnTap: () {
  571. onPaySucessShow();
  572. },days: 1);
  573. } else {
  574. onPaySucessShow();
  575. }
  576. },cancelOnTap: () {
  577. if (checkReponse.showEvaluate) {
  578. ///是否展示好评领会员弹框
  579. MemberEvaluationPopUpDialog.show(confirmOnTap: () {
  580. AppReviewService.requestAppReview(Get.context!);
  581. ///领取会员后请求
  582. memberRepository.memberEvaluate().then((_){
  583. }).catchError((error) {
  584. });;//memberEvaluate
  585. onPaySucessShow();
  586. },cancelOnTap: () {
  587. onPaySucessShow();
  588. },days: 1);
  589. } else {
  590. onPaySucessShow();
  591. }
  592. });
  593. }
  594. else if (checkReponse.showEvaluate) {
  595. ///是否展示好评领会员弹框
  596. MemberEvaluationPopUpDialog.show(confirmOnTap: () {
  597. AppReviewService.requestAppReview(Get.context!);
  598. onPaySucessShow();
  599. },cancelOnTap: () {
  600. onPaySucessShow();
  601. },days: 1);
  602. }
  603. } else {
  604. onPaySucessShow();
  605. }
  606. }).catchError((erro) {
  607. onPaySucessShow();
  608. });
  609. }
  610. ///支付成功之后展示
  611. void onPaySucessShow() {
  612. try {
  613. WechatQrCodeDialog.dismiss();
  614. AlipayQrCodeDialog.dismiss();
  615. CustomLoadingDialog.hide();
  616. LoadingDialog.hide();
  617. } catch (e) {
  618. debugPrint('zk---onPaymentSuccess error: $e');
  619. }
  620. showPaymentSuccessDialog(onConfirm: back, onCancel: back);
  621. }
  622. }