store_repository.dart 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 '../../utils/async_util.dart';
  9. import '../../utils/atmob_log.dart';
  10. import '../../utils/error_handler.dart';
  11. import '../../utils/payment_status_manager.dart';
  12. import '../api/request/order_pay_request.dart';
  13. import '../api/request/order_status_request.dart';
  14. import '../api/request/subscribe_check_request.dart';
  15. import '../api/request/subscribe_resume_request.dart';
  16. import '../api/response/item_retention_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. StoreRepository(this.atmobApi, this.accountRepository) {
  36. print('$tag....init');
  37. refreshNewDiscountGoodsInfoList();
  38. }
  39. void refreshNewDiscountGoodsInfoList() {
  40. goodsInfoFuture?.cancel();
  41. goodsInfoFuture = AsyncUtil.retryWithExponentialBackoff(
  42. () => getMemberNewUserGoodsList(),
  43. 4,
  44. predicate: (error) {
  45. if (error is ServerErrorException) {
  46. return error.code != ErrorCode.noLoginError;
  47. }
  48. return true;
  49. },
  50. );
  51. goodsInfoFuture?.catchError((error) {
  52. ErrorHandler.toastError(error);
  53. });
  54. }
  55. Future<ItemListResponse> getGoodsInfoList() async {
  56. return await atmobApi
  57. .getGoodsList(AppBaseRequest())
  58. .then(HttpHandler.handle(true));
  59. }
  60. Future<MemberNewUserResponse> getMemberNewUserGoodsList() async {
  61. return await atmobApi
  62. .getMemberUserResponse(AppBaseRequest())
  63. .then(HttpHandler.handle(true))
  64. .then((data) {
  65. _newDiscountGoodsInfoList.clear();
  66. _newDiscountPayWayList.clear();
  67. _charactersList.clear();
  68. if (data.goodsInfoList?.isNotEmpty == true) {
  69. _newDiscountGoodsInfoList.addAll(data.goodsInfoList!);
  70. }
  71. if (data.payInfoList?.isNotEmpty == true) {
  72. _newDiscountPayWayList.addAll(data.payInfoList!);
  73. }
  74. if (data.characterInfos?.isNotEmpty == true) {
  75. _charactersList.addAll(data.characterInfos!);
  76. }
  77. return data;
  78. });
  79. }
  80. Future<OrderPayResponse> submitAndRequestPay({
  81. required int goodsId,
  82. required int payPlatform,
  83. required int payMethod,
  84. }) {
  85. return atmobApi
  86. .orderPay(
  87. OrderPayRequest(
  88. goodsId: goodsId,
  89. payPlatform: payPlatform,
  90. payMethod: payMethod,
  91. ),
  92. )
  93. .then(HttpHandler.handle(false));
  94. }
  95. Future<int> orderStatus(String outTradeNo, {String? receiptData}) {
  96. return atmobApi
  97. .orderStatus(OrderStatusRequest(outTradeNo, receiptData))
  98. .then(HttpHandler.handle(false))
  99. .then((data) {
  100. if (data.payStatus == PaymentStatus.payStatusSuccess) {
  101. accountRepository.refreshUserInfo();
  102. }
  103. return data.payStatus;
  104. });
  105. }
  106. Future<ItemRetentionResponse> getItemRetention() {
  107. return atmobApi
  108. .getItemRetention(AppBaseRequest())
  109. .then(HttpHandler.handle(true));
  110. }
  111. /// 检查订阅状态(IOS使用),验证用户是否可发起支付
  112. Future<void> subscribeCheck(
  113. int payPlatform,
  114. int payMethod,
  115. String receiptData,
  116. ) {
  117. return atmobApi
  118. .subscribeCheck(
  119. SubscribeCheckRequest(payPlatform, payMethod, receiptData),
  120. )
  121. .then(HttpHandler.handle(true));
  122. }
  123. /// 恢复订阅(IOS使用)
  124. Future<void> subscribeResume(
  125. int payPlatform,
  126. int payMethod,
  127. String receiptData,
  128. ) {
  129. return atmobApi
  130. .subscribeResume(
  131. SubscribeResumeRequest(payPlatform, payMethod, receiptData),
  132. )
  133. .then(HttpHandler.handle(true));
  134. }
  135. }