store_repository.dart 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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/response/item_retention_response.dart';
  16. import '../api/response/member_agreement_check_response.dart';
  17. import '../api/response/member_new_user_response.dart';
  18. import '../bean/character_info.dart';
  19. import '../bean/goods_info.dart';
  20. import '../bean/pay_way_info.dart';
  21. import '../consts/error_code.dart';
  22. import 'account_repository.dart';
  23. @lazySingleton
  24. class StoreRepository {
  25. final tag = 'StoreRepository';
  26. final AtmobApi atmobApi;
  27. final AccountRepository accountRepository;
  28. CancelableFuture? goodsInfoFuture;
  29. final RxList<GoodsInfo> _newDiscountGoodsInfoList = <GoodsInfo>[].obs;
  30. RxList<GoodsInfo> get newDiscountGoodsInfoList => _newDiscountGoodsInfoList;
  31. final RxList<PayWayInfo> _newDiscountPayWayList = <PayWayInfo>[].obs;
  32. RxList<PayWayInfo> get newDiscountPayWayList => _newDiscountPayWayList;
  33. final RxList<CharacterInfo> _charactersList = <CharacterInfo>[].obs;
  34. RxList<CharacterInfo> get charactersList => _charactersList;
  35. final RxBool hasAutoRenewal = false.obs;
  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. /// 检查当前是否有生效中的支付宝签约协议
  113. Future<MemberAgreementCheckResponse> checkMemberAgreement() async {
  114. return await atmobApi
  115. .memberAgreementCheck(AppBaseRequest())
  116. .then(HttpHandler.handle(true))
  117. .then((response) {
  118. hasAutoRenewal.value = response.exist;
  119. return response;
  120. });
  121. }
  122. /// 解约支付宝签约协议
  123. Future<void> unSignMemberAgreement() async {
  124. return await atmobApi
  125. .memberAgreementUnSign(AppBaseRequest())
  126. .then(HttpHandler.handle(true));
  127. }
  128. static StoreRepository getInstance() => getIt.get<StoreRepository>();
  129. }