diff --git a/lib/util/download/download.dart b/lib/util/download/download.dart new file mode 100644 index 0000000..5c44221 --- /dev/null +++ b/lib/util/download/download.dart @@ -0,0 +1,53 @@ +import 'dart:collection'; + +import 'package:dio/dio.dart'; +import 'package:flutter/material.dart'; +import 'package:fluttertoast/fluttertoast.dart'; +import 'package:image_gallery_saver/image_gallery_saver.dart'; +import 'package:path_provider/path_provider.dart'; +import 'package:permission_handler/permission_handler.dart'; + +class Download { + static HashMap hashMap = HashMap(); + + Future fileToGallery( + String url, { + String postfix, + Function(int, int) progress, + }) async { + // 检查是否已有读写内存的权限 + bool status = await Permission.storage.isGranted; + + //判断如果还没拥有读写权限就申请获取权限 + if (status || await Permission.storage.request().isGranted) { + if (hashMap.containsKey(url)) { + return "正在下载"; + } else { + hashMap[url] = false; + } + try { + var strs = url.split('.'); + if (strs.length < 0) { + return "文件链接错误"; + } + var appDocDir = await getTemporaryDirectory(); + String savePath = appDocDir.path + "/" + DateTime.now().toIso8601String().toString() + (strs[strs.length - 1].length > 4 ? (postfix ?? "") : strs[strs.length - 1]); + await Dio().download(url, savePath, onReceiveProgress: (count, toal) { + if(progress!=null){ + progress(count, toal); + } + }); + print(savePath); + final result = await ImageGallerySaver.saveFile(savePath); + print(result); + return "保存成功"; + } catch (e) { + return "下载失败,原因:" + e.toString(); + } finally { + hashMap.remove(url); + } + } else { + return "请前往系统设置开启应用的内存读取权限再尝试"; + } + } +} diff --git a/lib/util/net_util.dart b/lib/util/net_util.dart index 82c3f6a..a0f76a1 100644 --- a/lib/util/net_util.dart +++ b/lib/util/net_util.dart @@ -55,7 +55,7 @@ class NetUtil { if (_dio == null) { var setting = await NativeUtil.getSetting(); String domain = setting['domain']; //'http://www.hairuyi.com/'; - _config(domain, proxyUrl: '192.168.0.66:8866'); + _config(domain, proxyUrl: '192.168.0.102:8866'); } return _dio; } diff --git a/lib/zhiying_comm.dart b/lib/zhiying_comm.dart index 69b38c6..29f6b73 100644 --- a/lib/zhiying_comm.dart +++ b/lib/zhiying_comm.dart @@ -36,4 +36,5 @@ export 'package:cached_network_image/cached_network_image.dart'; export 'util/photo_preview/photo_preview.dart'; export 'models/base/base_change_notifier.dart'; export 'util/parse_util.dart'; +export 'util/download/Download.dart'; diff --git a/pubspec.yaml b/pubspec.yaml index ee1fa8e..903d8a0 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -61,6 +61,17 @@ dependencies: # fluwx_no_pay: 2.3.1 # loading 动画 loading_indicator: ^1.2.0 + #权限授权 + permission_handler: + git: + url: 'http://192.168.0.138:3000/FnuoOS_Flutter_Components/permission_handler.git' + ref: '0.0.1' + #文件夹路径管理 + path_provider: ^1.4.0 + + #保存文件到相册 + image_gallery_saver: ^1.5.0 + dev_dependencies: