store_repository.dart 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. import 'package:injectable/injectable.dart';
  2. import 'package:keyboard/data/api/atmob_api.dart';
  3. import 'package:keyboard/data/api/response/item_list_response.dart';
  4. import 'package:keyboard/data/api/response/order_pay_response.dart';
  5. import 'package:keyboard/utils/http_handler.dart';
  6. import 'package:get/get.dart';
  7. import 'package:keyboard/widget/platform_util.dart';
  8. import '../../base/app_base_request.dart';
  9. import '../../utils/async_util.dart';
  10. import '../../utils/atmob_log.dart';
  11. import '../../utils/error_handler.dart';
  12. import '../../utils/payment_status_manager.dart';
  13. import '../api/request/order_pay_request.dart';
  14. import '../api/request/order_status_request.dart';
  15. import '../api/request/subscribe_check_request.dart';
  16. import '../api/request/subscribe_resume_request.dart';
  17. import '../api/response/item_retention_response.dart';
  18. import '../api/response/member_new_user_response.dart';
  19. import '../bean/character_info.dart';
  20. import '../bean/goods_info.dart';
  21. import '../bean/pay_way_info.dart';
  22. import '../consts/error_code.dart';
  23. import 'account_repository.dart';
  24. @lazySingleton
  25. class StoreRepository {
  26. final tag = 'StoreRepository';
  27. final AtmobApi atmobApi;
  28. final AccountRepository accountRepository;
  29. CancelableFuture? goodsInfoFuture;
  30. final RxList<GoodsInfo> _newDiscountGoodsInfoList = <GoodsInfo>[].obs;
  31. RxList<GoodsInfo> get newDiscountGoodsInfoList => _newDiscountGoodsInfoList;
  32. final RxList<PayWayInfo> _newDiscountPayWayList = <PayWayInfo>[].obs;
  33. RxList<PayWayInfo> get newDiscountPayWayList => _newDiscountPayWayList;
  34. final RxList<CharacterInfo> _charactersList = <CharacterInfo>[].obs;
  35. RxList<CharacterInfo> get charactersList => _charactersList;
  36. StoreRepository(this.atmobApi, this.accountRepository) {
  37. print('$tag....init');
  38. refreshNewDiscountGoodsInfoList();
  39. }
  40. void refreshNewDiscountGoodsInfoList() {
  41. goodsInfoFuture?.cancel();
  42. goodsInfoFuture = AsyncUtil.retryWithExponentialBackoff(
  43. () => getMemberNewUserGoodsList(),
  44. 4,
  45. predicate: (error) {
  46. if (error is ServerErrorException) {
  47. return error.code != ErrorCode.noLoginError;
  48. }
  49. return true;
  50. },
  51. );
  52. goodsInfoFuture?.catchError((error) {
  53. ErrorHandler.toastError(error);
  54. });
  55. }
  56. Future<ItemListResponse> getGoodsInfoList() async {
  57. return await atmobApi
  58. .getGoodsList(AppBaseRequest())
  59. .then(HttpHandler.handle(true));
  60. }
  61. Future<MemberNewUserResponse> getMemberNewUserGoodsList() async {
  62. return await atmobApi
  63. .getMemberUserResponse(AppBaseRequest())
  64. .then(HttpHandler.handle(true))
  65. .then((data) {
  66. _newDiscountGoodsInfoList.clear();
  67. _newDiscountPayWayList.clear();
  68. _charactersList.clear();
  69. if (data.goodsInfoList?.isNotEmpty == true) {
  70. _newDiscountGoodsInfoList.addAll(data.goodsInfoList!);
  71. }
  72. if (data.payInfoList?.isNotEmpty == true) {
  73. _newDiscountPayWayList.addAll(data.payInfoList!);
  74. }
  75. if (data.characterInfos?.isNotEmpty == true) {
  76. _charactersList.addAll(data.characterInfos!);
  77. }
  78. return data;
  79. });
  80. }
  81. Future<OrderPayResponse> submitAndRequestPay({
  82. required int goodsId,
  83. required int payPlatform,
  84. required int payMethod,
  85. }) {
  86. return atmobApi
  87. .orderPay(
  88. OrderPayRequest(
  89. goodsId: goodsId,
  90. payPlatform: payPlatform,
  91. payMethod: payMethod,
  92. ),
  93. )
  94. .then(HttpHandler.handle(false));
  95. }
  96. Future<int> orderStatus(String outTradeNo, {String? receiptData}) {
  97. return atmobApi
  98. .orderStatus(OrderStatusRequest(outTradeNo, receiptData))
  99. .then(HttpHandler.handle(false))
  100. .then((data) {
  101. if (data.payStatus == PaymentStatus.payStatusSuccess) {
  102. accountRepository.refreshUserInfo();
  103. }
  104. return data.payStatus;
  105. });
  106. }
  107. Future<ItemRetentionResponse> getItemRetention() {
  108. return atmobApi
  109. .getItemRetention(AppBaseRequest())
  110. .then(HttpHandler.handle(true));
  111. }
  112. /// 检查订阅状态(IOS使用),验证用户是否可发起支付
  113. Future<void> subscribeCheck(
  114. int payPlatform,
  115. int payMethod,
  116. String receiptData,
  117. ) {
  118. return atmobApi
  119. .subscribeCheck(
  120. SubscribeCheckRequest(payPlatform, payMethod, receiptData),
  121. )
  122. .then(HttpHandler.handle(true));
  123. }
  124. /// 恢复订阅(IOS使用)
  125. Future<void> subscribeResume(
  126. int payPlatform,
  127. int payMethod,
  128. String receiptData,
  129. ) {
  130. return atmobApi
  131. .subscribeResume(
  132. SubscribeResumeRequest(payPlatform, payMethod, receiptData),
  133. )
  134. .then(HttpHandler.handle(true));
  135. }
  136. }