You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

261 lines
8.2 KiB

import 'dart:ui';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:image_pickers/image_pickers.dart';
import 'package:scan/scan.dart';
import '../../retrofit/business_api.dart';
import '../../retrofit/data/base_data.dart';
import '../../retrofit/data/ticket_details.dart';
import '../../utils/business_instance.dart';
import '../generated/l10n.dart';
import '../utils/font_weight.dart';
import '../view_widget/settlement_tips_dialog.dart';
class ScanCodePage extends StatefulWidget {
final Map<String, dynamic> arguments;
ScanCodePage({this.arguments});
@override
State<StatefulWidget> createState() {
return _ScanCodePage();
}
}
class _ScanCodePage extends State<ScanCodePage> {
ScanController controller = ScanController();
final screenWidth = window.physicalSize.width;
final screenHeight = window.physicalSize.height;
BusinessApiService businessService;
TicketDetails ticketDetails;
@override
void initState() {
super.initState();
}
@override
void dispose() {
if (this.controller != null) {
this.controller.pause();
}
super.dispose();
}
///票券详情
queryTicketDetails(code) async {
if (businessService == null) {
businessService = BusinessApiService(Dio(),
context: context,
token: BusinessInstance.instance.businessToken,
tenant: BusinessInstance.instance.businessTenant,
storeId: widget.arguments["storeId"]);
}
BaseData<TicketDetails> baseData =
await businessService.ticketCode(code).catchError((error) {});
if (baseData != null && baseData.isSuccess) {
ticketDetails = baseData.data;
Navigator.of(context)
.popAndPushNamed('/router/order_write_off', arguments: {
"ticketDetails": ticketDetails,
});
} else {
Navigator.of(context).pop();
SmartDialog.showToast(baseData.msg, alignment: Alignment.center);
}
}
///会员充值支付
rechargePay(authCode) async {
try{
EasyLoading.show(
status: S.current.zhengzaijiazai,
maskType: EasyLoadingMaskType.black);
if (businessService == null) {
businessService = BusinessApiService(Dio(),
context: context,
token: BusinessInstance.instance.businessToken,
tenant: BusinessInstance.instance.businessTenant,
storeId: widget.arguments["storeId"]);
}
BaseData baseData = await businessService.posMemberRecharge({
"rechargePreferentialId": widget?.arguments["rechargePreferentialId"] ?? "",
"memberSourceId": widget?.arguments["memberSourceId"] ?? "",
"source": widget?.arguments["source"] ?? 1,
"authCode": authCode ?? null
}).catchError((error) {});
if (baseData != null && baseData.isSuccess) {
Navigator.of(context).pop(1);
Navigator.of(context).popAndPushNamed('/router/pay_success_page',arguments: {
"rechargeId":baseData.data,
"rechargeMoney":widget?.arguments["rechargeMoney"],
"payIndex":int.tryParse(widget?.arguments["source"]),
});
} else {
Navigator.of(context).pop([baseData.msg,baseData.data]);
}}finally{
EasyLoading.dismiss();
}
}
///收银台支付
cashierPay(authCode) async {
try{
EasyLoading.show(
status: S.current.zhengzaijiazai,
maskType: EasyLoadingMaskType.black);
if (businessService == null) {
businessService = BusinessApiService(Dio(),
context: context,
token: BusinessInstance.instance.businessToken,
tenant: BusinessInstance.instance.businessTenant,
storeId: widget.arguments["storeId"]);
}
BaseData baseData = await businessService.prePayOrder({
"orderId": widget?.arguments["orderId"] ?? "",
"payChannel": widget?.arguments["payChannel"] ?? "",
"mid":widget?.arguments["mid"] ?? "",
"authCode": authCode ?? null
}).catchError((error) {});
if (baseData != null && baseData.isSuccess) {
Navigator.of(context).pop(1);
SmartDialog.show(
widget: SettlementTips(
() {},
text: baseData.data,
color: Color(0xFF30415B),
));
} else {
Navigator.of(context).pop([baseData.msg,baseData.data]);
}}finally{
EasyLoading.dismiss();
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Stack(
children: [
Positioned(
child: Column(
children: [
Expanded(
child: ScanView(
controller: controller,
scanAreaScale: 0.7,
scanLineColor: Colors.green.shade400,
onCapture: (data) {
if (data != null &&
data != "" &&
widget.arguments["scanCodeType"] == null) {
queryTicketDetails(data);
} else {
if(widget.arguments["scanCodeType"] == "充值支付"){
rechargePay(data);
}else{
cashierPay(data);
}
}
},
),
flex: 1,
),
],
),
top: 0,
bottom: 0,
left: 0,
right: 0,
),
Positioned(
child: Container(
margin: EdgeInsets.only(left: 12.w, right: 16.w, top: 16.h),
child: Row(
mainAxisAlignment: MainAxisAlignment.end,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
GestureDetector(
behavior: HitTestBehavior.opaque,
onTap: () {
Navigator.of(context).pop();
},
child: Container(
padding: EdgeInsets.symmetric(horizontal: 10.w),
alignment: Alignment.centerLeft,
child: Image.asset(
"assets/image/return_left.webp",
fit: BoxFit.fill,
color: Colors.white,
),
),
),
Spacer(),
],
),
),
top: MediaQuery.of(context).padding.top,
left: 0,
right: 0,
),
Positioned(
child: GestureDetector(
behavior: HitTestBehavior.opaque,
onTap: () {
pickImage();
},
child: Container(
decoration: BoxDecoration(
color: Colors.white.withAlpha(35),
borderRadius: BorderRadius.circular(100),
),
padding: EdgeInsets.all(8.w),
margin: EdgeInsets.only(left: 12.w, right: 16.w, top: 16.h),
child: Icon(
Icons.image,
size: 25.w,
color: Colors.white,
),
),
),
right: 0,
bottom: 50.h,
),
],
),
);
}
pickImage() async {
List<Media> medias = await ImagePickers.pickerPaths(
galleryMode: GalleryMode.image,
selectCount: 1,
showGif: true,
showCamera: false,
compressSize: 500,
uiConfig: UIConfig(
uiThemeColor: Color(0xFFFFFFFF),
),
cropConfig: CropConfig(
enableCrop: false,
),
);
if (medias != null && medias.length > 0) {
String result = await Scan.parse(medias[0].path);
if (result != null && result != "") {
queryTicketDetails(result);
} else {
SmartDialog.show(
widget: SettlementTips(
() {},
text: "照片中未识别到二维码/条码",
color: Color(0xFF30415B),
));
}
}
}
}