member_controller.dart 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  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 RxBool _isShowMemberSubscribeTxt = false.obs;
  54. bool get isShowMemberSubscribeTxt => _isShowMemberSubscribeTxt.value;
  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. CancelableFuture? _memberDataFuture;
  75. Duration? get activityDuration => memberRepository.activityDuration.value;
  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. MemberController(
  107. this.accountRepository, this.memberRepository, this.paymentStatusManager);
  108. @override
  109. void onReady() async {
  110. super.onReady();
  111. _isTherePopUpPrompt();
  112. _startAnimationSwitcher();
  113. scrollController.addListener(() {
  114. double offset = scrollController.offset;
  115. double opacity = offset / 100;
  116. if (opacity > 1) {
  117. opacity = 1;
  118. } else if (opacity < 0) {
  119. opacity = 0;
  120. }
  121. _toolBarOpacity.value = opacity;
  122. });
  123. refreshMemberData();
  124. precacheImage(
  125. Assets.images.iconMemberSpecialProductsSelect.provider(), Get.context!);
  126. precacheImage(
  127. Assets.images.iconMemberSpecialProductsNormal.provider(), Get.context!);
  128. }
  129. ///是否弹窗提示
  130. void _isTherePopUpPrompt() {
  131. MemberPageType pageType =
  132. Get.arguments ?? MemberPageType.universalAccessEnter;
  133. if (pageType == MemberPageType.afeterTrialMemberEnter) {
  134. MemberFirstWeekDiscountDialog.show(confirmOnTap: () {
  135. onBuyClick();
  136. }, onPrivacyPolicyClick: () {
  137. onPrivacyPolicyClick();
  138. }, onTermOfServiceClick: () {
  139. onTermOfServiceClick();
  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(
  168. fontSize: 11.sp,
  169. color: Colors.white,
  170. fontWeight: FontWeight.w400),
  171. children: [
  172. TextSpan(text: userIdStr),
  173. TextSpan(text: '用户 '),
  174. TextSpan(text: secondsOrHour),
  175. TextSpan(text: '前购买了'),
  176. TextSpan(
  177. text: _storeTypes[index],
  178. style: TextStyle(color: '#FFC95D'.color)),
  179. ]))
  180. ],
  181. ));
  182. }
  183. String getUserName(String phone) {
  184. if (phone.length > 4) {
  185. phone = phone.substring(phone.length - 4);
  186. }
  187. return '${StringName.mineAccountLoggedDesc}$phone';
  188. }
  189. void back() {
  190. Get.back();
  191. }
  192. void onLoginClick() {
  193. if (accountRepository.isLogin.value) {
  194. return;
  195. }
  196. LoginPage.start();
  197. }
  198. void refreshMemberData() {
  199. _memberDataFuture?.cancel();
  200. _memberDataFuture =
  201. AsyncUtil.retryWithExponentialBackoff(() => _requestMemberData(), 4);
  202. _memberDataFuture?.catchError((error) {
  203. ErrorHandler.toastError(error);
  204. });
  205. }
  206. Future<void> _requestMemberData() {
  207. return memberRepository
  208. .getMemberList(itemListType: Platform.isIOS ? 2 : 0)
  209. .then((response) {
  210. goodsList.clear();
  211. payItemList.clear();
  212. evaluateList.clear();
  213. _selectedGoods.value = null;
  214. _checkMemberHasSubscribe(response.goodsList);
  215. if (response.goodsList?.isNotEmpty == true) {
  216. goodsList.addAll(response.goodsList!);
  217. _selectedGoods.value = goodsList.first;
  218. }
  219. if (response.payInfoList?.isNotEmpty == true) {
  220. payItemList.addAll(response.payInfoList!);
  221. _selectedPayWay.value = payItemList.first;
  222. }
  223. if (response.evaluateList?.isNotEmpty == true) {
  224. evaluateList.addAll(response.evaluateList!);
  225. }
  226. });
  227. }
  228. void _checkMemberHasSubscribe(List<GoodsBean>? goodsList) {
  229. if (goodsList == null) {
  230. _isShowMemberSubscribeTxt.value = false;
  231. return;
  232. }
  233. for (var goods in goodsList) {
  234. if (goods.subscribable == 1) {
  235. _isShowMemberSubscribeTxt.value = true;
  236. return;
  237. }
  238. }
  239. _isShowMemberSubscribeTxt.value = false;
  240. }
  241. void onGoodsItemClick(GoodsBean item) {
  242. _selectedGoods.value = item;
  243. onBuyClick();
  244. }
  245. void onPrivacyPolicyClick() {
  246. BrowserPage.start(WebUrl.privacyPolicy);
  247. }
  248. void onTermOfServiceClick() {
  249. BrowserPage.start(WebUrl.userAgreement);
  250. }
  251. void onRenewalAgreementClick() {
  252. BrowserPage.start(WebUrl.renewalAgreement);
  253. }
  254. void onPayWayItemClick(PayItemBean item) {
  255. _selectedPayWay.value = item;
  256. }
  257. void onPopBack({bool iosIsBack = true}) {
  258. if (Platform.isIOS) {
  259. if (iosIsBack) {
  260. back();
  261. return;
  262. }
  263. if (accountRepository.memberIsExpired()) {
  264. FocusScope.of(Get.context!).unfocus();
  265. userCancelsPaymentDisplay();
  266. }
  267. } else {
  268. if (accountRepository.memberIsExpired()) {
  269. userCancelsPaymentDisplay();
  270. } else {
  271. back();
  272. }
  273. }
  274. }
  275. ///用户取消支付展示
  276. void userCancelsPaymentDisplay() {
  277. if (Platform.isAndroid) {
  278. back();
  279. } else if (Platform.isIOS) {
  280. // 要执行的代码
  281. Future.delayed(Duration(milliseconds: 500), () {
  282. showRetainDialog(isBack: true);
  283. });
  284. } else {
  285. showRetainDialog(isBack: true);
  286. }
  287. }
  288. ///支付错误的时候调用
  289. void showRetainDialog({bool isBack = false, int? errno}) {
  290. MemberRetainDialog.show(
  291. payClick: () {
  292. onBuyClick();
  293. },
  294. cancelClick: () {
  295. errorPayToast(errno);
  296. if (Platform.isAndroid && isBack) back();
  297. },
  298. isShowRecommendView: isBack);
  299. }
  300. void onBuyClick() async {
  301. final buyGoods = selectedGoods;
  302. final buyPayWay = selectedPayWay;
  303. if (buyGoods == null) {
  304. ToastUtil.show(StringName.memberPleaseChoiceGoods);
  305. return;
  306. }
  307. if (buyPayWay == null) {
  308. ToastUtil.show(StringName.memberPleaseChoicePayment);
  309. return;
  310. }
  311. memberRepository.setLastSelectedMember(buyGoods, buyPayWay);
  312. //增加渠道登录判断
  313. if (Platform.isAndroid && atmobPlatformInfo.tgPlatform == ChannelId.sd) {
  314. if (!accountRepository.isLogin.value) {
  315. ToastUtil.show(StringName.accountNoLogin);
  316. final isLogin = await LoginPage.start();
  317. if (!isLogin) {
  318. return;
  319. }
  320. }
  321. }
  322. int goodsId = buyGoods.id;
  323. int payPlatform = buyPayWay.payPlatform;
  324. int payMethod = buyPayWay.payMethod;
  325. int payWayType =
  326. getPayWayType(payMethod: payMethod, payPlatform: payPlatform);
  327. LoadingDialog.show(StringName.payLoading,
  328. backDismiss: true, clickMaskDismiss: true);
  329. memberRepository
  330. .submitAndRequestPay(
  331. goodsId: goodsId, payPlatform: payPlatform, payMethod: payMethod)
  332. .then((response) {
  333. if (payWayType == PayWayType.paymentWayWechat) {
  334. _onWeChatPay(response.outTradeNo, response.wechatPayPrepayJson!,
  335. payMethod, buyGoods, buyPayWay);
  336. } else if (payWayType == PayWayType.paymentWayWechatScan) {
  337. _onWechatScanPay(response.outTradeNo, response.wechatPayQrcodeUrl!,
  338. buyPayWay, buyGoods);
  339. } else if (payWayType == PayWayType.paymentWayAlipay) {
  340. _onAliPay(response.outTradeNo, response.alipayOrderString!, payMethod,
  341. buyGoods, buyPayWay);
  342. } else if (payWayType == PayWayType.paymentWayAlipayScan) {
  343. _onAliScanPay(response.outTradeNo, response.alipayQrcodeHtml!,
  344. buyPayWay, buyGoods);
  345. } else if (payWayType == PayWayType.paymentWayApple) {
  346. _onApplePay(response.outTradeNo, response.appAccountToken ?? "",
  347. buyPayWay, buyGoods);
  348. } else {
  349. LoadingDialog.hide();
  350. ToastUtil.show(StringName.payNotSupport);
  351. }
  352. }).catchError((error) {
  353. LoadingDialog.hide();
  354. //处理购买失败
  355. showRetainDialog();
  356. if (error is ServerErrorException) {
  357. if (error.code == ErrorCode.payOrderError) {
  358. refreshMemberData();
  359. ToastUtil.show(error.message);
  360. } else if (error.code == ErrorCode.noLoginError) {
  361. ToastUtil.show(StringName.accountNoLogin);
  362. LoginPage.start();
  363. } else {
  364. ToastUtil.show(error.message);
  365. }
  366. } else {
  367. ToastUtil.show(StringName.memberPaymentFailed);
  368. }
  369. });
  370. }
  371. ///发起购买请求
  372. Future<void> _onApplePay(
  373. String outTradeNo,
  374. String appAccountToken,
  375. PayItemBean payWayInfo,
  376. GoodsBean goodsInfo,
  377. ) async {
  378. final result = await ApplePay().purchase(
  379. productId: goodsInfo.appleGoodsId ?? "",
  380. appAccountToken: appAccountToken,
  381. );
  382. if (result["success"] == true) {
  383. var receipt = result['receipt'];
  384. print('购买成功: ${result['receipt']}');
  385. checkPaymentStatus(
  386. outTradeNo,
  387. payWayInfo,
  388. goodsInfo,
  389. receiptData: receipt,
  390. );
  391. } else {
  392. LoadingDialog.hide();
  393. print('购买失败: ${result['error']}');
  394. showRetainDialog(onCancelClick: () {
  395. ToastUtil.show("支付失败,请稍后重试");
  396. });
  397. }
  398. }
  399. void _onAliScanPay(String outTradeNo, String qrHtml, PayItemBean paymentWay,
  400. GoodsBean goodsBean) {
  401. LoadingDialog.hide();
  402. AlipayQrCodeDialog.show(
  403. qrCodeHtml: qrHtml,
  404. loadSuccessCallback: () {
  405. checkPaymentStatus(outTradeNo, paymentWay, goodsBean);
  406. },
  407. onCloseCallback: () async {
  408. //关闭后再持续查询几秒
  409. CustomLoadingDialog.show();
  410. await Future.delayed(Duration(seconds: 4));
  411. paymentStatusManager.removePollingSubscription(outTradeNo);
  412. CustomLoadingDialog.hide();
  413. });
  414. }
  415. void checkPaymentStatus(
  416. String orderNo, PayItemBean paymentWay, GoodsBean goodsBean,
  417. {String? receiptData}) {
  418. LoadingDialog.show(StringName.payQuerypayState);
  419. paymentStatusManager.registerPaymentSuccessCallback(orderNo, this);
  420. paymentStatusManager.checkPaymentStatus(orderNo, paymentWay, goodsBean,
  421. receiptData: receiptData);
  422. }
  423. void _onWeChatPay(String outTradeNo, String payJson, int payMethod,
  424. GoodsBean buyGoods, PayItemBean buyPayWay) {
  425. final bean = WechatPaymentSignBean.stringToBean(payJson);
  426. final payInfo = WechatPayInfo(
  427. appId: bean.appId,
  428. partnerId: bean.partnerId,
  429. prepayId: bean.prepayId,
  430. package: bean.package,
  431. noncestr: bean.nonceStr,
  432. timestamp: bean.timeStamp,
  433. sign: bean.sign);
  434. requestSdkPay(payInfo, outTradeNo, payMethod, buyGoods, buyPayWay);
  435. }
  436. void _onAliPay(String outTradeNo, String payJson, int payMethod,
  437. GoodsBean buyGoods, PayItemBean buyPayWay) {
  438. final payInfo = AliPayInfo(payJson);
  439. requestSdkPay(payInfo, outTradeNo, payMethod, buyGoods, buyPayWay);
  440. }
  441. void _onWechatScanPay(String outTradeNo, String qrCodeUrl,
  442. PayItemBean paymentWay, GoodsBean goodsBean) {
  443. LoadingDialog.hide();
  444. WechatQrCodeDialog.show(
  445. qrCodeUrl: qrCodeUrl,
  446. loadSuccessCallback: () {
  447. checkPaymentStatus(outTradeNo, paymentWay, goodsBean);
  448. },
  449. onCloseCallback: () async {
  450. //关闭后再持续查询几秒
  451. CustomLoadingDialog.show();
  452. await Future.delayed(Duration(seconds: 4));
  453. paymentStatusManager.removePollingSubscription(outTradeNo);
  454. CustomLoadingDialog.hide();
  455. });
  456. }
  457. ///查询订阅状态
  458. Future<void> _requestCheckRestoreStatus(String? receiptData) async {
  459. memberRepository.subscriptionCheck(3, receiptData ?? "").then((value) {
  460. _checkResponse.value = value;
  461. }).catchError((error) {});
  462. }
  463. /// 点击了恢复订阅
  464. Future<void> clickRecoverSubscribe() async {
  465. PayItemBean? paymentWay = _selectedPayWay.value;
  466. if (paymentWay == null) {
  467. return;
  468. }
  469. int payPlatform = paymentWay.payPlatform;
  470. int payMethod = paymentWay.payMethod;
  471. CustomLoadingDialog.show();
  472. Future.delayed(const Duration(seconds: 20), () {
  473. CustomLoadingDialog.hide();
  474. //ToastUtil.show("没有发现可恢复的记录");
  475. });
  476. final result = await ApplePay().restore();
  477. if (result["success"] == true) {
  478. // CustomLoadingDialog.hide();
  479. var receipt = result['receipt'];
  480. print('查找恢复记录成功: ${result['receipt']}');
  481. checkRestoreStatus(receipt);
  482. } else {
  483. CustomLoadingDialog.hide();
  484. ToastUtil.show("恢复失败");
  485. print('恢复失败: ${result['error']}');
  486. }
  487. // 显示恢复订阅弹窗
  488. // RecoverSubscribeDialog.show("周会员2025年3月6日到期。", () {
  489. // AtmobLog.d(tag, "恢复订阅弹窗 => 点击确认");
  490. // });
  491. }
  492. /// 检查恢复订阅结果
  493. Future<void> checkRestoreStatus(String? receiptData) async {
  494. PayItemBean? paymentWay = _selectedPayWay.value;
  495. if (paymentWay == null) {
  496. // ToastUtil.showToast(StringName.storeChoicePayment.tr);
  497. return;
  498. }
  499. if (receiptData == null) {
  500. return;
  501. }
  502. int payPlatform = paymentWay.payPlatform;
  503. int payMethod = paymentWay.payMethod;
  504. // var code = await storeRepository.resume(payPlatform, payMethod, receiptData);
  505. memberRepository.subscriptionResume(3, receiptData).then((data) async {
  506. CustomLoadingDialog.hide();
  507. ToastUtil.show("恢复成功");
  508. await AccountRepository.getInstance().getMemberStatus();
  509. //accountRepository.refreshMemberStatus();
  510. Get.back();
  511. }).catchError((error) {
  512. CustomLoadingDialog.hide();
  513. ToastUtil.show("恢复失败");
  514. });
  515. // if (code == 0) {
  516. // CustomLoadingDialog.hide();
  517. // ToastUtil.show("Restore success");
  518. // userRepository.getUserInfo();
  519. // Get.back();
  520. // } else {
  521. // CustomLoadingDialog.hide();
  522. // ToastUtil.show("Restore fail");
  523. // }
  524. }
  525. void requestSdkPay(dynamic payInfo, String outTradeNo, int payMethod,
  526. GoodsBean buyGoods, PayItemBean buyPayWay) {
  527. AgilePay.startPay(payInfo, success: (String? result) {
  528. checkPaymentStatus(outTradeNo, buyPayWay, buyGoods, receiptData: result);
  529. }, payError: (int errno, String? errorMessage) {
  530. LoadingDialog.hide();
  531. debugPrint('zk---payError: $errno, $errorMessage');
  532. errorEventReport(payMethod);
  533. showRetainDialog(errno: errno);
  534. }, error: (int errno, String? error) {
  535. LoadingDialog.hide();
  536. debugPrint('zk---error: $errno, $error');
  537. errorEventReport(payMethod);
  538. showRetainDialog(errno: errno);
  539. });
  540. }
  541. void errorEventReport(int payMethod) {
  542. if (payMethod == PayMethod.wechat) {
  543. // EventHandler.report();
  544. } else if (payMethod == PayMethod.alipay) {
  545. // EventHandler.report();
  546. } else if (payMethod == PayMethod.apple) {
  547. // EventHandler.report();
  548. }
  549. }
  550. void errorPayToast(int? errno) {
  551. if (errno == null) {
  552. return;
  553. }
  554. if (errno == AgilePayCode.payCodeNotSupport) {
  555. ToastUtil.show(StringName.payNotSupport);
  556. } else if (errno == AgilePayCode.payCodeCancelError) {
  557. ToastUtil.show(StringName.payUserCancel);
  558. } else if (errno == AgilePayCode.payCodeWxEnvError) {
  559. ToastUtil.show(StringName.payWxEvnError);
  560. } else if (errno == AgilePayCode.payCodeNotConnectStore) {
  561. ToastUtil.show(StringName.payNotConnectStore);
  562. } else {
  563. ToastUtil.show(StringName.payError);
  564. }
  565. }
  566. @override
  567. void onClose() {
  568. super.onClose();
  569. _changeStreamController?.close();
  570. _memberDataFuture?.cancel();
  571. scrollController.dispose();
  572. paymentStatusManager.unregisterPaymentSuccessCallback(this);
  573. }
  574. @override
  575. void onPaymentSuccess(
  576. String orderNo, PayItemBean paymentWay, GoodsBean storeItemBean) {
  577. ///购买成功消失
  578. LoadingDialog.hide();
  579. ///购买成功之后弹出
  580. afterTheFirstPurchasePromptSharingBoxPops();
  581. }
  582. @override
  583. void onPaymentFail() {
  584. ///购买成功消失
  585. LoadingDialog.hide();
  586. }
  587. ///第一次购买之后弹出提示分享框
  588. void afterTheFirstPurchasePromptSharingBoxPops() {
  589. paymentStatusManager.onOrderFirstCheck().then((checkReponse) {
  590. //是否展示邀请弹框
  591. if (checkReponse.showInvite || checkReponse.showEvaluate) {
  592. MemberPageType pageType =
  593. Get.arguments ?? MemberPageType.universalAccessEnter;
  594. //去分享
  595. if (checkReponse.showInvite &&
  596. pageType == MemberPageType.addFriendToEnter) {
  597. MemberPaymentCompletedDialog.show(confirmOnTap: () {
  598. WechatShareUtil.shareWebPage().catchError((error) {
  599. ToastUtil.show(error);
  600. });
  601. if (checkReponse.showEvaluate) {
  602. ///是否展示好评领会员弹框
  603. MemberEvaluationPopUpDialog.show(
  604. confirmOnTap: () {
  605. AppReviewService.requestAppReview(Get.context!);
  606. onPaySucessShow();
  607. },
  608. cancelOnTap: () {
  609. onPaySucessShow();
  610. },
  611. days: 1);
  612. } else {
  613. onPaySucessShow();
  614. }
  615. }, cancelOnTap: () {
  616. if (checkReponse.showEvaluate) {
  617. ///是否展示好评领会员弹框
  618. MemberEvaluationPopUpDialog.show(
  619. confirmOnTap: () {
  620. AppReviewService.requestAppReview(Get.context!);
  621. ///领取会员后请求
  622. memberRepository
  623. .memberEvaluate()
  624. .then((_) {})
  625. .catchError((error) {});
  626. ; //memberEvaluate
  627. onPaySucessShow();
  628. },
  629. cancelOnTap: () {
  630. onPaySucessShow();
  631. },
  632. days: 1);
  633. } else {
  634. onPaySucessShow();
  635. }
  636. });
  637. } else if (checkReponse.showEvaluate) {
  638. ///是否展示好评领会员弹框
  639. MemberEvaluationPopUpDialog.show(
  640. confirmOnTap: () {
  641. AppReviewService.requestAppReview(Get.context!);
  642. onPaySucessShow();
  643. },
  644. cancelOnTap: () {
  645. onPaySucessShow();
  646. },
  647. days: 1);
  648. }
  649. } else {
  650. onPaySucessShow();
  651. }
  652. }).catchError((erro) {
  653. onPaySucessShow();
  654. });
  655. }
  656. ///支付成功之后展示
  657. void onPaySucessShow() {
  658. try {
  659. WechatQrCodeDialog.dismiss();
  660. AlipayQrCodeDialog.dismiss();
  661. CustomLoadingDialog.hide();
  662. LoadingDialog.hide();
  663. } catch (e) {
  664. debugPrint('zk---onPaymentSuccess error: $e');
  665. }
  666. showPaymentSuccessDialog(onConfirm: back, onCancel: back);
  667. }
  668. }