function_upgrade_page.dart
13.7 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:fluwx/fluwx.dart';
import 'package:one_key/models/seller_version_model.dart';
import 'package:one_key/models/wechat_pay_model.dart';
import 'package:one_key/provider/collect_store_provider.dart';
import 'package:one_key/provider/user_model.dart';
import 'package:one_key/res/colors.dart';
import 'package:one_key/utils/event_bus.dart';
import 'package:one_key/utils/utils.dart';
import 'package:one_key/widgets/system_app_bar.dart';
import 'package:provider/provider.dart';
class FunctionUpgradePage extends StatefulWidget {
const FunctionUpgradePage({super.key, required this.collectStoreProvider});
final CollectStoreProvider collectStoreProvider;
@override
State<FunctionUpgradePage> createState() => _FunctionUpgradePageState();
}
class _FunctionUpgradePageState extends State<FunctionUpgradePage> {
final List<_UpgradeModule> _modules = [];
bool _loading = true;
bool _loadFailed = false;
late FluwxCancelable _wxPay;
final Fluwx _fluwx = Fluwx();
_UpgradeModule? get _selectedModule {
for (final module in _modules) {
if (module.selected) {
return module;
}
}
return null;
}
double get _total => _selectedModule?.price ?? 0;
bool get _hasSelection => _selectedModule != null;
@override
void initState() {
super.initState();
Utils.changeStatusColor(color: Colors.transparent);
_wxPay = _fluwx.addSubscriber((res) {
_wxPay.cancel();
if (res is WeChatPaymentResponse) {
if (res.errCode == 0) {
_handlePaySuccess();
} else if (res.errCode == -1) {
Utils.showToast('支付错误');
} else if (res.errCode == -2) {
Utils.showToast('您已取消支付');
}
}
});
WidgetsBinding.instance.addPostFrameCallback((_) {
_loadModules();
});
}
Future<void> _loadModules() async {
setState(() {
_loading = true;
_loadFailed = false;
});
final List<SellerVersionSunModel>? list =
await widget.collectStoreProvider.getFunctionUpgradeList(context);
if (!mounted) {
return;
}
setState(() {
_modules.clear();
if (list != null) {
for (final item in list) {
final _UpgradeModule? module = _mapModule(item);
if (module != null) {
module.selected = false;
_modules.add(module);
}
}
}
_loading = false;
_loadFailed = list == null;
});
}
_UpgradeModule? _mapModule(SellerVersionSunModel item) {
final String? vid = item.vid;
final String? name = item.vname;
if (vid == null || name == null || name.trim().isEmpty) {
return null;
}
final String vtype = item.vtype ?? '';
final String desc;
if (vtype == '1') {
desc = '包含现有全部功能组件模块以及未来扩展的功能模块。';
} else if (vtype == '3') {
desc = '$name为独立功能模块,开通后有效期一年。';
} else {
desc = '$name套餐,开通后有效期一年。';
}
return _UpgradeModule(
vid: vid,
name: name,
desc: desc,
price: item.price ?? 0,
);
}
void _toggleModule(int index) {
final bool selected = _modules[index].selected;
setState(() {
for (final module in _modules) {
module.selected = false;
}
_modules[index].selected = !selected;
});
}
Future<void> _pay() async {
final _UpgradeModule? selected = _selectedModule;
if (selected == null) {
Utils.showToast('请先选择需要开通的模块');
return;
}
if (selected.price <= 0) {
Utils.showToast('当前模块暂不支持购买');
return;
}
final bool isInstalled = await _fluwx.isWeChatInstalled;
if (!isInstalled) {
Utils.showToast('请先安装微信');
return;
}
final String? sellerid =
Provider.of<UserModel>(context, listen: false).user?.sellerid;
if (sellerid == null || sellerid.isEmpty) {
Utils.showToast('商家信息异常');
return;
}
final bool success = await widget.collectStoreProvider.appPayJiSuVersion(
context,
{'sellerid': sellerid, 'vid': selected.vid},
);
if (!success || !mounted) {
return;
}
final WechatPaySunModel? sunModel = widget.collectStoreProvider.sunModel;
if (sunModel == null ||
sunModel.appId == null ||
sunModel.package == null ||
sunModel.timeStamp == null ||
sunModel.nonceStr == null) {
Utils.showToast('支付参数异常');
return;
}
final String prepayId = sunModel.package!.substring(10);
final Map<String, String?> signParams = {
'appid': sunModel.appId,
'partnerid': '1489752232',
'prepayid': prepayId,
'timestamp': sunModel.timeStamp,
'noncestr': sunModel.nonceStr,
'package': 'Sign=WXPay',
};
final String sign = Utils.keySort(signParams);
_fluwx
.pay(
which: Payment(
appId: sunModel.appId!,
partnerId: '1489752232',
prepayId: prepayId,
packageValue: 'Sign=WXPay',
nonceStr: sunModel.nonceStr!,
timestamp: int.parse(sunModel.timeStamp!),
sign: sign,
),
)
.then((data) {
print('version pay: $data');
}).catchError((error) {
print(error.toString());
Utils.showToast('支付调起失败');
});
}
Future<void> _handlePaySuccess() async {
Utils.showToast('购买成功');
await widget.collectStoreProvider
.refreshSellerVersion(context, showLoading: false);
EventBus().emit('refreshSellerVersion');
if (!mounted) {
return;
}
if (Navigator.canPop(context)) {
Navigator.pop(context);
}
}
String _formatPrice(double price) {
if (price == price.roundToDouble()) {
return price.toInt().toString();
}
return price.toStringAsFixed(2);
}
@override
void dispose() {
_wxPay.cancel();
EventBus().emit('changeStatusColor');
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: MyColours.color_F0F0F0,
appBar: SystemAppBar('功能升级'),
body: Column(
children: [
Expanded(child: _buildBody()),
_buildBottomBar(),
],
),
);
}
Widget _buildBody() {
if (_loading) {
return const Center(
child: CircularProgressIndicator(color: MyColours.color_529EA4),
);
}
return SingleChildScrollView(
padding: EdgeInsets.all(ScreenUtil().setWidth(24)),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
width: double.infinity,
padding: EdgeInsets.symmetric(
horizontal: ScreenUtil().setWidth(24),
vertical: ScreenUtil().setWidth(18),
),
decoration: BoxDecoration(
color: MyColours.color_529EA4,
borderRadius: BorderRadius.circular(
ScreenUtil().setWidth(10),
),
),
child: Text(
'请选择购买的组件模块,所有模块购买激活后均有一年有效期。',
style: TextStyle(
color: MyColours.color_ffffff,
fontSize: ScreenUtil().setSp(26),
height: 1.5,
),
),
),
SizedBox(height: ScreenUtil().setWidth(24)),
if (_loadFailed)
_buildRetry()
else if (_modules.isEmpty)
_buildEmpty()
else
for (int index = 0; index < _modules.length; index++)
_buildModuleCard(index),
],
),
);
}
Widget _buildRetry() {
return Container(
width: double.infinity,
padding: EdgeInsets.symmetric(vertical: ScreenUtil().setWidth(80)),
alignment: Alignment.center,
child: Column(
children: [
Text(
'加载失败,请稍后重试',
style: TextStyle(
color: MyColours.color_999999,
fontSize: ScreenUtil().setSp(26),
),
),
SizedBox(height: ScreenUtil().setWidth(24)),
GestureDetector(
onTap: _loadModules,
child: Container(
padding: EdgeInsets.symmetric(
horizontal: ScreenUtil().setWidth(40),
vertical: ScreenUtil().setWidth(16),
),
decoration: BoxDecoration(
color: MyColours.color_529EA4,
borderRadius: BorderRadius.circular(
ScreenUtil().setWidth(8),
),
),
child: Text(
'重新加载',
style: TextStyle(
color: MyColours.color_ffffff,
fontSize: ScreenUtil().setSp(26),
),
),
),
),
],
),
);
}
Widget _buildEmpty() {
return Container(
width: double.infinity,
padding: EdgeInsets.symmetric(vertical: ScreenUtil().setWidth(80)),
alignment: Alignment.center,
child: Text(
'暂无可升级的功能',
style: TextStyle(
color: MyColours.color_999999,
fontSize: ScreenUtil().setSp(26),
),
),
);
}
Widget _buildModuleCard(int index) {
final _UpgradeModule module = _modules[index];
final bool selected = module.selected;
return GestureDetector(
onTap: () => _toggleModule(index),
child: Container(
margin: EdgeInsets.only(bottom: ScreenUtil().setWidth(20)),
padding: EdgeInsets.all(ScreenUtil().setWidth(24)),
decoration: BoxDecoration(
color: MyColours.color_ffffff,
borderRadius: BorderRadius.circular(ScreenUtil().setWidth(12)),
border: Border.all(
color: selected ? MyColours.color_529EA4 : MyColours.color_d9d9d9,
width: ScreenUtil().setWidth(2),
),
),
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Image.asset(
Utils.getImgPath(selected ? 'base_radio_sel' : 'base_radio_un'),
width: ScreenUtil().setWidth(36),
height: ScreenUtil().setWidth(36),
),
SizedBox(width: ScreenUtil().setWidth(18)),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Expanded(
child: Text(
module.name,
style: TextStyle(
color: MyColours.color_353535,
fontSize: ScreenUtil().setSp(30),
fontWeight: FontWeight.bold,
),
),
),
SizedBox(width: ScreenUtil().setWidth(16)),
Text(
'¥${_formatPrice(module.price)}',
style: TextStyle(
color: MyColours.color_529EA4,
fontSize: ScreenUtil().setSp(30),
fontWeight: FontWeight.bold,
),
),
],
),
SizedBox(height: ScreenUtil().setWidth(12)),
Text(
module.desc,
style: TextStyle(
color: MyColours.color_999999,
fontSize: ScreenUtil().setSp(24),
height: 1.4,
),
),
],
),
),
],
),
),
);
}
Widget _buildBottomBar() {
return Container(
color: const Color(0xFF98C0C8),
padding: EdgeInsets.fromLTRB(
ScreenUtil().setWidth(24),
ScreenUtil().setWidth(16),
ScreenUtil().setWidth(24),
ScreenUtil().setWidth(16),
),
child: SafeArea(
top: false,
child: Row(
children: [
Text(
'¥${_formatPrice(_total)}',
style: TextStyle(
color: MyColours.color_ffffff,
fontSize: ScreenUtil().setSp(36),
fontWeight: FontWeight.bold,
),
),
const Spacer(),
GestureDetector(
onTap: _hasSelection ? _pay : null,
child: Container(
width: ScreenUtil().setWidth(220),
height: ScreenUtil().setWidth(78),
alignment: Alignment.center,
decoration: BoxDecoration(
color: MyColours.color_ffffff,
borderRadius:
BorderRadius.circular(ScreenUtil().setWidth(39)),
),
child: Text(
'立即支付',
style: TextStyle(
color: _hasSelection
? MyColours.color_515151
: MyColours.color_999999,
fontSize: ScreenUtil().setSp(30),
fontWeight: FontWeight.bold,
),
),
),
),
],
),
),
);
}
}
class _UpgradeModule {
_UpgradeModule({
required this.vid,
required this.name,
required this.desc,
required this.price,
});
final String vid;
final String name;
final String desc;
final double price;
bool selected = false;
}