bloc.dart
4.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
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<ShoppingEvent, ShoppingState> {
ProductEntity? _productData;
ProductEntity? get productData => _productData;
PaymentChannel _curPaymentChannel = PaymentChannel.wechatPay;
PaymentChannel get curPaymentChannel => _curPaymentChannel;
ShoppingBloc(this._productData) : super(ShoppingState().init()) {
//页面初始化时刻
on<InitEvent>(_init);
on<ChangePaymentChannelEvent>(_changePaymentChannel);
on<DoPayEvent>(_startPay);
}
void _init(InitEvent event, Emitter<ShoppingState> emit) async {
//处理一些初始化操作,然后刷新界面
emit(state.clone());
}
/// 更换支付渠道
void _changePaymentChannel(ChangePaymentChannelEvent event,
Emitter<ShoppingState> emitter) async {
Log.d("_curPaymentChannel=$_curPaymentChannel, event.paymentChannel=${event.paymentChannel}");
if (_curPaymentChannel != event.paymentChannel) {
_curPaymentChannel = event.paymentChannel;
emitter(PaymentChannelChangeState());
}
}
void _startPay(DoPayEvent event,
Emitter<ShoppingState> 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<String, dynamic> 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<String, dynamic> 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;
}