member_controller.dart 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  1. import 'dart:async';
  2. import 'dart:io';
  3. import 'dart:math';
  4. import 'package:agile_pay/flutter_pay.dart';
  5. import 'package:flutter/material.dart';
  6. import 'package:flutter_screenutil/flutter_screenutil.dart';
  7. import 'package:get/get.dart';
  8. import 'package:injectable/injectable.dart';
  9. import 'package:location/base/base_controller.dart';
  10. import 'package:location/data/bean/goods_bean.dart';
  11. import 'package:location/data/bean/pay_item_bean.dart';
  12. import 'package:location/data/repositories/account_repository.dart';
  13. import 'package:location/data/repositories/member_repository.dart';
  14. import 'package:location/dialog/alipay_qr_code_dialog.dart';
  15. import 'package:location/handler/error_handler.dart';
  16. import 'package:location/module/login/login_page.dart';
  17. import 'package:location/resource/assets.gen.dart';
  18. import 'package:location/utils/async_util.dart';
  19. import 'package:location/utils/common_expand.dart';
  20. import 'package:location/utils/toast_util.dart';
  21. import '../../data/api/response/subscription_check_response.dart';
  22. import '../../data/bean/member_status_info.dart';
  23. import '../../data/bean/wechat_payment_sign_bean.dart';
  24. import '../../data/consts/error_code.dart';
  25. import '../../data/consts/payment_type.dart';
  26. import '../../data/consts/web_url.dart';
  27. import '../../dialog/common_confirm_dialog_impl.dart';
  28. import '../../dialog/loading_dialog.dart';
  29. import '../../dialog/member_retain_dialog.dart';
  30. import '../../dialog/wechat_qr_code_dialog.dart';
  31. import '../../resource/string.gen.dart';
  32. import '../../sdk/wechat/wechat_share_util.dart';
  33. import '../../utils/http_handler.dart';
  34. import '../../utils/payment_status_manager.dart';
  35. import '../../widget/animated_switcher_widget.dart';
  36. import '../browser/browser_view.dart';
  37. import 'member_evaluate_bean.dart';
  38. import 'member_fun_bean.dart';
  39. import 'package:apple_pay/apple_pay.dart';
  40. import 'member_payment_completed_dialog.dart';
  41. @injectable
  42. class MemberController extends BaseController implements PaymentStatusCallback {
  43. final AccountRepository accountRepository;
  44. final MemberRepository memberRepository;
  45. final PaymentStatusManager paymentStatusManager;
  46. final switcherController = SwitcherController();
  47. final ScrollController scrollController = ScrollController();
  48. StreamController? _changeStreamController;
  49. final random = Random();
  50. final RxDouble _toolBarOpacity = 0.0.obs;
  51. double get toolBarOpacity => _toolBarOpacity.value;
  52. final List<String> _storeTypes = ['终身会员', '年度会员', '月度会员'];
  53. String? get phone => accountRepository.loginPhoneNum.value;
  54. bool get isLogin => accountRepository.isLogin.value;
  55. MemberStatusInfo? get memberStatusInfo =>
  56. accountRepository.memberStatusInfo.value;
  57. final RxList<GoodsBean> goodsList = <GoodsBean>[].obs;
  58. final Rxn<GoodsBean> _selectedGoods = Rxn<GoodsBean>();
  59. GoodsBean? get selectedGoods => _selectedGoods.value;
  60. final Rxn<PayItemBean> _selectedPayWay = Rxn<PayItemBean>();
  61. PayItemBean? get selectedPayWay => _selectedPayWay.value;
  62. final RxList<PayItemBean> payItemList = <PayItemBean>[].obs;
  63. CancelableFuture? _memberDataFuture;
  64. ///广告弹窗防抖
  65. bool _isPopBackInProgress = false; // 操作进行中标志
  66. ///检查续订的状态
  67. final Rx<SubscriptionCheckResponse> _checkResponse =
  68. Rx(SubscriptionCheckResponse(outTradeNo: ""));
  69. SubscriptionCheckResponse? get requestCheckResponse => _checkResponse.value;
  70. final List<MemberFunBean> funList = [
  71. MemberFunBean(1, Assets.images.iconMemberFun1.path,
  72. StringName.memberFunName1, StringName.memberFunName1Desc),
  73. MemberFunBean(2, Assets.images.iconMemberFun2.path,
  74. StringName.memberFunName2, StringName.memberFunName2Desc),
  75. // MemberFunBean(3, Assets.images.iconMemberFun3.path,
  76. // StringName.memberFunName3, StringName.memberFunName3Desc),
  77. // MemberFunBean(4, Assets.images.iconMemberFun4.path,
  78. // StringName.memberFunName4, StringName.memberFunName4Desc), //该功能还未开发
  79. MemberFunBean(5, Assets.images.iconMemberFun5.path,
  80. StringName.memberFunName5, StringName.memberFunName5Desc),
  81. MemberFunBean(6, Assets.images.iconMemberFun6.path,
  82. StringName.memberFunName6, StringName.memberFunName6Desc),
  83. ];
  84. final List<MemberEvaluateBean> evaluateList = [
  85. MemberEvaluateBean(1, Assets.images.iconEvaluate1.path, '用户189****7913',
  86. "上班没时间,远程遛娃,非常方便很好用。"),
  87. MemberEvaluateBean(2, Assets.images.iconEvaluate2.path, '用户177****2345',
  88. "这个功能太棒了!尤其是夜间出行时,一键报警让我感觉特别安心。"),
  89. MemberEvaluateBean(3, Assets.images.iconEvaluate3.path, '用户138****6789',
  90. "强烈推荐!我和家人经常用这个功能来共享位置,尤其是旅游时,走散了也不怕。"),
  91. MemberEvaluateBean(4, Assets.images.iconEvaluate4.path, '用户159****3456',
  92. "实时定位非常精准,用来监控孩子的行踪特别方便,再也不用担心他们乱跑了。"),
  93. MemberEvaluateBean(5, Assets.images.iconEvaluate5.path, '用户182****9012',
  94. "用来遛狗也很方便,再也不用担心狗狗跑丢了,真是个好工具!"),
  95. ];
  96. MemberController(
  97. this.accountRepository, this.memberRepository, this.paymentStatusManager);
  98. @override
  99. void onReady() async {
  100. super.onReady();
  101. _startAnimationSwitcher();
  102. scrollController.addListener(() {
  103. double offset = scrollController.offset;
  104. double opacity = offset / 100;
  105. if (opacity > 1) {
  106. opacity = 1;
  107. } else if (opacity < 0) {
  108. opacity = 0;
  109. }
  110. _toolBarOpacity.value = opacity;
  111. });
  112. refreshMemberData();
  113. }
  114. void _startAnimationSwitcher() {
  115. _changeStreamController = AsyncUtil.interval(
  116. (time) => changeSwitcherContent(), Duration(seconds: 3), -1);
  117. }
  118. Future<void> changeSwitcherContent() async {
  119. int userId = random.nextInt(10000);
  120. String userIdStr = userId.toString();
  121. int padLength = 4 - userIdStr.length;
  122. for (int i = 0; i < padLength; i++) {
  123. userIdStr = random.nextInt(10).toString() + userIdStr;
  124. }
  125. bool isHour = random.nextBool();
  126. String secondsOrHour;
  127. if (isHour) {
  128. secondsOrHour = "${1 + random.nextInt(8)}小时";
  129. } else {
  130. secondsOrHour = "${1 + random.nextInt(59)}分钟";
  131. }
  132. int index = random.nextInt(_storeTypes.length);
  133. switcherController.updateWidget(Row(
  134. mainAxisAlignment: MainAxisAlignment.center,
  135. children: [
  136. RichText(
  137. text: TextSpan(
  138. style: TextStyle(fontSize: 12.sp, color: Colors.white),
  139. children: [
  140. TextSpan(text: userIdStr),
  141. TextSpan(text: '用户 '),
  142. TextSpan(text: secondsOrHour),
  143. TextSpan(text: '前购买了'),
  144. TextSpan(
  145. text: _storeTypes[index],
  146. style: TextStyle(color: '#FFC95D'.color)),
  147. ]))
  148. ],
  149. ));
  150. }
  151. String getUserName(String phone) {
  152. if (phone.length > 4) {
  153. phone = phone.substring(phone.length - 4);
  154. }
  155. return '${StringName.mineAccountLoggedDesc}$phone';
  156. }
  157. void back() {
  158. Get.back();
  159. }
  160. void onLoginClick() {
  161. if (accountRepository.isLogin.value) {
  162. return;
  163. }
  164. LoginPage.start();
  165. }
  166. void refreshMemberData() {
  167. _memberDataFuture?.cancel();
  168. _memberDataFuture =
  169. AsyncUtil.retryWithExponentialBackoff(() => _requestMemberData(), 4);
  170. _memberDataFuture?.catchError((error) {
  171. ErrorHandler.toastError(error);
  172. });
  173. }
  174. Future<void> _requestMemberData() {
  175. return memberRepository
  176. .getMemberList(itemListType: Platform.isIOS ? 2 : 0)
  177. .then((response) {
  178. goodsList.clear();
  179. payItemList.clear();
  180. _selectedGoods.value = null;
  181. if (response.goodsList?.isNotEmpty == true) {
  182. goodsList.addAll(response.goodsList!);
  183. _selectedGoods.value = goodsList.first;
  184. }
  185. if (response.payInfoList?.isNotEmpty == true) {
  186. payItemList.addAll(response.payInfoList!);
  187. _selectedPayWay.value = payItemList.first;
  188. }
  189. });
  190. }
  191. void onGoodsItemClick(GoodsBean item) {
  192. _selectedGoods.value = item;
  193. }
  194. void onPrivacyPolicyClick() {
  195. BrowserPage.start(WebUrl.privacyPolicy);
  196. }
  197. void onTermOfServiceClick() {
  198. BrowserPage.start(WebUrl.userAgreement);
  199. }
  200. void onPayWayItemClick(PayItemBean item) {
  201. _selectedPayWay.value = item;
  202. }
  203. void onPopBack() {
  204. if (Platform.isIOS) {
  205. back();
  206. if (accountRepository.memberIsExpired()) {
  207. FocusScope.of(Get.context!).unfocus();
  208. showRetainDialog();
  209. }
  210. } else {
  211. if (accountRepository.memberIsExpired()) {
  212. showRetainDialog();
  213. } else {
  214. back();
  215. }
  216. }
  217. }
  218. void showRetainDialog() {
  219. if (_isPopBackInProgress) {
  220. // 如果正在处理返回逻辑,直接跳过
  221. return;
  222. }
  223. _isPopBackInProgress = true; // 锁定状态
  224. MemberRetainDialog.show(payClick: () {
  225. onBuyClick();
  226. _isPopBackInProgress = false;
  227. }, cancelClick: () {
  228. _isPopBackInProgress = false;
  229. if (!Platform.isIOS) {
  230. back();
  231. }
  232. });
  233. }
  234. void onBuyClick() {
  235. if (selectedGoods == null) {
  236. ToastUtil.show(StringName.memberPleaseChoiceGoods);
  237. return;
  238. }
  239. if (selectedPayWay == null && !Platform.isIOS) {
  240. ToastUtil.show(StringName.memberPleaseChoicePayment);
  241. return;
  242. }
  243. final buyGoods = selectedGoods!;
  244. final buyPayWay = selectedPayWay!;
  245. int goodsId = buyGoods.id;
  246. int payPlatform = buyPayWay.payPlatform;
  247. int payMethod = buyPayWay.payMethod;
  248. int payWayType =
  249. getPayWayType(payMethod: payMethod, payPlatform: payPlatform);
  250. LoadingDialog.show(StringName.payLoading);
  251. memberRepository
  252. .submitAndRequestPay(
  253. goodsId: goodsId, payPlatform: payPlatform, payMethod: payMethod)
  254. .then((response) {
  255. if (payWayType == PayWayType.paymentWayWechat) {
  256. _onWeChatPay(response.outTradeNo, response.wechatPayPrepayJson!,
  257. payMethod, buyGoods, buyPayWay);
  258. } else if (payWayType == PayWayType.paymentWayWechatScan) {
  259. _onWechatScanPay(response.outTradeNo, response.wechatPayQrcodeUrl!,
  260. buyPayWay, buyGoods);
  261. } else if (payWayType == PayWayType.paymentWayAlipay) {
  262. _onAliPay(response.outTradeNo, response.alipayOrderString!, payMethod,
  263. buyGoods, buyPayWay);
  264. } else if (payWayType == PayWayType.paymentWayAlipayScan) {
  265. _onAliScanPay(response.outTradeNo, response.alipayQrcodeHtml!,
  266. buyPayWay, buyGoods);
  267. } else if (payWayType == PayWayType.paymentWayApple) {
  268. _onApplePay(response.outTradeNo, response.appAccountToken ?? "",
  269. buyPayWay, buyGoods);
  270. } else {
  271. ToastUtil.show(StringName.payNotSupport);
  272. }
  273. }).catchError((error) {
  274. if (error is ServerErrorException) {
  275. if (error.code == ErrorCode.payOrderError) {
  276. refreshMemberData();
  277. ToastUtil.show(error.message);
  278. } else if (error.code == ErrorCode.noLoginError) {
  279. ToastUtil.show(StringName.accountNoLogin);
  280. LoginPage.start();
  281. } else {
  282. ToastUtil.show(error.message);
  283. }
  284. } else {
  285. ToastUtil.show(StringName.memberPaymentFailed);
  286. }
  287. }).whenComplete(() {
  288. LoadingDialog.hide();
  289. });
  290. }
  291. ///发起购买请求
  292. Future<void> _onApplePay(String outTradeNo,
  293. String appAccountToken,
  294. PayItemBean payWayInfo,
  295. GoodsBean goodsInfo,) async {
  296. final result = await ApplePay().purchase(
  297. productId: goodsInfo.appleGoodsId ?? "",
  298. appAccountToken: appAccountToken,
  299. );
  300. if (result["success"] == true) {
  301. var receipt = result['receipt'];
  302. print('购买成功: ${result['receipt']}');
  303. checkPaymentStatus(
  304. outTradeNo,
  305. payWayInfo,
  306. goodsInfo,
  307. receiptData: receipt,
  308. );
  309. } else {
  310. LoadingDialog.hide();
  311. ToastUtil.show("支付失败,请稍后重试");
  312. print('购买失败: ${result['error']}');
  313. }
  314. }
  315. void _onAliScanPay(String outTradeNo, String qrHtml, PayItemBean paymentWay,
  316. GoodsBean goodsBean) {
  317. AlipayQrCodeDialog.show(
  318. qrCodeHtml: qrHtml,
  319. loadSuccessCallback: () {
  320. checkPaymentStatus(outTradeNo, paymentWay, goodsBean);
  321. },
  322. onCloseCallback: () async {
  323. //关闭后再持续查询几秒
  324. CustomLoadingDialog.show();
  325. await Future.delayed(Duration(seconds: 4));
  326. paymentStatusManager.removePollingSubscription(outTradeNo);
  327. CustomLoadingDialog.hide();
  328. });
  329. }
  330. void checkPaymentStatus(
  331. String orderNo, PayItemBean paymentWay, GoodsBean goodsBean,
  332. {String? receiptData}) {
  333. paymentStatusManager.registerPaymentSuccessCallback(orderNo, this);
  334. paymentStatusManager.checkPaymentStatus(orderNo, paymentWay, goodsBean,
  335. receiptData: receiptData);
  336. }
  337. void _onWeChatPay(String outTradeNo, String payJson, int payMethod,
  338. GoodsBean buyGoods, PayItemBean buyPayWay) {
  339. final bean = WechatPaymentSignBean.stringToBean(payJson);
  340. final payInfo = WechatPayInfo(
  341. appId: bean.appId,
  342. partnerId: bean.partnerId,
  343. prepayId: bean.prepayId,
  344. package: bean.package,
  345. noncestr: bean.nonceStr,
  346. timestamp: bean.timeStamp,
  347. sign: bean.sign);
  348. requestSdkPay(payInfo, outTradeNo, payMethod, buyGoods, buyPayWay);
  349. }
  350. void _onAliPay(String outTradeNo, String payJson, int payMethod,
  351. GoodsBean buyGoods, PayItemBean buyPayWay) {
  352. final payInfo = AliPayInfo(payJson);
  353. requestSdkPay(payInfo, outTradeNo, payMethod, buyGoods, buyPayWay);
  354. }
  355. void _onWechatScanPay(String outTradeNo, String qrCodeUrl,
  356. PayItemBean paymentWay, GoodsBean goodsBean) {
  357. WechatQrCodeDialog.show(
  358. qrCodeUrl: qrCodeUrl,
  359. loadSuccessCallback: () {
  360. checkPaymentStatus(outTradeNo, paymentWay, goodsBean);
  361. },
  362. onCloseCallback: () async {
  363. //关闭后再持续查询几秒
  364. CustomLoadingDialog.show();
  365. await Future.delayed(Duration(seconds: 4));
  366. paymentStatusManager.removePollingSubscription(outTradeNo);
  367. CustomLoadingDialog.hide();
  368. });
  369. }
  370. ///查询订阅状态
  371. Future<void> _requestCheckRestoreStatus(String? receiptData) async {
  372. memberRepository.subscriptionCheck(3, receiptData ?? "").then((value) {
  373. _checkResponse.value = value;
  374. }).catchError((error) {});
  375. }
  376. /// 点击了恢复订阅
  377. Future<void> clickRecoverSubscribe() async {
  378. PayItemBean? paymentWay = _selectedPayWay.value;
  379. if (paymentWay == null) {
  380. return;
  381. }
  382. int payPlatform = paymentWay.payPlatform;
  383. int payMethod = paymentWay.payMethod;
  384. CustomLoadingDialog.show();
  385. Future.delayed(const Duration(seconds: 20), () {
  386. CustomLoadingDialog.hide();
  387. //ToastUtil.show("没有发现可恢复的记录");
  388. });
  389. final result = await ApplePay().restore();
  390. if (result["success"] == true) {
  391. // CustomLoadingDialog.hide();
  392. var receipt = result['receipt'];
  393. print('查找恢复记录成功: ${result['receipt']}');
  394. checkRestoreStatus(receipt);
  395. } else {
  396. CustomLoadingDialog.hide();
  397. ToastUtil.show("恢复失败");
  398. print('恢复失败: ${result['error']}');
  399. }
  400. // 显示恢复订阅弹窗
  401. // RecoverSubscribeDialog.show("周会员2025年3月6日到期。", () {
  402. // AtmobLog.d(tag, "恢复订阅弹窗 => 点击确认");
  403. // });
  404. }
  405. /// 检查恢复订阅结果
  406. Future<void> checkRestoreStatus(String? receiptData) async {
  407. PayItemBean? paymentWay = _selectedPayWay.value;
  408. if (paymentWay == null) {
  409. // ToastUtil.showToast(StringName.storeChoicePayment.tr);
  410. return;
  411. }
  412. if (receiptData == null) {
  413. return;
  414. }
  415. int payPlatform = paymentWay.payPlatform;
  416. int payMethod = paymentWay.payMethod;
  417. // var code = await storeRepository.resume(payPlatform, payMethod, receiptData);
  418. memberRepository.subscriptionResume(3, receiptData).then((data) async {
  419. CustomLoadingDialog.hide();
  420. ToastUtil.show("恢复成功");
  421. await AccountRepository.getInstance().getMemberStatus();
  422. //accountRepository.refreshMemberStatus();
  423. Get.back();
  424. }).catchError((error) {
  425. CustomLoadingDialog.hide();
  426. ToastUtil.show("恢复失败");
  427. });
  428. // if (code == 0) {
  429. // CustomLoadingDialog.hide();
  430. // ToastUtil.show("Restore success");
  431. // userRepository.getUserInfo();
  432. // Get.back();
  433. // } else {
  434. // CustomLoadingDialog.hide();
  435. // ToastUtil.show("Restore fail");
  436. // }
  437. }
  438. void requestSdkPay(dynamic payInfo, String outTradeNo, int payMethod,
  439. GoodsBean buyGoods, PayItemBean buyPayWay) {
  440. AgilePay.startPay(payInfo, success: (String? result) {
  441. LoadingDialog.show(StringName.payQuerypayState);
  442. checkPaymentStatus(outTradeNo, buyPayWay, buyGoods, receiptData: result);
  443. }, payError: (int error, String? errorMessage) {
  444. debugPrint('zk---payError: $error, $errorMessage');
  445. errorPayToast(error);
  446. errorEventReport(payMethod);
  447. }, error: (int errno, String? error) {
  448. debugPrint('zk---error: $errno, $error');
  449. errorPayToast(errno);
  450. errorEventReport(payMethod);
  451. });
  452. }
  453. void errorEventReport(int payMethod) {
  454. if (payMethod == PayMethod.wechat) {
  455. // EventHandler.report();
  456. } else if (payMethod == PayMethod.alipay) {
  457. // EventHandler.report();
  458. } else if (payMethod == PayMethod.apple) {
  459. // EventHandler.report();
  460. }
  461. }
  462. void errorPayToast(int errno) {
  463. if (errno == AgilePayCode.payCodeNotSupport) {
  464. ToastUtil.show(StringName.payNotSupport);
  465. } else if (errno == AgilePayCode.payCodeCancelError) {
  466. ToastUtil.show(StringName.payUserCancel);
  467. } else if (errno == AgilePayCode.payCodeWxEnvError) {
  468. ToastUtil.show(StringName.payWxEvnError);
  469. } else if (errno == AgilePayCode.payCodeNotConnectStore) {
  470. ToastUtil.show(StringName.payNotConnectStore);
  471. } else {
  472. ToastUtil.show(StringName.payError);
  473. }
  474. }
  475. @override
  476. void onClose() {
  477. super.onClose();
  478. _changeStreamController?.close();
  479. _memberDataFuture?.cancel();
  480. scrollController.dispose();
  481. paymentStatusManager.unregisterPaymentSuccessCallback(this);
  482. }
  483. @override
  484. void onPaymentSuccess(
  485. String orderNo, PayItemBean paymentWay, GoodsBean storeItemBean) {
  486. ///购买成功之后弹出
  487. afterTheFirstPurchasePromptSharingBoxPops();
  488. try {
  489. WechatQrCodeDialog.dismiss();
  490. AlipayQrCodeDialog.dismiss();
  491. CustomLoadingDialog.hide();
  492. LoadingDialog.hide();
  493. } catch (e) {
  494. debugPrint('zk---onPaymentSuccess error: $e');
  495. }
  496. showPaymentSuccessDialog(onConfirm: back, onCancel: back);
  497. }
  498. ///第一次购买之后弹出提示分享框
  499. void afterTheFirstPurchasePromptSharingBoxPops() {
  500. paymentStatusManager.onOrderFirstCheck().then((checkReponse) {
  501. if (checkReponse.first) {
  502. MemberPaymentCompletedDialog.show(confirmOnTap: () {
  503. WechatShareUtil.shareWebPage().catchError((error) {
  504. ToastUtil.show(error);
  505. });
  506. });
  507. }
  508. });
  509. }
  510. }