member_controller.dart 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  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(
  267. {bool isBack = false, int? errno, VoidCallback? onCancelClick}) {
  268. if (isBack == false) {
  269. errorPayToast(errno);
  270. return;
  271. }
  272. _isShowPayFailedDialog = true;
  273. MemberRetainDialog.show(payClick: () {
  274. onBuyClick();
  275. }, cancelClick: () {
  276. errorPayToast(errno);
  277. if (Platform.isAndroid && isBack) back();
  278. // 新增:如果传入了 onCancelClick,则调用
  279. if (onCancelClick != null) {
  280. onCancelClick();
  281. }
  282. });
  283. }
  284. void onBuyClick() async {
  285. final buyGoods = selectedGoods;
  286. final buyPayWay = selectedPayWay;
  287. if (buyGoods == null) {
  288. ToastUtil.show(StringName.memberPleaseChoiceGoods);
  289. return;
  290. }
  291. if (buyPayWay == null) {
  292. ToastUtil.show(StringName.memberPleaseChoicePayment);
  293. return;
  294. }
  295. memberRepository.setLastSelectedMember(buyGoods, buyPayWay);
  296. //增加渠道登录判断
  297. if (Platform.isAndroid && atmobPlatformInfo.tgPlatform == ChannelId.sd) {
  298. if (!accountRepository.isLogin.value) {
  299. ToastUtil.show(StringName.accountNoLogin);
  300. final isLogin = await LoginPage.start();
  301. if (!isLogin) {
  302. return;
  303. }
  304. }
  305. }
  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. backDismiss: true, clickMaskDismiss: true);
  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. LoadingDialog.hide();
  334. ToastUtil.show(StringName.payNotSupport);
  335. }
  336. }).catchError((error) {
  337. LoadingDialog.hide();
  338. //处理购买失败
  339. showRetainDialog();
  340. if (error is ServerErrorException) {
  341. if (error.code == ErrorCode.payOrderError) {
  342. refreshMemberData();
  343. ToastUtil.show(error.message);
  344. } else if (error.code == ErrorCode.noLoginError) {
  345. ToastUtil.show(StringName.accountNoLogin);
  346. LoginPage.start();
  347. } else {
  348. ToastUtil.show(error.message);
  349. }
  350. } else {
  351. ToastUtil.show(StringName.memberPaymentFailed);
  352. }
  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. print('购买失败: ${result['error']}');
  376. showRetainDialog(onCancelClick: () {
  377. ToastUtil.show("支付失败,请稍后重试");
  378. });
  379. }
  380. }
  381. void _onAliScanPay(String outTradeNo, String qrHtml, PayItemBean paymentWay,
  382. GoodsBean goodsBean) {
  383. LoadingDialog.hide();
  384. AlipayQrCodeDialog.show(
  385. qrCodeHtml: qrHtml,
  386. loadSuccessCallback: () {
  387. checkPaymentStatus(outTradeNo, paymentWay, goodsBean);
  388. },
  389. onCloseCallback: () async {
  390. //关闭后再持续查询几秒
  391. CustomLoadingDialog.show();
  392. await Future.delayed(Duration(seconds: 4));
  393. paymentStatusManager.removePollingSubscription(outTradeNo);
  394. CustomLoadingDialog.hide();
  395. });
  396. }
  397. void checkPaymentStatus(
  398. String orderNo, PayItemBean paymentWay, GoodsBean goodsBean,
  399. {String? receiptData}) {
  400. LoadingDialog.show(StringName.payQuerypayState);
  401. paymentStatusManager.registerPaymentSuccessCallback(orderNo, this);
  402. paymentStatusManager.checkPaymentStatus(orderNo, paymentWay, goodsBean,
  403. receiptData: receiptData);
  404. }
  405. void _onWeChatPay(String outTradeNo, String payJson, int payMethod,
  406. GoodsBean buyGoods, PayItemBean buyPayWay) {
  407. final bean = WechatPaymentSignBean.stringToBean(payJson);
  408. final payInfo = WechatPayInfo(
  409. appId: bean.appId,
  410. partnerId: bean.partnerId,
  411. prepayId: bean.prepayId,
  412. package: bean.package,
  413. noncestr: bean.nonceStr,
  414. timestamp: bean.timeStamp,
  415. sign: bean.sign);
  416. requestSdkPay(payInfo, outTradeNo, payMethod, buyGoods, buyPayWay);
  417. }
  418. void _onAliPay(String outTradeNo, String payJson, int payMethod,
  419. GoodsBean buyGoods, PayItemBean buyPayWay) {
  420. final payInfo = AliPayInfo(payJson);
  421. requestSdkPay(payInfo, outTradeNo, payMethod, buyGoods, buyPayWay);
  422. }
  423. void _onWechatScanPay(String outTradeNo, String qrCodeUrl,
  424. PayItemBean paymentWay, GoodsBean goodsBean) {
  425. LoadingDialog.hide();
  426. WechatQrCodeDialog.show(
  427. qrCodeUrl: qrCodeUrl,
  428. loadSuccessCallback: () {
  429. checkPaymentStatus(outTradeNo, paymentWay, goodsBean);
  430. },
  431. onCloseCallback: () async {
  432. //关闭后再持续查询几秒
  433. CustomLoadingDialog.show();
  434. await Future.delayed(Duration(seconds: 4));
  435. paymentStatusManager.removePollingSubscription(outTradeNo);
  436. CustomLoadingDialog.hide();
  437. });
  438. }
  439. ///查询订阅状态
  440. Future<void> _requestCheckRestoreStatus(String? receiptData) async {
  441. memberRepository.subscriptionCheck(3, receiptData ?? "").then((value) {
  442. _checkResponse.value = value;
  443. }).catchError((error) {});
  444. }
  445. /// 点击了恢复订阅
  446. Future<void> clickRecoverSubscribe() async {
  447. PayItemBean? paymentWay = _selectedPayWay.value;
  448. if (paymentWay == null) {
  449. return;
  450. }
  451. int payPlatform = paymentWay.payPlatform;
  452. int payMethod = paymentWay.payMethod;
  453. CustomLoadingDialog.show();
  454. Future.delayed(const Duration(seconds: 20), () {
  455. CustomLoadingDialog.hide();
  456. //ToastUtil.show("没有发现可恢复的记录");
  457. });
  458. final result = await ApplePay().restore();
  459. if (result["success"] == true) {
  460. // CustomLoadingDialog.hide();
  461. var receipt = result['receipt'];
  462. print('查找恢复记录成功: ${result['receipt']}');
  463. checkRestoreStatus(receipt);
  464. } else {
  465. CustomLoadingDialog.hide();
  466. ToastUtil.show("恢复失败");
  467. print('恢复失败: ${result['error']}');
  468. }
  469. // 显示恢复订阅弹窗
  470. // RecoverSubscribeDialog.show("周会员2025年3月6日到期。", () {
  471. // AtmobLog.d(tag, "恢复订阅弹窗 => 点击确认");
  472. // });
  473. }
  474. /// 检查恢复订阅结果
  475. Future<void> checkRestoreStatus(String? receiptData) async {
  476. PayItemBean? paymentWay = _selectedPayWay.value;
  477. if (paymentWay == null) {
  478. // ToastUtil.showToast(StringName.storeChoicePayment.tr);
  479. return;
  480. }
  481. if (receiptData == null) {
  482. return;
  483. }
  484. int payPlatform = paymentWay.payPlatform;
  485. int payMethod = paymentWay.payMethod;
  486. // var code = await storeRepository.resume(payPlatform, payMethod, receiptData);
  487. memberRepository.subscriptionResume(3, receiptData).then((data) async {
  488. CustomLoadingDialog.hide();
  489. ToastUtil.show("恢复成功");
  490. await AccountRepository.getInstance().getMemberStatus();
  491. //accountRepository.refreshMemberStatus();
  492. Get.back();
  493. }).catchError((error) {
  494. CustomLoadingDialog.hide();
  495. ToastUtil.show("恢复失败");
  496. });
  497. // if (code == 0) {
  498. // CustomLoadingDialog.hide();
  499. // ToastUtil.show("Restore success");
  500. // userRepository.getUserInfo();
  501. // Get.back();
  502. // } else {
  503. // CustomLoadingDialog.hide();
  504. // ToastUtil.show("Restore fail");
  505. // }
  506. }
  507. void requestSdkPay(dynamic payInfo, String outTradeNo, int payMethod,
  508. GoodsBean buyGoods, PayItemBean buyPayWay) {
  509. AgilePay.startPay(payInfo, success: (String? result) {
  510. checkPaymentStatus(outTradeNo, buyPayWay, buyGoods, receiptData: result);
  511. }, payError: (int errno, String? errorMessage) {
  512. LoadingDialog.hide();
  513. debugPrint('zk---payError: $errno, $errorMessage');
  514. errorEventReport(payMethod);
  515. showRetainDialog(errno: errno);
  516. }, error: (int errno, String? error) {
  517. LoadingDialog.hide();
  518. debugPrint('zk---error: $errno, $error');
  519. errorEventReport(payMethod);
  520. showRetainDialog(errno: errno);
  521. });
  522. }
  523. void errorEventReport(int payMethod) {
  524. if (payMethod == PayMethod.wechat) {
  525. // EventHandler.report();
  526. } else if (payMethod == PayMethod.alipay) {
  527. // EventHandler.report();
  528. } else if (payMethod == PayMethod.apple) {
  529. // EventHandler.report();
  530. }
  531. }
  532. void errorPayToast(int? errno) {
  533. if (errno == null) {
  534. return;
  535. }
  536. if (errno == AgilePayCode.payCodeNotSupport) {
  537. ToastUtil.show(StringName.payNotSupport);
  538. } else if (errno == AgilePayCode.payCodeCancelError) {
  539. ToastUtil.show(StringName.payUserCancel);
  540. } else if (errno == AgilePayCode.payCodeWxEnvError) {
  541. ToastUtil.show(StringName.payWxEvnError);
  542. } else if (errno == AgilePayCode.payCodeNotConnectStore) {
  543. ToastUtil.show(StringName.payNotConnectStore);
  544. } else {
  545. ToastUtil.show(StringName.payError);
  546. }
  547. }
  548. @override
  549. void onClose() {
  550. super.onClose();
  551. _changeStreamController?.close();
  552. _memberDataFuture?.cancel();
  553. scrollController.dispose();
  554. paymentStatusManager.unregisterPaymentSuccessCallback(this);
  555. }
  556. @override
  557. void onPaymentSuccess(
  558. String orderNo, PayItemBean paymentWay, GoodsBean storeItemBean) {
  559. ///购买成功消失
  560. LoadingDialog.hide();
  561. ///购买成功之后弹出
  562. afterTheFirstPurchasePromptSharingBoxPops();
  563. }
  564. @override
  565. void onPaymentFail() {
  566. ///购买成功消失
  567. LoadingDialog.hide();
  568. }
  569. ///第一次购买之后弹出提示分享框
  570. void afterTheFirstPurchasePromptSharingBoxPops() {
  571. paymentStatusManager.onOrderFirstCheck().then((checkReponse) {
  572. //是否展示邀请弹框
  573. if (checkReponse.showInvite || checkReponse.showEvaluate) {
  574. MemberPageType pageType =
  575. Get.arguments ?? MemberPageType.universalAccessEnter;
  576. //去分享
  577. if (checkReponse.showInvite &&
  578. pageType == MemberPageType.addFriendToEnter) {
  579. MemberPaymentCompletedDialog.show(confirmOnTap: () {
  580. WechatShareUtil.shareWebPage().catchError((error) {
  581. ToastUtil.show(error);
  582. });
  583. if (checkReponse.showEvaluate) {
  584. ///是否展示好评领会员弹框
  585. MemberEvaluationPopUpDialog.show(
  586. confirmOnTap: () {
  587. AppReviewService.requestAppReview(Get.context!);
  588. onPaySucessShow();
  589. },
  590. cancelOnTap: () {
  591. onPaySucessShow();
  592. },
  593. days: 1);
  594. } else {
  595. onPaySucessShow();
  596. }
  597. }, cancelOnTap: () {
  598. if (checkReponse.showEvaluate) {
  599. ///是否展示好评领会员弹框
  600. MemberEvaluationPopUpDialog.show(
  601. confirmOnTap: () {
  602. AppReviewService.requestAppReview(Get.context!);
  603. ///领取会员后请求
  604. memberRepository
  605. .memberEvaluate()
  606. .then((_) {})
  607. .catchError((error) {});
  608. ; //memberEvaluate
  609. onPaySucessShow();
  610. },
  611. cancelOnTap: () {
  612. onPaySucessShow();
  613. },
  614. days: 1);
  615. } else {
  616. onPaySucessShow();
  617. }
  618. });
  619. } else if (checkReponse.showEvaluate) {
  620. ///是否展示好评领会员弹框
  621. MemberEvaluationPopUpDialog.show(
  622. confirmOnTap: () {
  623. AppReviewService.requestAppReview(Get.context!);
  624. onPaySucessShow();
  625. },
  626. cancelOnTap: () {
  627. onPaySucessShow();
  628. },
  629. days: 1);
  630. }
  631. } else {
  632. onPaySucessShow();
  633. }
  634. }).catchError((erro) {
  635. onPaySucessShow();
  636. });
  637. }
  638. ///支付成功之后展示
  639. void onPaySucessShow() {
  640. try {
  641. WechatQrCodeDialog.dismiss();
  642. AlipayQrCodeDialog.dismiss();
  643. CustomLoadingDialog.hide();
  644. LoadingDialog.hide();
  645. } catch (e) {
  646. debugPrint('zk---onPaymentSuccess error: $e');
  647. }
  648. showPaymentSuccessDialog(onConfirm: back, onCancel: back);
  649. }
  650. }