@haluo/util 2.0.23 → 2.0.24

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.
@@ -1,321 +0,0 @@
1
- /**
2
- * @file 阿里云OSS上传统一工具类
3
- * @description 整合所有项目的aliOss业务逻辑,通过传参形式支持不同业务场景
4
- * @Author: wanghui
5
- * @createBy: @2025.11.17
6
- *
7
- * 文档:https://help.aliyun.com/document_detail/64041.html
8
- *
9
- * 使用示例:
10
- * ```typescript
11
- * import { createAliOssUploader } from '@haluo/util'
12
- *
13
- * // 创建上传实例,传入业务相关的API函数
14
- * const ossUploader = createAliOssUploader({
15
- * getAliyunSts: (params) => api.getAliyunSts(params),
16
- * darkWaterUploadImage: (params) => api.darkWaterUploadImage(params),
17
- * multiTransferImage: (params) => api.multiTransferImage(params),
18
- * aliyunPersist: (params) => api.aliyunPersist(params),
19
- * generatePrePresignedUrl: (params) => api.generatePrePresignedUrl(params),
20
- * uploadConf: (params) => api.uploadConf(params),
21
- * dateFormat: util.date.format, // 或 util.filter.format
22
- * messageWarning: (msg) => window.$message.warning(msg)
23
- * })
24
- *
25
- * // 图片上传
26
- * ossUploader.ossUploadImage({
27
- * file: file,
28
- * imageType: 'official', // nowater、official、panoram、forum、avatar、square、carport
29
- * quality: 0.7, // 压缩质量
30
- * businessType: 1500, // 业务类型:0(无水印)、1(头像)、4(GIF)、6(视频)、8(UnProtect)、1000(文章)、1199(私有)、1299(加密)、1500(默认)
31
- * batchTransfer: false, // 是否批量转换水印
32
- * notCompress: false, // 是否不压缩
33
- * idCard: false, // 是否身份证上传
34
- * onProgress: (e) => console.log(e.percent),
35
- * onSuccess: (val) => console.log(val),
36
- * onError: (err) => console.error(err)
37
- * })
38
- *
39
- * // 文件上传
40
- * ossUploader.ossUploadFile({
41
- * file: file,
42
- * isVideo: false, // 是否视频
43
- * isUnProtect: false, // 是否UnProtect
44
- * isDocument: false, // 是否文档
45
- * businessType: 1500,
46
- * onProgress: (e) => console.log(e.percent),
47
- * onSuccess: (val) => console.log(val),
48
- * onError: (err) => console.error(err)
49
- * })
50
- * ```
51
- */
52
- /**
53
- * 业务类型枚举
54
- * 不同的businessType对应不同的OSS bucket和水印策略
55
- */
56
- export declare enum BusinessType {
57
- NO_WATER = 0,
58
- AVATAR = 1,
59
- GIF = 4,
60
- VIDEO = 6,
61
- UNPROTECT = 8,
62
- ARTICLE = 1000,
63
- PRIVATE = 1199,
64
- ENCRYPTED = 1299,
65
- DEFAULT = 1500
66
- }
67
- /**
68
- * bucket 图片类型后缀枚举
69
- */
70
- export declare const SuffixEnum: {
71
- readonly nowater: "!nowater";
72
- readonly official: "!official";
73
- readonly panoram: "!panoram";
74
- readonly forum: "!forum";
75
- readonly avatar: "!avatar";
76
- readonly square: "!square";
77
- readonly carport: "!carport";
78
- };
79
- /** 图片类型 */
80
- export type ImageType = keyof typeof SuffixEnum;
81
- /**
82
- * OSS客户端配置接口
83
- */
84
- export interface OSSClientParams {
85
- region?: string;
86
- accessKeyId?: string;
87
- accessKeySecret?: string;
88
- stsToken?: string;
89
- bucket?: string;
90
- realmName?: string;
91
- suffix?: string;
92
- style?: string;
93
- [key: string]: any;
94
- }
95
- /**
96
- * 上传结果接口
97
- */
98
- export interface UploadResult {
99
- imgUrl?: string;
100
- imgOrgUrl?: string;
101
- url?: string;
102
- fileName?: string;
103
- name?: string;
104
- bucket?: string;
105
- [key: string]: any;
106
- }
107
- /**
108
- * 上传选项接口
109
- */
110
- export interface UploadOption {
111
- file: File;
112
- imageType?: ImageType | string;
113
- quality?: number;
114
- businessType?: BusinessType | number;
115
- batchTransfer?: boolean;
116
- notCompress?: boolean;
117
- idCard?: boolean;
118
- isVideo?: boolean;
119
- isUnProtect?: boolean;
120
- isDocument?: boolean;
121
- onProgress?: (e: {
122
- percent: number;
123
- }) => void;
124
- onSuccess?: (val: UploadResult) => void;
125
- onError?: (err: string) => void;
126
- }
127
- /**
128
- * API响应接口
129
- */
130
- interface ApiResponse<T = any> {
131
- data: {
132
- code: number;
133
- data: T;
134
- };
135
- }
136
- /**
137
- * API函数配置接口
138
- * 业务相关的API通过此接口传入,实现业务逻辑解耦
139
- */
140
- export interface AliOssApiConfig {
141
- /** 获取阿里云STS临时凭证 - 必需 */
142
- getAliyunSts: (params: {
143
- businessType: number;
144
- }) => Promise<ApiResponse<OSSClientParams>>;
145
- /** 暗水印上传接口 - 可选,用于carport类型 */
146
- darkWaterUploadImage?: (params: {
147
- file: File;
148
- }) => Promise<ApiResponse<string>>;
149
- /** 批量转换水印接口 - 可选,用于batchTransfer */
150
- multiTransferImage?: (params: {
151
- file: File;
152
- }) => Promise<ApiResponse<string>>;
153
- /** 阿里云持久化接口 - 可选,用于avatar类型 */
154
- aliyunPersist?: (params: {
155
- object: string;
156
- businessType: number;
157
- }) => Promise<ApiResponse<string>>;
158
- /** 生成预签名URL接口 - 可选,用于idCard */
159
- generatePrePresignedUrl?: (params: {
160
- objectId: string;
161
- expireMils: number;
162
- }) => Promise<ApiResponse<string>>;
163
- /** 上传配置接口 - 可选,用于document上传 */
164
- uploadConf?: (businessType: number) => Promise<ApiResponse>;
165
- /** 日期格式化函数 - 必需,支持 util.date.format 或 util.filter.format */
166
- dateFormat: (date: Date, format: string) => string;
167
- /** 消息提示函数 - 可选 */
168
- messageWarning?: (msg: string) => void;
169
- }
170
- /**
171
- * 生成随机字符串
172
- * @param length 字符串长度
173
- * @returns 随机字符串
174
- */
175
- declare function randomString(length: number): string;
176
- /**
177
- * 阿里云OSS上传类
178
- * 整合所有项目的aliOss业务逻辑,通过依赖注入实现业务API解耦
179
- */
180
- export declare class AliOssClass {
181
- private apiConfig;
182
- private clientCache;
183
- constructor(apiConfig: AliOssApiConfig);
184
- /**
185
- * 创建OSS客户端
186
- * @param businessType 业务类型
187
- * @returns OSS客户端实例
188
- */
189
- createOssClient(businessType: number): Promise<any>;
190
- /**
191
- * 预加载图片获取尺寸
192
- * @param file 文件对象
193
- * @returns 图片对象
194
- */
195
- imgUpload(file: File): Promise<HTMLImageElement>;
196
- /**
197
- * 生成文件名
198
- * @param imageType 图片类型
199
- * @param extension 文件扩展名
200
- * @returns 文件名信息
201
- */
202
- private generateFileName;
203
- /**
204
- * 检查图片是否为长图
205
- * @param width 图片宽度
206
- * @param height 图片高度
207
- * @returns 是否为长图
208
- */
209
- private isLongImage;
210
- /**
211
- * 执行OSS上传
212
- * @param client OSS客户端
213
- * @param fileName 文件名
214
- * @param file 文件对象
215
- * @param year 年份
216
- * @param mimeType MIME类型
217
- * @param onProgress 进度回调
218
- * @returns 上传结果
219
- */
220
- private performOssUpload;
221
- /**
222
- * 确定业务类型
223
- * @param option 上传选项
224
- * @param isGif 是否为GIF
225
- * @returns 业务类型
226
- */
227
- private determineBusinessType;
228
- /**
229
- * 加载图片并返回结果
230
- * 用于标准的图片上传场景
231
- * @param params 加载参数
232
- */
233
- loadImage(params: {
234
- url: string;
235
- val: any;
236
- suffix: string;
237
- file: File;
238
- option: UploadOption;
239
- resolve: (value: any) => void;
240
- reject: (reason?: any) => void;
241
- isGif?: boolean;
242
- }): void;
243
- /**
244
- * 加载图片并返回结果(新版)
245
- * 用于avatar、idCard等特殊场景,返回格式不同
246
- * @param params 加载参数
247
- */
248
- loadImageNew(params: {
249
- url: string;
250
- val: any;
251
- file: File;
252
- option: UploadOption;
253
- resolve: (value: any) => void;
254
- reject: (reason?: any) => void;
255
- }): void;
256
- /**
257
- * 图片上传主方法
258
- * 整合了所有项目的图片上传逻辑
259
- * @param option 上传选项
260
- * @returns Promise
261
- */
262
- ossUploadImage: (option: UploadOption) => Promise<any>;
263
- /**
264
- * 文件上传方法
265
- * 支持视频、文档、APK等文件类型
266
- * @param option 上传选项
267
- * @returns Promise
268
- */
269
- ossUploadFile: (option: UploadOption) => Promise<any>;
270
- /**
271
- * 商品详情图片上传
272
- * 注释:此方法用于商品详情页的图片上传,不压缩,使用NoWater客户端
273
- * @param option 上传选项
274
- * @returns Promise
275
- */
276
- shopDetailUpdate: (option: UploadOption) => Promise<any>;
277
- /**
278
- * 生成随机字符串
279
- * @param num 字符串长度
280
- */
281
- randomString: typeof randomString;
282
- /**
283
- * 后缀枚举
284
- */
285
- suffixEnum: {
286
- readonly nowater: "!nowater";
287
- readonly official: "!official";
288
- readonly panoram: "!panoram";
289
- readonly forum: "!forum";
290
- readonly avatar: "!avatar";
291
- readonly square: "!square";
292
- readonly carport: "!carport";
293
- };
294
- /**
295
- * 业务类型枚举
296
- */
297
- businessType: typeof BusinessType;
298
- }
299
- /**
300
- * 创建阿里云OSS上传器(工厂函数)
301
- * @param apiConfig API配置对象,包含业务相关的API函数
302
- * @returns OSS上传器实例
303
- */
304
- export declare function createAliOssUploader(apiConfig: AliOssApiConfig): AliOssClass;
305
- /**
306
- * 默认导出
307
- */
308
- declare const _default: {
309
- createAliOssUploader: typeof createAliOssUploader;
310
- BusinessType: typeof BusinessType;
311
- SuffixEnum: {
312
- readonly nowater: "!nowater";
313
- readonly official: "!official";
314
- readonly panoram: "!panoram";
315
- readonly forum: "!forum";
316
- readonly avatar: "!avatar";
317
- readonly square: "!square";
318
- readonly carport: "!carport";
319
- };
320
- };
321
- export default _default;