|
@@ -29,17 +29,39 @@ class VerifyCodeViewModel extends AutoDisposeNotifier<verifyCodeState> with DioC
|
|
|
void fetchCode() async {
|
|
|
final result = await authRepository.fetchCaptchaImage(cancelToken: cancelToken);
|
|
|
|
|
|
- //Base64转为本地图片缓存
|
|
|
- final bytes = base64.decode(result.data?.img?.replaceFirst('data:image/png;base64,', '') ?? '');
|
|
|
- final tempDir = DirectoryUtil.getTempPath();
|
|
|
- final file = File('$tempDir/verify${DateTime.now().millisecondsSinceEpoch}.png');
|
|
|
- await file.writeAsBytes(bytes);
|
|
|
- Log.d('存入的File路径为:${file.path}');
|
|
|
-
|
|
|
- if (result.isSuccess) {
|
|
|
- state = state.copyWith(key: result.data?.key, imgFilePath: file.path);
|
|
|
+ // 获取 Base64 字符串并去掉前缀
|
|
|
+ String base64String = result.data?.img ?? '';
|
|
|
+ String fileExtension = ''; // 用于保存文件扩展名
|
|
|
+
|
|
|
+ // 检查图像类型并去掉正确的前缀
|
|
|
+ if (base64String.startsWith('data:image/jpeg;base64,')) {
|
|
|
+ base64String = base64String.replaceFirst('data:image/jpeg;base64,', '');
|
|
|
+ fileExtension = '.jpg'; // 设置文件扩展名为 .jpg
|
|
|
+ } else if (base64String.startsWith('data:image/png;base64,')) {
|
|
|
+ base64String = base64String.replaceFirst('data:image/png;base64,', '');
|
|
|
+ fileExtension = '.png'; // 设置文件扩展名为 .png
|
|
|
} else {
|
|
|
- ToastEngine.show(result.errorMsg ?? "UnKnow Error");
|
|
|
+ // 如果图像格式不支持,可以显示错误信息
|
|
|
+ ToastEngine.show("Unsupported image format");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ // Base64 转为本地图片缓存
|
|
|
+ final bytes = base64.decode(base64String);
|
|
|
+ final tempDir = DirectoryUtil.getTempPath();
|
|
|
+ final file = File('$tempDir/verify${DateTime.now().millisecondsSinceEpoch}$fileExtension'); // 使用正确的文件扩展名
|
|
|
+ await file.writeAsBytes(bytes);
|
|
|
+ Log.d('存入的File路径为:${file.path}');
|
|
|
+
|
|
|
+ if (result.isSuccess) {
|
|
|
+ state = state.copyWith(key: result.data?.key, imgFilePath: file.path);
|
|
|
+ } else {
|
|
|
+ ToastEngine.show(result.errorMsg ?? "Unknown Error");
|
|
|
+ }
|
|
|
+ } catch (e) {
|
|
|
+ Log.e('Error decoding Base64 image: $e');
|
|
|
+ ToastEngine.show("Error decoding image");
|
|
|
}
|
|
|
}
|
|
|
|