feat:webview支持微信支付,接入sdk
This commit is contained in:
parent
7e024a67c2
commit
785e692618
@ -31,10 +31,15 @@ android {
|
||||
// 这里“debug”不是一个自定义变量,而是一个特定的关键词,凡是使用--debug模式,都会引用到这里
|
||||
// 目前看来,debug模式没办法在buildTypes里面按flavors指定编译签名,所有口味的debug模式只能用同一个签名
|
||||
debug {
|
||||
storeFile file("starlock.keystore")
|
||||
storePassword '123456'
|
||||
keyAlias = 'starlock'
|
||||
keyPassword '123456'
|
||||
// storeFile file("starlock.keystore")
|
||||
// storePassword '123456'
|
||||
// keyAlias = 'starlock'
|
||||
// keyPassword '123456'
|
||||
|
||||
storeFile file("sky.jks")
|
||||
storePassword 'sky2028'
|
||||
keyAlias = 'upload'
|
||||
keyPassword 'sky2028'
|
||||
}
|
||||
// 下面的local、pre、sky、xhj 都是自定义变量,自身不起任何作用,而是看哪里引用了它们
|
||||
local {
|
||||
|
||||
@ -4,6 +4,8 @@
|
||||
<dict>
|
||||
<key>aps-environment</key>
|
||||
<string>development</string>
|
||||
<key>com.apple.developer.associated-domains</key>
|
||||
<array/>
|
||||
<key>com.apple.external-accessory.wireless-configuration</key>
|
||||
<true/>
|
||||
</dict>
|
||||
|
||||
@ -1,8 +1,12 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:fluwx/fluwx.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:star_lock/mine/mall/lockMall_entity.dart';
|
||||
import 'package:star_lock/mine/mall/lockMall_state.dart';
|
||||
import 'package:star_lock/network/api_repository.dart';
|
||||
import 'package:star_lock/tools/pay/wx_pay_tool.dart';
|
||||
import 'package:star_lock/webview/webview_logic.dart';
|
||||
import 'package:webview_flutter/webview_flutter.dart';
|
||||
|
||||
@ -40,9 +44,52 @@ class LockMallLogic extends BaseGetXController {
|
||||
),
|
||||
);
|
||||
state.mallWebView.loadRequest(Uri.parse(state.lockMallUrl.value));
|
||||
// FlutterBridge.postMessage({action:'',data:'{}',callFun:'回调给js的方法'})
|
||||
state.mallWebView.addJavaScriptChannel(
|
||||
"FlutterBridge",
|
||||
onMessageReceived: (JavaScriptMessage message) async {
|
||||
flutterBridge(message);
|
||||
},
|
||||
);
|
||||
// onMessageReceived
|
||||
}
|
||||
}
|
||||
|
||||
//监听webview的调用
|
||||
Future<void> flutterBridge(JavaScriptMessage message) async {
|
||||
final dynamic obj = jsonDecode(message.message);
|
||||
print(obj);
|
||||
if (obj is! Map && obj['action'] is String) {
|
||||
return;
|
||||
}
|
||||
String action = obj['action'];
|
||||
dynamic data = obj['data'];
|
||||
String? callFun = obj['callFun'];
|
||||
switch (action) {
|
||||
case 'WechatPayParams':
|
||||
//微信支付
|
||||
wxPay(data, callFun);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
//微信支付
|
||||
Future<void> wxPay(dynamic data, String? callFun) async {
|
||||
WxPayTool.pay(WxPayTool.mapToPayment(data), (response) {
|
||||
if (response is WeChatPaymentResponse) {
|
||||
Map data = {
|
||||
'type': response.type,
|
||||
'extData': response.extData,
|
||||
'errCode': response.errCode,
|
||||
'errStr': response.errStr,
|
||||
};
|
||||
state.mallWebView.runJavaScript(
|
||||
'window.$callFun(`${json.encode(data)}`)',
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
//判断webview 是否可以有路由可以回退,无则退出当前页面
|
||||
Future<bool> canGoBack(bool didPop) async {
|
||||
bool canGoBack = await state.mallWebView.canGoBack();
|
||||
|
||||
44
star_lock/lib/tools/pay/wx_pay_tool.dart
Normal file
44
star_lock/lib/tools/pay/wx_pay_tool.dart
Normal file
@ -0,0 +1,44 @@
|
||||
import 'package:fluwx/fluwx.dart';
|
||||
|
||||
///
|
||||
/// 微信支付
|
||||
///
|
||||
///
|
||||
class WxPayTool {
|
||||
static bool isInit = false;
|
||||
static Fluwx fluwx = Fluwx();
|
||||
|
||||
static init(String appId, String universalLink) {
|
||||
fluwx.registerApi(appId: appId, universalLink: universalLink);
|
||||
}
|
||||
|
||||
static Future<void> pay(Payment payment, WeChatResponseSubscriber listener) async {
|
||||
if (!isInit) {
|
||||
isInit = true;
|
||||
await init(payment.appId, '123');
|
||||
//回调
|
||||
responseListener(WeChatResponse response) {
|
||||
if (response is WeChatPaymentResponse) {
|
||||
//支付回调
|
||||
listener.call(response);
|
||||
}
|
||||
}
|
||||
//开启监听
|
||||
fluwx.addSubscriber(responseListener);
|
||||
}
|
||||
fluwx.pay(which: payment);
|
||||
}
|
||||
|
||||
static Payment mapToPayment(dynamic data) {
|
||||
Payment payment = Payment(
|
||||
appId: data['appId'],
|
||||
partnerId: data['partnerId'],
|
||||
prepayId: data['prepayId'],
|
||||
packageValue: data['packageValue'],
|
||||
nonceStr: data['nonceStr'],
|
||||
timestamp: int.tryParse(data['timeStamp']) ?? 0,
|
||||
sign: data['sign'],
|
||||
);
|
||||
return payment;
|
||||
}
|
||||
}
|
||||
@ -154,6 +154,7 @@ dependencies:
|
||||
audio_service: ^0.18.12
|
||||
app_settings: ^5.1.1
|
||||
flutter_local_notifications: ^17.0.0
|
||||
fluwx: ^4.5.5
|
||||
|
||||
system_settings: ^2.0.0
|
||||
dev_dependencies:
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user