@haluo/util 2.0.18 → 2.0.20

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.
@@ -0,0 +1,272 @@
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
+ * 图片类型后缀枚举
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
+ imggeCarType?: any;
119
+ isVideo?: boolean;
120
+ isUnProtect?: boolean;
121
+ isDocument?: boolean;
122
+ onProgress?: (e: {
123
+ percent: number;
124
+ }) => void;
125
+ onSuccess?: (val: UploadResult) => void;
126
+ onError?: (err: string) => void;
127
+ }
128
+ /**
129
+ * API响应接口
130
+ */
131
+ interface ApiResponse<T = any> {
132
+ data: {
133
+ code: number;
134
+ data: T;
135
+ };
136
+ }
137
+ /**
138
+ * API函数配置接口
139
+ * 业务相关的API通过此接口传入,实现业务逻辑解耦
140
+ */
141
+ export interface AliOssApiConfig {
142
+ /** 获取阿里云STS临时凭证 - 必需 */
143
+ getAliyunSts: (params: {
144
+ businessType: number;
145
+ imggeCarType?: any;
146
+ }) => Promise<ApiResponse<OSSClientParams>>;
147
+ /** 暗水印上传接口 - 可选,用于carport类型 */
148
+ darkWaterUploadImage?: (params: {
149
+ file: File;
150
+ }) => Promise<ApiResponse<string>>;
151
+ /** 批量转换水印接口 - 可选,用于batchTransfer */
152
+ multiTransferImage?: (params: {
153
+ file: File;
154
+ }) => Promise<ApiResponse<string>>;
155
+ /** 阿里云持久化接口 - 可选,用于avatar类型 */
156
+ aliyunPersist?: (params: {
157
+ object: string;
158
+ businessType: number;
159
+ }) => Promise<ApiResponse<string>>;
160
+ /** 生成预签名URL接口 - 可选,用于idCard */
161
+ generatePrePresignedUrl?: (params: {
162
+ objectId: string;
163
+ expireMils: number;
164
+ }) => Promise<ApiResponse<string>>;
165
+ /** 上传配置接口 - 可选,用于document上传 */
166
+ uploadConf?: (businessType: number) => Promise<ApiResponse>;
167
+ /** 日期格式化函数 - 必需,支持 util.date.format 或 util.filter.format */
168
+ dateFormat: (date: Date, format: string) => string;
169
+ /** 消息提示函数 - 可选 */
170
+ messageWarning?: (msg: string) => void;
171
+ }
172
+ /**
173
+ * 生成随机字符串
174
+ * @param length 字符串长度
175
+ * @returns 随机字符串
176
+ */
177
+ declare function randomString(length: number): string;
178
+ /**
179
+ * 创建阿里云OSS上传器
180
+ * @param apiConfig API配置对象,包含业务相关的API函数
181
+ * @returns OSS上传器实例
182
+ */
183
+ export declare function createAliOssUploader(apiConfig: AliOssApiConfig): {
184
+ /**
185
+ * 图片上传
186
+ * @param option 上传选项
187
+ */
188
+ ossUploadImage: (option: UploadOption) => Promise<any>;
189
+ /**
190
+ * 文件上传(视频、文档、APK等)
191
+ * @param option 上传选项
192
+ */
193
+ ossUploadFile: (option: UploadOption) => Promise<any>;
194
+ /**
195
+ * 商品详情图片上传
196
+ * @param option 上传选项
197
+ */
198
+ shopDetailUpdate: (option: UploadOption) => Promise<any>;
199
+ /**
200
+ * 后缀枚举
201
+ */
202
+ suffixEnum: {
203
+ readonly nowater: "!nowater";
204
+ readonly official: "!official";
205
+ readonly panoram: "!panoram";
206
+ readonly forum: "!forum";
207
+ readonly avatar: "!avatar";
208
+ readonly square: "!square";
209
+ readonly carport: "!carport";
210
+ };
211
+ /**
212
+ * 业务类型枚举
213
+ */
214
+ businessType: typeof BusinessType;
215
+ /**
216
+ * 生成随机字符串
217
+ * @param num 字符串长度
218
+ */
219
+ randomString: typeof randomString;
220
+ /**
221
+ * 创建OSS客户端(高级用法)
222
+ * @param businessType 业务类型
223
+ * @param imggeCarType 车辆图片类型
224
+ */
225
+ createOssClient: (businessType: number, imggeCarType?: any) => Promise<any>;
226
+ /**
227
+ * 预加载图片
228
+ * @param file 文件对象
229
+ */
230
+ imgUpload: (file: File) => Promise<HTMLImageElement>;
231
+ /**
232
+ * 加载图片并返回结果
233
+ */
234
+ loadImage: (params: {
235
+ url: string;
236
+ val: any;
237
+ suffix: string;
238
+ file: File;
239
+ option: UploadOption;
240
+ resolve: (value: any) => void;
241
+ reject: (reason?: any) => void;
242
+ isGif?: boolean | undefined;
243
+ }) => void;
244
+ /**
245
+ * 加载图片并返回结果(新版)
246
+ */
247
+ loadImageNew: (params: {
248
+ url: string;
249
+ val: any;
250
+ file: File;
251
+ option: UploadOption;
252
+ resolve: (value: any) => void;
253
+ reject: (reason?: any) => void;
254
+ }) => void;
255
+ };
256
+ /**
257
+ * 默认导出
258
+ */
259
+ declare const _default: {
260
+ createAliOssUploader: typeof createAliOssUploader;
261
+ BusinessType: typeof BusinessType;
262
+ SuffixEnum: {
263
+ readonly nowater: "!nowater";
264
+ readonly official: "!official";
265
+ readonly panoram: "!panoram";
266
+ readonly forum: "!forum";
267
+ readonly avatar: "!avatar";
268
+ readonly square: "!square";
269
+ readonly carport: "!carport";
270
+ };
271
+ };
272
+ export default _default;