import 'dart:convert'; import 'package:PiliPlus/http/init.dart'; import 'package:PiliPlus/http/loading_state.dart'; import 'package:PiliPlus/http/ua_type.dart'; import 'package:PiliPlus/main.dart'; import 'package:PiliPlus/utils/accounts/account.dart'; import 'package:dio/dio.dart'; import 'package:flutter/material.dart'; import 'package:flutter_inappwebview/flutter_inappwebview.dart'; import 'package:get/get.dart'; class GeetestWebviewDialog extends StatelessWidget { const GeetestWebviewDialog(this.gt, this.challenge, {super.key}); final String gt; final String challenge; static const _geetestJsUri = 'https://static.geetest.com/static/js/fullpage.0.0.0.js'; static Future> _getConfig( String gt, String challenge, ) async { final res = await Request().get( 'https://api.geetest.com/gettype.php', queryParameters: {'gt': gt}, options: Options( responseType: ResponseType.plain, extra: {'account': const NoAccount()}, ), ); if (res.data case String data) { if (data.startsWith('(') && data.endsWith(')')) { final Map config; try { config = jsonDecode(data.substring(1, data.length - 1)); } catch (e) { return Error(e.toString()); } if (config['status'] == 'success') { return Success( jsonEncode( config['data'] as Map..addAll({ "gt": gt, "challenge": challenge, "offline": false, "new_captcha": true, "product": "bind", "width": "100%", "https": true, "protocol": "https://", }), ), ); } else { return Error(data); } } } return Error(res.data['message']); } @override Widget build(BuildContext context) { final future = _getConfig(gt, challenge); return AlertDialog( title: const Text('验证码'), content: SizedBox( width: 300, height: 400, child: InAppWebView( webViewEnvironment: webViewEnvironment, initialSettings: InAppWebViewSettings( clearCache: true, javaScriptEnabled: true, forceDark: ForceDark.AUTO, useHybridComposition: false, algorithmicDarkeningAllowed: true, useShouldOverrideUrlLoading: true, userAgent: UaType.mob.ua, mixedContentMode: MixedContentMode.MIXED_CONTENT_ALWAYS_ALLOW, ), initialData: InAppWebViewInitialData( data: '', ), onWebViewCreated: (ctr) { ctr ..addJavaScriptHandler( handlerName: 'success', callback: (args) { if (args.isNotEmpty) { if (args[0] case Map data) { Get.back(result: data); return; } } debugPrint('geetest invalid result: $args'); }, ) ..addJavaScriptHandler( handlerName: 'error', callback: (args) { debugPrint('geetest error: $args'); }, ); }, onLoadStop: (ctr, _) async { final config = await future; if (config.isSuccess) { ctr.evaluateJavascript( source: 'let t=Geetest(${config.data}).onSuccess(()=>R("success",t.getValidate())).onError((o)=>R("error",o));t.onReady(()=>t.verify());', ); } else { config.toast(); Get.back(); } }, ), ), actions: [ TextButton( onPressed: Get.back, child: Text( '取消', style: TextStyle(color: ColorScheme.of(context).outline), ), ), ], ); } }