@haluo/util 2.0.1 → 2.0.2-0.1

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.
Files changed (61) hide show
  1. package/README.md +12 -1
  2. package/dist/index.d.ts +39 -0
  3. package/dist/index.js +74 -33
  4. package/dist/modules/cookie/index.d.ts +27 -0
  5. package/dist/modules/cookie/index.js +55 -54
  6. package/dist/modules/date/index.d.ts +52 -0
  7. package/dist/modules/date/index.js +192 -192
  8. package/dist/modules/dom/index.d.ts +28 -0
  9. package/dist/modules/dom/index.js +62 -62
  10. package/dist/modules/filter/index.d.ts +24 -0
  11. package/dist/modules/filter/index.js +48 -44
  12. package/dist/modules/format/index.d.ts +15 -0
  13. package/dist/modules/format/index.js +22 -21
  14. package/dist/modules/match/index.d.ts +12 -0
  15. package/dist/modules/match/index.js +31 -31
  16. package/dist/modules/monitor/index.d.ts +3 -0
  17. package/dist/modules/monitor/index.js +17 -17
  18. package/dist/modules/monitor/lib/jsError.d.ts +1 -0
  19. package/dist/modules/monitor/lib/jsError.js +53 -53
  20. package/dist/modules/monitor/lib/timing.d.ts +1 -0
  21. package/dist/modules/monitor/lib/timing.js +69 -69
  22. package/dist/modules/monitor/lib/xhr.d.ts +1 -0
  23. package/dist/modules/monitor/lib/xhr.js +48 -48
  24. package/dist/modules/monitor/utils/onload.d.ts +1 -0
  25. package/dist/modules/monitor/utils/onload.js +11 -11
  26. package/dist/modules/monitor/utils/tracker.d.ts +7 -0
  27. package/dist/modules/monitor/utils/tracker.js +63 -63
  28. package/dist/modules/number/index.d.ts +47 -0
  29. package/dist/modules/number/index.js +118 -102
  30. package/dist/modules/open-app/index.d.ts +84 -0
  31. package/dist/modules/open-app/index.js +239 -0
  32. package/dist/modules/sentry/index.d.ts +15 -0
  33. package/dist/modules/sentry/index.js +81 -81
  34. package/dist/modules/tools/index.d.ts +166 -0
  35. package/dist/modules/tools/index.js +393 -393
  36. package/dist/modules/upload/ALIOSS_EXAMPLE.d.ts +12 -0
  37. package/dist/modules/upload/ALIOSS_EXAMPLE.js +320 -0
  38. package/dist/modules/upload/aliOss.d.ts +324 -0
  39. package/dist/modules/upload/aliOss.js +838 -0
  40. package/dist/modules/upload/index.d.ts +51 -0
  41. package/dist/modules/upload/index.js +55 -0
  42. package/dist/tsconfig.tsbuildinfo +1 -1
  43. package/dist/types/index.d.ts +3 -11
  44. package/dist/types/index.js +2 -2
  45. package/dist/types/modules/cookie/index.d.ts +27 -25
  46. package/dist/types/modules/date/index.d.ts +52 -1
  47. package/dist/types/modules/dom/index.d.ts +28 -1
  48. package/dist/types/modules/filter/index.d.ts +24 -25
  49. package/dist/types/modules/format/index.d.ts +15 -13
  50. package/dist/types/modules/match/index.d.ts +12 -1
  51. package/dist/types/modules/monitor/index.d.ts +3 -3
  52. package/dist/types/modules/monitor/lib/jsError.d.ts +1 -1
  53. package/dist/types/modules/monitor/lib/timing.d.ts +1 -1
  54. package/dist/types/modules/monitor/lib/xhr.d.ts +1 -1
  55. package/dist/types/modules/monitor/utils/onload.d.ts +1 -1
  56. package/dist/types/modules/monitor/utils/tracker.d.ts +7 -7
  57. package/dist/types/modules/number/index.d.ts +41 -39
  58. package/dist/types/modules/sentry/index.d.ts +15 -15
  59. package/dist/types/modules/tools/index.d.ts +166 -1
  60. package/dist/types/types/index.d.ts +3 -3
  61. package/package.json +18 -14
@@ -0,0 +1,838 @@
1
+ "use strict";
2
+ /**
3
+ * @file 阿里云OSS上传统一工具类
4
+ * @description 整合所有项目的aliOss业务逻辑,通过传参形式支持不同业务场景
5
+ * @Author: wanghui
6
+ * @createBy: @2025.11.17
7
+ *
8
+ * 文档:https://help.aliyun.com/document_detail/64041.html
9
+ *
10
+ * 使用示例:
11
+ * ```typescript
12
+ * import { createAliOssUploader } from '@haluo/util'
13
+ *
14
+ * // 创建上传实例,传入业务相关的API函数
15
+ * const ossUploader = createAliOssUploader({
16
+ * getAliyunSts: (params) => api.getAliyunSts(params),
17
+ * darkWaterUploadImage: (params) => api.darkWaterUploadImage(params),
18
+ * multiTransferImage: (params) => api.multiTransferImage(params),
19
+ * aliyunPersist: (params) => api.aliyunPersist(params),
20
+ * generatePrePresignedUrl: (params) => api.generatePrePresignedUrl(params),
21
+ * uploadConf: (params) => api.uploadConf(params),
22
+ * dateFormat: util.date.format, // 或 util.filter.format
23
+ * messageWarning: (msg) => window.$message.warning(msg)
24
+ * })
25
+ *
26
+ * // 图片上传
27
+ * ossUploader.ossUploadImage({
28
+ * file: file,
29
+ * imageType: 'official', // nowater、official、panoram、forum、avatar、square、carport
30
+ * quality: 0.7, // 压缩质量
31
+ * businessType: 1500, // 业务类型:0(无水印)、1(头像)、4(GIF)、6(视频)、8(UnProtect)、1000(文章)、1199(私有)、1299(加密)、1500(默认)
32
+ * batchTransfer: false, // 是否批量转换水印
33
+ * notCompress: false, // 是否不压缩
34
+ * idCard: false, // 是否身份证上传
35
+ * onProgress: (e) => console.log(e.percent),
36
+ * onSuccess: (val) => console.log(val),
37
+ * onError: (err) => console.error(err)
38
+ * })
39
+ *
40
+ * // 文件上传
41
+ * ossUploader.ossUploadFile({
42
+ * file: file,
43
+ * isVideo: false, // 是否视频
44
+ * isUnProtect: false, // 是否UnProtect
45
+ * isDocument: false, // 是否文档
46
+ * businessType: 1500,
47
+ * onProgress: (e) => console.log(e.percent),
48
+ * onSuccess: (val) => console.log(val),
49
+ * onError: (err) => console.error(err)
50
+ * })
51
+ * ```
52
+ */
53
+ var __assign = (this && this.__assign) || function () {
54
+ __assign = Object.assign || function(t) {
55
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
56
+ s = arguments[i];
57
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
58
+ t[p] = s[p];
59
+ }
60
+ return t;
61
+ };
62
+ return __assign.apply(this, arguments);
63
+ };
64
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
65
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
66
+ return new (P || (P = Promise))(function (resolve, reject) {
67
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
68
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
69
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
70
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
71
+ });
72
+ };
73
+ var __generator = (this && this.__generator) || function (thisArg, body) {
74
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
75
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
76
+ function verb(n) { return function (v) { return step([n, v]); }; }
77
+ function step(op) {
78
+ if (f) throw new TypeError("Generator is already executing.");
79
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
80
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
81
+ if (y = 0, t) op = [op[0] & 2, t.value];
82
+ switch (op[0]) {
83
+ case 0: case 1: t = op; break;
84
+ case 4: _.label++; return { value: op[1], done: false };
85
+ case 5: _.label++; y = op[1]; op = [0]; continue;
86
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
87
+ default:
88
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
89
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
90
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
91
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
92
+ if (t[2]) _.ops.pop();
93
+ _.trys.pop(); continue;
94
+ }
95
+ op = body.call(thisArg, _);
96
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
97
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
98
+ }
99
+ };
100
+ var __importDefault = (this && this.__importDefault) || function (mod) {
101
+ return (mod && mod.__esModule) ? mod : { "default": mod };
102
+ };
103
+ Object.defineProperty(exports, "__esModule", { value: true });
104
+ exports.createAliOssUploader = exports.AliOssClass = exports.SuffixEnum = exports.BusinessType = void 0;
105
+ var ali_oss_1 = __importDefault(require("ali-oss"));
106
+ var lrz_1 = __importDefault(require("lrz"));
107
+ // ==================== 常量定义 ====================
108
+ /** 支持的图片格式 */
109
+ var SUPPORTED_IMAGE_TYPES = ['image/png', 'image/gif', 'image/jpg', 'image/jpeg'];
110
+ /** 小图片阈值(KB) */
111
+ var SMALL_IMAGE_THRESHOLD = 100;
112
+ /** 长图宽高比阈值 */
113
+ var LONG_IMAGE_RATIO = 2;
114
+ /** 默认文件大小限制(MB) */
115
+ var DEFAULT_FILE_SIZE_LIMIT = 10;
116
+ /** 默认压缩质量 */
117
+ var DEFAULT_QUALITY = 0.7;
118
+ /** 默认图片类型 */
119
+ var DEFAULT_IMAGE_TYPE = 'official';
120
+ /** OSS默认区域 */
121
+ var DEFAULT_OSS_REGION = 'oss-cn-beijing';
122
+ /** 预签名URL过期时间(毫秒) */
123
+ var PRESIGNED_URL_EXPIRE_TIME = 60000;
124
+ /** 随机字符串字符集 */
125
+ var RANDOM_CHARS = '0123456789abcdefghijklmnopqrstuvwxyz';
126
+ /** OSS元数据 */
127
+ var OSS_META_PEOPLE = 'x-oss-meta-motor';
128
+ // ==================== 枚举定义 ====================
129
+ /**
130
+ * 业务类型枚举
131
+ * 不同的businessType对应不同的OSS bucket和水印策略
132
+ */
133
+ var BusinessType;
134
+ (function (BusinessType) {
135
+ BusinessType[BusinessType["NO_WATER"] = 0] = "NO_WATER";
136
+ BusinessType[BusinessType["AVATAR"] = 1] = "AVATAR";
137
+ BusinessType[BusinessType["GIF"] = 4] = "GIF";
138
+ BusinessType[BusinessType["VIDEO"] = 6] = "VIDEO";
139
+ BusinessType[BusinessType["UNPROTECT"] = 8] = "UNPROTECT";
140
+ BusinessType[BusinessType["ARTICLE"] = 1000] = "ARTICLE";
141
+ BusinessType[BusinessType["PRIVATE"] = 1199] = "PRIVATE";
142
+ BusinessType[BusinessType["ENCRYPTED"] = 1299] = "ENCRYPTED";
143
+ BusinessType[BusinessType["DEFAULT"] = 1500] = "DEFAULT"; // 默认
144
+ })(BusinessType = exports.BusinessType || (exports.BusinessType = {}));
145
+ /**
146
+ * 图片类型后缀枚举
147
+ */
148
+ exports.SuffixEnum = {
149
+ nowater: '!nowater',
150
+ official: '!official',
151
+ panoram: '!panoram',
152
+ forum: '!forum',
153
+ avatar: '!avatar',
154
+ square: '!square',
155
+ carport: '!carport' // 车库、经销商水印
156
+ };
157
+ /** 不打水印的图片类型 */
158
+ var NO_WATER_IMAGE_TYPES = ['nowater', 'forum'];
159
+ // ==================== 工具函数 ====================
160
+ /**
161
+ * 生成随机字符串
162
+ * @param length 字符串长度
163
+ * @returns 随机字符串
164
+ */
165
+ function randomString(length) {
166
+ var result = '';
167
+ for (var i = 0; i < length; i++) {
168
+ var randomIndex = Math.floor(Math.random() * RANDOM_CHARS.length);
169
+ result += RANDOM_CHARS[randomIndex];
170
+ }
171
+ return result;
172
+ }
173
+ /**
174
+ * 过滤对象中的undefined值
175
+ * @param obj 原始对象
176
+ * @returns 过滤后的对象
177
+ */
178
+ function filterUndefined(obj) {
179
+ var result = {};
180
+ Object.keys(obj).forEach(function (key) {
181
+ if (obj[key] !== undefined) {
182
+ result[key] = obj[key];
183
+ }
184
+ });
185
+ return result;
186
+ }
187
+ /**
188
+ * 检查是否为不打水印的图片类型
189
+ * @param imageType 图片类型
190
+ * @returns 是否不打水印
191
+ */
192
+ function isNoWaterImageType(imageType) {
193
+ return NO_WATER_IMAGE_TYPES.includes(imageType);
194
+ }
195
+ /**
196
+ * 检查文件类型是否支持
197
+ * @param fileType 文件类型
198
+ * @returns 是否支持
199
+ */
200
+ function isSupportedImageType(fileType) {
201
+ return SUPPORTED_IMAGE_TYPES.includes(fileType);
202
+ }
203
+ /**
204
+ * 计算文件大小(KB)
205
+ * @param size 文件大小(字节)
206
+ * @returns 文件大小(KB)
207
+ */
208
+ function getFileSizeInKB(size) {
209
+ return Math.floor(size / 1024);
210
+ }
211
+ /**
212
+ * 计算文件大小(MB)
213
+ * @param size 文件大小(字节)
214
+ * @returns 文件大小(MB)
215
+ */
216
+ function getFileSizeInMB(size) {
217
+ return size / (1024 * 1024);
218
+ }
219
+ // ==================== 主类 ====================
220
+ /**
221
+ * 阿里云OSS上传类
222
+ * 整合所有项目的aliOss业务逻辑,通过依赖注入实现业务API解耦
223
+ */
224
+ var AliOssClass = /** @class */ (function () {
225
+ function AliOssClass(apiConfig) {
226
+ /**
227
+ * 生成随机字符串
228
+ * @param num 字符串长度
229
+ */
230
+ this.randomString = randomString;
231
+ /**
232
+ * 后缀枚举
233
+ */
234
+ this.suffixEnum = exports.SuffixEnum;
235
+ /**
236
+ * 业务类型枚举
237
+ */
238
+ this.businessType = BusinessType;
239
+ this.apiConfig = apiConfig;
240
+ this.clientCache = new Map();
241
+ }
242
+ /**
243
+ * 创建OSS客户端
244
+ * @param businessType 业务类型
245
+ * @param imggeCarType 车辆图片类型(可选,用于特定业务)
246
+ * @returns OSS客户端实例
247
+ */
248
+ AliOssClass.prototype.createOssClient = function (businessType, imggeCarType) {
249
+ return __awaiter(this, void 0, void 0, function () {
250
+ var cacheKey, res, clientParams, filteredParams, client;
251
+ return __generator(this, function (_a) {
252
+ switch (_a.label) {
253
+ case 0:
254
+ cacheKey = "".concat(businessType, "_").concat(imggeCarType || 'default');
255
+ return [4 /*yield*/, this.apiConfig.getAliyunSts({ businessType: businessType, imggeCarType: imggeCarType })];
256
+ case 1:
257
+ res = _a.sent();
258
+ if (res.data.code !== 0) {
259
+ throw new Error('获取OSS配置失败');
260
+ }
261
+ clientParams = res.data.data || {};
262
+ clientParams.suffix = clientParams.style || '';
263
+ filteredParams = filterUndefined(clientParams);
264
+ client = new ali_oss_1.default(__assign({ region: DEFAULT_OSS_REGION }, filteredParams));
265
+ // 保存客户端选项供后续使用
266
+ client.options = __assign(__assign({}, client.options), { realmName: clientParams.realmName, suffix: clientParams.suffix });
267
+ this.clientCache.set(cacheKey, client);
268
+ return [2 /*return*/, client];
269
+ }
270
+ });
271
+ });
272
+ };
273
+ /**
274
+ * 预加载图片获取尺寸
275
+ * @param file 文件对象
276
+ * @returns 图片对象
277
+ */
278
+ AliOssClass.prototype.imgUpload = function (file) {
279
+ return new Promise(function (resolve, reject) {
280
+ var URL = window.URL || window.webkitURL;
281
+ var image = new Image();
282
+ var objectUrl = URL.createObjectURL(file);
283
+ image.onload = function () {
284
+ URL.revokeObjectURL(objectUrl); // 释放内存
285
+ resolve(image);
286
+ };
287
+ image.onerror = function () {
288
+ URL.revokeObjectURL(objectUrl); // 释放内存
289
+ reject(new Error('图片加载失败'));
290
+ };
291
+ image.src = objectUrl;
292
+ });
293
+ };
294
+ /**
295
+ * 生成文件名
296
+ * @param imageType 图片类型
297
+ * @param extension 文件扩展名
298
+ * @returns 文件名信息
299
+ */
300
+ AliOssClass.prototype.generateFileName = function (imageType, extension) {
301
+ var now = new Date();
302
+ var year = this.apiConfig.dateFormat(now, 'YYYY');
303
+ var date = this.apiConfig.dateFormat(now, 'YYYYMMDD');
304
+ var dateTime = this.apiConfig.dateFormat(now, 'YYYYMMDDHHmmss');
305
+ var randomStr = randomString(4);
306
+ var fileName = "".concat(imageType, "/").concat(date, "/").concat(dateTime, "_").concat(randomStr).concat(extension);
307
+ return { year: year, fileName: fileName };
308
+ };
309
+ /**
310
+ * 检查图片是否为长图
311
+ * @param width 图片宽度
312
+ * @param height 图片高度
313
+ * @returns 是否为长图
314
+ */
315
+ AliOssClass.prototype.isLongImage = function (width, height) {
316
+ var maxDimension = Math.max(width, height);
317
+ var minDimension = Math.min(width, height);
318
+ return maxDimension / minDimension > LONG_IMAGE_RATIO;
319
+ };
320
+ /**
321
+ * 执行OSS上传
322
+ * @param client OSS客户端
323
+ * @param fileName 文件名
324
+ * @param file 文件对象
325
+ * @param year 年份
326
+ * @param mimeType MIME类型
327
+ * @param onProgress 进度回调
328
+ * @returns 上传结果
329
+ */
330
+ AliOssClass.prototype.performOssUpload = function (client, fileName, file, year, mimeType, onProgress) {
331
+ return __awaiter(this, void 0, void 0, function () {
332
+ return __generator(this, function (_a) {
333
+ switch (_a.label) {
334
+ case 0: return [4 /*yield*/, client.multipartUpload(fileName, file, {
335
+ progress: function (p) {
336
+ onProgress === null || onProgress === void 0 ? void 0 : onProgress({ percent: Math.floor(p * 100) });
337
+ },
338
+ meta: { year: year, people: OSS_META_PEOPLE },
339
+ mime: mimeType
340
+ })];
341
+ case 1: return [2 /*return*/, _a.sent()];
342
+ }
343
+ });
344
+ });
345
+ };
346
+ /**
347
+ * 确定业务类型
348
+ * @param option 上传选项
349
+ * @param isGif 是否为GIF
350
+ * @returns 业务类型
351
+ */
352
+ AliOssClass.prototype.determineBusinessType = function (option, isGif) {
353
+ // 如果已指定businessType,直接使用
354
+ if (option.businessType !== undefined) {
355
+ return option.businessType;
356
+ }
357
+ var imageType = option.imageType || DEFAULT_IMAGE_TYPE;
358
+ // 注释:GIF图片使用businessType=4
359
+ if (isGif) {
360
+ return BusinessType.GIF;
361
+ }
362
+ // 注释:根据imageType确定默认businessType
363
+ if (imageType === 'avatar') {
364
+ return BusinessType.AVATAR;
365
+ }
366
+ if (isNoWaterImageType(imageType)) {
367
+ return BusinessType.NO_WATER;
368
+ }
369
+ return BusinessType.DEFAULT;
370
+ };
371
+ /**
372
+ * 加载图片并返回结果
373
+ * 用于标准的图片上传场景
374
+ * @param params 加载参数
375
+ */
376
+ AliOssClass.prototype.loadImage = function (params) {
377
+ var url = params.url, val = params.val, suffix = params.suffix, file = params.file, option = params.option, resolve = params.resolve, reject = params.reject, isGif = params.isGif;
378
+ var img = new Image();
379
+ img.src = url;
380
+ img.onload = function () {
381
+ var _a;
382
+ if (!img)
383
+ return;
384
+ val.imgOrgUrl = "".concat(url, "?_").concat(img.width, "_").concat(img.height);
385
+ // 注释:全景图和GIF图片缩略图后缀不带300,其他需要
386
+ val.imgUrl =
387
+ suffix === '!panoram' || isGif
388
+ ? "".concat(url, "?_").concat(img.width, "_").concat(img.height)
389
+ : "".concat(url, "300?_").concat(img.width, "_").concat(img.height);
390
+ val.name = file.name;
391
+ val.fileName = file.name;
392
+ (_a = option.onSuccess) === null || _a === void 0 ? void 0 : _a.call(option, val);
393
+ resolve(val);
394
+ img = null;
395
+ };
396
+ img.onerror = function () {
397
+ var _a;
398
+ (_a = option.onError) === null || _a === void 0 ? void 0 : _a.call(option, '图片加载失败');
399
+ reject('图片加载失败');
400
+ img = null;
401
+ };
402
+ };
403
+ /**
404
+ * 加载图片并返回结果(新版)
405
+ * 用于avatar、idCard等特殊场景,返回格式不同
406
+ * @param params 加载参数
407
+ */
408
+ AliOssClass.prototype.loadImageNew = function (params) {
409
+ var url = params.url, val = params.val, file = params.file, option = params.option, resolve = params.resolve, reject = params.reject;
410
+ var img = new Image();
411
+ img.src = url;
412
+ img.onload = function () {
413
+ var _a;
414
+ val.imgOrgUrl = url;
415
+ val.imgUrl = val.name;
416
+ val.name = file.name;
417
+ val.fileName = file.name;
418
+ (_a = option.onSuccess) === null || _a === void 0 ? void 0 : _a.call(option, val);
419
+ resolve(val);
420
+ img = null;
421
+ };
422
+ img.onerror = function () {
423
+ var _a;
424
+ (_a = option.onError) === null || _a === void 0 ? void 0 : _a.call(option, '图片加载失败');
425
+ reject('图片加载失败');
426
+ img = null;
427
+ };
428
+ };
429
+ /**
430
+ * 图片上传主方法
431
+ * 整合了所有项目的图片上传逻辑
432
+ * @param option 上传选项
433
+ * @returns Promise
434
+ */
435
+ AliOssClass.prototype.ossUploadImage = function (option) {
436
+ var _a, _b;
437
+ return __awaiter(this, void 0, void 0, function () {
438
+ var file, callbacks;
439
+ var _this = this;
440
+ return __generator(this, function (_c) {
441
+ file = option.file;
442
+ callbacks = {
443
+ onError: option.onError || (function () { }),
444
+ onSuccess: option.onSuccess || (function () { }),
445
+ onProgress: option.onProgress || (function () { })
446
+ };
447
+ option.onError = callbacks.onError;
448
+ option.onSuccess = callbacks.onSuccess;
449
+ option.onProgress = callbacks.onProgress;
450
+ // 文件类型验证
451
+ if (!isSupportedImageType(file.type)) {
452
+ callbacks.onError('');
453
+ (_b = (_a = this.apiConfig).messageWarning) === null || _b === void 0 ? void 0 : _b.call(_a, '上传失败,请上传后缀为png、gif、jpg的文件');
454
+ return [2 /*return*/, Promise.reject('文件类型不支持')];
455
+ }
456
+ return [2 /*return*/, new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
457
+ var imageType, suffix, extensionName, _a, year, fileName, isSmallImage, quality, isGif, idCard, businessType, client_1, result_1, val, res, res, image, rst, isLong, postFile, imgSize, maxSizeMB, errorMsg, client, result, res, res, clientParams, url, error_1, errorMsg;
458
+ return __generator(this, function (_b) {
459
+ switch (_b.label) {
460
+ case 0:
461
+ _b.trys.push([0, 16, , 17]);
462
+ imageType = option.imageType || DEFAULT_IMAGE_TYPE;
463
+ suffix = exports.SuffixEnum[imageType] || exports.SuffixEnum.official;
464
+ extensionName = ".".concat(file.name.split('.').pop());
465
+ _a = this.generateFileName(imageType, extensionName), year = _a.year, fileName = _a.fileName;
466
+ isSmallImage = getFileSizeInKB(file.size) <= SMALL_IMAGE_THRESHOLD;
467
+ quality = isSmallImage ? 1 : (option.quality || DEFAULT_QUALITY);
468
+ isGif = file.type === 'image/gif';
469
+ idCard = option.idCard || false;
470
+ businessType = this.determineBusinessType(option, isGif);
471
+ if (!(businessType === BusinessType.ENCRYPTED)) return [3 /*break*/, 3];
472
+ return [4 /*yield*/, this.createOssClient(businessType, option.imggeCarType)];
473
+ case 1:
474
+ client_1 = _b.sent();
475
+ return [4 /*yield*/, this.performOssUpload(client_1, fileName, file, year, file.type, callbacks.onProgress)];
476
+ case 2:
477
+ result_1 = _b.sent();
478
+ if (result_1.res.statusCode === 200) {
479
+ val = __assign(__assign({}, result_1), { imgUrl: "".concat(result_1.bucket, "://").concat(result_1.name), imgOrgUrl: "".concat(result_1.bucket, "://").concat(result_1.name), fileName: file.name });
480
+ callbacks.onSuccess(val);
481
+ resolve(val);
482
+ }
483
+ else {
484
+ callbacks.onError('上传失败');
485
+ reject('上传失败');
486
+ }
487
+ return [2 /*return*/];
488
+ case 3:
489
+ if (!(option.batchTransfer && this.apiConfig.multiTransferImage)) return [3 /*break*/, 5];
490
+ return [4 /*yield*/, this.apiConfig.multiTransferImage({ file: file })];
491
+ case 4:
492
+ res = _b.sent();
493
+ if (res.data.code === 0) {
494
+ this.loadImage({
495
+ url: res.data.data,
496
+ val: res.data,
497
+ suffix: suffix,
498
+ option: option,
499
+ file: file,
500
+ resolve: resolve,
501
+ reject: reject
502
+ });
503
+ }
504
+ else {
505
+ callbacks.onError('上传失败');
506
+ reject('上传失败');
507
+ }
508
+ return [2 /*return*/];
509
+ case 5:
510
+ if (!(option.imageType === 'carport' && this.apiConfig.darkWaterUploadImage)) return [3 /*break*/, 7];
511
+ return [4 /*yield*/, this.apiConfig.darkWaterUploadImage({ file: file })];
512
+ case 6:
513
+ res = _b.sent();
514
+ if (res.data.code === 0) {
515
+ this.loadImage({
516
+ url: res.data.data,
517
+ val: res.data,
518
+ suffix: suffix,
519
+ option: option,
520
+ file: file,
521
+ resolve: resolve,
522
+ reject: reject
523
+ });
524
+ }
525
+ else {
526
+ callbacks.onError('上传失败');
527
+ reject('上传失败');
528
+ }
529
+ return [2 /*return*/];
530
+ case 7: return [4 /*yield*/, this.imgUpload(file)
531
+ // 图片压缩
532
+ ];
533
+ case 8:
534
+ image = _b.sent();
535
+ return [4 /*yield*/, (0, lrz_1.default)(file, { quality: quality })
536
+ // 注释:长图压缩有问题,长宽比>2:1的图片不压缩
537
+ ];
538
+ case 9:
539
+ rst = _b.sent();
540
+ isLong = this.isLongImage(image.width, image.height);
541
+ postFile = void 0;
542
+ if (option.notCompress || isGif || isLong) {
543
+ // 注释:GIF、notCompress选项或长图不压缩
544
+ postFile = file;
545
+ }
546
+ else {
547
+ postFile = rst.file;
548
+ }
549
+ imgSize = postFile.size;
550
+ maxSizeMB = DEFAULT_FILE_SIZE_LIMIT;
551
+ if (getFileSizeInMB(imgSize) > maxSizeMB) {
552
+ errorMsg = "\u56FE\u7247\u4E0D\u80FD\u8D85\u8FC7".concat(maxSizeMB, "M");
553
+ callbacks.onError(errorMsg);
554
+ reject(errorMsg);
555
+ return [2 /*return*/];
556
+ }
557
+ // 设置文件名(确保压缩后的文件有名称)
558
+ if (!postFile.name) {
559
+ Object.defineProperty(postFile, 'name', {
560
+ value: file.name,
561
+ writable: false
562
+ });
563
+ }
564
+ return [4 /*yield*/, this.createOssClient(businessType, option.imggeCarType)
565
+ // 执行上传
566
+ ];
567
+ case 10:
568
+ client = _b.sent();
569
+ return [4 /*yield*/, this.performOssUpload(client, fileName, postFile, year, postFile.type, callbacks.onProgress)];
570
+ case 11:
571
+ result = _b.sent();
572
+ if (result.res.statusCode !== 200) {
573
+ callbacks.onError('上传失败');
574
+ reject('上传失败');
575
+ return [2 /*return*/];
576
+ }
577
+ if (!(idCard && this.apiConfig.generatePrePresignedUrl)) return [3 /*break*/, 13];
578
+ return [4 /*yield*/, this.apiConfig.generatePrePresignedUrl({
579
+ objectId: result.name,
580
+ expireMils: PRESIGNED_URL_EXPIRE_TIME
581
+ })];
582
+ case 12:
583
+ res = _b.sent();
584
+ if (res.data.code === 0) {
585
+ this.loadImageNew({
586
+ url: res.data.data,
587
+ val: result,
588
+ file: postFile,
589
+ option: option,
590
+ resolve: resolve,
591
+ reject: reject
592
+ });
593
+ }
594
+ else {
595
+ callbacks.onError('生成预签名URL失败');
596
+ reject('生成预签名URL失败');
597
+ }
598
+ return [2 /*return*/];
599
+ case 13:
600
+ if (!(imageType === 'avatar' && this.apiConfig.aliyunPersist)) return [3 /*break*/, 15];
601
+ return [4 /*yield*/, this.apiConfig.aliyunPersist({
602
+ object: "".concat(result.bucket, "://").concat(result.name),
603
+ businessType: BusinessType.AVATAR
604
+ })];
605
+ case 14:
606
+ res = _b.sent();
607
+ if (res.data.code === 0) {
608
+ this.loadImageNew({
609
+ url: res.data.data,
610
+ val: result,
611
+ file: postFile,
612
+ option: option,
613
+ resolve: resolve,
614
+ reject: reject
615
+ });
616
+ }
617
+ else {
618
+ callbacks.onError('头像处理失败');
619
+ reject('头像处理失败');
620
+ }
621
+ return [2 /*return*/];
622
+ case 15:
623
+ clientParams = client.options;
624
+ url = "".concat(clientParams.realmName).concat(result.name).concat(clientParams.suffix || '');
625
+ this.loadImage({
626
+ url: url,
627
+ val: result,
628
+ suffix: suffix,
629
+ file: postFile,
630
+ option: option,
631
+ resolve: resolve,
632
+ reject: reject,
633
+ isGif: isGif
634
+ });
635
+ return [3 /*break*/, 17];
636
+ case 16:
637
+ error_1 = _b.sent();
638
+ errorMsg = error_1.message || '上传异常';
639
+ callbacks.onError(errorMsg);
640
+ reject(errorMsg);
641
+ return [3 /*break*/, 17];
642
+ case 17: return [2 /*return*/];
643
+ }
644
+ });
645
+ }); })];
646
+ });
647
+ });
648
+ };
649
+ /**
650
+ * 文件上传方法
651
+ * 支持视频、文档、APK等文件类型
652
+ * @param option 上传选项
653
+ * @returns Promise
654
+ */
655
+ AliOssClass.prototype.ossUploadFile = function (option) {
656
+ return __awaiter(this, void 0, void 0, function () {
657
+ var file, callbacks;
658
+ var _this = this;
659
+ return __generator(this, function (_a) {
660
+ file = option.file;
661
+ callbacks = {
662
+ onError: option.onError || (function () { }),
663
+ onSuccess: option.onSuccess || (function () { }),
664
+ onProgress: option.onProgress || (function () { })
665
+ };
666
+ option.onError = callbacks.onError;
667
+ option.onSuccess = callbacks.onSuccess;
668
+ option.onProgress = callbacks.onProgress;
669
+ return [2 /*return*/, new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
670
+ var extensionName, fileType, _a, year, fileName, isVideo, isDocument, isUnProtect, businessType, client, result, clientParams, url, uploadResult, error_2, errorMsg;
671
+ return __generator(this, function (_b) {
672
+ switch (_b.label) {
673
+ case 0:
674
+ _b.trys.push([0, 5, , 6]);
675
+ extensionName = ".".concat(file.name.split('.').pop());
676
+ fileType = extensionName === '.apk' ? 'apk' : 'file';
677
+ _a = this.generateFileName(fileType, extensionName), year = _a.year, fileName = _a.fileName;
678
+ isVideo = !!option.isVideo;
679
+ isDocument = !!option.isDocument;
680
+ isUnProtect = !!option.isUnProtect;
681
+ businessType = option.businessType;
682
+ if (!businessType) {
683
+ if (isUnProtect) {
684
+ businessType = BusinessType.UNPROTECT;
685
+ }
686
+ else if (isVideo || isDocument) {
687
+ businessType = BusinessType.VIDEO; // 视频和文档都使用6
688
+ }
689
+ else {
690
+ businessType = BusinessType.DEFAULT;
691
+ }
692
+ }
693
+ if (!(isDocument && this.apiConfig.uploadConf)) return [3 /*break*/, 2];
694
+ return [4 /*yield*/, this.apiConfig.uploadConf(businessType)];
695
+ case 1:
696
+ _b.sent();
697
+ _b.label = 2;
698
+ case 2: return [4 /*yield*/, this.createOssClient(businessType, option.imggeCarType)
699
+ // 执行上传
700
+ ];
701
+ case 3:
702
+ client = _b.sent();
703
+ return [4 /*yield*/, this.performOssUpload(client, fileName, file, year, file.type, callbacks.onProgress)];
704
+ case 4:
705
+ result = _b.sent();
706
+ if (result.res.statusCode === 200) {
707
+ clientParams = client.options;
708
+ url = "".concat(clientParams.realmName).concat(result.name).concat(clientParams.suffix || '');
709
+ uploadResult = __assign(__assign({}, result), { url: url, fileName: file.name });
710
+ callbacks.onSuccess(uploadResult);
711
+ resolve(uploadResult);
712
+ }
713
+ else {
714
+ callbacks.onError('上传失败');
715
+ reject('上传失败');
716
+ }
717
+ return [3 /*break*/, 6];
718
+ case 5:
719
+ error_2 = _b.sent();
720
+ errorMsg = error_2.message || '上传异常';
721
+ callbacks.onError(errorMsg);
722
+ reject(errorMsg);
723
+ return [3 /*break*/, 6];
724
+ case 6: return [2 /*return*/];
725
+ }
726
+ });
727
+ }); })];
728
+ });
729
+ });
730
+ };
731
+ /**
732
+ * 商品详情图片上传
733
+ * 注释:此方法用于商品详情页的图片上传,不压缩,使用NoWater客户端
734
+ * @param option 上传选项
735
+ * @returns Promise
736
+ */
737
+ AliOssClass.prototype.shopDetailUpdate = function (option) {
738
+ var _a, _b;
739
+ return __awaiter(this, void 0, void 0, function () {
740
+ var file, callbacks;
741
+ var _this = this;
742
+ return __generator(this, function (_c) {
743
+ file = option.file;
744
+ callbacks = {
745
+ onError: option.onError || (function () { }),
746
+ onSuccess: option.onSuccess || (function () { }),
747
+ onProgress: option.onProgress || (function () { })
748
+ };
749
+ option.onError = callbacks.onError;
750
+ option.onSuccess = callbacks.onSuccess;
751
+ option.onProgress = callbacks.onProgress;
752
+ // 文件类型验证
753
+ if (!isSupportedImageType(file.type)) {
754
+ callbacks.onError('');
755
+ (_b = (_a = this.apiConfig).messageWarning) === null || _b === void 0 ? void 0 : _b.call(_a, '上传失败,请上传后缀为png、gif、jpg的文件');
756
+ return [2 /*return*/, Promise.reject('文件类型不支持')];
757
+ }
758
+ return [2 /*return*/, new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
759
+ var errorMsg, imageType, suffix, extensionName, _a, year, fileName, client, result, clientParams, url, error_3, errorMsg;
760
+ return __generator(this, function (_b) {
761
+ switch (_b.label) {
762
+ case 0:
763
+ _b.trys.push([0, 4, , 5]);
764
+ // 文件大小验证
765
+ if (getFileSizeInMB(file.size) > DEFAULT_FILE_SIZE_LIMIT) {
766
+ errorMsg = "\u56FE\u7247\u4E0D\u80FD\u8D85\u8FC7".concat(DEFAULT_FILE_SIZE_LIMIT, "M");
767
+ callbacks.onError(errorMsg);
768
+ reject(errorMsg);
769
+ return [2 /*return*/];
770
+ }
771
+ // 预加载图片(虽然不需要尺寸,但保持原有逻辑)
772
+ return [4 /*yield*/, this.imgUpload(file)];
773
+ case 1:
774
+ // 预加载图片(虽然不需要尺寸,但保持原有逻辑)
775
+ _b.sent();
776
+ imageType = option.imageType || DEFAULT_IMAGE_TYPE;
777
+ suffix = exports.SuffixEnum[imageType] || exports.SuffixEnum.official;
778
+ extensionName = ".".concat(file.name.split('.').pop());
779
+ _a = this.generateFileName(imageType, extensionName), year = _a.year, fileName = _a.fileName;
780
+ return [4 /*yield*/, this.createOssClient(BusinessType.NO_WATER, option.imggeCarType)
781
+ // 执行上传
782
+ ];
783
+ case 2:
784
+ client = _b.sent();
785
+ return [4 /*yield*/, this.performOssUpload(client, fileName, file, year, file.type, callbacks.onProgress)];
786
+ case 3:
787
+ result = _b.sent();
788
+ if (result.res.statusCode === 200) {
789
+ clientParams = client.options;
790
+ url = "".concat(clientParams.realmName).concat(result.name).concat(clientParams.suffix || '');
791
+ this.loadImage({
792
+ url: url,
793
+ val: result,
794
+ suffix: suffix,
795
+ file: file,
796
+ option: option,
797
+ resolve: resolve,
798
+ reject: reject
799
+ });
800
+ }
801
+ else {
802
+ callbacks.onError('上传失败');
803
+ reject('上传失败');
804
+ }
805
+ return [3 /*break*/, 5];
806
+ case 4:
807
+ error_3 = _b.sent();
808
+ errorMsg = error_3.message || '上传异常';
809
+ callbacks.onError(errorMsg);
810
+ reject(errorMsg);
811
+ return [3 /*break*/, 5];
812
+ case 5: return [2 /*return*/];
813
+ }
814
+ });
815
+ }); })];
816
+ });
817
+ });
818
+ };
819
+ return AliOssClass;
820
+ }());
821
+ exports.AliOssClass = AliOssClass;
822
+ /**
823
+ * 创建阿里云OSS上传器(工厂函数)
824
+ * @param apiConfig API配置对象,包含业务相关的API函数
825
+ * @returns OSS上传器实例
826
+ */
827
+ function createAliOssUploader(apiConfig) {
828
+ return new AliOssClass(apiConfig);
829
+ }
830
+ exports.createAliOssUploader = createAliOssUploader;
831
+ /**
832
+ * 默认导出
833
+ */
834
+ exports.default = {
835
+ createAliOssUploader: createAliOssUploader,
836
+ BusinessType: BusinessType,
837
+ SuffixEnum: exports.SuffixEnum
838
+ };