@haluo/util 2.0.22 → 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,23 +1,19 @@
1
1
  /**
2
- * @file 阿里云OSS上传统一工具类
2
+ * @file 阿里云OSS上传统一工具类 v4
3
3
  * @description 整合所有项目的aliOss业务逻辑,通过传参形式支持不同业务场景
4
4
  * @Author: wanghui
5
5
  * @createBy: @2025.11.17
6
6
  *
7
- * 文档:https://help.aliyun.com/document_detail/64041.html
8
- *
9
7
  * 使用示例:
10
8
  * ```typescript
11
9
  * import { createAliOssUploader } from '@haluo/util'
12
10
  *
13
11
  * // 创建上传实例,传入业务相关的API函数
14
12
  * const ossUploader = createAliOssUploader({
15
- * getAliyunSts: (params) => api.getAliyunSts(params),
13
+ * getSts: (params) => api.getSts(params),
16
14
  * darkWaterUploadImage: (params) => api.darkWaterUploadImage(params),
17
15
  * multiTransferImage: (params) => api.multiTransferImage(params),
18
- * aliyunPersist: (params) => api.aliyunPersist(params),
19
16
  * generatePrePresignedUrl: (params) => api.generatePrePresignedUrl(params),
20
- * uploadConf: (params) => api.uploadConf(params),
21
17
  * dateFormat: util.date.format, // 或 util.filter.format
22
18
  * messageWarning: (msg) => window.$message.warning(msg)
23
19
  * })
@@ -25,9 +21,9 @@
25
21
  * // 图片上传
26
22
  * ossUploader.ossUploadImage({
27
23
  * file: file,
24
+ * businessType: 1500, // 业务类型 BusinessType
28
25
  * imageType: 'official', // nowater、official、panoram、forum、avatar、square、carport
29
26
  * quality: 0.7, // 压缩质量
30
- * businessType: 1500, // 业务类型:0(无水印)、1(头像)、4(GIF)、6(视频)、8(UnProtect)、1000(文章)、1199(私有)、1299(加密)、1500(默认)
31
27
  * batchTransfer: false, // 是否批量转换水印
32
28
  * notCompress: false, // 是否不压缩
33
29
  * idCard: false, // 是否身份证上传
@@ -39,10 +35,10 @@
39
35
  * // 文件上传
40
36
  * ossUploader.ossUploadFile({
41
37
  * file: file,
38
+ * businessType: 1500, // 业务类型 BusinessType
42
39
  * isVideo: false, // 是否视频
43
40
  * isUnProtect: false, // 是否UnProtect
44
41
  * isDocument: false, // 是否文档
45
- * businessType: 1500,
46
42
  * onProgress: (e) => console.log(e.percent),
47
43
  * onSuccess: (val) => console.log(val),
48
44
  * onError: (err) => console.error(err)
@@ -51,21 +47,70 @@
51
47
  */
52
48
  /**
53
49
  * 业务类型枚举
54
- * 不同的businessType对应不同的OSS bucket和水印策略
55
50
  */
56
51
  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
52
+ /** 客户端上传图片(跟用户相关) */
53
+ /** 隐私图片 */
54
+ CREDIT = 130541736,
55
+ /** 内容 */
56
+ CONTENT = 101300784,
57
+ /** 头像 */
58
+ AVATAR = 134539448,
59
+ /** 二手车 */
60
+ USED_CAR = 171978237,
61
+ /** 商城 */
62
+ MALL = 115259415,
63
+ /** 魔友圈 */
64
+ HOOP = 155669648,
65
+ /** 用户 */
66
+ USER = 170244368,
67
+ /** 骑行 */
68
+ CYCLING = 165103952,
69
+ /** 玩车 */
70
+ MOTORCYCLE = 151637735,
71
+ /** 租车 */
72
+ RENTALCAR = 149095014,
73
+ /** 经销商 */
74
+ SHOP = 172655993,
75
+ /** 财务 */
76
+ FINANCE = 188717541,
77
+ /** 反馈 */
78
+ FEEDBACK = 182411383,
79
+ /** 驾校 */
80
+ DRIVING_SCHOOL = 172137757,
81
+ /** 评论图片 */
82
+ REPLY = 195929698,
83
+ /** 厂家 */
84
+ FACTORY = 118733601,
85
+ /** 骑行数据 */
86
+ CYCLING_DATA = 148681294,
87
+ /** 动图 */
88
+ GIF = 191290831,
89
+ /** 其他(原则上不可以使用) */
90
+ OTHER = 100694193,
91
+ /** 运营文件(跟用户无关) */
92
+ /** apk */
93
+ APK = 154033062,
94
+ /** 证书 */
95
+ CERT = 142668067,
96
+ /** oss数据文件(xlxs等) */
97
+ OSSDATA = 105038102,
98
+ /** 数据(题库等) */
99
+ DB = 180241065,
100
+ /** PDF */
101
+ PDF = 198157532,
102
+ /** 视频 */
103
+ VIDEOOPS = 172023381,
104
+ /** 运营图片(跟用户无关) */
105
+ /** 商城运营图片 */
106
+ MALLOPS = 137563987,
107
+ /** 运营图片 */
108
+ OSSOPS = 157532694,
109
+ /** 车库运营图片 */
110
+ CARPORT = 137563987
66
111
  }
67
112
  /**
68
- * 图片类型后缀枚举
113
+ * bucket 图片类型后缀枚举
69
114
  */
70
115
  export declare const SuffixEnum: {
71
116
  readonly nowater: "!nowater";
@@ -78,29 +123,15 @@ export declare const SuffixEnum: {
78
123
  };
79
124
  /** 图片类型 */
80
125
  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
126
  /**
96
127
  * 上传结果接口
97
128
  */
98
129
  export interface UploadResult {
99
- imgUrl?: string;
100
- imgOrgUrl?: string;
130
+ imgUrl: string;
131
+ imgOrgUrl: string;
132
+ fileName: string;
133
+ name: string;
101
134
  url?: string;
102
- fileName?: string;
103
- name?: string;
104
135
  bucket?: string;
105
136
  [key: string]: any;
106
137
  }
@@ -109,13 +140,12 @@ export interface UploadResult {
109
140
  */
110
141
  export interface UploadOption {
111
142
  file: File;
143
+ businessType: BusinessType | number;
112
144
  imageType?: ImageType | string;
113
145
  quality?: number;
114
- businessType?: BusinessType | number;
115
146
  batchTransfer?: boolean;
116
147
  notCompress?: boolean;
117
148
  idCard?: boolean;
118
- imggeCarType?: any;
119
149
  isVideo?: boolean;
120
150
  isUnProtect?: boolean;
121
151
  isDocument?: boolean;
@@ -139,11 +169,13 @@ interface ApiResponse<T = any> {
139
169
  * 业务相关的API通过此接口传入,实现业务逻辑解耦
140
170
  */
141
171
  export interface AliOssApiConfig {
142
- /** 获取阿里云STS临时凭证 - 必需 */
143
- getAliyunSts: (params: {
144
- businessType: number;
145
- imggeCarType?: any;
146
- }) => Promise<ApiResponse<OSSClientParams>>;
172
+ /** 获取STS,POST V4签名独有的表单元素- 必需 */
173
+ /** businessType: 业务编码(必传), docType: 文件格式如png(必传),size: _1024_567(可选) */
174
+ getSts: (params: {
175
+ businessType: BusinessType;
176
+ docType: string;
177
+ size?: string;
178
+ }) => any;
147
179
  /** 暗水印上传接口 - 可选,用于carport类型 */
148
180
  darkWaterUploadImage?: (params: {
149
181
  file: File;
@@ -152,103 +184,64 @@ export interface AliOssApiConfig {
152
184
  multiTransferImage?: (params: {
153
185
  file: File;
154
186
  }) => Promise<ApiResponse<string>>;
155
- /** 阿里云持久化接口 - 可选,用于avatar类型 */
156
- aliyunPersist?: (params: {
157
- object: string;
158
- businessType: number;
159
- }) => Promise<ApiResponse<string>>;
160
187
  /** 生成预签名URL接口 - 可选,用于idCard */
161
188
  generatePrePresignedUrl?: (params: {
162
189
  objectId: string;
163
190
  expireMils: number;
164
191
  }) => Promise<ApiResponse<string>>;
165
- /** 上传配置接口 - 可选,用于document上传 */
166
- uploadConf?: (businessType: number) => Promise<ApiResponse>;
167
192
  /** 日期格式化函数 - 必需,支持 util.date.format 或 util.filter.format */
168
193
  dateFormat: (date: Date, format: string) => string;
169
194
  /** 消息提示函数 - 可选 */
170
195
  messageWarning?: (msg: string) => void;
171
196
  }
172
- /**
173
- * 生成随机字符串
174
- * @param length 字符串长度
175
- * @returns 随机字符串
176
- */
177
- declare function randomString(length: number): string;
178
197
  /**
179
198
  * 阿里云OSS上传类
180
199
  * 整合所有项目的aliOss业务逻辑,通过依赖注入实现业务API解耦
181
200
  */
182
201
  export declare class AliOssClass {
183
202
  private apiConfig;
184
- private clientCache;
185
203
  constructor(apiConfig: AliOssApiConfig);
186
204
  /**
187
- * 创建OSS客户端
205
+ * 判断文件是否为图片(支持常见图片格式)
206
+ * @param {File} file - 上传的File对象
207
+ * @returns {boolean} 是否为图片
208
+ */
209
+ isImageFile(file: File): boolean;
210
+ /**
211
+ * 通过V4签名上传
212
+ * @param file 上传的文件
188
213
  * @param businessType 业务类型
189
- * @param imggeCarType 车辆图片类型(可选,用于特定业务)
190
- * @returns OSS客户端实例
214
+ * @param size 文件的尺寸
215
+ * @param resolve Promse
216
+ * @param reject Promse
217
+ * @returns 上传图片结果
191
218
  */
192
- createOssClient(businessType: number, imggeCarType?: any): Promise<any>;
219
+ upload(params: {
220
+ file: any;
221
+ businessType: BusinessType;
222
+ size?: string;
223
+ resolve?: Function;
224
+ reject?: Function;
225
+ callbacks?: any;
226
+ }): Promise<{}>;
193
227
  /**
194
228
  * 预加载图片获取尺寸
195
229
  * @param file 文件对象
196
230
  * @returns 图片对象
197
231
  */
198
- imgUpload(file: File): Promise<HTMLImageElement>;
199
- /**
200
- * 生成文件名
201
- * @param imageType 图片类型
202
- * @param extension 文件扩展名
203
- * @returns 文件名信息
204
- */
205
- private generateFileName;
232
+ getImageInfo(file: File): Promise<HTMLImageElement>;
206
233
  /**
207
234
  * 检查图片是否为长图
208
- * @param width 图片宽度
209
- * @param height 图片高度
235
+ * @param File 图片
210
236
  * @returns 是否为长图
211
237
  */
212
238
  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
- /**
225
- * 确定业务类型
226
- * @param option 上传选项
227
- * @param isGif 是否为GIF
228
- * @returns 业务类型
229
- */
230
- private determineBusinessType;
231
239
  /**
232
240
  * 加载图片并返回结果
233
241
  * 用于标准的图片上传场景
234
242
  * @param params 加载参数
235
243
  */
236
244
  loadImage(params: {
237
- url: string;
238
- val: any;
239
- suffix: string;
240
- file: File;
241
- option: UploadOption;
242
- resolve: (value: any) => void;
243
- reject: (reason?: any) => void;
244
- isGif?: boolean;
245
- }): void;
246
- /**
247
- * 加载图片并返回结果(新版)
248
- * 用于avatar、idCard等特殊场景,返回格式不同
249
- * @param params 加载参数
250
- */
251
- loadImageNew(params: {
252
245
  url: string;
253
246
  val: any;
254
247
  file: File;
@@ -271,29 +264,19 @@ export declare class AliOssClass {
271
264
  */
272
265
  ossUploadFile: (option: UploadOption) => Promise<any>;
273
266
  /**
274
- * 商品详情图片上传
275
- * 注释:此方法用于商品详情页的图片上传,不压缩,使用NoWater客户端
267
+ * 纯图片上传 pureOssUploadImage
268
+ * 注释:图片上传,不压缩
276
269
  * @param option 上传选项
277
270
  * @returns Promise
278
271
  */
279
- shopDetailUpdate: (option: UploadOption) => Promise<any>;
272
+ pureOssUploadImage: (option: UploadOption) => Promise<any>;
280
273
  /**
281
- * 生成随机字符串
282
- * @param num 字符串长度
283
- */
284
- randomString: typeof randomString;
285
- /**
286
- * 后缀枚举
274
+ * 商品详情图片上传(不建议直接使用,请使用 pureOssUploadImage)
275
+ * 注释:图片上传,不压缩
276
+ * @param option 上传选项
277
+ * @returns Promise
287
278
  */
288
- suffixEnum: {
289
- readonly nowater: "!nowater";
290
- readonly official: "!official";
291
- readonly panoram: "!panoram";
292
- readonly forum: "!forum";
293
- readonly avatar: "!avatar";
294
- readonly square: "!square";
295
- readonly carport: "!carport";
296
- };
279
+ shopDetailUpdate: (option: UploadOption) => Promise<any>;
297
280
  /**
298
281
  * 业务类型枚举
299
282
  */
@@ -311,14 +294,5 @@ export declare function createAliOssUploader(apiConfig: AliOssApiConfig): AliOss
311
294
  declare const _default: {
312
295
  createAliOssUploader: typeof createAliOssUploader;
313
296
  BusinessType: typeof BusinessType;
314
- SuffixEnum: {
315
- readonly nowater: "!nowater";
316
- readonly official: "!official";
317
- readonly panoram: "!panoram";
318
- readonly forum: "!forum";
319
- readonly avatar: "!avatar";
320
- readonly square: "!square";
321
- readonly carport: "!carport";
322
- };
323
297
  };
324
298
  export default _default;