feat:webview处理Scheme,和携带公共UA头

This commit is contained in:
anfe 2024-04-08 13:32:47 +08:00
parent 4cd6e49564
commit abbef04d6c
5 changed files with 65 additions and 11 deletions

View File

@ -66,7 +66,7 @@ keytool -list -v -keystore android/app/sky.jks
## 编译
```bash
flutter build apk --split-per-abi --release --flavor sky -t lib/main_sky.dart
flutter build apk --release --flavor sky -t lib/main_sky_full.dart
```
编译后的包:
通用build/app/outputs/apk/sky/release/app-sky-universal-release.apk

View File

@ -11,13 +11,16 @@ enum Flavor {
class StarLockAMapKey {
//iOS平台的key
final String iosKey;
//Android平台的key
final String androidKey;
const StarLockAMapKey({required this.iosKey, required this.androidKey});
}
class F {
static Flavor? appFlavor;
// ()
static bool isLite = false;
@ -77,22 +80,20 @@ class F {
throw Exception('flavor[$name] apiPrefix not found');
}
}
// StarLockAMapKey
static StarLockAMapKey get aMapKey {
switch (appFlavor) {
case Flavor.xie:
case Flavor.dev:
return const StarLockAMapKey(
androidKey: 'b56b681ee89f4db43a5aa1879ae8cbfe',
iosKey: 'bd4496e6598ef49796e3a80715035b4d');
androidKey: 'b56b681ee89f4db43a5aa1879ae8cbfe', iosKey: 'bd4496e6598ef49796e3a80715035b4d');
case Flavor.pre:
return const StarLockAMapKey(
androidKey: '11d49b3f4fc09c04a02bbb7500925ba2',
iosKey: '883a3355d2d77c2fdc2667030dc97ffe');
androidKey: '11d49b3f4fc09c04a02bbb7500925ba2', iosKey: '883a3355d2d77c2fdc2667030dc97ffe');
case Flavor.sky:
return const StarLockAMapKey(
androidKey: 'fb0d2a3e4208b36452cf636aa025a24f',
iosKey: '86ca725a12a629c280e116a317aaba19');
androidKey: 'fb0d2a3e4208b36452cf636aa025a24f', iosKey: '86ca725a12a629c280e116a317aaba19');
// case Flavor.xhj:
// return const StarLockAMapKey(
// androidKey: 'todo',
@ -101,5 +102,4 @@ class F {
throw Exception('flavor[$name] aMapKey not found');
}
}
}

View File

@ -1,5 +1,6 @@
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:star_lock/webview/webview_logic.dart';
import 'package:webview_flutter/webview_flutter.dart';
class LockMallState {
@ -7,5 +8,16 @@ class LockMallState {
var webProgress = 0.0.obs;
late WebViewController mallWebView = WebViewController()
..setJavaScriptMode(JavaScriptMode.unrestricted)
..setBackgroundColor(Colors.white);
..setBackgroundColor(Colors.white)
..setUserAgent(WebViewLogic.userAgent)
..setNavigationDelegate(NavigationDelegate(onNavigationRequest: onNavigationRequest));
//
Future<NavigationDecision> onNavigationRequest(NavigationRequest request) async {
if (WebViewLogic.judgePaySchemes(request.url)) {
await WebViewLogic.runScheme(request.url);
return NavigationDecision.prevent;
}
return NavigationDecision.navigate;
}
}

View File

@ -7,9 +7,16 @@ import 'package:star_lock/login/login/entity/LoginData.dart';
import '../tools/platform_info_services.dart';
import '../tools/storage.dart';
FutureOr<Request> requestInterceptor(Request request) async {
request.headers['User-Agent'] =
//UA
String getUserAgent() {
//便
String ua =
'StarLock/${PlatformInfoService.to.info.version}/${PlatformInfoService.to.info.buildNumber}/${GetPlatform.isAndroid ? 'Android' : 'iOS'}';
return ua;
}
FutureOr<Request> requestInterceptor(Request request) async {
request.headers['User-Agent'] = getUserAgent();
request.headers['Accept-Language'] = 'zh-CN';
// request.headers['Content-Type'] = 'application/json';
// request.headers['token'] = StoreService.to.userToken!;

View File

@ -0,0 +1,35 @@
import 'package:star_lock/network/request_interceptor.dart';
import 'package:url_launcher/url_launcher.dart';
///
/// webview的工具类
///
/// * Schemes的跳转
/// * webview的UA表示
///
class WebViewLogic {
//pay的Scheme
static const List<String> paySchemes = ['weixin:', 'alipay:'];
//UA信息
static String get userAgent => getUserAgent();
//Scheme
static bool judgePaySchemes(String url) {
for (int i = 0, j = paySchemes.length; i < j; i++) {
if (url.contains(paySchemes[i])) {
return true;
}
}
return false;
}
//Scheme业务逻辑
static Future<void> runScheme(String url) async {
if (await canLaunchUrl(Uri.parse(url))) {
await launchUrl(Uri.parse(url));
} else {
throw 'Could not launch $url';
}
}
}