member_controller.dart 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  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/repositories/account_repository.dart';
  14. import 'package:location/data/repositories/member_repository.dart';
  15. import 'package:location/dialog/alipay_qr_code_dialog.dart';
  16. import 'package:location/handler/error_handler.dart';
  17. import 'package:location/module/login/login_page.dart';
  18. import 'package:location/resource/assets.gen.dart';
  19. import 'package:location/utils/async_util.dart';
  20. import 'package:location/utils/common_expand.dart';
  21. import 'package:location/utils/toast_util.dart';
  22. import '../../data/api/response/subscription_check_response.dart';
  23. import '../../data/bean/goods_evaluate_info.dart';
  24. import '../../data/bean/member_status_info.dart';
  25. import '../../data/bean/wechat_payment_sign_bean.dart';
  26. import '../../data/consts/error_code.dart';
  27. import '../../data/consts/payment_type.dart';
  28. import '../../data/consts/web_url.dart';
  29. import '../../data/repositories/phone_event_repository.dart';
  30. import '../../dialog/common_confirm_dialog_impl.dart';
  31. import '../../dialog/loading_dialog.dart';
  32. import '../../dialog/member_retain_dialog.dart';
  33. import '../../dialog/wechat_qr_code_dialog.dart';
  34. import '../../resource/string.gen.dart';
  35. import '../../sdk/wechat/wechat_share_util.dart';
  36. import '../../utils/app_review_service.dart';
  37. import '../../utils/http_handler.dart';
  38. import '../../utils/mmkv_util.dart';
  39. import '../../utils/payment_status_manager.dart';
  40. import '../../widget/animated_switcher_widget.dart';
  41. import '../browser/browser_view.dart';
  42. import '../mine/mine_controller.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. import 'member_user_cancel_pay_dialog.dart';
  50. @injectable
  51. class MemberController extends BaseController implements PaymentStatusCallback {
  52. final AccountRepository accountRepository;
  53. final MemberRepository memberRepository;
  54. final PaymentStatusManager paymentStatusManager;
  55. final switcherController = SwitcherController();
  56. final ScrollController scrollController = ScrollController();
  57. StreamController? _changeStreamController;
  58. final random = Random();
  59. final RxDouble _toolBarOpacity = 0.0.obs;
  60. double get toolBarOpacity => _toolBarOpacity.value;
  61. final List<String> _storeTypes = ['终身会员', '年度会员', '月度会员'];
  62. String? get phone => accountRepository.loginPhoneNum.value;
  63. bool get isLogin => accountRepository.isLogin.value;
  64. MemberStatusInfo? get memberStatusInfo =>
  65. accountRepository.memberStatusInfo.value;
  66. final RxList<GoodsBean> goodsList = <GoodsBean>[].obs;
  67. final Rxn<GoodsBean> _selectedGoods = Rxn<GoodsBean>();
  68. GoodsBean? get selectedGoods => _selectedGoods.value;
  69. final Rxn<PayItemBean> _selectedPayWay = Rxn<PayItemBean>();
  70. PayItemBean? get selectedPayWay => _selectedPayWay.value;
  71. final RxList<PayItemBean> payItemList = <PayItemBean>[].obs;
  72. ///评论
  73. final RxList<GoodsEvaluateInfo> evaluateList = <GoodsEvaluateInfo>[].obs;
  74. final RxBool isShowCount = RxBool(true);
  75. CancelableFuture? _memberDataFuture;
  76. ///广告弹窗防抖
  77. bool _isPopBackInProgress = false; // 操作进行中标志
  78. ///检查续订的状态
  79. final Rx<SubscriptionCheckResponse> _checkResponse =
  80. Rx(SubscriptionCheckResponse(outTradeNo: ""));
  81. SubscriptionCheckResponse? get requestCheckResponse => _checkResponse.value;
  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. static const String _keMemberPageShowKey = "member_page_show_key";
  109. MemberController(
  110. this.accountRepository, this.memberRepository, this.paymentStatusManager);
  111. @override
  112. void onReady() async {
  113. super.onReady();
  114. _isTherePopUpPrompt();
  115. _startAnimationSwitcher();
  116. scrollController.addListener(() {
  117. double offset = scrollController.offset;
  118. double opacity = offset / 100;
  119. if (opacity > 1) {
  120. opacity = 1;
  121. } else if (opacity < 0) {
  122. opacity = 0;
  123. }
  124. _toolBarOpacity.value = opacity;
  125. });
  126. refreshMemberData();
  127. }
  128. ///是否弹窗提示
  129. void _isTherePopUpPrompt() {
  130. MemberPageType pageType = Get.arguments ?? MemberPageType.universalAccessEnter;
  131. if (pageType == MemberPageType.afeterTrialMemberEnter) {
  132. MemberFirstWeekDiscountDialog.show(
  133. confirmOnTap: () {
  134. onBuyClick();
  135. },
  136. onPrivacyPolicyClick: () {
  137. onPrivacyPolicyClick();
  138. },
  139. onTermOfServiceClick: () {
  140. onTermOfServiceClick();
  141. }
  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(fontSize: 11.sp, color: Colors.white,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. }
  229. void onPrivacyPolicyClick() {
  230. BrowserPage.start(WebUrl.privacyPolicy);
  231. }
  232. void onTermOfServiceClick() {
  233. BrowserPage.start(WebUrl.userAgreement);
  234. }
  235. void onPayWayItemClick(PayItemBean item) {
  236. _selectedPayWay.value = item;
  237. }
  238. void onPopBack() {
  239. if (Platform.isIOS) {
  240. back();
  241. if (accountRepository.memberIsExpired()) {
  242. FocusScope.of(Get.context!).unfocus();
  243. userCancelsPaymentDisplay();
  244. }
  245. } else {
  246. if (accountRepository.memberIsExpired()) {
  247. userCancelsPaymentDisplay();
  248. } else {
  249. back();
  250. }
  251. }
  252. }
  253. ///用户取消支付展示
  254. void userCancelsPaymentDisplay() {
  255. if (_isPopBackInProgress) {
  256. // 如果正在处理返回逻辑,直接跳过
  257. return;
  258. }
  259. _isPopBackInProgress = true; // 锁定状态
  260. //_keMemberPageShowKey
  261. String? memberPageKeyStr = KVUtil.getString(_keMemberPageShowKey, '');
  262. if ((memberPageKeyStr ?? '').isEmpty && (accountRepository.memberStatusInfo.value?.trialed == false)) {
  263. ///永久化存储
  264. KVUtil.putString(_keMemberPageShowKey, _keMemberPageShowKey);
  265. MemberUserCancelPayDialog.show(payClick: () {
  266. //获取会员
  267. MineController attributionController = Get.find<MineController>();
  268. attributionController.onMemberTryOutClick();
  269. _isPopBackInProgress = false;
  270. }, cancelClick: () {
  271. _isPopBackInProgress = false;
  272. if (!Platform.isIOS) {
  273. back();
  274. }
  275. });
  276. } else {
  277. _isPopBackInProgress = false;
  278. if (Platform.isAndroid) {
  279. back();
  280. }
  281. }
  282. }
  283. ///支付错误的时候调用
  284. void showRetainDialog() {
  285. MemberRetainDialog.show(payClick: () {
  286. onBuyClick();
  287. }, cancelClick: () {
  288. if (!Platform.isIOS) {
  289. back();
  290. }
  291. });
  292. }
  293. void onBuyClick() {
  294. if (selectedGoods == null) {
  295. ToastUtil.show(StringName.memberPleaseChoiceGoods);
  296. return;
  297. }
  298. if (selectedPayWay == null && !Platform.isIOS) {
  299. ToastUtil.show(StringName.memberPleaseChoicePayment);
  300. return;
  301. }
  302. final buyGoods = selectedGoods!;
  303. final buyPayWay = selectedPayWay!;
  304. int goodsId = buyGoods.id;
  305. int payPlatform = buyPayWay.payPlatform;
  306. int payMethod = buyPayWay.payMethod;
  307. int payWayType =
  308. getPayWayType(payMethod: payMethod, payPlatform: payPlatform);
  309. LoadingDialog.show(StringName.payLoading);
  310. memberRepository
  311. .submitAndRequestPay(
  312. goodsId: goodsId, payPlatform: payPlatform, payMethod: payMethod)
  313. .then((response) {
  314. if (payWayType == PayWayType.paymentWayWechat) {
  315. _onWeChatPay(response.outTradeNo, response.wechatPayPrepayJson!,
  316. payMethod, buyGoods, buyPayWay);
  317. } else if (payWayType == PayWayType.paymentWayWechatScan) {
  318. _onWechatScanPay(response.outTradeNo, response.wechatPayQrcodeUrl!,
  319. buyPayWay, buyGoods);
  320. } else if (payWayType == PayWayType.paymentWayAlipay) {
  321. _onAliPay(response.outTradeNo, response.alipayOrderString!, payMethod,
  322. buyGoods, buyPayWay);
  323. } else if (payWayType == PayWayType.paymentWayAlipayScan) {
  324. _onAliScanPay(response.outTradeNo, response.alipayQrcodeHtml!,
  325. buyPayWay, buyGoods);
  326. } else if (payWayType == PayWayType.paymentWayApple) {
  327. _onApplePay(response.outTradeNo, response.appAccountToken ?? "",
  328. buyPayWay, buyGoods);
  329. } else {
  330. ToastUtil.show(StringName.payNotSupport);
  331. }
  332. }).catchError((error) {
  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. }).whenComplete(() {
  349. LoadingDialog.hide();
  350. });
  351. }
  352. ///发起购买请求
  353. Future<void> _onApplePay(String outTradeNo,
  354. String appAccountToken,
  355. PayItemBean payWayInfo,
  356. GoodsBean goodsInfo,) 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. ToastUtil.show("支付失败,请稍后重试");
  373. print('购买失败: ${result['error']}');
  374. showRetainDialog();
  375. }
  376. }
  377. void _onAliScanPay(String outTradeNo, String qrHtml, PayItemBean paymentWay,
  378. GoodsBean goodsBean) {
  379. AlipayQrCodeDialog.show(
  380. qrCodeHtml: qrHtml,
  381. loadSuccessCallback: () {
  382. checkPaymentStatus(outTradeNo, paymentWay, goodsBean);
  383. },
  384. onCloseCallback: () async {
  385. //关闭后再持续查询几秒
  386. CustomLoadingDialog.show();
  387. await Future.delayed(Duration(seconds: 4));
  388. paymentStatusManager.removePollingSubscription(outTradeNo);
  389. CustomLoadingDialog.hide();
  390. });
  391. }
  392. void checkPaymentStatus(
  393. String orderNo, PayItemBean paymentWay, GoodsBean goodsBean,
  394. {String? receiptData}) {
  395. paymentStatusManager.registerPaymentSuccessCallback(orderNo, this);
  396. paymentStatusManager.checkPaymentStatus(orderNo, paymentWay, goodsBean,
  397. receiptData: receiptData);
  398. }
  399. void _onWeChatPay(String outTradeNo, String payJson, int payMethod,
  400. GoodsBean buyGoods, PayItemBean buyPayWay) {
  401. final bean = WechatPaymentSignBean.stringToBean(payJson);
  402. final payInfo = WechatPayInfo(
  403. appId: bean.appId,
  404. partnerId: bean.partnerId,
  405. prepayId: bean.prepayId,
  406. package: bean.package,
  407. noncestr: bean.nonceStr,
  408. timestamp: bean.timeStamp,
  409. sign: bean.sign);
  410. requestSdkPay(payInfo, outTradeNo, payMethod, buyGoods, buyPayWay);
  411. }
  412. void _onAliPay(String outTradeNo, String payJson, int payMethod,
  413. GoodsBean buyGoods, PayItemBean buyPayWay) {
  414. final payInfo = AliPayInfo(payJson);
  415. requestSdkPay(payInfo, outTradeNo, payMethod, buyGoods, buyPayWay);
  416. }
  417. void _onWechatScanPay(String outTradeNo, String qrCodeUrl,
  418. PayItemBean paymentWay, GoodsBean goodsBean) {
  419. WechatQrCodeDialog.show(
  420. qrCodeUrl: qrCodeUrl,
  421. loadSuccessCallback: () {
  422. checkPaymentStatus(outTradeNo, paymentWay, goodsBean);
  423. },
  424. onCloseCallback: () async {
  425. //关闭后再持续查询几秒
  426. CustomLoadingDialog.show();
  427. await Future.delayed(Duration(seconds: 4));
  428. paymentStatusManager.removePollingSubscription(outTradeNo);
  429. CustomLoadingDialog.hide();
  430. });
  431. }
  432. ///查询订阅状态
  433. Future<void> _requestCheckRestoreStatus(String? receiptData) async {
  434. memberRepository.subscriptionCheck(3, receiptData ?? "").then((value) {
  435. _checkResponse.value = value;
  436. }).catchError((error) {});
  437. }
  438. /// 点击了恢复订阅
  439. Future<void> clickRecoverSubscribe() async {
  440. PayItemBean? paymentWay = _selectedPayWay.value;
  441. if (paymentWay == null) {
  442. return;
  443. }
  444. int payPlatform = paymentWay.payPlatform;
  445. int payMethod = paymentWay.payMethod;
  446. CustomLoadingDialog.show();
  447. Future.delayed(const Duration(seconds: 20), () {
  448. CustomLoadingDialog.hide();
  449. //ToastUtil.show("没有发现可恢复的记录");
  450. });
  451. final result = await ApplePay().restore();
  452. if (result["success"] == true) {
  453. // CustomLoadingDialog.hide();
  454. var receipt = result['receipt'];
  455. print('查找恢复记录成功: ${result['receipt']}');
  456. checkRestoreStatus(receipt);
  457. } else {
  458. CustomLoadingDialog.hide();
  459. ToastUtil.show("恢复失败");
  460. print('恢复失败: ${result['error']}');
  461. }
  462. // 显示恢复订阅弹窗
  463. // RecoverSubscribeDialog.show("周会员2025年3月6日到期。", () {
  464. // AtmobLog.d(tag, "恢复订阅弹窗 => 点击确认");
  465. // });
  466. }
  467. /// 检查恢复订阅结果
  468. Future<void> checkRestoreStatus(String? receiptData) async {
  469. PayItemBean? paymentWay = _selectedPayWay.value;
  470. if (paymentWay == null) {
  471. // ToastUtil.showToast(StringName.storeChoicePayment.tr);
  472. return;
  473. }
  474. if (receiptData == null) {
  475. return;
  476. }
  477. int payPlatform = paymentWay.payPlatform;
  478. int payMethod = paymentWay.payMethod;
  479. // var code = await storeRepository.resume(payPlatform, payMethod, receiptData);
  480. memberRepository.subscriptionResume(3, receiptData).then((data) async {
  481. CustomLoadingDialog.hide();
  482. ToastUtil.show("恢复成功");
  483. await AccountRepository.getInstance().getMemberStatus();
  484. //accountRepository.refreshMemberStatus();
  485. Get.back();
  486. }).catchError((error) {
  487. CustomLoadingDialog.hide();
  488. ToastUtil.show("恢复失败");
  489. });
  490. // if (code == 0) {
  491. // CustomLoadingDialog.hide();
  492. // ToastUtil.show("Restore success");
  493. // userRepository.getUserInfo();
  494. // Get.back();
  495. // } else {
  496. // CustomLoadingDialog.hide();
  497. // ToastUtil.show("Restore fail");
  498. // }
  499. }
  500. void requestSdkPay(dynamic payInfo, String outTradeNo, int payMethod,
  501. GoodsBean buyGoods, PayItemBean buyPayWay) {
  502. AgilePay.startPay(payInfo, success: (String? result) {
  503. LoadingDialog.show(StringName.payQuerypayState);
  504. checkPaymentStatus(outTradeNo, buyPayWay, buyGoods, receiptData: result);
  505. }, payError: (int error, String? errorMessage) {
  506. debugPrint('zk---payError: $error, $errorMessage');
  507. errorPayToast(error);
  508. errorEventReport(payMethod);
  509. showRetainDialog();
  510. }, error: (int errno, String? error) {
  511. debugPrint('zk---error: $errno, $error');
  512. errorPayToast(errno);
  513. errorEventReport(payMethod);
  514. showRetainDialog();
  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. LoadingDialog.hide();
  552. ///购买成功之后弹出
  553. afterTheFirstPurchasePromptSharingBoxPops();
  554. }
  555. @override
  556. void onPaymentFail() {
  557. ///购买成功消失
  558. LoadingDialog.hide();
  559. }
  560. ///第一次购买之后弹出提示分享框
  561. void afterTheFirstPurchasePromptSharingBoxPops() {
  562. paymentStatusManager.onOrderFirstCheck().then((checkReponse) {
  563. //是否展示邀请弹框
  564. if (checkReponse.showInvite || checkReponse.showEvaluate) {
  565. MemberPageType pageType = Get.arguments ?? MemberPageType.universalAccessEnter;
  566. //去分享
  567. if (checkReponse.showInvite && pageType == MemberPageType.addFriendToEnter) {
  568. MemberPaymentCompletedDialog.show(confirmOnTap: () {
  569. WechatShareUtil.shareWebPage().catchError((error) {
  570. ToastUtil.show(error);
  571. });
  572. if (checkReponse.showEvaluate) {
  573. ///是否展示好评领会员弹框
  574. MemberEvaluationPopUpDialog.show(confirmOnTap: () {
  575. AppReviewService.requestAppReview(Get.context!);
  576. onPaySucessShow();
  577. },cancelOnTap: () {
  578. onPaySucessShow();
  579. },days: 1);
  580. } else {
  581. onPaySucessShow();
  582. }
  583. },cancelOnTap: () {
  584. if (checkReponse.showEvaluate) {
  585. ///是否展示好评领会员弹框
  586. MemberEvaluationPopUpDialog.show(confirmOnTap: () {
  587. AppReviewService.requestAppReview(Get.context!);
  588. ///领取会员后请求
  589. memberRepository.memberEvaluate().then((_){
  590. }).catchError((error) {
  591. });;//memberEvaluate
  592. onPaySucessShow();
  593. },cancelOnTap: () {
  594. onPaySucessShow();
  595. },days: 1);
  596. } else {
  597. onPaySucessShow();
  598. }
  599. });
  600. }
  601. else if (checkReponse.showEvaluate) {
  602. ///是否展示好评领会员弹框
  603. MemberEvaluationPopUpDialog.show(confirmOnTap: () {
  604. AppReviewService.requestAppReview(Get.context!);
  605. onPaySucessShow();
  606. },cancelOnTap: () {
  607. onPaySucessShow();
  608. },days: 1);
  609. }
  610. } else {
  611. onPaySucessShow();
  612. }
  613. }).catchError((erro) {
  614. onPaySucessShow();
  615. });
  616. }
  617. ///支付成功之后展示
  618. void onPaySucessShow() {
  619. try {
  620. WechatQrCodeDialog.dismiss();
  621. AlipayQrCodeDialog.dismiss();
  622. CustomLoadingDialog.hide();
  623. LoadingDialog.hide();
  624. } catch (e) {
  625. debugPrint('zk---onPaymentSuccess error: $e');
  626. }
  627. showPaymentSuccessDialog(onConfirm: back, onCancel: back);
  628. }
  629. }