store_repository.dart 5.4 KB

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