store_repository.dart 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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_resume_request.dart';
  15. import '../api/response/item_retention_response.dart';
  16. import '../api/response/member_new_user_response.dart';
  17. import '../bean/character_info.dart';
  18. import '../bean/goods_info.dart';
  19. import '../bean/pay_way_info.dart';
  20. import '../consts/error_code.dart';
  21. import 'account_repository.dart';
  22. @lazySingleton
  23. class StoreRepository {
  24. final tag = 'StoreRepository';
  25. final AtmobApi atmobApi;
  26. final AccountRepository accountRepository;
  27. CancelableFuture? goodsInfoFuture;
  28. final RxList<GoodsInfo> _newDiscountGoodsInfoList = <GoodsInfo>[].obs;
  29. RxList<GoodsInfo> get newDiscountGoodsInfoList => _newDiscountGoodsInfoList;
  30. final RxList<PayWayInfo> _newDiscountPayWayList = <PayWayInfo>[].obs;
  31. RxList<PayWayInfo> get newDiscountPayWayList => _newDiscountPayWayList;
  32. final RxList<CharacterInfo> _charactersList = <CharacterInfo>[].obs;
  33. RxList<CharacterInfo> get charactersList => _charactersList;
  34. StoreRepository(this.atmobApi, this.accountRepository) {
  35. print('$tag....init');
  36. refreshNewDiscountGoodsInfoList();
  37. }
  38. void refreshNewDiscountGoodsInfoList() {
  39. goodsInfoFuture?.cancel();
  40. goodsInfoFuture = AsyncUtil.retryWithExponentialBackoff(
  41. () => getMemberNewUserGoodsList(),
  42. 4,
  43. predicate: (error) {
  44. if (error is ServerErrorException) {
  45. return error.code != ErrorCode.noLoginError;
  46. }
  47. return true;
  48. },
  49. );
  50. goodsInfoFuture?.catchError((error) {
  51. ErrorHandler.toastError(error);
  52. });
  53. }
  54. Future<ItemListResponse> getGoodsInfoList() async {
  55. return await atmobApi
  56. .getGoodsList(AppBaseRequest())
  57. .then(HttpHandler.handle(true));
  58. }
  59. Future<MemberNewUserResponse> getMemberNewUserGoodsList() async {
  60. return await atmobApi
  61. .getMemberUserResponse(AppBaseRequest())
  62. .then(HttpHandler.handle(true))
  63. .then((data) {
  64. _newDiscountGoodsInfoList.clear();
  65. _newDiscountPayWayList.clear();
  66. _charactersList.clear();
  67. if (data.goodsInfoList?.isNotEmpty == true) {
  68. _newDiscountGoodsInfoList.addAll(data.goodsInfoList!);
  69. }
  70. if (data.payInfoList?.isNotEmpty == true) {
  71. _newDiscountPayWayList.addAll(data.payInfoList!);
  72. }
  73. if (data.characterInfos?.isNotEmpty == true) {
  74. _charactersList.addAll(data.characterInfos!);
  75. }
  76. return data;
  77. });
  78. }
  79. Future<OrderPayResponse> submitAndRequestPay({
  80. required int goodsId,
  81. required int payPlatform,
  82. required int payMethod,
  83. }) {
  84. return atmobApi
  85. .orderPay(
  86. OrderPayRequest(
  87. goodsId: goodsId,
  88. payPlatform: payPlatform,
  89. payMethod: payMethod,
  90. ),
  91. )
  92. .then(HttpHandler.handle(false));
  93. }
  94. Future<int> orderStatus(String outTradeNo, {String? receiptData}) {
  95. return atmobApi
  96. .orderStatus(OrderStatusRequest(outTradeNo, receiptData))
  97. .then(HttpHandler.handle(false))
  98. .then((data) {
  99. if (data.payStatus == PaymentStatus.payStatusSuccess) {
  100. accountRepository.refreshUserInfo();
  101. }
  102. return data.payStatus;
  103. });
  104. }
  105. Future<ItemRetentionResponse> getItemRetention() {
  106. return atmobApi
  107. .getItemRetention(AppBaseRequest())
  108. .then(HttpHandler.handle(true));
  109. }
  110. resume(int payPlatform, int payMethod, String receiptData) {
  111. return atmobApi
  112. .resume(SubscribeResumeRequest(payPlatform, payMethod, receiptData))
  113. .then(HttpHandler.handle(true));
  114. }
  115. }