store_repository.dart 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 '../../base/app_base_request.dart';
  8. import '../../di/get_it.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_agreement_check_response.dart';
  19. import '../api/response/member_new_user_response.dart';
  20. import '../bean/character_info.dart';
  21. import '../bean/goods_info.dart';
  22. import '../bean/pay_way_info.dart';
  23. import '../consts/error_code.dart';
  24. import 'account_repository.dart';
  25. @lazySingleton
  26. class StoreRepository {
  27. final tag = 'StoreRepository';
  28. final AtmobApi atmobApi;
  29. final AccountRepository accountRepository;
  30. CancelableFuture? goodsInfoFuture;
  31. final RxList<GoodsInfo> _newDiscountGoodsInfoList = <GoodsInfo>[].obs;
  32. RxList<GoodsInfo> get newDiscountGoodsInfoList => _newDiscountGoodsInfoList;
  33. final RxList<PayWayInfo> _newDiscountPayWayList = <PayWayInfo>[].obs;
  34. RxList<PayWayInfo> get newDiscountPayWayList => _newDiscountPayWayList;
  35. final RxList<CharacterInfo> _charactersList = <CharacterInfo>[].obs;
  36. RxList<CharacterInfo> get charactersList => _charactersList;
  37. final RxBool hasAutoRenewal = false.obs;
  38. StoreRepository(this.atmobApi, this.accountRepository) {
  39. print('$tag....init');
  40. refreshNewDiscountGoodsInfoList();
  41. }
  42. void refreshNewDiscountGoodsInfoList() {
  43. goodsInfoFuture?.cancel();
  44. goodsInfoFuture = AsyncUtil.retryWithExponentialBackoff(
  45. () => getMemberNewUserGoodsList(),
  46. 4,
  47. predicate: (error) {
  48. if (error is ServerErrorException) {
  49. return error.code != ErrorCode.noLoginError;
  50. }
  51. return true;
  52. },
  53. );
  54. goodsInfoFuture?.catchError((error) {
  55. ErrorHandler.toastError(error);
  56. });
  57. }
  58. Future<ItemListResponse> getGoodsInfoList() async {
  59. return await atmobApi
  60. .getGoodsList(AppBaseRequest())
  61. .then(HttpHandler.handle(true));
  62. }
  63. Future<MemberNewUserResponse> getMemberNewUserGoodsList() async {
  64. return await atmobApi
  65. .getMemberUserResponse(AppBaseRequest())
  66. .then(HttpHandler.handle(true))
  67. .then((data) {
  68. _newDiscountGoodsInfoList.clear();
  69. _newDiscountPayWayList.clear();
  70. _charactersList.clear();
  71. if (data.goodsInfoList?.isNotEmpty == true) {
  72. _newDiscountGoodsInfoList.addAll(data.goodsInfoList!);
  73. }
  74. if (data.payInfoList?.isNotEmpty == true) {
  75. _newDiscountPayWayList.addAll(data.payInfoList!);
  76. }
  77. if (data.characterInfos?.isNotEmpty == true) {
  78. _charactersList.addAll(data.characterInfos!);
  79. }
  80. return data;
  81. });
  82. }
  83. Future<OrderPayResponse> submitAndRequestPay({
  84. required int goodsId,
  85. required int payPlatform,
  86. required int payMethod,
  87. }) {
  88. return atmobApi
  89. .orderPay(
  90. OrderPayRequest(
  91. goodsId: goodsId,
  92. payPlatform: payPlatform,
  93. payMethod: payMethod,
  94. ),
  95. )
  96. .then(HttpHandler.handle(false));
  97. }
  98. Future<int> orderStatus(String outTradeNo, {String? receiptData}) {
  99. return atmobApi
  100. .orderStatus(OrderStatusRequest(outTradeNo, receiptData))
  101. .then(HttpHandler.handle(false))
  102. .then((data) {
  103. if (data.payStatus == PaymentStatus.payStatusSuccess) {
  104. accountRepository.refreshUserInfo();
  105. }
  106. return data.payStatus;
  107. });
  108. }
  109. Future<ItemRetentionResponse> getItemRetention() {
  110. return atmobApi
  111. .getItemRetention(AppBaseRequest())
  112. .then(HttpHandler.handle(true));
  113. }
  114. /// 检查订阅状态(IOS使用),验证用户是否可发起支付
  115. Future<void> subscribeCheck(
  116. int payPlatform,
  117. int payMethod,
  118. String receiptData,
  119. ) {
  120. return atmobApi
  121. .subscribeCheck(
  122. SubscribeCheckRequest(payPlatform, payMethod, receiptData),
  123. )
  124. .then(HttpHandler.handle(true));
  125. }
  126. /// 恢复订阅(IOS使用)
  127. Future<void> subscribeResume(
  128. int payPlatform,
  129. int payMethod,
  130. String receiptData,
  131. ) {
  132. return atmobApi
  133. .subscribeResume(
  134. SubscribeResumeRequest(payPlatform, payMethod, receiptData),
  135. )
  136. .then(HttpHandler.handle(true));
  137. }
  138. /// 检查当前是否有生效中的支付宝签约协议
  139. Future<MemberAgreementCheckResponse> checkMemberAgreement() async {
  140. return await atmobApi
  141. .memberAgreementCheck(AppBaseRequest())
  142. .then(HttpHandler.handle(true))
  143. .then((response) {
  144. hasAutoRenewal.value = response.exist;
  145. return response;
  146. });
  147. }
  148. /// 解约支付宝签约协议
  149. Future<void> unSignMemberAgreement() async {
  150. return await atmobApi
  151. .memberAgreementUnSign(AppBaseRequest())
  152. .then(HttpHandler.handle(true));
  153. }
  154. static StoreRepository getInstance() => getIt.get<StoreRepository>();
  155. }