@4399ywkf/design 1.3.9 → 1.3.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -14,6 +14,27 @@ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len
|
|
|
14
14
|
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
|
|
15
15
|
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
|
16
16
|
import { useCallback, useRef, useState } from 'react';
|
|
17
|
+
// 在文件顶部添加工具函数
|
|
18
|
+
var getFilenameFromUrl = function getFilenameFromUrl(url) {
|
|
19
|
+
var fallback = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'image.png';
|
|
20
|
+
try {
|
|
21
|
+
// 从URL中提取文件名
|
|
22
|
+
var urlObj = new URL(url);
|
|
23
|
+
var pathname = urlObj.pathname;
|
|
24
|
+
var filename = pathname.split('/').pop();
|
|
25
|
+
|
|
26
|
+
// 如果提取到了文件名且包含扩展名,则使用它
|
|
27
|
+
if (filename && filename.includes('.')) {
|
|
28
|
+
return filename;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// 否则使用fallback
|
|
32
|
+
return fallback;
|
|
33
|
+
} catch (_unused) {
|
|
34
|
+
return fallback;
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
|
|
17
38
|
/**
|
|
18
39
|
* 增强版图片下载钩子
|
|
19
40
|
*/
|
|
@@ -35,7 +56,7 @@ export var useDownloadImage = function useDownloadImage() {
|
|
|
35
56
|
// 单张图片下载
|
|
36
57
|
var downloadImage = useCallback( /*#__PURE__*/function () {
|
|
37
58
|
var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(url, options) {
|
|
38
|
-
var _abortControllerRef$c, _response$headers$get, _response$headers$get2, _options$onSuccess, response, _options$onError, filename, blob, finalBlob, objectUrl, link, _options$onError2, err;
|
|
59
|
+
var _abortControllerRef$c, _response$headers$get, _response$headers$get2, _options$onSuccess, response, _options$onError, errMsg, filename, blob, finalBlob, objectUrl, link, _link, _options$onError2, err;
|
|
39
60
|
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
40
61
|
while (1) switch (_context.prev = _context.next) {
|
|
41
62
|
case 0:
|
|
@@ -44,37 +65,55 @@ export var useDownloadImage = function useDownloadImage() {
|
|
|
44
65
|
setError(null);
|
|
45
66
|
setProgress(0);
|
|
46
67
|
abortControllerRef.current = new AbortController();
|
|
47
|
-
|
|
68
|
+
console.log('🟩 [downloadImage] 发起请求...', url, options);
|
|
69
|
+
_context.next = 8;
|
|
48
70
|
return fetch(url, _objectSpread(_objectSpread({}, options === null || options === void 0 ? void 0 : options.fetchOptions), {}, {
|
|
49
71
|
signal: (_abortControllerRef$c = abortControllerRef.current) === null || _abortControllerRef$c === void 0 ? void 0 : _abortControllerRef$c.signal
|
|
50
72
|
}));
|
|
51
|
-
case
|
|
73
|
+
case 8:
|
|
52
74
|
response = _context.sent;
|
|
75
|
+
console.log('🟩 [downloadImage] 响应状态:', response.status, response.statusText);
|
|
76
|
+
console.log('🟩 [downloadImage] 响应类型:', response.type);
|
|
77
|
+
console.log('🟩 [downloadImage] Content-Type:', response.headers.get('content-type'));
|
|
78
|
+
console.log('🟩 [downloadImage] Content-Disposition:', response.headers.get('content-disposition'));
|
|
53
79
|
if (response.ok) {
|
|
54
|
-
_context.next =
|
|
80
|
+
_context.next = 18;
|
|
55
81
|
break;
|
|
56
82
|
}
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
83
|
+
errMsg = "\u4E0B\u8F7D\u5931\u8D25: ".concat(response.status, " ").concat(response.statusText);
|
|
84
|
+
console.error('❌ [downloadImage] 响应非 200:', errMsg);
|
|
85
|
+
options === null || options === void 0 || (_options$onError = options.onError) === null || _options$onError === void 0 || _options$onError.call(options, new Error(errMsg));
|
|
86
|
+
throw new Error(errMsg);
|
|
87
|
+
case 18:
|
|
60
88
|
// // 检查内容类型
|
|
61
89
|
filename = (_response$headers$get = (_response$headers$get2 = response.headers.get('Content-Disposition')) === null || _response$headers$get2 === void 0 || (_response$headers$get2 = _response$headers$get2.split('filename=')) === null || _response$headers$get2 === void 0 ? void 0 : _response$headers$get2[1]) !== null && _response$headers$get !== void 0 ? _response$headers$get : 'image.png';
|
|
62
|
-
|
|
90
|
+
console.log('🟩 [downloadImage] 解析文件名:', filename);
|
|
91
|
+
_context.next = 22;
|
|
63
92
|
return response.blob();
|
|
64
|
-
case
|
|
93
|
+
case 22:
|
|
65
94
|
blob = _context.sent;
|
|
95
|
+
console.log('🟩 [downloadImage] Blob 大小:', blob.size, '字节');
|
|
96
|
+
|
|
97
|
+
// 检查 blob 是否为空
|
|
98
|
+
if (blob.size === 0) {
|
|
99
|
+
console.warn('⚠️ [downloadImage] Blob 内容为空,可能是资源无效或被拦截。');
|
|
100
|
+
}
|
|
101
|
+
|
|
66
102
|
// 处理图片压缩(如果需要)
|
|
67
103
|
finalBlob = blob;
|
|
68
104
|
if (!(options !== null && options !== void 0 && options.compress && options.quality)) {
|
|
69
|
-
_context.next =
|
|
105
|
+
_context.next = 32;
|
|
70
106
|
break;
|
|
71
107
|
}
|
|
72
|
-
|
|
108
|
+
console.log('🟨 [downloadImage] 启用压缩, 质量:', options.quality);
|
|
109
|
+
_context.next = 30;
|
|
73
110
|
return compressImage(blob, options.quality);
|
|
74
|
-
case
|
|
111
|
+
case 30:
|
|
75
112
|
finalBlob = _context.sent;
|
|
76
|
-
|
|
113
|
+
console.log('🟨 [downloadImage] 压缩后大小:', finalBlob.size);
|
|
114
|
+
case 32:
|
|
77
115
|
objectUrl = URL.createObjectURL(finalBlob);
|
|
116
|
+
console.log('🟩 [downloadImage] 生成 Object URL:', objectUrl);
|
|
78
117
|
link = document.createElement('a');
|
|
79
118
|
link.href = objectUrl;
|
|
80
119
|
// 如果没有提供文件名,则使用默认名称
|
|
@@ -87,29 +126,48 @@ export var useDownloadImage = function useDownloadImage() {
|
|
|
87
126
|
URL.revokeObjectURL(objectUrl);
|
|
88
127
|
setProgress(100);
|
|
89
128
|
options === null || options === void 0 || (_options$onSuccess = options.onSuccess) === null || _options$onSuccess === void 0 || _options$onSuccess.call(options);
|
|
129
|
+
console.log('✅ [downloadImage] 下载完成:', filename);
|
|
90
130
|
return _context.abrupt("return", {
|
|
91
131
|
success: true
|
|
92
132
|
});
|
|
93
|
-
case
|
|
94
|
-
_context.prev =
|
|
133
|
+
case 47:
|
|
134
|
+
_context.prev = 47;
|
|
95
135
|
_context.t0 = _context["catch"](0);
|
|
96
|
-
|
|
97
|
-
|
|
136
|
+
_context.prev = 49;
|
|
137
|
+
_link = document.createElement('a');
|
|
138
|
+
_link.href = url;
|
|
139
|
+
_link.download = (options === null || options === void 0 ? void 0 : options.filename) || getFilenameFromUrl(url);
|
|
140
|
+
_link.target = '_blank';
|
|
141
|
+
|
|
142
|
+
// 触发点击
|
|
143
|
+
document.body.appendChild(_link);
|
|
144
|
+
_link.click();
|
|
145
|
+
|
|
146
|
+
// 清理
|
|
147
|
+
document.body.removeChild(_link);
|
|
148
|
+
return _context.abrupt("return", {
|
|
149
|
+
success: true
|
|
150
|
+
});
|
|
151
|
+
case 60:
|
|
152
|
+
_context.prev = 60;
|
|
153
|
+
_context.t1 = _context["catch"](49);
|
|
154
|
+
err = _context.t1 instanceof Error ? _context.t1 : new Error('未知错误');
|
|
98
155
|
setError(err);
|
|
99
156
|
options === null || options === void 0 || (_options$onError2 = options.onError) === null || _options$onError2 === void 0 || _options$onError2.call(options, err);
|
|
100
157
|
return _context.abrupt("return", {
|
|
101
158
|
success: false,
|
|
102
159
|
error: err
|
|
103
160
|
});
|
|
104
|
-
case
|
|
105
|
-
_context.prev =
|
|
161
|
+
case 66:
|
|
162
|
+
_context.prev = 66;
|
|
106
163
|
setLoading(false);
|
|
107
|
-
|
|
108
|
-
|
|
164
|
+
console.log('🧹 [downloadImage] 状态重置完成。');
|
|
165
|
+
return _context.finish(66);
|
|
166
|
+
case 70:
|
|
109
167
|
case "end":
|
|
110
168
|
return _context.stop();
|
|
111
169
|
}
|
|
112
|
-
}, _callee, null, [[0,
|
|
170
|
+
}, _callee, null, [[0, 47, 66, 70], [49, 60]]);
|
|
113
171
|
}));
|
|
114
172
|
return function (_x, _x2) {
|
|
115
173
|
return _ref.apply(this, arguments);
|