@haluo/util 2.0.21 → 2.0.23
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.
- package/dist/index.d.ts +1 -3
- package/dist/index.js +61 -33
- package/dist/modules/cookie/index.js +24 -18
- package/dist/modules/date/index.js +56 -49
- package/dist/modules/dom/index.js +23 -16
- package/dist/modules/filter/index.js +25 -15
- package/dist/modules/format/index.js +12 -6
- package/dist/modules/match/index.js +10 -6
- package/dist/modules/monitor/index.js +14 -7
- package/dist/modules/monitor/lib/jsError.js +35 -39
- package/dist/modules/monitor/lib/timing.js +28 -24
- package/dist/modules/monitor/lib/xhr.js +33 -26
- package/dist/modules/monitor/utils/onload.js +4 -1
- package/dist/modules/monitor/utils/tracker.js +27 -13
- package/dist/modules/number/index.js +35 -31
- package/dist/modules/open-app/index.js +61 -66
- package/dist/modules/sentry/index.js +27 -19
- package/dist/modules/tools/index.d.ts +1 -1
- package/dist/modules/tools/index.js +169 -158
- package/dist/modules/upload/aliOss copy.d.ts +321 -0
- package/dist/modules/upload/aliOss copy.js +734 -0
- package/dist/modules/upload/aliOss.d.ts +51 -28
- package/dist/modules/upload/aliOss.js +163 -73
- package/dist/modules/upload/backup-aliOss.d.ts +321 -0
- package/dist/modules/upload/backup-aliOss.js +734 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/dist/types/index.js +2 -1
- package/package.json +3 -16
|
@@ -12,6 +12,7 @@
|
|
|
12
12
|
*
|
|
13
13
|
* // 创建上传实例,传入业务相关的API函数
|
|
14
14
|
* const ossUploader = createAliOssUploader({
|
|
15
|
+
* getSts: (params) => api.getSts(params),
|
|
15
16
|
* getAliyunSts: (params) => api.getAliyunSts(params),
|
|
16
17
|
* darkWaterUploadImage: (params) => api.darkWaterUploadImage(params),
|
|
17
18
|
* multiTransferImage: (params) => api.multiTransferImage(params),
|
|
@@ -54,18 +55,28 @@
|
|
|
54
55
|
* 不同的businessType对应不同的OSS bucket和水印策略
|
|
55
56
|
*/
|
|
56
57
|
export declare enum BusinessType {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
58
|
+
CREDIT = 130541736,
|
|
59
|
+
CONTENT = 101300784,
|
|
60
|
+
AVATAR = 134539448,
|
|
61
|
+
USED_CAR = 171978237,
|
|
62
|
+
MALL = 115259415,
|
|
63
|
+
HOOP = 155669648,
|
|
64
|
+
USER = 170244368,
|
|
65
|
+
CYCLING = 165103952,
|
|
66
|
+
MOTORCYCLE = 151637735,
|
|
67
|
+
RENTALCAR = 149095014,
|
|
68
|
+
SHOP = 172655993,
|
|
69
|
+
FINANCE = 188717541,
|
|
70
|
+
FEEDBACK = 182411383,
|
|
71
|
+
DRIVING_SCHOOL = 172137757,
|
|
72
|
+
REPLY = 195929698,
|
|
73
|
+
FACTORY = 118733601,
|
|
74
|
+
CYCLING_DATA = 148681294,
|
|
75
|
+
GIF = 191290831,
|
|
76
|
+
OTHER = 100694193
|
|
66
77
|
}
|
|
67
78
|
/**
|
|
68
|
-
* 图片类型后缀枚举
|
|
79
|
+
* bucket 图片类型后缀枚举
|
|
69
80
|
*/
|
|
70
81
|
export declare const SuffixEnum: {
|
|
71
82
|
readonly nowater: "!nowater";
|
|
@@ -109,13 +120,12 @@ export interface UploadResult {
|
|
|
109
120
|
*/
|
|
110
121
|
export interface UploadOption {
|
|
111
122
|
file: File;
|
|
123
|
+
businessType?: BusinessType | number;
|
|
112
124
|
imageType?: ImageType | string;
|
|
113
125
|
quality?: number;
|
|
114
|
-
businessType?: BusinessType | number;
|
|
115
126
|
batchTransfer?: boolean;
|
|
116
127
|
notCompress?: boolean;
|
|
117
128
|
idCard?: boolean;
|
|
118
|
-
imggeCarType?: any;
|
|
119
129
|
isVideo?: boolean;
|
|
120
130
|
isUnProtect?: boolean;
|
|
121
131
|
isDocument?: boolean;
|
|
@@ -139,10 +149,16 @@ interface ApiResponse<T = any> {
|
|
|
139
149
|
* 业务相关的API通过此接口传入,实现业务逻辑解耦
|
|
140
150
|
*/
|
|
141
151
|
export interface AliOssApiConfig {
|
|
142
|
-
/**
|
|
152
|
+
/** 获取STS,POST V4签名独有的表单元素- 必需 */
|
|
153
|
+
/** businessType: 业务编码(必传), docType: 文件格式如png(必传),size: _1024_567(可选) */
|
|
154
|
+
getSts: (params: {
|
|
155
|
+
businessType: BusinessType;
|
|
156
|
+
docType: string;
|
|
157
|
+
size?: string;
|
|
158
|
+
}) => Promise<ApiResponse<OSSClientParams>>;
|
|
159
|
+
/** 获取阿里云STS临时凭证 - 旧版非必需 */
|
|
143
160
|
getAliyunSts: (params: {
|
|
144
161
|
businessType: number;
|
|
145
|
-
imggeCarType?: any;
|
|
146
162
|
}) => Promise<ApiResponse<OSSClientParams>>;
|
|
147
163
|
/** 暗水印上传接口 - 可选,用于carport类型 */
|
|
148
164
|
darkWaterUploadImage?: (params: {
|
|
@@ -183,19 +199,37 @@ export declare class AliOssClass {
|
|
|
183
199
|
private apiConfig;
|
|
184
200
|
private clientCache;
|
|
185
201
|
constructor(apiConfig: AliOssApiConfig);
|
|
202
|
+
/**
|
|
203
|
+
* 通过V4签名上传
|
|
204
|
+
* @param file 上传的文件
|
|
205
|
+
* @param businessType 业务类型
|
|
206
|
+
* @param size 文件的尺寸
|
|
207
|
+
* @returns 上传图片结果
|
|
208
|
+
*/
|
|
209
|
+
upload(file: any, businessType: BusinessType, size?: string): Promise<any>;
|
|
186
210
|
/**
|
|
187
211
|
* 创建OSS客户端
|
|
188
212
|
* @param businessType 业务类型
|
|
189
|
-
* @param imggeCarType 车辆图片类型(可选,用于特定业务)
|
|
190
213
|
* @returns OSS客户端实例
|
|
191
214
|
*/
|
|
192
|
-
createOssClient(businessType: number
|
|
215
|
+
createOssClient(businessType: number): Promise<any>;
|
|
216
|
+
/**
|
|
217
|
+
* 执行OSS上传
|
|
218
|
+
* @param client OSS客户端
|
|
219
|
+
* @param fileName 文件名
|
|
220
|
+
* @param file 文件对象
|
|
221
|
+
* @param year 年份
|
|
222
|
+
* @param mimeType MIME类型
|
|
223
|
+
* @param onProgress 进度回调
|
|
224
|
+
* @returns 上传结果
|
|
225
|
+
*/
|
|
226
|
+
private performOssUpload;
|
|
193
227
|
/**
|
|
194
228
|
* 预加载图片获取尺寸
|
|
195
229
|
* @param file 文件对象
|
|
196
230
|
* @returns 图片对象
|
|
197
231
|
*/
|
|
198
|
-
|
|
232
|
+
getImageInfo(file: File): Promise<HTMLImageElement>;
|
|
199
233
|
/**
|
|
200
234
|
* 生成文件名
|
|
201
235
|
* @param imageType 图片类型
|
|
@@ -210,17 +244,6 @@ export declare class AliOssClass {
|
|
|
210
244
|
* @returns 是否为长图
|
|
211
245
|
*/
|
|
212
246
|
private isLongImage;
|
|
213
|
-
/**
|
|
214
|
-
* 执行OSS上传
|
|
215
|
-
* @param client OSS客户端
|
|
216
|
-
* @param fileName 文件名
|
|
217
|
-
* @param file 文件对象
|
|
218
|
-
* @param year 年份
|
|
219
|
-
* @param mimeType MIME类型
|
|
220
|
-
* @param onProgress 进度回调
|
|
221
|
-
* @returns 上传结果
|
|
222
|
-
*/
|
|
223
|
-
private performOssUpload;
|
|
224
247
|
/**
|
|
225
248
|
* 确定业务类型
|
|
226
249
|
* @param option 上传选项
|
|
@@ -12,6 +12,7 @@
|
|
|
12
12
|
*
|
|
13
13
|
* // 创建上传实例,传入业务相关的API函数
|
|
14
14
|
* const ossUploader = createAliOssUploader({
|
|
15
|
+
* getSts: (params) => api.getSts(params),
|
|
15
16
|
* getAliyunSts: (params) => api.getAliyunSts(params),
|
|
16
17
|
* darkWaterUploadImage: (params) => api.darkWaterUploadImage(params),
|
|
17
18
|
* multiTransferImage: (params) => api.multiTransferImage(params),
|
|
@@ -53,7 +54,12 @@ import OSS from 'ali-oss';
|
|
|
53
54
|
import lrz from 'lrz';
|
|
54
55
|
// ==================== 常量定义 ====================
|
|
55
56
|
/** 支持的图片格式 */
|
|
56
|
-
const SUPPORTED_IMAGE_TYPES = [
|
|
57
|
+
const SUPPORTED_IMAGE_TYPES = [
|
|
58
|
+
'image/png',
|
|
59
|
+
'image/gif',
|
|
60
|
+
'image/jpg',
|
|
61
|
+
'image/jpeg',
|
|
62
|
+
];
|
|
57
63
|
/** 小图片阈值(KB) */
|
|
58
64
|
const SMALL_IMAGE_THRESHOLD = 100;
|
|
59
65
|
/** 长图宽高比阈值 */
|
|
@@ -79,18 +85,37 @@ const OSS_META_PEOPLE = 'x-oss-meta-motor';
|
|
|
79
85
|
*/
|
|
80
86
|
export var BusinessType;
|
|
81
87
|
(function (BusinessType) {
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
88
|
+
// NO_WATER = 0, // 不打水印
|
|
89
|
+
// AVATAR = 1, // 头像
|
|
90
|
+
// GIF = 4, // GIF图片
|
|
91
|
+
// VIDEO = 6, // 视频
|
|
92
|
+
// UNPROTECT = 8, // UnProtect(不受保护)
|
|
93
|
+
// ARTICLE = 1000, // 文章图片
|
|
94
|
+
// PRIVATE = 1199, // 私有
|
|
95
|
+
// ENCRYPTED = 1299, // 加密
|
|
96
|
+
// DEFAULT = 1500, // 默认
|
|
97
|
+
BusinessType[BusinessType["CREDIT"] = 130541736] = "CREDIT";
|
|
98
|
+
BusinessType[BusinessType["CONTENT"] = 101300784] = "CONTENT";
|
|
99
|
+
BusinessType[BusinessType["AVATAR"] = 134539448] = "AVATAR";
|
|
100
|
+
BusinessType[BusinessType["USED_CAR"] = 171978237] = "USED_CAR";
|
|
101
|
+
BusinessType[BusinessType["MALL"] = 115259415] = "MALL";
|
|
102
|
+
BusinessType[BusinessType["HOOP"] = 155669648] = "HOOP";
|
|
103
|
+
BusinessType[BusinessType["USER"] = 170244368] = "USER";
|
|
104
|
+
BusinessType[BusinessType["CYCLING"] = 165103952] = "CYCLING";
|
|
105
|
+
BusinessType[BusinessType["MOTORCYCLE"] = 151637735] = "MOTORCYCLE";
|
|
106
|
+
BusinessType[BusinessType["RENTALCAR"] = 149095014] = "RENTALCAR";
|
|
107
|
+
BusinessType[BusinessType["SHOP"] = 172655993] = "SHOP";
|
|
108
|
+
BusinessType[BusinessType["FINANCE"] = 188717541] = "FINANCE";
|
|
109
|
+
BusinessType[BusinessType["FEEDBACK"] = 182411383] = "FEEDBACK";
|
|
110
|
+
BusinessType[BusinessType["DRIVING_SCHOOL"] = 172137757] = "DRIVING_SCHOOL";
|
|
111
|
+
BusinessType[BusinessType["REPLY"] = 195929698] = "REPLY";
|
|
112
|
+
BusinessType[BusinessType["FACTORY"] = 118733601] = "FACTORY";
|
|
113
|
+
BusinessType[BusinessType["CYCLING_DATA"] = 148681294] = "CYCLING_DATA";
|
|
114
|
+
BusinessType[BusinessType["GIF"] = 191290831] = "GIF";
|
|
115
|
+
BusinessType[BusinessType["OTHER"] = 100694193] = "OTHER";
|
|
91
116
|
})(BusinessType || (BusinessType = {}));
|
|
92
117
|
/**
|
|
93
|
-
* 图片类型后缀枚举
|
|
118
|
+
* bucket 图片类型后缀枚举
|
|
94
119
|
*/
|
|
95
120
|
export const SuffixEnum = {
|
|
96
121
|
nowater: '!nowater',
|
|
@@ -99,7 +124,7 @@ export const SuffixEnum = {
|
|
|
99
124
|
forum: '!forum',
|
|
100
125
|
avatar: '!avatar',
|
|
101
126
|
square: '!square',
|
|
102
|
-
carport: '!carport' // 车库、经销商水印
|
|
127
|
+
carport: '!carport', // 车库、经销商水印
|
|
103
128
|
};
|
|
104
129
|
/** 不打水印的图片类型 */
|
|
105
130
|
const NO_WATER_IMAGE_TYPES = ['nowater', 'forum'];
|
|
@@ -124,7 +149,7 @@ function randomString(length) {
|
|
|
124
149
|
*/
|
|
125
150
|
function filterUndefined(obj) {
|
|
126
151
|
const result = {};
|
|
127
|
-
Object.keys(obj).forEach(key => {
|
|
152
|
+
Object.keys(obj).forEach((key) => {
|
|
128
153
|
if (obj[key] !== undefined) {
|
|
129
154
|
result[key] = obj[key];
|
|
130
155
|
}
|
|
@@ -175,19 +200,75 @@ export class AliOssClass {
|
|
|
175
200
|
this.apiConfig = apiConfig;
|
|
176
201
|
this.clientCache = new Map();
|
|
177
202
|
}
|
|
203
|
+
/**
|
|
204
|
+
* 通过V4签名上传
|
|
205
|
+
* @param file 上传的文件
|
|
206
|
+
* @param businessType 业务类型
|
|
207
|
+
* @param size 文件的尺寸
|
|
208
|
+
* @returns 上传图片结果
|
|
209
|
+
*/
|
|
210
|
+
async upload(file, businessType, size) {
|
|
211
|
+
try {
|
|
212
|
+
// -------------------------- 步骤 1:请求后端获取所有签名字段 --------------------------
|
|
213
|
+
const signResponse = await this.apiConfig.getSts({
|
|
214
|
+
businessType,
|
|
215
|
+
docType: file.type,
|
|
216
|
+
size, // _1024_567(可选)
|
|
217
|
+
});
|
|
218
|
+
if (signResponse.data.code !== 0) {
|
|
219
|
+
console.error(signResponse);
|
|
220
|
+
throw new Error(`获取签名失败:${signResponse.data.message}`);
|
|
221
|
+
}
|
|
222
|
+
const signData = await signResponse.data.data;
|
|
223
|
+
console.log('后端返回的签名字段:', signData);
|
|
224
|
+
// -------------------------- 步骤 2:构造 FormData(字段名必须和后端返回一致)--------------------------
|
|
225
|
+
const formData = new FormData();
|
|
226
|
+
formData.append('autherid', signData['autherid']); // 业务字段(原样传)
|
|
227
|
+
formData.append('uid', signData['uid']); // 业务字段(原样传)
|
|
228
|
+
formData.append('key', signData['key']);
|
|
229
|
+
formData.append('policy', signData['policy']);
|
|
230
|
+
formData.append('success_action_status', '200');
|
|
231
|
+
formData.append('x-oss-credential', signData['credential']);
|
|
232
|
+
formData.append('x-oss-date', signData['date']);
|
|
233
|
+
if (signData['securityToken']) {
|
|
234
|
+
formData.append('x-oss-security-token', signData['securityToken']);
|
|
235
|
+
}
|
|
236
|
+
formData.append('x-oss-signature', signData['signature']);
|
|
237
|
+
formData.append('x-oss-signature-version', signData['signatureVersion']);
|
|
238
|
+
formData.append('file', file);
|
|
239
|
+
// -------------------------- 步骤 3:发送 POST 请求到 OSS --------------------------
|
|
240
|
+
const uploadResponse = await fetch(signData.uploadUrl, {
|
|
241
|
+
method: 'POST',
|
|
242
|
+
body: formData,
|
|
243
|
+
// 重点:不要手动设置 Content-Type!浏览器会自动处理为 multipart/form-data 并带边界符
|
|
244
|
+
headers: { Accept: '*/*' },
|
|
245
|
+
});
|
|
246
|
+
// -------------------------- 步骤 4:处理上传结果 --------------------------
|
|
247
|
+
const responseText = await uploadResponse.text();
|
|
248
|
+
console.log('responseText', responseText);
|
|
249
|
+
if (uploadResponse.ok) {
|
|
250
|
+
console.log('上传成功,文件路径:', signData['key'], signData['fileUrl']);
|
|
251
|
+
}
|
|
252
|
+
else {
|
|
253
|
+
throw new Error(`上传失败(OSS 返回 ${uploadResponse.status}):${responseText}`);
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
catch (err) {
|
|
257
|
+
console.error('详细错误:', err);
|
|
258
|
+
}
|
|
259
|
+
}
|
|
178
260
|
/**
|
|
179
261
|
* 创建OSS客户端
|
|
180
262
|
* @param businessType 业务类型
|
|
181
|
-
* @param imggeCarType 车辆图片类型(可选,用于特定业务)
|
|
182
263
|
* @returns OSS客户端实例
|
|
183
264
|
*/
|
|
184
|
-
async createOssClient(businessType
|
|
185
|
-
const cacheKey = `${businessType}
|
|
265
|
+
async createOssClient(businessType) {
|
|
266
|
+
const cacheKey = `${businessType}`;
|
|
186
267
|
// 注释:某些项目使用缓存,某些不使用。这里提供可选的缓存机制
|
|
187
268
|
// if (this.clientCache.has(cacheKey)) {
|
|
188
269
|
// return this.clientCache.get(cacheKey)
|
|
189
270
|
// }
|
|
190
|
-
const res = await this.apiConfig.getAliyunSts({ businessType
|
|
271
|
+
const res = await this.apiConfig.getAliyunSts({ businessType });
|
|
191
272
|
if (res.data.code !== 0) {
|
|
192
273
|
throw new Error('获取OSS配置失败');
|
|
193
274
|
}
|
|
@@ -197,23 +278,42 @@ export class AliOssClass {
|
|
|
197
278
|
const filteredParams = filterUndefined(clientParams);
|
|
198
279
|
const client = new OSS({
|
|
199
280
|
region: DEFAULT_OSS_REGION,
|
|
200
|
-
...filteredParams
|
|
281
|
+
...filteredParams,
|
|
201
282
|
});
|
|
202
283
|
// 保存客户端选项供后续使用
|
|
203
284
|
client.options = {
|
|
204
285
|
...client.options,
|
|
205
286
|
realmName: clientParams.realmName,
|
|
206
|
-
suffix: clientParams.suffix
|
|
287
|
+
suffix: clientParams.suffix,
|
|
207
288
|
};
|
|
208
289
|
this.clientCache.set(cacheKey, client);
|
|
209
290
|
return client;
|
|
210
291
|
}
|
|
292
|
+
/**
|
|
293
|
+
* 执行OSS上传
|
|
294
|
+
* @param client OSS客户端
|
|
295
|
+
* @param fileName 文件名
|
|
296
|
+
* @param file 文件对象
|
|
297
|
+
* @param year 年份
|
|
298
|
+
* @param mimeType MIME类型
|
|
299
|
+
* @param onProgress 进度回调
|
|
300
|
+
* @returns 上传结果
|
|
301
|
+
*/
|
|
302
|
+
async performOssUpload(client, fileName, file, year, mimeType, onProgress) {
|
|
303
|
+
return await client.multipartUpload(fileName, file, {
|
|
304
|
+
progress: (p) => {
|
|
305
|
+
onProgress?.({ percent: Math.floor(p * 100) });
|
|
306
|
+
},
|
|
307
|
+
meta: { year, people: OSS_META_PEOPLE },
|
|
308
|
+
mime: mimeType,
|
|
309
|
+
});
|
|
310
|
+
}
|
|
211
311
|
/**
|
|
212
312
|
* 预加载图片获取尺寸
|
|
213
313
|
* @param file 文件对象
|
|
214
314
|
* @returns 图片对象
|
|
215
315
|
*/
|
|
216
|
-
|
|
316
|
+
getImageInfo(file) {
|
|
217
317
|
return new Promise((resolve, reject) => {
|
|
218
318
|
const URL = window.URL || window.webkitURL;
|
|
219
319
|
const image = new Image();
|
|
@@ -255,25 +355,6 @@ export class AliOssClass {
|
|
|
255
355
|
const minDimension = Math.min(width, height);
|
|
256
356
|
return maxDimension / minDimension > LONG_IMAGE_RATIO;
|
|
257
357
|
}
|
|
258
|
-
/**
|
|
259
|
-
* 执行OSS上传
|
|
260
|
-
* @param client OSS客户端
|
|
261
|
-
* @param fileName 文件名
|
|
262
|
-
* @param file 文件对象
|
|
263
|
-
* @param year 年份
|
|
264
|
-
* @param mimeType MIME类型
|
|
265
|
-
* @param onProgress 进度回调
|
|
266
|
-
* @returns 上传结果
|
|
267
|
-
*/
|
|
268
|
-
async performOssUpload(client, fileName, file, year, mimeType, onProgress) {
|
|
269
|
-
return await client.multipartUpload(fileName, file, {
|
|
270
|
-
progress: (p) => {
|
|
271
|
-
onProgress?.({ percent: Math.floor(p * 100) });
|
|
272
|
-
},
|
|
273
|
-
meta: { year, people: OSS_META_PEOPLE },
|
|
274
|
-
mime: mimeType
|
|
275
|
-
});
|
|
276
|
-
}
|
|
277
358
|
/**
|
|
278
359
|
* 确定业务类型
|
|
279
360
|
* @param option 上传选项
|
|
@@ -295,9 +376,9 @@ export class AliOssClass {
|
|
|
295
376
|
return BusinessType.AVATAR;
|
|
296
377
|
}
|
|
297
378
|
if (isNoWaterImageType(imageType)) {
|
|
298
|
-
return BusinessType.
|
|
379
|
+
return BusinessType.OTHER;
|
|
299
380
|
}
|
|
300
|
-
return BusinessType.
|
|
381
|
+
return BusinessType.OTHER;
|
|
301
382
|
}
|
|
302
383
|
/**
|
|
303
384
|
* 加载图片并返回结果
|
|
@@ -360,12 +441,15 @@ export class AliOssClass {
|
|
|
360
441
|
* @returns Promise
|
|
361
442
|
*/
|
|
362
443
|
ossUploadImage = async (option) => {
|
|
444
|
+
if (!(option.file instanceof File)) {
|
|
445
|
+
return Promise.reject('file is not instanceof File');
|
|
446
|
+
}
|
|
363
447
|
const file = option.file;
|
|
364
448
|
// 初始化回调函数
|
|
365
449
|
const callbacks = {
|
|
366
450
|
onError: option.onError || (() => { }),
|
|
367
451
|
onSuccess: option.onSuccess || (() => { }),
|
|
368
|
-
onProgress: option.onProgress || (() => { })
|
|
452
|
+
onProgress: option.onProgress || (() => { }),
|
|
369
453
|
};
|
|
370
454
|
option.onError = callbacks.onError;
|
|
371
455
|
option.onSuccess = callbacks.onSuccess;
|
|
@@ -379,27 +463,28 @@ export class AliOssClass {
|
|
|
379
463
|
return new Promise(async (resolve, reject) => {
|
|
380
464
|
try {
|
|
381
465
|
const imageType = option.imageType || DEFAULT_IMAGE_TYPE;
|
|
382
|
-
const suffix = SuffixEnum[imageType] ||
|
|
466
|
+
const suffix = SuffixEnum[imageType] ||
|
|
467
|
+
SuffixEnum.official;
|
|
383
468
|
// 生成文件名
|
|
384
469
|
const extensionName = `.${file.name.split('.').pop()}`;
|
|
385
470
|
const { year, fileName } = this.generateFileName(imageType, extensionName);
|
|
386
471
|
// 注释:小图片(≤100KB)不压缩,quality设为1 (MAIN-2481)
|
|
387
472
|
const isSmallImage = getFileSizeInKB(file.size) <= SMALL_IMAGE_THRESHOLD;
|
|
388
|
-
let quality = isSmallImage ? 1 :
|
|
473
|
+
let quality = isSmallImage ? 1 : option.quality || DEFAULT_QUALITY;
|
|
389
474
|
const isGif = file.type === 'image/gif';
|
|
390
475
|
const idCard = option.idCard || false;
|
|
391
476
|
// 确定业务类型
|
|
392
477
|
const businessType = this.determineBusinessType(option, isGif);
|
|
393
478
|
// 注释:特殊业务类型1299(加密)直接返回bucket路径,不加载图片
|
|
394
|
-
if (businessType === BusinessType.
|
|
395
|
-
const client = await this.createOssClient(businessType
|
|
479
|
+
if (businessType === BusinessType.OTHER) {
|
|
480
|
+
const client = await this.createOssClient(businessType);
|
|
396
481
|
const result = await this.performOssUpload(client, fileName, file, year, file.type, callbacks.onProgress);
|
|
397
482
|
if (result.res.statusCode === 200) {
|
|
398
483
|
const val = {
|
|
399
484
|
...result,
|
|
400
485
|
imgUrl: `${result.bucket}://${result.name}`,
|
|
401
486
|
imgOrgUrl: `${result.bucket}://${result.name}`,
|
|
402
|
-
fileName: file.name
|
|
487
|
+
fileName: file.name,
|
|
403
488
|
};
|
|
404
489
|
callbacks.onSuccess(val);
|
|
405
490
|
resolve(val);
|
|
@@ -421,7 +506,7 @@ export class AliOssClass {
|
|
|
421
506
|
option,
|
|
422
507
|
file,
|
|
423
508
|
resolve,
|
|
424
|
-
reject
|
|
509
|
+
reject,
|
|
425
510
|
});
|
|
426
511
|
}
|
|
427
512
|
else {
|
|
@@ -431,7 +516,8 @@ export class AliOssClass {
|
|
|
431
516
|
return;
|
|
432
517
|
}
|
|
433
518
|
// 注释:carport类型需要打暗水印
|
|
434
|
-
if (option.imageType === 'carport' &&
|
|
519
|
+
if (option.imageType === 'carport' &&
|
|
520
|
+
this.apiConfig.darkWaterUploadImage) {
|
|
435
521
|
const res = await this.apiConfig.darkWaterUploadImage({ file });
|
|
436
522
|
if (res.data.code === 0) {
|
|
437
523
|
this.loadImage({
|
|
@@ -441,7 +527,7 @@ export class AliOssClass {
|
|
|
441
527
|
option,
|
|
442
528
|
file,
|
|
443
529
|
resolve,
|
|
444
|
-
reject
|
|
530
|
+
reject,
|
|
445
531
|
});
|
|
446
532
|
}
|
|
447
533
|
else {
|
|
@@ -451,7 +537,7 @@ export class AliOssClass {
|
|
|
451
537
|
return;
|
|
452
538
|
}
|
|
453
539
|
// 预加载图片获取尺寸
|
|
454
|
-
const image = await this.
|
|
540
|
+
const image = await this.getImageInfo(file);
|
|
455
541
|
// 图片压缩
|
|
456
542
|
const rst = await lrz(file, { quality });
|
|
457
543
|
// 注释:长图压缩有问题,长宽比>2:1的图片不压缩
|
|
@@ -478,13 +564,12 @@ export class AliOssClass {
|
|
|
478
564
|
if (!postFile.name) {
|
|
479
565
|
Object.defineProperty(postFile, 'name', {
|
|
480
566
|
value: file.name,
|
|
481
|
-
writable: false
|
|
567
|
+
writable: false,
|
|
482
568
|
});
|
|
483
569
|
}
|
|
484
570
|
// 注释:根据不同场景选择不同的OSS客户端
|
|
485
571
|
// GIF -> UnProtect(8), idCard -> Private(1199), nowater/forum -> NoWater(0), 其他 -> 默认
|
|
486
|
-
const client = await this.createOssClient(businessType
|
|
487
|
-
// 执行上传
|
|
572
|
+
const client = await this.createOssClient(businessType);
|
|
488
573
|
const result = await this.performOssUpload(client, fileName, postFile, year, postFile.type, callbacks.onProgress);
|
|
489
574
|
if (result.res.statusCode !== 200) {
|
|
490
575
|
callbacks.onError('上传失败');
|
|
@@ -495,7 +580,7 @@ export class AliOssClass {
|
|
|
495
580
|
if (idCard && this.apiConfig.generatePrePresignedUrl) {
|
|
496
581
|
const res = await this.apiConfig.generatePrePresignedUrl({
|
|
497
582
|
objectId: result.name,
|
|
498
|
-
expireMils: PRESIGNED_URL_EXPIRE_TIME
|
|
583
|
+
expireMils: PRESIGNED_URL_EXPIRE_TIME,
|
|
499
584
|
});
|
|
500
585
|
if (res.data.code === 0) {
|
|
501
586
|
this.loadImageNew({
|
|
@@ -504,7 +589,7 @@ export class AliOssClass {
|
|
|
504
589
|
file: postFile,
|
|
505
590
|
option,
|
|
506
591
|
resolve,
|
|
507
|
-
reject
|
|
592
|
+
reject,
|
|
508
593
|
});
|
|
509
594
|
}
|
|
510
595
|
else {
|
|
@@ -517,7 +602,7 @@ export class AliOssClass {
|
|
|
517
602
|
if (imageType === 'avatar' && this.apiConfig.aliyunPersist) {
|
|
518
603
|
const res = await this.apiConfig.aliyunPersist({
|
|
519
604
|
object: `${result.bucket}://${result.name}`,
|
|
520
|
-
businessType: BusinessType.AVATAR
|
|
605
|
+
businessType: BusinessType.AVATAR,
|
|
521
606
|
});
|
|
522
607
|
if (res.data.code === 0) {
|
|
523
608
|
this.loadImageNew({
|
|
@@ -526,7 +611,7 @@ export class AliOssClass {
|
|
|
526
611
|
file: postFile,
|
|
527
612
|
option,
|
|
528
613
|
resolve,
|
|
529
|
-
reject
|
|
614
|
+
reject,
|
|
530
615
|
});
|
|
531
616
|
}
|
|
532
617
|
else {
|
|
@@ -546,7 +631,7 @@ export class AliOssClass {
|
|
|
546
631
|
option,
|
|
547
632
|
resolve,
|
|
548
633
|
reject,
|
|
549
|
-
isGif
|
|
634
|
+
isGif,
|
|
550
635
|
});
|
|
551
636
|
}
|
|
552
637
|
catch (error) {
|
|
@@ -563,12 +648,15 @@ export class AliOssClass {
|
|
|
563
648
|
* @returns Promise
|
|
564
649
|
*/
|
|
565
650
|
ossUploadFile = async (option) => {
|
|
651
|
+
if (!(option.file instanceof File)) {
|
|
652
|
+
return Promise.reject('file is not instanceof File');
|
|
653
|
+
}
|
|
566
654
|
const file = option.file;
|
|
567
655
|
// 初始化回调函数
|
|
568
656
|
const callbacks = {
|
|
569
657
|
onError: option.onError || (() => { }),
|
|
570
658
|
onSuccess: option.onSuccess || (() => { }),
|
|
571
|
-
onProgress: option.onProgress || (() => { })
|
|
659
|
+
onProgress: option.onProgress || (() => { }),
|
|
572
660
|
};
|
|
573
661
|
option.onError = callbacks.onError;
|
|
574
662
|
option.onSuccess = callbacks.onSuccess;
|
|
@@ -587,21 +675,20 @@ export class AliOssClass {
|
|
|
587
675
|
let businessType = option.businessType;
|
|
588
676
|
if (!businessType) {
|
|
589
677
|
if (isUnProtect) {
|
|
590
|
-
businessType = BusinessType.
|
|
678
|
+
businessType = BusinessType.OTHER;
|
|
591
679
|
}
|
|
592
680
|
else if (isVideo || isDocument) {
|
|
593
|
-
businessType = BusinessType.
|
|
681
|
+
businessType = BusinessType.OTHER; // 视频和文档都使用6
|
|
594
682
|
}
|
|
595
683
|
else {
|
|
596
|
-
businessType = BusinessType.
|
|
684
|
+
businessType = BusinessType.OTHER;
|
|
597
685
|
}
|
|
598
686
|
}
|
|
599
687
|
// 注释:文档上传可能需要先调用uploadConf获取配置
|
|
600
688
|
if (isDocument && this.apiConfig.uploadConf) {
|
|
601
689
|
await this.apiConfig.uploadConf(businessType);
|
|
602
690
|
}
|
|
603
|
-
const client = await this.createOssClient(businessType
|
|
604
|
-
// 执行上传
|
|
691
|
+
const client = await this.createOssClient(businessType);
|
|
605
692
|
const result = await this.performOssUpload(client, fileName, file, year, file.type, callbacks.onProgress);
|
|
606
693
|
if (result.res.statusCode === 200) {
|
|
607
694
|
const clientParams = client.options;
|
|
@@ -609,7 +696,7 @@ export class AliOssClass {
|
|
|
609
696
|
const uploadResult = {
|
|
610
697
|
...result,
|
|
611
698
|
url,
|
|
612
|
-
fileName: file.name
|
|
699
|
+
fileName: file.name,
|
|
613
700
|
};
|
|
614
701
|
callbacks.onSuccess(uploadResult);
|
|
615
702
|
resolve(uploadResult);
|
|
@@ -633,12 +720,15 @@ export class AliOssClass {
|
|
|
633
720
|
* @returns Promise
|
|
634
721
|
*/
|
|
635
722
|
shopDetailUpdate = async (option) => {
|
|
723
|
+
if (!(option.file instanceof File)) {
|
|
724
|
+
return Promise.reject('file is not instanceof File');
|
|
725
|
+
}
|
|
636
726
|
const file = option.file;
|
|
637
727
|
// 初始化回调函数
|
|
638
728
|
const callbacks = {
|
|
639
729
|
onError: option.onError || (() => { }),
|
|
640
730
|
onSuccess: option.onSuccess || (() => { }),
|
|
641
|
-
onProgress: option.onProgress || (() => { })
|
|
731
|
+
onProgress: option.onProgress || (() => { }),
|
|
642
732
|
};
|
|
643
733
|
option.onError = callbacks.onError;
|
|
644
734
|
option.onSuccess = callbacks.onSuccess;
|
|
@@ -659,15 +749,15 @@ export class AliOssClass {
|
|
|
659
749
|
return;
|
|
660
750
|
}
|
|
661
751
|
// 预加载图片(虽然不需要尺寸,但保持原有逻辑)
|
|
662
|
-
await this.
|
|
752
|
+
await this.getImageInfo(file);
|
|
663
753
|
const imageType = option.imageType || DEFAULT_IMAGE_TYPE;
|
|
664
|
-
const suffix = SuffixEnum[imageType] ||
|
|
754
|
+
const suffix = SuffixEnum[imageType] ||
|
|
755
|
+
SuffixEnum.official;
|
|
665
756
|
// 生成文件名
|
|
666
757
|
const extensionName = `.${file.name.split('.').pop()}`;
|
|
667
758
|
const { year, fileName } = this.generateFileName(imageType, extensionName);
|
|
668
759
|
// 使用NoWater客户端
|
|
669
|
-
const client = await this.createOssClient(BusinessType.
|
|
670
|
-
// 执行上传
|
|
760
|
+
const client = await this.createOssClient(BusinessType.OTHER);
|
|
671
761
|
const result = await this.performOssUpload(client, fileName, file, year, file.type, callbacks.onProgress);
|
|
672
762
|
if (result.res.statusCode === 200) {
|
|
673
763
|
const clientParams = client.options;
|
|
@@ -679,7 +769,7 @@ export class AliOssClass {
|
|
|
679
769
|
file,
|
|
680
770
|
option,
|
|
681
771
|
resolve,
|
|
682
|
-
reject
|
|
772
|
+
reject,
|
|
683
773
|
});
|
|
684
774
|
}
|
|
685
775
|
else {
|
|
@@ -722,5 +812,5 @@ export function createAliOssUploader(apiConfig) {
|
|
|
722
812
|
export default {
|
|
723
813
|
createAliOssUploader,
|
|
724
814
|
BusinessType,
|
|
725
|
-
SuffixEnum
|
|
815
|
+
SuffixEnum,
|
|
726
816
|
};
|