store_repository.dart 3.9 KB

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