From 3fce297ff4c4b6573334b3eb055776ec88ad2a41 Mon Sep 17 00:00:00 2001 From: liwei1dao Date: Thu, 25 Sep 2025 10:50:50 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E6=94=AF=E4=BB=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/core/bindings/initial_binding.dart | 4 + lib/data/services/network/api.dart | 9 ++ lib/data/services/pay_service.dart | 120 +++++++++++------- .../login/controllers/weixinLogin.dart | 9 ++ 4 files changed, 93 insertions(+), 49 deletions(-) diff --git a/lib/core/bindings/initial_binding.dart b/lib/core/bindings/initial_binding.dart index 00c74738a..7968424ff 100644 --- a/lib/core/bindings/initial_binding.dart +++ b/lib/core/bindings/initial_binding.dart @@ -1,3 +1,4 @@ +import 'package:voitrans/data/services/pay_service.dart'; import 'package:voitrans/data/services/qq_music.dart'; import '../../../data/services/user_portrait.dart'; @@ -87,6 +88,9 @@ class InitialBinding extends Bindings { // OTA检查服务 Get.lazyPut(() => OtaCheckService(), fenix: true); + // 支付服务 + Get.lazyPut(() => PayService(), fenix: true); + // 杰理蓝牙服务 // // 语言管理器(需要最先初始化) diff --git a/lib/data/services/network/api.dart b/lib/data/services/network/api.dart index 499c93939..8e36e726c 100644 --- a/lib/data/services/network/api.dart +++ b/lib/data/services/network/api.dart @@ -171,4 +171,13 @@ class Api { params: params, ); } + + //创建支付订单 + static paycreateorder([params]) { + return DioManager().request( + NWMethod.post, + '/api/home/pay_createorder', + params: params, + ); + } } diff --git a/lib/data/services/pay_service.dart b/lib/data/services/pay_service.dart index 82b241e04..1a398dc03 100644 --- a/lib/data/services/pay_service.dart +++ b/lib/data/services/pay_service.dart @@ -1,9 +1,9 @@ import 'package:get/get.dart'; -import 'package:fluwx/fluwx.dart'; +import 'package:voitrans/data/services/network/api.dart'; + import '../../modules/login/controllers/weixinLogin.dart'; -/// 支付结果回调 -typedef PaymentResultCallback = void Function(bool success, String? message); +typedef PaymentResultCallback = void Function(bool success, String message); /// 支付服务 class PayService extends GetxService { @@ -12,11 +12,20 @@ class PayService extends GetxService { @override void onInit() { super.onInit(); + //微信 + FlWX().register( + appSecret: '9ceb39df312b3ba95a229b2aa0d15496', + appId: 'wx4b5ec8957ad5e425', + partnerId: '1719983673', + universalLink: 'https://voitrans.net/app/', + params: FlWXCallbackParams(onHttp: (url) async { + return ''; + })); _flwx = FlWX(); } /// 微信支付 - /// + /// /// [paymentData] 支付参数,包含以下字段: /// - appId: 应用ID /// - partnerId: 商户号 @@ -25,7 +34,7 @@ class PayService extends GetxService { /// - nonceStr: 随机字符串 /// - timestamp: 时间戳 /// - sign: 签名 - /// + /// /// [onResult] 支付结果回调 Future wechatPay({ required Map paymentData, @@ -70,54 +79,65 @@ class PayService extends GetxService { } /// 创建微信支付订单 - /// - /// [orderInfo] 订单信息,包含: - /// - orderId: 订单ID - /// - amount: 支付金额(分) - /// - description: 商品描述 - /// - userId: 用户ID - /// + /// + /// [productId] 商品ID + /// [productName] 商品名称 + /// [productDesc] 商品描述 + /// [amount] 支付金额(分) + /// [paymentMethod] 支付方式(1=微信支付) + /// [clientIp] 客户端IP(可选) + /// [notifyUrl] 通知URL(可选) + /// [extraData] 扩展数据(可选) + /// /// 返回支付参数,用于调用 [wechatPay] 方法 Future?> createWechatPayOrder({ - required String orderId, + required String productId, + required String productName, + String? productDesc, required int amount, - required String description, - String? userId, + int paymentMethod = 4, // 1表示微信支付 + String? clientIp, + String? notifyUrl, + String? extraData, }) async { try { - // TODO: 这里需要调用后端API创建支付订单 - // 示例代码,实际需要根据后端接口调整 - - // final response = await Api.createPayOrder({ - // 'orderId': orderId, - // 'amount': amount, - // 'description': description, - // 'userId': userId, - // 'payType': 'wechat', - // }); - - // if (response['success'] == true) { - // return response['data'] as Map; - // } - - // 临时返回示例数据结构,实际使用时需要删除 - return { - 'appId': 'your_app_id', - 'partnerId': 'your_partner_id', - 'prepayId': 'prepay_id_from_server', - 'packageValue': 'Sign=WXPay', - 'nonceStr': 'random_string', - 'timestamp': DateTime.now().millisecondsSinceEpoch ~/ 1000, - 'sign': 'signature_from_server', - }; + // 调用后端API创建支付订单 + final response = await Api.paycreateorder({ + 'product_id': productId, + 'product_name': productName, + 'product_desc': productDesc ?? '', + 'amount': amount, + 'payment_method': paymentMethod, + 'client_ip': clientIp ?? '', + 'notify_url': notifyUrl ?? '', + 'extra_data': extraData ?? '', + }); + print('创建支付订单: $response'); + // 检查响应是否成功 + if (response != null) { + final pay_params = response["pay_params"] as Map; + // 返回微信支付所需的参数 + return { + 'appId': pay_params['appid'], + 'partnerId': pay_params['partnerid'], + 'prepayId': pay_params['prepayid'], + 'packageValue': pay_params['package'] ?? 'Sign=WXPay', + 'nonceStr': pay_params['noncestr'], + 'timestamp': pay_params['timestamp'], + 'sign': pay_params['sign'], + }; + } else { + print('创建支付订单失败: ${response?['message'] ?? '未知错误'}'); + return null; + } } catch (e) { - print('创建支付订单失败: $e'); + print('创建支付订单异常: $e'); return null; } } /// 查询支付结果 - /// + /// /// [orderId] 订单ID /// 返回支付状态:success, failed, pending Future queryPaymentStatus(String orderId) async { @@ -125,7 +145,7 @@ class PayService extends GetxService { // TODO: 调用后端API查询支付状态 // final response = await Api.queryPaymentStatus({'orderId': orderId}); // return response['status'] as String?; - + // 临时返回,实际使用时需要删除 return 'pending'; } catch (e) { @@ -135,7 +155,7 @@ class PayService extends GetxService { } /// 完整的支付流程 - /// + /// /// [orderId] 订单ID /// [amount] 支付金额(分) /// [description] 商品描述 @@ -151,10 +171,12 @@ class PayService extends GetxService { try { // 1. 创建支付订单 final paymentData = await createWechatPayOrder( - orderId: orderId, - amount: amount, - description: description, - userId: userId, + productId: 'prod_001', + productName: '会员充值', + productDesc: '1个月会员服务', + amount: 1, // 29.99元,单位:分 + paymentMethod: 4, // 微信支付 + notifyUrl: 'https://api.ideapsound.com/api/home/pay_wechatnotify', ); if (paymentData == null) { @@ -172,4 +194,4 @@ class PayService extends GetxService { return false; } } -} \ No newline at end of file +} diff --git a/lib/modules/login/controllers/weixinLogin.dart b/lib/modules/login/controllers/weixinLogin.dart index 38819a60a..89345511e 100644 --- a/lib/modules/login/controllers/weixinLogin.dart +++ b/lib/modules/login/controllers/weixinLogin.dart @@ -73,6 +73,11 @@ class FlWX { /// 获取当前设置的 appSecret String? get appSecret => _appSecret; + String? _partnerId; + + /// 获取当前设置的 partnerId + String? get partnerId => _partnerId; + String _appName = ''; String token = ''; String code = ''; @@ -109,6 +114,9 @@ class FlWX { /// 用于获取用户信息 String? appSecret, + /// 商户号 + String? partnerId, + /// app名称(用于分享时使用) String appName = '', @@ -123,6 +131,7 @@ class FlWX { _isRegister = true; _appId = appId; _appSecret = appSecret; + _partnerId = partnerId; _appName = appName; _shareThumbnail = shareThumbnail; _params = params;