@hzab/utils 1.1.1 → 1.1.3

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,341 +1,342 @@
1
- import dayjs from "dayjs";
2
- import { Axios, AxiosRequestConfig } from "axios";
3
-
4
- import { axios } from "@hzab/data-model";
5
-
6
- import { formatDirStr, mergeDirStr, getFileNameByFileObj } from "./uploadUtils";
7
-
8
- /**
9
- * 获取上传相关配置入参
10
- */
11
- export interface IGetSignatureOpt {
12
- /**
13
- * 配置接口地址
14
- */
15
- serverUrl?: string;
16
- /**
17
- * axios 实例
18
- */
19
- axios?: Axios;
20
- /**
21
- * axios 配置
22
- */
23
- axiosConf?: AxiosRequestConfig;
24
- /**
25
- * 请求入参
26
- */
27
- params?: {
28
- path?: string;
29
- };
30
- }
31
-
32
- export interface IOfflineUploadProps {
33
- /**
34
- * axios 实例
35
- */
36
- axios?: Axios;
37
- /**
38
- * axios 配置
39
- */
40
- axiosConf?: AxiosRequestConfig;
41
- /**
42
- * 配置接口地址
43
- */
44
- serverUrl?: string;
45
- /**
46
- * 获取配置接口请求入参
47
- */
48
- signatureParams?: {
49
- path?: string;
50
- };
51
- }
52
-
53
- /**
54
- * 文件上传入参
55
- */
56
- export interface IUploadOpt {
57
- /**
58
- * 配置接口地址
59
- */
60
- serverUrl?: string;
61
- /**
62
- * 是否开启 Hash 命名
63
- */
64
- useHashName?: boolean;
65
- /**
66
- * 文件上传接口地址
67
- */
68
- uploadUrl?: string;
69
- /**
70
- * 获取配置接口请求入参
71
- */
72
- params?: {
73
- path?: string;
74
- // 兼容 老数据
75
- dir?: string;
76
- };
77
- /**
78
- * 文件上传请求入参
79
- */
80
- ossParams?: {
81
- path?: string;
82
- // 兼容 老数据
83
- dir?: string;
84
- };
85
- /**
86
- * axios 实例
87
- */
88
- axios?: Axios;
89
- /**
90
- * axios 配置
91
- */
92
- axiosConf?: AxiosRequestConfig;
93
- }
94
-
95
- /**
96
- * 获取上传配置
97
- * @param opt
98
- * @returns
99
- */
100
- export function getSignature(opt: IGetSignatureOpt = {}) {
101
- const { serverUrl = "/api/v1/upload/init" } = opt;
102
- // 减 10 秒,避免发起请求时 刚好过期的情况
103
- const { axios: _ax = axios, params = {}, axiosConf } = opt;
104
- if (
105
- window._$offlineSignatureRes &&
106
- window._$offlineSignatureRes.data &&
107
- serverUrl + JSON.stringify(params) === window._$offlineSignatureRes.serverUrlParams &&
108
- dayjs().valueOf() - window._$offlineSignatureRes.__saveTime < window._$offlineSignatureRes.expireTimeMilles - 10000
109
- ) {
110
- return Promise.resolve(window._$offlineSignatureRes.data);
111
- }
112
- // 处理 path 格式,必须为非 / 开头, / 结尾。如: test/
113
- params.path = formatDirStr(params.path);
114
-
115
- return _ax
116
- .post(
117
- serverUrl,
118
- {
119
- ...params,
120
- },
121
- {
122
- ...axiosConf,
123
- },
124
- )
125
- .then((res) => {
126
- if (res?.data?.code === 500) {
127
- return Promise.reject(res.data);
128
- }
129
- if (res?.data?.code === 200) {
130
- window._$offlineSignatureRes = {
131
- data: res?.data?.data,
132
- };
133
- if (window._$offlineSignatureRes) {
134
- window._$offlineSignatureRes.__saveTime = dayjs().valueOf();
135
- window._$offlineSignatureRes.serverUrlParams = serverUrl + JSON.stringify(params);
136
- }
137
- return window._$offlineSignatureRes.data;
138
- }
139
- });
140
- }
141
-
142
- export interface IGetPreviewUrlsOpt {
143
- previewUrl?: string;
144
- params?: Object;
145
- axios?: Axios;
146
- axiosConf?: AxiosRequestConfig;
147
- }
148
-
149
- /**
150
- * 获取预览地址-批量
151
- * @param urlList
152
- * @param opt
153
- * @returns
154
- */
155
- export const getPreviewUrls = function (urlList, opt: IGetPreviewUrlsOpt = {}) {
156
- return new Promise(async (resolve, reject) => {
157
- const { axios: _ax = axios, axiosConf, previewUrl } = opt || {};
158
-
159
- const _axios = _ax;
160
-
161
- let _list = urlList;
162
- if (_list && !Array.isArray(_list)) {
163
- _list = [_list];
164
- }
165
-
166
- return _axios
167
- .post(previewUrl || "/api/v1/upload/preview", _list, { ...axiosConf })
168
- .then((res) => {
169
- if (res?.data?.code == 200) {
170
- resolve(res?.data?.data);
171
- } else {
172
- reject(res);
173
- }
174
- return res;
175
- })
176
- .catch((err) => {
177
- console.error("offline upload err", err);
178
- reject(err);
179
- return Promise.reject(err);
180
- });
181
- });
182
- };
183
-
184
- /**
185
- * 获取预览地址
186
- * @param url
187
- * @param opt
188
- * @returns
189
- */
190
- export const getPreviewUrl = function (url, opt: IGetPreviewUrlsOpt = {}) {
191
- return new Promise(async (resolve, reject) => {
192
- const { axios: _ax = axios, axiosConf, previewUrl } = opt || {};
193
-
194
- const _axios = _ax;
195
-
196
- return _axios
197
- .post(previewUrl || "/api/v1/upload/preview", [url], { ...axiosConf })
198
- .then((res) => {
199
- if (res?.data?.code == 200) {
200
- resolve(res?.data?.data?.[0]);
201
- } else {
202
- reject(res);
203
- }
204
- return res;
205
- })
206
- .catch((err) => {
207
- console.error("offline upload err", err);
208
- reject(err);
209
- return Promise.reject(err);
210
- });
211
- });
212
- };
213
-
214
- /**
215
- * 私有部署版文件上传
216
- */
217
- export class OfflineUpload {
218
- axios;
219
- axiosConf;
220
- serverUrl;
221
- signatureParams;
222
- constructor(props: IOfflineUploadProps = {}) {
223
- this.axios = props.axios || axios;
224
- this.axiosConf = props.axiosConf || {};
225
- this.serverUrl = props.serverUrl || "/api/v1/upload/init";
226
- this.signatureParams = props.signatureParams || {};
227
- }
228
-
229
- getSignature(serverUrl = this.serverUrl, opt) {
230
- // path 前缀 oss-upload 文件目录
231
- opt.params.path = mergeDirStr("web-upload/", opt.params.path ?? opt.params.dir);
232
- return getSignature({
233
- ...opt,
234
- serverUrl,
235
- axios: opt?.axios || this.axios,
236
- axiosConf: { ...this.axiosConf, ...opt?.axiosConf },
237
- });
238
- }
239
-
240
- upload(file, opt: IUploadOpt = {}) {
241
- return new Promise(async (resolve, reject) => {
242
- // filename 表示待上传的本地文件名称。
243
- const filename = getFileNameByFileObj(file, opt);
244
- try {
245
- const fileInfo = await this.getSignature(opt.serverUrl || this.serverUrl, {
246
- ...opt,
247
- params: {
248
- /** 最终保存的文件名称(不传默认文件名) */
249
- filename,
250
- /** 存储路径(不传默认基础路径) */
251
- path: opt.params?.dir || this.signatureParams?.dir,
252
- /** 文件存储平台(1、s3,2、本地存储,3、FTP,默认s3) */
253
- platformCode: undefined,
254
- ...this.signatureParams,
255
- ...opt.params,
256
- },
257
- });
258
- const { uploadUrl, ossParams: propOssParams } = opt || {};
259
- const formData = new FormData();
260
- formData.set("fileInfo", fileInfo);
261
- formData.set("file", file);
262
-
263
- if (propOssParams) {
264
- for (const key in propOssParams) {
265
- if (Object.hasOwnProperty.call(propOssParams, key)) {
266
- formData.set(key, propOssParams[key]);
267
- }
268
- }
269
- }
270
-
271
- const _axios = opt?.axios || this.axios;
272
-
273
- return _axios
274
- .post(uploadUrl || "/api/v1/upload/execute", formData, { ...this.axiosConf, ...opt?.axiosConf })
275
- .then((res) => {
276
- if (res?.data?.code == 200) {
277
- resolve(res?.data?.data);
278
- } else {
279
- reject(res);
280
- }
281
- return res;
282
- })
283
- .catch((err) => {
284
- console.error("offline upload err", err);
285
- reject(err);
286
- return Promise.reject(err);
287
- });
288
- } catch (error) {
289
- reject(error);
290
- return Promise.reject(error);
291
- }
292
- });
293
- }
294
-
295
- /**
296
- * 获取预览地址
297
- * @param urlList
298
- * @param opt
299
- * @returns
300
- */
301
- getPreviewUrls(urlList, opt) {
302
- return getPreviewUrls(urlList, {
303
- ...opt,
304
- axios: opt?.axios || this.axios,
305
- axiosConf: { ...this.axiosConf, ...opt?.axiosConf },
306
- });
307
- }
308
- }
309
-
310
- /**
311
- * 批量处理预览地址
312
- * @param fileList
313
- * @param opt
314
- * @returns
315
- */
316
- export const handlePreviewUrls = async function (fileList, opt: IGetPreviewUrlsOpt) {
317
- const res = await getPreviewUrls(
318
- fileList?.map((it) => it.storeUrl || it.url),
319
- {
320
- ...opt,
321
- },
322
- );
323
-
324
- fileList.forEach((it, i) => {
325
- // TODO: storeUrl 来源于【本地 url 直接使用预览地址,解决 previewFile 无法正常使用 previewUrl 的问题】
326
- if (!it.storeUrl) {
327
- it.storeUrl = it.url;
328
- }
329
- // TODO: 本地 url 直接使用预览地址,解决 previewFile 无法正常使用 previewUrl 的问题
330
- it.url = res[i].presignedUrl;
331
-
332
- // 常规预览地址写法
333
- it.previewUrl = res[i].presignedUrl;
334
- if (it.uploadInfo) {
335
- it.uploadInfo.previewUrl = it.previewUrl;
336
- }
337
- });
338
- return fileList;
339
- };
340
-
341
- export default OfflineUpload;
1
+ import dayjs from "dayjs";
2
+ import { Axios, AxiosRequestConfig } from "axios";
3
+
4
+ import { axios } from "@hzab/data-model";
5
+
6
+ import { formatDirStr, mergeDirStr, getFileNameByFileObj } from "./uploadUtils";
7
+
8
+ /**
9
+ * 获取上传相关配置入参
10
+ */
11
+ export interface IGetSignatureOpt {
12
+ /**
13
+ * 配置接口地址
14
+ */
15
+ serverUrl?: string;
16
+ /**
17
+ * axios 实例
18
+ */
19
+ axios?: Axios;
20
+ /**
21
+ * axios 配置
22
+ */
23
+ axiosConf?: AxiosRequestConfig;
24
+ /**
25
+ * 请求入参
26
+ */
27
+ params?: {
28
+ path?: string;
29
+ };
30
+ }
31
+
32
+ export interface IOfflineUploadProps {
33
+ /**
34
+ * axios 实例
35
+ */
36
+ axios?: Axios;
37
+ /**
38
+ * axios 配置
39
+ */
40
+ axiosConf?: AxiosRequestConfig;
41
+ /**
42
+ * 配置接口地址
43
+ */
44
+ serverUrl?: string;
45
+ /**
46
+ * 获取配置接口请求入参
47
+ */
48
+ signatureParams?: {
49
+ path?: string;
50
+ };
51
+ }
52
+
53
+ /**
54
+ * 文件上传入参
55
+ */
56
+ export interface IUploadOpt {
57
+ /**
58
+ * 配置接口地址
59
+ */
60
+ serverUrl?: string;
61
+ /**
62
+ * 是否开启 Hash 命名
63
+ */
64
+ useHashName?: boolean;
65
+ /**
66
+ * 文件上传接口地址
67
+ */
68
+ uploadUrl?: string;
69
+ /**
70
+ * 获取配置接口请求入参
71
+ */
72
+ params?: {
73
+ path?: string;
74
+ // 兼容 老数据
75
+ dir?: string;
76
+ };
77
+ /**
78
+ * 文件上传请求入参
79
+ */
80
+ ossParams?: {
81
+ path?: string;
82
+ // 兼容 老数据
83
+ dir?: string;
84
+ };
85
+ /**
86
+ * axios 实例
87
+ */
88
+ axios?: Axios;
89
+ /**
90
+ * axios 配置
91
+ */
92
+ axiosConf?: AxiosRequestConfig;
93
+ }
94
+
95
+ /**
96
+ * 获取上传配置
97
+ * @param opt
98
+ * @returns
99
+ */
100
+ export function getSignature(opt: IGetSignatureOpt = {}) {
101
+ const { serverUrl = "/api/v1/upload/init" } = opt;
102
+ // 减 10 秒,避免发起请求时 刚好过期的情况
103
+ const { axios: _ax = axios, params = {}, axiosConf } = opt;
104
+ if (
105
+ window._$offlineSignatureRes &&
106
+ window._$offlineSignatureRes.data &&
107
+ serverUrl + JSON.stringify(params) === window._$offlineSignatureRes.serverUrlParams &&
108
+ dayjs().valueOf() - window._$offlineSignatureRes.__saveTime < window._$offlineSignatureRes.expireTimeMilles - 10000
109
+ ) {
110
+ return Promise.resolve(window._$offlineSignatureRes.data);
111
+ }
112
+ // 处理 path 格式,必须为非 / 开头, / 结尾。如: test/
113
+ params.path = formatDirStr(params.path);
114
+
115
+ return _ax
116
+ .post(
117
+ serverUrl,
118
+ {
119
+ ...params,
120
+ },
121
+ {
122
+ ...axiosConf,
123
+ },
124
+ )
125
+ .then((res) => {
126
+ if (res?.data?.code === 500) {
127
+ return Promise.reject(res.data);
128
+ }
129
+ if (res?.data?.code === 200) {
130
+ window._$offlineSignatureRes = {
131
+ data: res?.data?.data,
132
+ };
133
+ if (window._$offlineSignatureRes) {
134
+ window._$offlineSignatureRes.__saveTime = dayjs().valueOf();
135
+ window._$offlineSignatureRes.serverUrlParams = serverUrl + JSON.stringify(params);
136
+ }
137
+ return window._$offlineSignatureRes.data;
138
+ }
139
+ });
140
+ }
141
+
142
+ export interface IGetPreviewUrlsOpt {
143
+ previewUrl?: string;
144
+ params?: Object;
145
+ axios?: Axios;
146
+ axiosConf?: AxiosRequestConfig;
147
+ }
148
+
149
+ /**
150
+ * 获取预览地址-批量
151
+ * @param urlList
152
+ * @param opt
153
+ * @returns
154
+ */
155
+ export const getPreviewUrls = function (urlList, opt: IGetPreviewUrlsOpt = {}) {
156
+ return new Promise(async (resolve, reject) => {
157
+ const { axios: _ax = axios, axiosConf, previewUrl } = opt || {};
158
+
159
+ const _axios = _ax;
160
+
161
+ let _list = urlList;
162
+ if (_list && !Array.isArray(_list)) {
163
+ _list = [_list];
164
+ }
165
+
166
+ return _axios
167
+ .post(previewUrl || "/api/v1/upload/preview", _list, { ...axiosConf })
168
+ .then((res) => {
169
+ if (res?.data?.code == 200) {
170
+ resolve(res?.data?.data);
171
+ } else {
172
+ reject(res);
173
+ }
174
+ return res;
175
+ })
176
+ .catch((err) => {
177
+ console.error("offline upload err", err);
178
+ reject(err);
179
+ return Promise.reject(err);
180
+ });
181
+ });
182
+ };
183
+
184
+ /**
185
+ * 获取预览地址
186
+ * @param url
187
+ * @param opt
188
+ * @returns
189
+ */
190
+ export const getPreviewUrl = function (url, opt: IGetPreviewUrlsOpt = {}) {
191
+ return new Promise(async (resolve, reject) => {
192
+ const { axios: _ax = axios, axiosConf, previewUrl } = opt || {};
193
+
194
+ const _axios = _ax;
195
+
196
+ return _axios
197
+ .post(previewUrl || "/api/v1/upload/preview", [url], { ...axiosConf })
198
+ .then((res) => {
199
+ if (res?.data?.code == 200) {
200
+ resolve(res?.data?.data?.[0]);
201
+ } else {
202
+ reject(res);
203
+ }
204
+ return res;
205
+ })
206
+ .catch((err) => {
207
+ console.error("offline upload err", err);
208
+ reject(err);
209
+ return Promise.reject(err);
210
+ });
211
+ });
212
+ };
213
+
214
+ /**
215
+ * 私有部署版文件上传
216
+ */
217
+ export class OfflineUpload {
218
+ axios;
219
+ axiosConf;
220
+ serverUrl;
221
+ signatureParams;
222
+ constructor(props: IOfflineUploadProps = {}) {
223
+ this.axios = props.axios || axios;
224
+ this.axiosConf = props.axiosConf || {};
225
+ this.serverUrl = props.serverUrl || "/api/v1/upload/init";
226
+ this.signatureParams = props.signatureParams || {};
227
+ }
228
+
229
+ getSignature(serverUrl = this.serverUrl, opt) {
230
+ // path 前缀 oss-upload 文件目录
231
+ const prePath = opt?.params?.hasPrePath !== false ? "web-upload/" : "";
232
+ opt.params.dir = mergeDirStr(prePath, opt.params.path ?? opt.params.dir);
233
+ return getSignature({
234
+ ...opt,
235
+ serverUrl,
236
+ axios: opt?.axios || this.axios,
237
+ axiosConf: { ...this.axiosConf, ...opt?.axiosConf },
238
+ });
239
+ }
240
+
241
+ upload(file, opt: IUploadOpt = {}) {
242
+ return new Promise(async (resolve, reject) => {
243
+ // filename 表示待上传的本地文件名称。
244
+ const filename = getFileNameByFileObj(file, opt);
245
+ try {
246
+ const fileInfo = await this.getSignature(opt.serverUrl || this.serverUrl, {
247
+ ...opt,
248
+ params: {
249
+ /** 最终保存的文件名称(不传默认文件名) */
250
+ filename,
251
+ /** 存储路径(不传默认基础路径) */
252
+ path: opt.params?.dir || this.signatureParams?.dir,
253
+ /** 文件存储平台(1、s3,2、本地存储,3、FTP,默认s3) */
254
+ platformCode: undefined,
255
+ ...this.signatureParams,
256
+ ...opt.params,
257
+ },
258
+ });
259
+ const { uploadUrl, ossParams: propOssParams } = opt || {};
260
+ const formData = new FormData();
261
+ formData.set("fileInfo", fileInfo);
262
+ formData.set("file", file);
263
+
264
+ if (propOssParams) {
265
+ for (const key in propOssParams) {
266
+ if (Object.hasOwnProperty.call(propOssParams, key)) {
267
+ formData.set(key, propOssParams[key]);
268
+ }
269
+ }
270
+ }
271
+
272
+ const _axios = opt?.axios || this.axios;
273
+
274
+ return _axios
275
+ .post(uploadUrl || "/api/v1/upload/execute", formData, { ...this.axiosConf, ...opt?.axiosConf })
276
+ .then((res) => {
277
+ if (res?.data?.code == 200) {
278
+ resolve(res?.data?.data);
279
+ } else {
280
+ reject(res);
281
+ }
282
+ return res;
283
+ })
284
+ .catch((err) => {
285
+ console.error("offline upload err", err);
286
+ reject(err);
287
+ return Promise.reject(err);
288
+ });
289
+ } catch (error) {
290
+ reject(error);
291
+ return Promise.reject(error);
292
+ }
293
+ });
294
+ }
295
+
296
+ /**
297
+ * 获取预览地址
298
+ * @param urlList
299
+ * @param opt
300
+ * @returns
301
+ */
302
+ getPreviewUrls(urlList, opt) {
303
+ return getPreviewUrls(urlList, {
304
+ ...opt,
305
+ axios: opt?.axios || this.axios,
306
+ axiosConf: { ...this.axiosConf, ...opt?.axiosConf },
307
+ });
308
+ }
309
+ }
310
+
311
+ /**
312
+ * 批量处理预览地址
313
+ * @param fileList
314
+ * @param opt
315
+ * @returns
316
+ */
317
+ export const handlePreviewUrls = async function (fileList, opt: IGetPreviewUrlsOpt) {
318
+ const res = await getPreviewUrls(
319
+ fileList?.map((it) => it.storeUrl || it.url),
320
+ {
321
+ ...opt,
322
+ },
323
+ );
324
+
325
+ fileList.forEach((it, i) => {
326
+ // TODO: storeUrl 来源于【本地 url 直接使用预览地址,解决 previewFile 无法正常使用 previewUrl 的问题】
327
+ if (!it.storeUrl) {
328
+ it.storeUrl = it.url;
329
+ }
330
+ // TODO: 本地 url 直接使用预览地址,解决 previewFile 无法正常使用 previewUrl 的问题
331
+ it.url = res[i].presignedUrl;
332
+
333
+ // 常规预览地址写法
334
+ it.previewUrl = res[i].presignedUrl;
335
+ if (it.uploadInfo) {
336
+ it.uploadInfo.previewUrl = it.previewUrl;
337
+ }
338
+ });
339
+ return fileList;
340
+ };
341
+
342
+ export default OfflineUpload;