2024-04-08 17:55:06 +08:00
|
|
|
import 'dart:convert';
|
|
|
|
|
|
2024-05-03 09:21:08 +08:00
|
|
|
import 'package:flutter/services.dart';
|
2024-04-08 17:55:06 +08:00
|
|
|
import 'package:fluwx/fluwx.dart';
|
2024-04-08 14:48:16 +08:00
|
|
|
import 'package:get/get.dart';
|
2024-05-03 09:21:08 +08:00
|
|
|
import 'package:star_lock/flavors.dart';
|
|
|
|
|
import 'package:star_lock/main/lockMian/lockMain/xhj/lockMain_xhj_logic.dart';
|
2024-01-25 15:43:10 +08:00
|
|
|
import 'package:star_lock/mine/mall/lockMall_entity.dart';
|
|
|
|
|
import 'package:star_lock/mine/mall/lockMall_state.dart';
|
2024-05-10 13:57:38 +08:00
|
|
|
import 'package:star_lock/mine/mall/webview/webview_logic.dart';
|
2024-01-25 15:43:10 +08:00
|
|
|
import 'package:star_lock/network/api_repository.dart';
|
2024-02-27 11:50:53 +08:00
|
|
|
import 'package:webview_flutter/webview_flutter.dart';
|
2024-01-25 15:43:10 +08:00
|
|
|
|
|
|
|
|
import '../../tools/baseGetXController.dart';
|
2024-09-02 18:09:57 +08:00
|
|
|
import '../../tools/wechat/pay/wx_pay_tool.dart';
|
|
|
|
|
import '../../tools/wechat/wechatManageTool.dart';
|
2024-01-25 15:43:10 +08:00
|
|
|
|
|
|
|
|
class LockMallLogic extends BaseGetXController {
|
2024-04-15 16:20:22 +08:00
|
|
|
late LockMallState state;
|
|
|
|
|
|
2024-05-03 09:21:08 +08:00
|
|
|
DateTime? _lastPressedAt; // 记录上一次按下返回键的时间
|
|
|
|
|
|
2024-04-15 16:20:22 +08:00
|
|
|
LockMallLogic({required bool allowReturn})
|
2024-05-03 09:21:08 +08:00
|
|
|
: state = LockMallState(allowReturn: allowReturn);
|
2024-01-25 15:43:10 +08:00
|
|
|
|
|
|
|
|
//获取商城跳转地址
|
|
|
|
|
Future<void> getMallURLRequest() async {
|
2024-06-07 11:12:45 +08:00
|
|
|
final LockMallDataEntity entity = await ApiRepository.to.getMallURLData();
|
2024-01-25 15:43:10 +08:00
|
|
|
if (entity.errorCode!.codeIsSuccessful) {
|
|
|
|
|
state.lockMallUrl.value = entity.data!.url!;
|
2024-02-27 11:50:53 +08:00
|
|
|
state.mallWebView.setNavigationDelegate(
|
|
|
|
|
NavigationDelegate(
|
|
|
|
|
onProgress: (int progress) {
|
|
|
|
|
state.webProgress.value = progress / 100;
|
|
|
|
|
},
|
|
|
|
|
onPageStarted: (String url) {
|
|
|
|
|
state.webProgress.value = 0.0;
|
|
|
|
|
},
|
|
|
|
|
onPageFinished: (String url) {
|
|
|
|
|
state.webProgress.value = 1.0;
|
2024-04-29 11:25:39 +08:00
|
|
|
refreshGoBack();
|
2024-06-07 11:12:45 +08:00
|
|
|
update();
|
2024-02-27 11:50:53 +08:00
|
|
|
},
|
|
|
|
|
onWebResourceError: (WebResourceError error) {},
|
2024-04-08 14:17:15 +08:00
|
|
|
onNavigationRequest: (NavigationRequest request) async {
|
|
|
|
|
//路由跳转逻辑
|
|
|
|
|
if (WebViewLogic.judgePaySchemes(request.url)) {
|
|
|
|
|
await WebViewLogic.runScheme(request.url);
|
|
|
|
|
return NavigationDecision.prevent;
|
|
|
|
|
}
|
2024-02-27 11:50:53 +08:00
|
|
|
return NavigationDecision.navigate;
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
);
|
2024-01-25 15:43:10 +08:00
|
|
|
state.mallWebView.loadRequest(Uri.parse(state.lockMallUrl.value));
|
2024-04-08 17:55:06 +08:00
|
|
|
state.mallWebView.addJavaScriptChannel(
|
2024-06-07 11:12:45 +08:00
|
|
|
'FlutterBridge',
|
2024-04-08 17:55:06 +08:00
|
|
|
onMessageReceived: (JavaScriptMessage message) async {
|
|
|
|
|
flutterBridge(message);
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//监听webview的调用
|
|
|
|
|
Future<void> flutterBridge(JavaScriptMessage message) async {
|
2024-05-18 14:32:59 +08:00
|
|
|
final dynamic obj = json.decode(message.message);
|
2024-04-08 17:55:06 +08:00
|
|
|
if (obj is! Map && obj['action'] is String) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-06-07 11:12:45 +08:00
|
|
|
final String action = obj['action'];
|
|
|
|
|
final dynamic data = obj['data'];
|
|
|
|
|
final String? callFun = obj['callFun'];
|
2024-04-08 17:55:06 +08:00
|
|
|
switch (action) {
|
|
|
|
|
case 'WechatPayParams':
|
|
|
|
|
//微信支付
|
2024-09-02 18:09:57 +08:00
|
|
|
WechatManageTool.getAppInfo(() {
|
|
|
|
|
wxPay(data, callFun);
|
|
|
|
|
});
|
|
|
|
|
// wxPay(data, callFun);
|
2024-04-08 17:55:06 +08:00
|
|
|
break;
|
2024-01-25 15:43:10 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-08 17:55:06 +08:00
|
|
|
//微信支付
|
|
|
|
|
Future<void> wxPay(dynamic data, String? callFun) async {
|
2024-06-07 11:12:45 +08:00
|
|
|
WxPayTool.pay(WxPayTool.mapToPayment(data), (WeChatResponse response) {
|
2024-04-08 17:55:06 +08:00
|
|
|
if (response is WeChatPaymentResponse) {
|
2024-06-07 11:12:45 +08:00
|
|
|
final Map data = <String, dynamic>{
|
2024-04-08 17:55:06 +08:00
|
|
|
'type': response.type,
|
|
|
|
|
'extData': response.extData,
|
|
|
|
|
'errCode': response.errCode,
|
|
|
|
|
'errStr': response.errStr,
|
|
|
|
|
};
|
|
|
|
|
state.mallWebView.runJavaScript(
|
|
|
|
|
'window.$callFun(`${json.encode(data)}`)',
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-08 14:48:16 +08:00
|
|
|
//判断webview 是否可以有路由可以回退,无则退出当前页面
|
|
|
|
|
Future<bool> canGoBack(bool didPop) async {
|
2024-06-07 11:12:45 +08:00
|
|
|
final bool canGoBack = await state.mallWebView.canGoBack();
|
2024-05-03 09:21:08 +08:00
|
|
|
bool isMall = true;
|
|
|
|
|
if (Get.isRegistered<LockMainXHJLogic>() && F.isXHJ) {
|
|
|
|
|
isMall = Get.find<LockMainXHJLogic>().isMall;
|
|
|
|
|
}
|
|
|
|
|
if (canGoBack && isMall) {
|
2024-04-08 14:48:16 +08:00
|
|
|
await state.mallWebView.goBack();
|
2024-05-03 09:21:08 +08:00
|
|
|
} else if (state.allowReturn && isMall) {
|
2024-04-29 11:25:39 +08:00
|
|
|
Get.back();
|
2024-05-03 09:21:08 +08:00
|
|
|
} else {
|
|
|
|
|
if (_lastPressedAt == null ||
|
2024-08-21 14:12:15 +08:00
|
|
|
DateTime.now().difference(_lastPressedAt!) > const Duration(seconds: 2)) {
|
2024-05-03 09:21:08 +08:00
|
|
|
// 如果两次返回键时间间隔大于 2 秒,则提示再次按下返回键退出
|
|
|
|
|
_lastPressedAt = DateTime.now();
|
2024-08-21 14:12:15 +08:00
|
|
|
showToast('再返回一次退出'.tr + F.title);
|
2024-05-03 09:21:08 +08:00
|
|
|
return false; // 阻止返回操作
|
|
|
|
|
}
|
|
|
|
|
SystemNavigator.pop();
|
|
|
|
|
return true;
|
2024-04-08 14:48:16 +08:00
|
|
|
}
|
2024-04-29 11:25:39 +08:00
|
|
|
refreshGoBack();
|
2024-04-08 14:48:16 +08:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-29 11:25:39 +08:00
|
|
|
//刷新当前路由状态
|
2024-05-03 09:21:08 +08:00
|
|
|
void refreshGoBack() {
|
2024-04-29 11:25:39 +08:00
|
|
|
//如果属于一直返回按钮,则根据是否有路由可以回退刷新
|
2024-05-03 09:21:08 +08:00
|
|
|
if (state.allowReturn) {
|
2024-04-29 11:25:39 +08:00
|
|
|
return;
|
|
|
|
|
}
|
2024-06-07 11:12:45 +08:00
|
|
|
state.mallWebView.canGoBack().then((bool value) {
|
2024-04-29 11:25:39 +08:00
|
|
|
state.canGoBack = value;
|
|
|
|
|
update();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-25 15:43:10 +08:00
|
|
|
@override
|
|
|
|
|
Future<void> onReady() async {
|
|
|
|
|
super.onReady();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
void onInit() {
|
|
|
|
|
super.onInit();
|
|
|
|
|
getMallURLRequest();
|
|
|
|
|
}
|
|
|
|
|
}
|