import 'package:bloc/bloc.dart'; import 'package:fluwx/fluwx.dart'; import 'package:tobias/tobias.dart'; import 'package:wow_english/generated/json/base/json_convert_content.dart'; import 'package:wow_english/models/product_entity.dart'; import '../../common/request/dao/shop_dao.dart'; import '../../common/request/exception.dart'; import '../../utils/loading.dart'; import '../../utils/log_util.dart'; import '../../utils/toast_util.dart'; import 'event.dart'; import 'state.dart'; class ShoppingBloc extends Bloc { ProductEntity? _productData; ProductEntity? get productData => _productData; PaymentChannel _curPaymentChannel = PaymentChannel.wechatPay; PaymentChannel get curPaymentChannel => _curPaymentChannel; ShoppingBloc(this._productData) : super(ShoppingState().init()) { //页面初始化时刻 on(_init); on(_changePaymentChannel); on(_startPay); } void _init(InitEvent event, Emitter emit) async { //处理一些初始化操作,然后刷新界面 emit(state.clone()); } /// 更换支付渠道 void _changePaymentChannel(ChangePaymentChannelEvent event, Emitter emitter) async { Log.d("_curPaymentChannel=$_curPaymentChannel, event.paymentChannel=${event.paymentChannel}"); if (_curPaymentChannel != event.paymentChannel) { _curPaymentChannel = event.paymentChannel; emitter(PaymentChannelChangeState()); } } void _startPay(DoPayEvent event, Emitter emitter) async { Log.d("开始支付 ${event.productEntity} ${event.paymentChannel}"); //如果event.productEntity为空,中断流程并toast提示 if (event.productEntity == null) { showToast("商品信息为空"); return; } final productEntitySafely = event.productEntity!; try { await loading(() async { final Map orderInfo = await ShopDao.createOrder(productEntitySafely); Log.d("orderInfo $orderInfo"); final String? orderNo = orderInfo.getOrNull("orderNo"); if (orderNo == null) { showToast("订单创建失败"); return; } Log.d("orderNo $orderNo"); if (event.paymentChannel == PaymentChannel.wechatPay) { Fluwx fluwx = Fluwx(); fluwx.registerApi(appId: "wxd930ea5d5a228f5f", universalLink: "https://app-api.wowenglish.com.cn/.well-known/apple-app-site-association"); // fluwx.pay( // which: Payment( // appId: _orderInfo['appid'].toString(), // partnerId: _orderInfo['partnerid'].toString(), // prepayId: _orderInfo['prepayid'].toString(), // packageValue: _orderInfo['package'].toString(), // nonceStr: _orderInfo['noncestr'].toString(), // timestamp: _orderInfo['timestamp'], // sign: _orderInfo['sign'].toString(), // )); } else { final Map aliPayOrderInfo = await ShopDao.getAliPayToken(orderNo); Log.d("aliPayOrderInfo=$aliPayOrderInfo type=${aliPayOrderInfo.runtimeType}"); final String? aliPayInfo = aliPayOrderInfo.getOrNull("token"); if (aliPayInfo == null) { showToast("支付宝订单创建失败"); return; } Log.d("aliPayInfo=$aliPayInfo"); ///打印aliPayOrderInfo的type Tobias tobias = Tobias(); final Map aliPayResult = await tobias.pay(aliPayInfo); Log.d("aliPayResult=$aliPayResult"); // 判断resultStatus 为9000则代表支付成功 if (aliPayResult.getOrNull("resultStatus") == "9000") { showToast("支付成功"); emit(PaySuccessState()); } else { showToast("支付失败"); } } }); } catch (e) { if (e is ApiException) { showToast(e.message ?? '请求失败,请检查网络连接'); } } } } enum PaymentChannel { wechatPay(1, "微信支付"), aliPay(2, "支付宝支付"); const PaymentChannel(this.payChannelType, this.payChannelName); final int payChannelType; final String payChannelName; }