From e0012622d86b8fba10b39080bf194dd34a4bc043 Mon Sep 17 00:00:00 2001 From: datang Date: Fri, 12 Aug 2022 09:57:23 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=A0=E9=99=A4=E5=90=8E=E5=90=88=E5=B9=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/gen/assets.gen.dart | 3 + lib/main.dart | 157 ++++++++------ lib/model/pay/pay_model.dart | 73 +++++++ lib/model/pay/pay_model.g.dart | 28 +++ lib/model/pay/wx_pay_model.dart | 37 ++++ lib/model/pay/wx_pay_model.g.dart | 16 ++ lib/ui/home/content_page.dart | 58 +++++- lib/ui/login/login_page.dart | 2 +- lib/ui/user/user_page.dart | 46 +++-- lib/utils/pay_util.dart | 332 +++++++++++++++--------------- pubspec.lock | 172 +++++++++++++--- pubspec.yaml | 17 +- 12 files changed, 653 insertions(+), 288 deletions(-) create mode 100644 lib/model/pay/pay_model.dart create mode 100644 lib/model/pay/pay_model.g.dart create mode 100644 lib/model/pay/wx_pay_model.dart create mode 100644 lib/model/pay/wx_pay_model.g.dart diff --git a/lib/gen/assets.gen.dart b/lib/gen/assets.gen.dart index 377f5e6..06adb78 100644 --- a/lib/gen/assets.gen.dart +++ b/lib/gen/assets.gen.dart @@ -35,6 +35,9 @@ class $AssetsIconsGen { /// File path: assets/icons/privacy.png AssetGenImage get privacy => const AssetGenImage('assets/icons/privacy.png'); + /// File path: assets/icons/sms.png + AssetGenImage get sms => const AssetGenImage('assets/icons/sms.png'); + /// File path: assets/icons/switch1.png AssetGenImage get switch1 => const AssetGenImage('assets/icons/switch1.png'); diff --git a/lib/main.dart b/lib/main.dart index 8cc681f..f528395 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,90 +1,110 @@ + +import 'dart:async'; import 'package:bot_toast/bot_toast.dart'; +import 'package:call_log/call_log.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_background_service/flutter_background_service.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart'; +import 'package:flutter_sms/flutter_sms.dart'; import 'package:get/get_navigation/src/root/get_material_app.dart'; +import 'package:power_logger/power_logger.dart'; + import 'package:project_telephony/providers/user_provider.dart'; import 'package:project_telephony/ui/tab_navigator.dart'; -import 'package:project_telephony/utils/hive_store.dart'; import 'package:provider/provider.dart'; +import 'package:telephony/telephony.dart'; +import 'package:project_telephony/utils/hive_store.dart'; -// import 'package:telephony/telephony.dart'; - -// onBackgroundMessage(SmsMessage message) { -// debugPrint("onBackgroundMessage called"); -// } +void _sendSMS(String message, List recipents) async { + String _result = await sendSMS(message: message, recipients: recipents) + .catchError((onError) { + print(onError); + }); + print(_result); +} void main() async { WidgetsFlutterBinding.ensureInitialized(); - // await initializeService(); + FlutterError.onError = (details) { + LoggerData.addData(details); + FlutterError.presentError(details); + }; + await initializeService(); + WidgetsFlutterBinding.ensureInitialized(); runApp(const MyApp()); - // SystemUiOverlayStyle systemUiOverlayStyle = const SystemUiOverlayStyle( - // statusBarColor: Colors.transparent, //状态栏背景色 - // statusBarIconBrightness: Brightness.dark); //状态栏字体颜色 + } -// Future initializeService() async { -// final service = FlutterBackgroundService(); -// service.setNotificationInfo(title: '短信助手', content: '正在后台运行'); -// await service.configure( -// androidConfiguration: AndroidConfiguration( -// onStart: onStart, -// autoStart: true, -// isForegroundMode: true, -// ), -// iosConfiguration: IosConfiguration( -// autoStart: true, -// onForeground: onStart, -// onBackground: onIosBackground, -// ), -// ); -// } +Future initializeService() async { + final service = FlutterBackgroundService(); + service.setNotificationInfo(title: '短信助手', content: '正在后台运行'); + await service.configure( + androidConfiguration: AndroidConfiguration( + onStart: onStart, + autoStart: true, + isForegroundMode: true, + ), + iosConfiguration: IosConfiguration( + autoStart: true, + onForeground: onStart, + onBackground: onIosBackground, + ), + ); +} void onIosBackground() { WidgetsFlutterBinding.ensureInitialized(); // print('FLUTTER BACKGROUND FETCH'); } -// void onStart() { -// int flag = 0; -// String? phoneNum, callState; -// WidgetsFlutterBinding.ensureInitialized(); -// -// Timer.periodic(const Duration(seconds: 1), (timer) async { -// CallState state = await Telephony.instance.callState; -// callState = state.name; -// // print(callState!+"$flag"); -// if (callState == "IDLE") { -// if (flag != 0) { -// flag = 0; -// final Iterable result = await CallLog.query(); -// phoneNum = result.first.number; -// print(phoneNum); -// Phone.telephony.sendSms( -// to: phoneNum!, -// message: "hello", -// isMultipart: true, -// ); -// phoneNum = result.first.number; -// // print(phoneNum); -// Phone.telephony.sendSms( -// to: phoneNum!, -// message: "hello", -// isMultipart: true, -// ); -// } -// } else if (callState == "RINGING") { -// flag++; -// } else if (callState == "OFFHOOK") { -// flag++; -// } -// }); -// } - -// class Phone { -// static Telephony telephony = Telephony.instance; -// } +void onStart() { + int flag = 0; + String? phoneNum, callState; + WidgetsFlutterBinding.ensureInitialized(); + + Timer.periodic(const Duration(seconds: 1), (timer) async { + CallState state = await Telephony.instance.callState; + callState = state.name; + print(callState!+" $flag"); + if (callState == "IDLE") { + if (flag != 0) { + flag = 0; + // print("object"); + final Iterable result = await CallLog.query(); + phoneNum = result.first.number; + // print(phoneNum); + // String message = "This is a test message!"; + // List recipents = ["10000", "10086"]; + // String _result = await sendSMS(message: message, recipients: recipents, sendDirect: true) + // .catchError((onError) { + // print(onError); + // }); + // print(_result); + phoneNum = result.first.number; + print(phoneNum); + final SmsSendStatusListener listener = (SendStatus status) { + print(status); + }; + Phone.telephony.sendSms( + to: phoneNum!, + message: "hello", + statusListener: listener, + isMultipart: true, + + ); + } + } else if (callState == "RINGING") { + flag++; + } else if (callState == "OFFHOOK") { + flag++; + } + }); +} + +class Phone { + static Telephony telephony = Telephony.instance; +} class MyApp extends StatefulWidget { const MyApp({Key? key}) : super(key: key); @@ -95,7 +115,9 @@ class MyApp extends StatefulWidget { class _MyAppState extends State { String _message = ""; - + // This will not work as the instance will be replaced by + // the one in background. + final telephony = Telephony.instance; @override void initState() { super.initState(); @@ -104,6 +126,7 @@ class _MyAppState extends State { //Hive.initFlutter; await HiveStore.init(); }); + final inbox = Telephony.instance.getInboxSms(); // JPush jPush=JPush(); // jPush.setup( // appKey: "", @@ -154,7 +177,7 @@ class _MyAppState extends State { ChangeNotifierProvider(create: (context) => UserProvider()), ], child: MediaQuery( - data: MediaQueryData.fromWindow(WidgetsBinding.instance!.window), + data: MediaQueryData.fromWindow(WidgetsBinding.instance.window), child: ScreenUtilInit( designSize: const Size(750, 1334), builder: (context, child) { diff --git a/lib/model/pay/pay_model.dart b/lib/model/pay/pay_model.dart new file mode 100644 index 0000000..cad3fa0 --- /dev/null +++ b/lib/model/pay/pay_model.dart @@ -0,0 +1,73 @@ +import 'package:equatable/equatable.dart'; +import 'package:json_annotation/json_annotation.dart'; + +part 'pay_model.g.dart'; + +@JsonSerializable() +class PayModel extends Equatable { + @JsonKey(name: 'alipay_trade_app_pay_response') + final AliPayTradeAppPayResponse aliPayTradeAppPayResponse; + final String sign; + @JsonKey(name: 'sign_type') + final String signType; + + const PayModel({ + required this.aliPayTradeAppPayResponse, + required this.sign, + required this.signType, + }); + + factory PayModel.fromJson(Map json) => + _$PayModelFromJson(json); + + @override + List get props => [aliPayTradeAppPayResponse, sign, signType]; +} + +@JsonSerializable() +class AliPayTradeAppPayResponse extends Equatable { + final String code; + final String msg; + @JsonKey(name: 'app_id') + final String appId; + @JsonKey(name: 'out_trade_no') + final String outTradeNo; + @JsonKey(name: 'trade_no') + final String tradeNo; + @JsonKey(name: 'total_amount') + final String totalAmount; + @JsonKey(name: 'seller_id') + final String sellerId; + final String charset; + final String timestamp; + + const AliPayTradeAppPayResponse({ + required this.code, + required this.msg, + required this.appId, + required this.outTradeNo, + required this.tradeNo, + required this.totalAmount, + required this.sellerId, + required this.charset, + required this.timestamp, + }); + + factory AliPayTradeAppPayResponse.fromJson(Map json) => + _$AliPayTradeAppPayResponseFromJson(json); + + @override + List get props { + return [ + code, + msg, + appId, + outTradeNo, + tradeNo, + totalAmount, + sellerId, + charset, + timestamp, + ]; + } +} diff --git a/lib/model/pay/pay_model.g.dart b/lib/model/pay/pay_model.g.dart new file mode 100644 index 0000000..e5a3963 --- /dev/null +++ b/lib/model/pay/pay_model.g.dart @@ -0,0 +1,28 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +part of 'pay_model.dart'; + +// ************************************************************************** +// JsonSerializableGenerator +// ************************************************************************** + +PayModel _$PayModelFromJson(Map json) => PayModel( + aliPayTradeAppPayResponse: AliPayTradeAppPayResponse.fromJson( + json['alipay_trade_app_pay_response'] as Map), + sign: json['sign'] as String, + signType: json['sign_type'] as String, +); + +AliPayTradeAppPayResponse _$AliPayTradeAppPayResponseFromJson( + Map json) => + AliPayTradeAppPayResponse( + code: json['code'] as String, + msg: json['msg'] as String, + appId: json['app_id'] as String, + outTradeNo: json['out_trade_no'] as String, + tradeNo: json['trade_no'] as String, + totalAmount: json['total_amount'] as String, + sellerId: json['seller_id'] as String, + charset: json['charset'] as String, + timestamp: json['timestamp'] as String, + ); diff --git a/lib/model/pay/wx_pay_model.dart b/lib/model/pay/wx_pay_model.dart new file mode 100644 index 0000000..601f906 --- /dev/null +++ b/lib/model/pay/wx_pay_model.dart @@ -0,0 +1,37 @@ +import 'package:equatable/equatable.dart'; +import 'package:json_annotation/json_annotation.dart'; + +part 'wx_pay_model.g.dart'; + +@JsonSerializable() +class WxPayModel extends Equatable { + final String prepayId; + final String partnerId; + final String timeStamp; + final String nonceStr; + final String package; + final String sign; + + factory WxPayModel.fromJson(Map json) => + _$WxPayModelFromJson(json); + + + const WxPayModel({ + required this.prepayId, + required this.partnerId, + required this.timeStamp, + required this.nonceStr, + required this.package, + required this.sign, + }); + + @override + List get props => [ + prepayId, + partnerId, + timeStamp, + nonceStr, + package, + sign, + ]; +} diff --git a/lib/model/pay/wx_pay_model.g.dart b/lib/model/pay/wx_pay_model.g.dart new file mode 100644 index 0000000..654a14e --- /dev/null +++ b/lib/model/pay/wx_pay_model.g.dart @@ -0,0 +1,16 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +part of 'wx_pay_model.dart'; + +// ************************************************************************** +// JsonSerializableGenerator +// ************************************************************************** + +WxPayModel _$WxPayModelFromJson(Map json) => WxPayModel( + prepayId: json['prepayId'] as String, + partnerId: json['partnerId'] as String, + timeStamp: json['timeStamp'] as String, + nonceStr: json['nonceStr'] as String, + package: json['package'] as String, + sign: json['sign'] as String, +); diff --git a/lib/ui/home/content_page.dart b/lib/ui/home/content_page.dart index 40c025e..9676032 100644 --- a/lib/ui/home/content_page.dart +++ b/lib/ui/home/content_page.dart @@ -1,10 +1,19 @@ + +import 'package:call_log/call_log.dart'; import 'package:flutter/material.dart'; +import 'package:flutter/services.dart'; +import 'package:flutter_sms/flutter_sms.dart'; +import 'package:get_phone_number/get_phone_number.dart'; +import 'package:permission_handler/permission_handler.dart'; import 'package:project_telephony/base/base_style.dart'; import 'package:project_telephony/ui/home/content_details_page.dart'; import 'package:project_telephony/ui/widget/centertipsalterwidget.dart'; import 'package:project_telephony/ui/widget/plone_back_button.dart'; import 'package:project_telephony/utils/headers.dart'; +import 'package:telephony/telephony.dart'; + +import '../user/privacy_rights_page.dart'; class ContentPage extends StatefulWidget { final bool? isAnswer; //true接听false未接听 @@ -60,15 +69,48 @@ class _ContentPageState extends State { textList.setAll(index, {textContent}); }, )); - } else {} + } else { + bool? permissionsGranted = await telephony.requestPhonePermissions; + // print(permissionsGranted); + // if(permissionsGranted!){ + // print(content); + // // sendSms(content); + // telephony.sendSms( + // to: "13395740386", + // message: content, + // ); + // }else{ + // print(content); + // } + // print(content); + // // sendSms(content); + // telephony.sendSms( + // to: "13395740386", + // message: content, + // isMultipart: true + // ); + List recipents=["13395740386"]; + _sendSMS(content,recipents); + // CallState state = await telephony.callState; + // print(state); + // DataState state1 = await telephony.cellularDataState; + // print(state1); + // String? operatorName = await telephony.networkOperatorName; + // print(operatorName); + // NetworkType type = await telephony.dataNetworkType; + // print(type); + // PhoneType type1 = await telephony.phoneType; + // print(type1); + String phoneNumber = await GetPhoneNumber().get(); + print('getPhoneNumber result: $phoneNumber'); + } setState(() {}); // print("这是数据" + textList[_s lect]); // print(index); }, onLongPress: () { - setState(() {}); - if (index == textList.length - 1) { + if (index != textList.length - 1) { showDialog( context: context, builder: (context) { @@ -78,6 +120,7 @@ class _ContentPageState extends State { ); }); } + setState(() {}); }, child: Container( // width: 686.w, @@ -102,4 +145,13 @@ class _ContentPageState extends State { ), ); } + + void _sendSMS(String message, List recipients) async { + String _result = await sendSMS( + message: message, recipients: recipients, sendDirect: true) + .catchError((onError) { + print(onError); + }); + print(_result); + } } diff --git a/lib/ui/login/login_page.dart b/lib/ui/login/login_page.dart index e532155..50d6aa0 100644 --- a/lib/ui/login/login_page.dart +++ b/lib/ui/login/login_page.dart @@ -132,7 +132,7 @@ class _LoginPageState extends State { data: {'phone': _phoneController.text, 'code': _smsCodeController.text}); if (base.code == 0) { await UserTool.userProvider.setToken(base.data['token']); - Get.back(); + Get.to(() => const TabNavigator()); } else { CloudToast.show(base.msg); } diff --git a/lib/ui/user/user_page.dart b/lib/ui/user/user_page.dart index 3343d8a..39a6270 100644 --- a/lib/ui/user/user_page.dart +++ b/lib/ui/user/user_page.dart @@ -5,6 +5,7 @@ import 'package:permission_handler/permission_handler.dart'; import 'package:permission_handler_platform_interface/permission_handler_platform_interface.dart'; import 'package:project_telephony/base/base_style.dart'; import 'package:project_telephony/ui/login/login_page.dart'; +import 'package:project_telephony/ui/tab_navigator.dart'; import 'package:project_telephony/ui/user/members_page.dart'; import 'package:project_telephony/ui/user/privacy_rights_page.dart'; import 'package:project_telephony/ui/widget/image_scaffold.dart'; @@ -12,9 +13,15 @@ import 'package:project_telephony/ui/widget/plone_bottom.dart'; import 'package:project_telephony/ui/widget/plone_image_picker.dart'; import 'package:project_telephony/utils/headers.dart'; import 'package:project_telephony/utils/permissionutils.dart'; +import 'package:provider/provider.dart'; import 'package:telephony/telephony.dart'; +import '../../constants/api.dart'; +import '../../model/network/api_client.dart'; import '../../permission.dart'; +import '../../providers/user_provider.dart'; +import '../../utils/toast/cloud_toast.dart'; +import '../../utils/user_tool.dart'; import '../home/home_page.dart'; class UserPage extends StatefulWidget { @@ -26,13 +33,9 @@ class UserPage extends StatefulWidget { final Telephony telephony = Telephony.instance; -@override -void initState() {} - class _UserPageState extends State { bool vle = false; - bool tooken = true; - + final userProvider = Provider.of(Get.context!, listen: false); @override Widget build(BuildContext context) { @@ -52,18 +55,27 @@ class _UserPageState extends State { _getSwitch(Assets.icons.switch1.path, "功能开关", true), _getSwitch(Assets.icons.privacy.path, "隐私政策", false), _getSwitch(Assets.icons.permissions.path, "权限说明", false), + _getSwitch(Assets.icons.sms.path, "短信标签", false), // const Spacer(), 182.hb, - PloneBottom( - border: false, - onTap: () { - Get.to(() => const HomePage()); - }, - textColor: const Color(0xFF1890FF), - color1: const Color(0xFFEBF5FF), - color2: const Color(0xFFEBF5FF), - text: "退出登录", - ) + userProvider.isLogin + ? PloneBottom( + border: false, + onTap: () async{ + var cancel = CloudToast.loading; + var base = await apiClient.request(API.manager.logout, showMessage: true); + if (base.code == 0) { + UserTool.userProvider.logout(); + Get.offAll(const HomePage()); + } + cancel(); + }, + textColor: const Color(0xFF1890FF), + color1: const Color(0xFFEBF5FF), + color2: const Color(0xFFEBF5FF), + text: "退出登录", + ) + : const SizedBox() ]), ); } @@ -82,7 +94,7 @@ class _UserPageState extends State { crossAxisAlignment: CrossAxisAlignment.start, children: [ Text( - tooken ? "登录/注册" : "xxxxx", + userProvider.isLogin ? userProvider.userInfo.name : "登录/注册", style: TextStyle( fontSize: BaseStyle.fontSize48, color: BaseStyle.color333333, @@ -90,7 +102,7 @@ class _UserPageState extends State { ), 24.hb, Text( - tooken ? "登录获取更多信息" : "欢迎您登录短信帮手", + userProvider.isLogin ? "欢迎您登录短信帮手" : "登录获取更多信息", style: TextStyle( fontSize: BaseStyle.fontSize28, color: BaseStyle.color333333), diff --git a/lib/utils/pay_util.dart b/lib/utils/pay_util.dart index fde58bd..fb7672f 100644 --- a/lib/utils/pay_util.dart +++ b/lib/utils/pay_util.dart @@ -1,166 +1,166 @@ -// import 'dart:async'; -// import 'dart:convert'; - -// import 'package:bot_toast/bot_toast.dart'; -// import 'package:cloud_car/model/pay/wx_pay_model.dart'; -// import 'package:cloud_car/utils/new_work/api_client.dart'; -// import 'package:cloud_car/utils/toast/cloud_toast.dart'; -// import 'package:dio/dio.dart'; -// import 'package:flutter/foundation.dart'; -// import 'package:fluwx/fluwx.dart'; -// import 'package:power_logger/power_logger.dart'; -// import 'package:tobias/tobias.dart'; - -// import '../model/pay/pay_model.dart'; - -// enum PAYTYPE { -// ///支付宝 -// ali, - -// ///微信 -// wx, - -// ///现金 -// cash, - -// ///pos -// pos -// } - -// class PayUtil { -// static final PayUtil _instance = PayUtil._(); - -// factory PayUtil() => _instance; - -// PayUtil._(); - -// void resultSatus(String status) { -// switch (status) { -// case '8000': -// BotToast.showText(text: '正在处理中'); -// break; -// case '4000': -// BotToast.showText(text: '订单支付失败'); -// break; -// case '5000': -// BotToast.showText(text: '重复请求'); -// break; -// case '6001': -// BotToast.showText(text: ' 用户中途取消'); -// break; -// case '6002': -// BotToast.showText(text: '网络连接出错'); -// break; -// case '6004': -// BotToast.showText(text: '支付结果未知,请查询商户订单列表中订单的支付状态'); -// break; -// default: -// BotToast.showText(text: '其他支付错误'); -// break; -// } -// } - -// String _resultStatus = ''; - -// ///支付宝支付 -// ///传入订单信息和确认订单请求地址 -// Future callAliPay(String order, {String? apiPath}) async { -// var install = await isAliPayInstalled(); -// if (!install) { -// BotToast.showText(text: '未安装支付宝!'); -// return false; -// } -// Map result = {}; -// try { -// result = await aliPay(order); -// } catch (e) { -// if (kDebugMode) { -// print(e.toString()); -// } -// } -// _resultStatus = result['resultStatus']; -// if (_resultStatus == '9000') { -// String res = result['result']; -// PayModel model = PayModel.fromJson(jsonDecode(res)); -// if (apiPath != null) { -// bool confirmResult = await _confirmPayResult( -// apiPath, model.aliPayTradeAppPayResponse.outTradeNo); -// return confirmResult; -// } else { -// CloudToast.show('支付成功'); -// return true; -// } -// } else { -// resultSatus(_resultStatus); -// return false; -// } -// } - -// Future _confirmPayResult(String path, String code) async { -// try { -// int status = 0; -// for (var i = 0; i < 3; i++) { -// await Future.delayed(const Duration(milliseconds: 1000), () async { -// Response response = await apiClient.dio.get(path, queryParameters: { -// "code": code, -// }); -// status = response.data['status'] as int; -// }); -// if (status == 2) { -// break; -// } -// } -// if (status == 2) { -// BotToast.showText(text: '交易成功'); -// return true; -// } else { -// BotToast.showText(text: '交易失败 错误码$status'); -// return false; -// } -// } catch (e) { -// BotToast.showText(text: '网络请求错误'); -// LoggerData.addData(e); -// return false; -// } -// } - -// ///微信支付 - -// StreamSubscription? _wxPayStream; - -// void wxPayAddListener( -// {required VoidCallback paySuccess, -// Function(BaseWeChatResponse)? payError}) { -// _wxPayStream = weChatResponseEventHandler.listen((event) { -// if (kDebugMode) { -// print('errorCode:${event.errCode} errorStr:${event.errStr}'); -// } -// if (event.errCode == 0) { -// paySuccess(); -// } else { -// LoggerData.addData( -// 'errorCode:${event.errCode} errorStr:${event.errStr ?? '支付失败'}'); -// CloudToast.show(event.errStr ?? '支付失败'); -// if (payError != null) payError(event); -// //payError == null ? null : payError(event); -// } -// }); -// } - -// void removeWxPayListener() { -// _wxPayStream?.cancel(); -// } - -// Future callWxPay({ -// required WxPayModel payModel, -// }) async { -// await payWithWeChat( -// appId: 'wx9bc3ffb23a749254', -// partnerId: payModel.partnerId, -// prepayId: payModel.prepayId, -// packageValue: payModel.package, -// nonceStr: payModel.nonceStr, -// timeStamp: int.parse(payModel.timeStamp), -// sign: payModel.sign); -// } -// } +import 'dart:async'; +import 'dart:convert'; + +import 'package:bot_toast/bot_toast.dart'; +import 'package:dio/dio.dart'; +import 'package:flutter/foundation.dart'; +import 'package:fluwx/fluwx.dart'; +import 'package:power_logger/power_logger.dart'; +import 'package:project_telephony/utils/toast/cloud_toast.dart'; +import 'package:tobias/tobias.dart'; + +import '../model/network/api_client.dart'; +import '../model/pay/pay_model.dart'; +import '../model/pay/wx_pay_model.dart'; + +enum PAYTYPE { + ///支付宝 + ali, + + ///微信 + wx, + + ///现金 + cash, + + ///pos + pos +} + +class PayUtil { + static final PayUtil _instance = PayUtil._(); + + factory PayUtil() => _instance; + + PayUtil._(); + + void resultSatus(String status) { + switch (status) { + case '8000': + BotToast.showText(text: '正在处理中'); + break; + case '4000': + BotToast.showText(text: '订单支付失败'); + break; + case '5000': + BotToast.showText(text: '重复请求'); + break; + case '6001': + BotToast.showText(text: ' 用户中途取消'); + break; + case '6002': + BotToast.showText(text: '网络连接出错'); + break; + case '6004': + BotToast.showText(text: '支付结果未知,请查询商户订单列表中订单的支付状态'); + break; + default: + BotToast.showText(text: '其他支付错误'); + break; + } + } + + String _resultStatus = ''; + + ///支付宝支付 + ///传入订单信息和确认订单请求地址 + Future callAliPay(String order, {String? apiPath}) async { + var install = await isAliPayInstalled(); + if (!install) { + BotToast.showText(text: '未安装支付宝!'); + return false; + } + Map result = {}; + try { + result = await aliPay(order); + } catch (e) { + if (kDebugMode) { + print(e.toString()); + } + } + _resultStatus = result['resultStatus']; + if (_resultStatus == '9000') { + String res = result['result']; + PayModel model = PayModel.fromJson(jsonDecode(res)); + if (apiPath != null) { + bool confirmResult = await _confirmPayResult( + apiPath, model.aliPayTradeAppPayResponse.outTradeNo); + return confirmResult; + } else { + CloudToast.show('支付成功'); + return true; + } + } else { + resultSatus(_resultStatus); + return false; + } + } + + Future _confirmPayResult(String path, String code) async { + try { + int status = 0; + for (var i = 0; i < 3; i++) { + await Future.delayed(const Duration(milliseconds: 1000), () async { + Response response = await apiClient.dio.get(path, queryParameters: { + "code": code, + }); + status = response.data['status'] as int; + }); + if (status == 2) { + break; + } + } + if (status == 2) { + BotToast.showText(text: '交易成功'); + return true; + } else { + BotToast.showText(text: '交易失败 错误码$status'); + return false; + } + } catch (e) { + BotToast.showText(text: '网络请求错误'); + LoggerData.addData(e); + return false; + } + } + + ///微信支付 + + StreamSubscription? _wxPayStream; + + void wxPayAddListener( + {required VoidCallback paySuccess, + Function(BaseWeChatResponse)? payError}) { + _wxPayStream = weChatResponseEventHandler.listen((event) { + if (kDebugMode) { + print('errorCode:${event.errCode} errorStr:${event.errStr}'); + } + if (event.errCode == 0) { + paySuccess(); + } else { + LoggerData.addData( + 'errorCode:${event.errCode} errorStr:${event.errStr ?? '支付失败'}'); + CloudToast.show(event.errStr ?? '支付失败'); + if (payError != null) payError(event); + //payError == null ? null : payError(event); + } + }); + } + + void removeWxPayListener() { + _wxPayStream?.cancel(); + } + + Future callWxPay({ + required WxPayModel payModel, + }) async { + await payWithWeChat( + appId: 'wx9bc3ffb23a749254', + partnerId: payModel.partnerId, + prepayId: payModel.prepayId, + packageValue: payModel.package, + nonceStr: payModel.nonceStr, + timeStamp: int.parse(payModel.timeStamp), + sign: payModel.sign); + } +} diff --git a/pubspec.lock b/pubspec.lock index 9502508..72fa926 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -36,13 +36,6 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "2.8.2" - auto_size_text: - dependency: transitive - description: - name: auto_size_text - url: "https://pub.dartlang.org" - source: hosted - version: "3.0.0" auto_size_text_pk: dependency: transitive description: @@ -239,27 +232,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "2.2.0" - device_info: - dependency: transitive - description: - name: device_info - url: "https://pub.dartlang.org" - source: hosted - version: "2.0.3" - device_info_platform_interface: - dependency: transitive - description: - name: device_info_platform_interface - url: "https://pub.dartlang.org" - source: hosted - version: "2.0.1" device_info_plus: dependency: "direct main" description: name: device_info_plus url: "https://pub.dartlang.org" source: hosted - version: "4.0.1" + version: "2.2.0" device_info_plus_linux: dependency: transitive description: @@ -294,7 +273,7 @@ packages: name: device_info_plus_windows url: "https://pub.dartlang.org" source: hosted - version: "3.0.1" + version: "2.1.1" dio: dependency: transitive description: @@ -322,7 +301,7 @@ packages: name: ffi url: "https://pub.dartlang.org" source: hosted - version: "2.0.1" + version: "1.2.1" file: dependency: transitive description: @@ -404,7 +383,7 @@ packages: name: flutter_lints url: "https://pub.dartlang.org" source: hosted - version: "1.0.4" + version: "2.0.1" flutter_native_splash: dependency: "direct dev" description: @@ -426,6 +405,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "5.5.3+2" + flutter_sms: + dependency: "direct main" + description: + name: flutter_sms + url: "https://pub.dartlang.org" + source: hosted + version: "2.3.3" flutter_test: dependency: "direct dev" description: flutter @@ -457,6 +443,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "4.6.5" + get_phone_number: + dependency: "direct main" + description: + name: get_phone_number + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.1" glob: dependency: transitive description: @@ -499,6 +492,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.1.0" + hive_generator: + dependency: "direct dev" + description: + name: hive_generator + url: "https://pub.dartlang.org" + source: hosted + version: "1.1.3" http: dependency: transitive description: @@ -562,6 +562,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "2.6.1" + import_sorter: + dependency: "direct dev" + description: + name: import_sorter + url: "https://pub.dartlang.org" + source: hosted + version: "4.6.0" intl: dependency: transitive description: @@ -603,7 +610,7 @@ packages: name: lints url: "https://pub.dartlang.org" source: hosted - version: "1.0.1" + version: "2.0.0" logger: dependency: transitive description: @@ -674,6 +681,48 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "2.0.2" + package_info_plus: + dependency: transitive + description: + name: package_info_plus + url: "https://pub.dartlang.org" + source: hosted + version: "1.4.2" + package_info_plus_linux: + dependency: transitive + description: + name: package_info_plus_linux + url: "https://pub.dartlang.org" + source: hosted + version: "1.0.5" + package_info_plus_macos: + dependency: transitive + description: + name: package_info_plus_macos + url: "https://pub.dartlang.org" + source: hosted + version: "1.3.0" + package_info_plus_platform_interface: + dependency: transitive + description: + name: package_info_plus_platform_interface + url: "https://pub.dartlang.org" + source: hosted + version: "1.0.2" + package_info_plus_web: + dependency: transitive + description: + name: package_info_plus_web + url: "https://pub.dartlang.org" + source: hosted + version: "1.0.5" + package_info_plus_windows: + dependency: transitive + description: + name: package_info_plus_windows + url: "https://pub.dartlang.org" + source: hosted + version: "1.0.5" path: dependency: transitive description: @@ -729,7 +778,7 @@ packages: name: path_provider_windows url: "https://pub.dartlang.org" source: hosted - version: "2.1.1" + version: "2.0.7" permission_handler: dependency: "direct main" description: @@ -799,7 +848,7 @@ packages: name: power_logger url: "https://pub.dartlang.org" source: hosted - version: "1.2.0" + version: "1.2.2" process: dependency: transitive description: @@ -1008,6 +1057,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.0.0" + tint: + dependency: transitive + description: + name: tint + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.0" tobias: dependency: "direct main" description: @@ -1029,6 +1085,62 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "2.0.4" + url_launcher: + dependency: transitive + description: + name: url_launcher + url: "https://pub.dartlang.org" + source: hosted + version: "6.1.5" + url_launcher_android: + dependency: transitive + description: + name: url_launcher_android + url: "https://pub.dartlang.org" + source: hosted + version: "6.0.17" + url_launcher_ios: + dependency: transitive + description: + name: url_launcher_ios + url: "https://pub.dartlang.org" + source: hosted + version: "6.0.17" + url_launcher_linux: + dependency: transitive + description: + name: url_launcher_linux + url: "https://pub.dartlang.org" + source: hosted + version: "3.0.1" + url_launcher_macos: + dependency: transitive + description: + name: url_launcher_macos + url: "https://pub.dartlang.org" + source: hosted + version: "3.0.1" + url_launcher_platform_interface: + dependency: transitive + description: + name: url_launcher_platform_interface + url: "https://pub.dartlang.org" + source: hosted + version: "2.1.0" + url_launcher_web: + dependency: transitive + description: + name: url_launcher_web + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.13" + url_launcher_windows: + dependency: transitive + description: + name: url_launcher_windows + url: "https://pub.dartlang.org" + source: hosted + version: "3.0.1" vector_math: dependency: transitive description: @@ -1070,7 +1182,7 @@ packages: name: win32 url: "https://pub.dartlang.org" source: hosted - version: "2.7.0" + version: "2.6.1" xdg_directories: dependency: transitive description: @@ -1094,4 +1206,4 @@ packages: version: "3.1.1" sdks: dart: ">=2.17.0 <3.0.0" - flutter: ">=3.0.0" + flutter: ">=2.10.0" diff --git a/pubspec.yaml b/pubspec.yaml index 0ff7908..aa6d7c1 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -18,7 +18,7 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev version: 1.0.0+1 environment: - sdk: ">=2.15.0 <3.0.0" + sdk: ">=2.16.0 <3.0.0" # Dependencies specify other packages that your package needs in order to work. # To automatically upgrade your package dependencies to the latest versions @@ -69,10 +69,16 @@ dependencies: # model生成 json_annotation: ^4.6.0 permission_handler_platform_interface: ^3.7.0 - power_logger: ^1.2.0 provider: ^6.0.3 equatable: any + #日志输出 + power_logger: ^1.2.2 +# 短信 + flutter_sms: ^2.3.3 + get_phone_number: ^2.0.1 +# flutter_telephony: any + # # jdk # jverify: ^2.2.5 ## pub 集成 @@ -88,14 +94,17 @@ dev_dependencies: # package. See that file for information about deactivating specific lint # rules and activating additional ones. # test: ^1.21.4 - flutter_lints: ^1.0.0 + #导入包整理 + import_sorter: ^4.5.1 + hive_generator: ^1.1.0 #model自动生成 json_serializable: ^6.1.3 build_runner: ^2.0.2 yaml: ^3.1.0 pub_semver: ^2.1.0 flutter_gen_runner: ^4.1.3 - # patgh: ^1.8.1 + flutter_lints: ^2.0.0 +# patgh: ^1.8.1 #脚本工具 grinder: ^0.9.1 flutter_native_splash: ^1.1.8+4