member_controller.dart 24 KB

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