@hzab/utils 1.0.8 → 1.0.10-beta
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/changelog.md +8 -0
- package/package.json +1 -1
- package/src/file/fileType.ts +222 -221
- package/src/globalConfig.ts +15 -0
- package/src/upload/OssUploadUtil.ts +6 -3
- package/src/url.ts +123 -120
package/changelog.md
CHANGED
package/package.json
CHANGED
package/src/file/fileType.ts
CHANGED
|
@@ -1,221 +1,222 @@
|
|
|
1
|
-
export const TYPE_VIDEO = "video";
|
|
2
|
-
export const TYPE_IMG = "image";
|
|
3
|
-
export const TYPE_AUDIO = "audio";
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* 检查 url 是否带有指定后缀
|
|
7
|
-
* @param {string} url url 地址
|
|
8
|
-
* @param {Array} types 后缀数组
|
|
9
|
-
* @returns
|
|
10
|
-
*/
|
|
11
|
-
export function checkUrlSuffix(url, types = [], caseSensitive = false) {
|
|
12
|
-
if (!url) {
|
|
13
|
-
return false;
|
|
14
|
-
}
|
|
15
|
-
let _url = url?.replace(/\?.+/, "");
|
|
16
|
-
const reg = new RegExp(`\.(${types.join("|")})$`, caseSensitive ? undefined : "i");
|
|
17
|
-
if (reg.test(_url)) {
|
|
18
|
-
return true;
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* 获取文件类型(文件自身类型,如:video/mp4、image/jpeg)
|
|
24
|
-
* @param file
|
|
25
|
-
* @returns
|
|
26
|
-
*/
|
|
27
|
-
export function getFileType(file) {
|
|
28
|
-
if (typeof file === "object" && file?.type) {
|
|
29
|
-
return file?.type;
|
|
30
|
-
}
|
|
31
|
-
if (typeof file === "string") {
|
|
32
|
-
let type = null;
|
|
33
|
-
if (checkVideoUrl(file)) {
|
|
34
|
-
type = "video/mp4";
|
|
35
|
-
} else if (checkImageUrl(file)) {
|
|
36
|
-
type = "image/jpeg";
|
|
37
|
-
} else if (checkAudioUrl(file)) {
|
|
38
|
-
type = "audio/3gpp";
|
|
39
|
-
} else if (checkUrlSuffix(file, ["pdf"])) {
|
|
40
|
-
type = "application/pdf";
|
|
41
|
-
} else if (checkUrlSuffix(file, ["xlsx", "xls", "csv", "xlsm", "xlsb"])) {
|
|
42
|
-
type = "application/vnd.ms-excel";
|
|
43
|
-
} else if (checkUrlSuffix(file, ["doc", "docx"])) {
|
|
44
|
-
type = "application/msword";
|
|
45
|
-
} else if (checkUrlSuffix(file, ["ppt", "pptx"])) {
|
|
46
|
-
type = "application/vnd.ms-powerpoint";
|
|
47
|
-
}
|
|
48
|
-
return type;
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* 判断 url 是否带有指定图片后缀
|
|
54
|
-
* @param {string} url
|
|
55
|
-
* @returns
|
|
56
|
-
*/
|
|
57
|
-
export function checkImageUrl(url) {
|
|
58
|
-
// base64
|
|
59
|
-
if (/^data:image\/\w+;base64/i.test(url)) {
|
|
60
|
-
return true;
|
|
61
|
-
}
|
|
62
|
-
const imgTypes = [
|
|
63
|
-
"apng",
|
|
64
|
-
"avif",
|
|
65
|
-
"bmp",
|
|
66
|
-
"gif",
|
|
67
|
-
"ico",
|
|
68
|
-
"cur",
|
|
69
|
-
"jpg",
|
|
70
|
-
"jpeg",
|
|
71
|
-
"jfif",
|
|
72
|
-
"pjpeg",
|
|
73
|
-
"pjp",
|
|
74
|
-
"png",
|
|
75
|
-
"svg",
|
|
76
|
-
"tif",
|
|
77
|
-
"tiff",
|
|
78
|
-
"webp",
|
|
79
|
-
];
|
|
80
|
-
return checkUrlSuffix(url, imgTypes);
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* 判断是否是合法的媒体类型
|
|
85
|
-
* @param {String} input
|
|
86
|
-
* @returns
|
|
87
|
-
*/
|
|
88
|
-
export function isValidMediaType(input) {
|
|
89
|
-
const validPrefixes = ["image/", "video/", "audio/"];
|
|
90
|
-
return validPrefixes.some((prefix) => input?.startsWith(prefix));
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
/**
|
|
94
|
-
* 判断 url 是否带有指定视频后缀
|
|
95
|
-
* @param {string} url
|
|
96
|
-
* @returns
|
|
97
|
-
*/
|
|
98
|
-
export function checkVideoUrl(url) {
|
|
99
|
-
const imgTypes = ["3gp", "mpg", "mpeg", "mp4", "m4v", "m4p", "ogv", "ogg", "mov", "webm"];
|
|
100
|
-
return checkUrlSuffix(url, imgTypes);
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
/**
|
|
104
|
-
* 判断 url 是否带有指定音频后缀
|
|
105
|
-
* @param {string} url
|
|
106
|
-
* @returns
|
|
107
|
-
*/
|
|
108
|
-
export function checkAudioUrl(url) {
|
|
109
|
-
const imgTypes = ["3gp", "adts", "mpeg", "mp3", "mp4", "ogg", "mov", "webm", "rtp", "amr", "wav"];
|
|
110
|
-
return checkUrlSuffix(url, imgTypes);
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
/**
|
|
114
|
-
* 获取文件类型(项目内部枚举,如:图片-image、视频-video)
|
|
115
|
-
* @param file
|
|
116
|
-
* @returns
|
|
117
|
-
*/
|
|
118
|
-
export function getFileTypeStr(file) {
|
|
119
|
-
const { type, url = file?.ossUrl } = file || {};
|
|
120
|
-
let fileType = "";
|
|
121
|
-
// 判断文件类型,获取对应展示的数据
|
|
122
|
-
if (url) {
|
|
123
|
-
// 图片
|
|
124
|
-
if (url.startsWith("data:image/") || checkImageUrl(url)) {
|
|
125
|
-
fileType = TYPE_IMG;
|
|
126
|
-
} else if (checkVideoUrl(url)) {
|
|
127
|
-
// 视频
|
|
128
|
-
fileType = TYPE_VIDEO;
|
|
129
|
-
} else if (checkAudioUrl(url)) {
|
|
130
|
-
// 音频
|
|
131
|
-
fileType = TYPE_AUDIO;
|
|
132
|
-
}
|
|
133
|
-
} else if (type) {
|
|
134
|
-
// 图片
|
|
135
|
-
if (type?.startsWith("image/")) {
|
|
136
|
-
fileType = TYPE_IMG;
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
// 视频
|
|
140
|
-
if (type?.startsWith("video/")) {
|
|
141
|
-
fileType = TYPE_VIDEO;
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
// 音频
|
|
145
|
-
if (type?.startsWith("audio/")) {
|
|
146
|
-
fileType = TYPE_AUDIO;
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
return fileType || type;
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
/**
|
|
153
|
-
* 根据文件的 MIME 类型 (file.type) 获取对应的文件后缀名
|
|
154
|
-
* @param {string} mimeType - 文件的 MIME 类型(如 'image/jpeg'、'application/pdf')
|
|
155
|
-
* @returns {string} 返回文件后缀名(带点,如 '.jpg'),未知类型返回 '.unknown'
|
|
156
|
-
*/
|
|
157
|
-
export const getFileTypeExt = function (mimeType) {
|
|
158
|
-
// 常见 MIME 类型到文件后缀的映射表(可根据需求扩展)
|
|
159
|
-
const mimeToExtMap = {
|
|
160
|
-
// 图片类型
|
|
161
|
-
"image/jpeg": "jpg",
|
|
162
|
-
"image/jpg": "jpg",
|
|
163
|
-
"image/png": "png",
|
|
164
|
-
"image/gif": "gif",
|
|
165
|
-
"image/webp": "webp",
|
|
166
|
-
"image/svg+xml": "svg",
|
|
167
|
-
"image/bmp": "bmp",
|
|
168
|
-
"image/tiff": "tiff",
|
|
169
|
-
|
|
170
|
-
// 文档类型
|
|
171
|
-
"application/pdf": "pdf",
|
|
172
|
-
"application/msword": "doc",
|
|
173
|
-
"application/vnd.openxmlformats-officedocument.wordprocessingml.document": "docx",
|
|
174
|
-
"application/vnd.ms-excel": "xls",
|
|
175
|
-
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": "xlsx",
|
|
176
|
-
"application/vnd.ms-powerpoint": "ppt",
|
|
177
|
-
"application/vnd.openxmlformats-officedocument.presentationml.presentation": "pptx",
|
|
178
|
-
"text/plain": "txt",
|
|
179
|
-
"text/html": "html",
|
|
180
|
-
"application/json": "json",
|
|
181
|
-
|
|
182
|
-
// 音频/视频类型
|
|
183
|
-
"audio/mpeg": "mp3",
|
|
184
|
-
"audio/wav": "wav",
|
|
185
|
-
"video/mp4": "mp4",
|
|
186
|
-
"video/avi": "avi",
|
|
187
|
-
"video/mpeg": "mpeg",
|
|
188
|
-
|
|
189
|
-
// 压缩包类型
|
|
190
|
-
"application/zip": "zip",
|
|
191
|
-
"application/x-rar-compressed": "rar",
|
|
192
|
-
"application/x-7z-compressed": "7z",
|
|
193
|
-
};
|
|
194
|
-
|
|
195
|
-
// 处理空值、非字符串的情况
|
|
196
|
-
if (!mimeType || typeof mimeType !== "string") {
|
|
197
|
-
// return "unknown";
|
|
198
|
-
return "";
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
// 去除首尾空格,统一转小写(避免大小写不一致问题)
|
|
202
|
-
const normalizedMimeType = mimeType.trim().toLowerCase();
|
|
203
|
-
|
|
204
|
-
// 优先匹配完整 MIME 类型
|
|
205
|
-
if (mimeToExtMap[normalizedMimeType]) {
|
|
206
|
-
return mimeToExtMap[normalizedMimeType];
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
// 兼容部分不标准的 MIME 类型(如只取主类型)
|
|
210
|
-
const mainType = normalizedMimeType.split("/")[0];
|
|
211
|
-
switch (mainType) {
|
|
212
|
-
case "image":
|
|
213
|
-
return "img";
|
|
214
|
-
case "audio":
|
|
215
|
-
return "audio";
|
|
216
|
-
case "video":
|
|
217
|
-
return "video";
|
|
218
|
-
default:
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
}
|
|
1
|
+
export const TYPE_VIDEO = "video";
|
|
2
|
+
export const TYPE_IMG = "image";
|
|
3
|
+
export const TYPE_AUDIO = "audio";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* 检查 url 是否带有指定后缀
|
|
7
|
+
* @param {string} url url 地址
|
|
8
|
+
* @param {Array} types 后缀数组
|
|
9
|
+
* @returns
|
|
10
|
+
*/
|
|
11
|
+
export function checkUrlSuffix(url, types = [], caseSensitive = false) {
|
|
12
|
+
if (!url) {
|
|
13
|
+
return false;
|
|
14
|
+
}
|
|
15
|
+
let _url = url?.replace(/\?.+/, "");
|
|
16
|
+
const reg = new RegExp(`\.(${types.join("|")})$`, caseSensitive ? undefined : "i");
|
|
17
|
+
if (reg.test(_url)) {
|
|
18
|
+
return true;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* 获取文件类型(文件自身类型,如:video/mp4、image/jpeg)
|
|
24
|
+
* @param file
|
|
25
|
+
* @returns
|
|
26
|
+
*/
|
|
27
|
+
export function getFileType(file) {
|
|
28
|
+
if (typeof file === "object" && file?.type) {
|
|
29
|
+
return file?.type;
|
|
30
|
+
}
|
|
31
|
+
if (typeof file === "string") {
|
|
32
|
+
let type = null;
|
|
33
|
+
if (checkVideoUrl(file)) {
|
|
34
|
+
type = "video/mp4";
|
|
35
|
+
} else if (checkImageUrl(file)) {
|
|
36
|
+
type = "image/jpeg";
|
|
37
|
+
} else if (checkAudioUrl(file)) {
|
|
38
|
+
type = "audio/3gpp";
|
|
39
|
+
} else if (checkUrlSuffix(file, ["pdf"])) {
|
|
40
|
+
type = "application/pdf";
|
|
41
|
+
} else if (checkUrlSuffix(file, ["xlsx", "xls", "csv", "xlsm", "xlsb"])) {
|
|
42
|
+
type = "application/vnd.ms-excel";
|
|
43
|
+
} else if (checkUrlSuffix(file, ["doc", "docx"])) {
|
|
44
|
+
type = "application/msword";
|
|
45
|
+
} else if (checkUrlSuffix(file, ["ppt", "pptx"])) {
|
|
46
|
+
type = "application/vnd.ms-powerpoint";
|
|
47
|
+
}
|
|
48
|
+
return type;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* 判断 url 是否带有指定图片后缀
|
|
54
|
+
* @param {string} url
|
|
55
|
+
* @returns
|
|
56
|
+
*/
|
|
57
|
+
export function checkImageUrl(url) {
|
|
58
|
+
// base64
|
|
59
|
+
if (/^data:image\/\w+;base64/i.test(url)) {
|
|
60
|
+
return true;
|
|
61
|
+
}
|
|
62
|
+
const imgTypes = [
|
|
63
|
+
"apng",
|
|
64
|
+
"avif",
|
|
65
|
+
"bmp",
|
|
66
|
+
"gif",
|
|
67
|
+
"ico",
|
|
68
|
+
"cur",
|
|
69
|
+
"jpg",
|
|
70
|
+
"jpeg",
|
|
71
|
+
"jfif",
|
|
72
|
+
"pjpeg",
|
|
73
|
+
"pjp",
|
|
74
|
+
"png",
|
|
75
|
+
"svg",
|
|
76
|
+
"tif",
|
|
77
|
+
"tiff",
|
|
78
|
+
"webp",
|
|
79
|
+
];
|
|
80
|
+
return checkUrlSuffix(url, imgTypes);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* 判断是否是合法的媒体类型
|
|
85
|
+
* @param {String} input
|
|
86
|
+
* @returns
|
|
87
|
+
*/
|
|
88
|
+
export function isValidMediaType(input) {
|
|
89
|
+
const validPrefixes = ["image/", "video/", "audio/"];
|
|
90
|
+
return validPrefixes.some((prefix) => input?.startsWith(prefix));
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* 判断 url 是否带有指定视频后缀
|
|
95
|
+
* @param {string} url
|
|
96
|
+
* @returns
|
|
97
|
+
*/
|
|
98
|
+
export function checkVideoUrl(url) {
|
|
99
|
+
const imgTypes = ["3gp", "mpg", "mpeg", "mp4", "m4v", "m4p", "ogv", "ogg", "mov", "webm"];
|
|
100
|
+
return checkUrlSuffix(url, imgTypes);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* 判断 url 是否带有指定音频后缀
|
|
105
|
+
* @param {string} url
|
|
106
|
+
* @returns
|
|
107
|
+
*/
|
|
108
|
+
export function checkAudioUrl(url) {
|
|
109
|
+
const imgTypes = ["3gp", "adts", "mpeg", "mp3", "mp4", "ogg", "mov", "webm", "rtp", "amr", "wav"];
|
|
110
|
+
return checkUrlSuffix(url, imgTypes);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* 获取文件类型(项目内部枚举,如:图片-image、视频-video)
|
|
115
|
+
* @param file
|
|
116
|
+
* @returns
|
|
117
|
+
*/
|
|
118
|
+
export function getFileTypeStr(file) {
|
|
119
|
+
const { type, url = file?.ossUrl } = file || {};
|
|
120
|
+
let fileType = "";
|
|
121
|
+
// 判断文件类型,获取对应展示的数据
|
|
122
|
+
if (url) {
|
|
123
|
+
// 图片
|
|
124
|
+
if (url.startsWith("data:image/") || checkImageUrl(url)) {
|
|
125
|
+
fileType = TYPE_IMG;
|
|
126
|
+
} else if (checkVideoUrl(url)) {
|
|
127
|
+
// 视频
|
|
128
|
+
fileType = TYPE_VIDEO;
|
|
129
|
+
} else if (checkAudioUrl(url)) {
|
|
130
|
+
// 音频
|
|
131
|
+
fileType = TYPE_AUDIO;
|
|
132
|
+
}
|
|
133
|
+
} else if (type) {
|
|
134
|
+
// 图片
|
|
135
|
+
if (type?.startsWith("image/")) {
|
|
136
|
+
fileType = TYPE_IMG;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// 视频
|
|
140
|
+
if (type?.startsWith("video/")) {
|
|
141
|
+
fileType = TYPE_VIDEO;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// 音频
|
|
145
|
+
if (type?.startsWith("audio/")) {
|
|
146
|
+
fileType = TYPE_AUDIO;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
return fileType || type;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* 根据文件的 MIME 类型 (file.type) 获取对应的文件后缀名
|
|
154
|
+
* @param {string} mimeType - 文件的 MIME 类型(如 'image/jpeg'、'application/pdf')
|
|
155
|
+
* @returns {string} 返回文件后缀名(带点,如 '.jpg'),未知类型返回 '.unknown'
|
|
156
|
+
*/
|
|
157
|
+
export const getFileTypeExt = function (mimeType) {
|
|
158
|
+
// 常见 MIME 类型到文件后缀的映射表(可根据需求扩展)
|
|
159
|
+
const mimeToExtMap = {
|
|
160
|
+
// 图片类型
|
|
161
|
+
"image/jpeg": "jpg",
|
|
162
|
+
"image/jpg": "jpg",
|
|
163
|
+
"image/png": "png",
|
|
164
|
+
"image/gif": "gif",
|
|
165
|
+
"image/webp": "webp",
|
|
166
|
+
"image/svg+xml": "svg",
|
|
167
|
+
"image/bmp": "bmp",
|
|
168
|
+
"image/tiff": "tiff",
|
|
169
|
+
|
|
170
|
+
// 文档类型
|
|
171
|
+
"application/pdf": "pdf",
|
|
172
|
+
"application/msword": "doc",
|
|
173
|
+
"application/vnd.openxmlformats-officedocument.wordprocessingml.document": "docx",
|
|
174
|
+
"application/vnd.ms-excel": "xls",
|
|
175
|
+
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": "xlsx",
|
|
176
|
+
"application/vnd.ms-powerpoint": "ppt",
|
|
177
|
+
"application/vnd.openxmlformats-officedocument.presentationml.presentation": "pptx",
|
|
178
|
+
"text/plain": "txt",
|
|
179
|
+
"text/html": "html",
|
|
180
|
+
"application/json": "json",
|
|
181
|
+
|
|
182
|
+
// 音频/视频类型
|
|
183
|
+
"audio/mpeg": "mp3",
|
|
184
|
+
"audio/wav": "wav",
|
|
185
|
+
"video/mp4": "mp4",
|
|
186
|
+
"video/avi": "avi",
|
|
187
|
+
"video/mpeg": "mpeg",
|
|
188
|
+
|
|
189
|
+
// 压缩包类型
|
|
190
|
+
"application/zip": "zip",
|
|
191
|
+
"application/x-rar-compressed": "rar",
|
|
192
|
+
"application/x-7z-compressed": "7z",
|
|
193
|
+
};
|
|
194
|
+
|
|
195
|
+
// 处理空值、非字符串的情况
|
|
196
|
+
if (!mimeType || typeof mimeType !== "string") {
|
|
197
|
+
// return "unknown";
|
|
198
|
+
return "";
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// 去除首尾空格,统一转小写(避免大小写不一致问题)
|
|
202
|
+
const normalizedMimeType = mimeType.trim().toLowerCase();
|
|
203
|
+
|
|
204
|
+
// 优先匹配完整 MIME 类型
|
|
205
|
+
if (mimeToExtMap[normalizedMimeType]) {
|
|
206
|
+
return mimeToExtMap[normalizedMimeType];
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
// 兼容部分不标准的 MIME 类型(如只取主类型)
|
|
210
|
+
const mainType = normalizedMimeType.split("/")[0];
|
|
211
|
+
switch (mainType) {
|
|
212
|
+
case "image":
|
|
213
|
+
return "img";
|
|
214
|
+
case "audio":
|
|
215
|
+
return "audio";
|
|
216
|
+
case "video":
|
|
217
|
+
return "video";
|
|
218
|
+
default:
|
|
219
|
+
//移动端默认获取源文件类型(后缀名)
|
|
220
|
+
return mainType?.match(/[^\/]*\/([^\/]+)$/)?.[1] || mimeType;
|
|
221
|
+
}
|
|
222
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
window.utilsGlobalServeConfig = {}
|
|
2
|
+
/** 获取全局配置(url、是否显示缩略图地址) */
|
|
3
|
+
export function getGlobalServeConfig() {
|
|
4
|
+
return window.utilsGlobalServeConfig || {}
|
|
5
|
+
}
|
|
6
|
+
/** 设置全局配置(url、是否显示缩略图地址)*/
|
|
7
|
+
export function setGlobalServeConfigs(data) {
|
|
8
|
+
Object.assign(window.utilsGlobalServeConfig || {}, data);
|
|
9
|
+
return window.utilsGlobalServeConfig || {}
|
|
10
|
+
}
|
|
11
|
+
/** 设置全局单个配置(url、是否显示缩略图地址)*/
|
|
12
|
+
export function setGlobalServeConfig(key, data) {
|
|
13
|
+
window.utilsGlobalServeConfig[key] = data
|
|
14
|
+
return window.utilsGlobalServeConfig || {}
|
|
15
|
+
}
|
|
@@ -2,6 +2,7 @@ import dayjs from "dayjs";
|
|
|
2
2
|
|
|
3
3
|
import { axios } from "@hzab/data-model";
|
|
4
4
|
import { formatDirStr } from "./uploadUtils";
|
|
5
|
+
import { getGlobalServeConfig } from "../globalConfig"
|
|
5
6
|
const isPublicMap = {
|
|
6
7
|
0: "&",
|
|
7
8
|
1: "?"
|
|
@@ -75,7 +76,7 @@ export interface IUploadOpt {
|
|
|
75
76
|
}
|
|
76
77
|
|
|
77
78
|
export function getSignature(opt: IGetSignatureOpt = {}) {
|
|
78
|
-
const { serverUrl = "/api/v1/
|
|
79
|
+
const { serverUrl = getGlobalServeConfig()?.serverUrl || "/api/v1/user/oss/getWebOssConfig" } = opt;
|
|
79
80
|
// 减 10 秒,避免发起请求时 刚好过期的情况
|
|
80
81
|
if (
|
|
81
82
|
window._$ossSignatureRes &&
|
|
@@ -127,7 +128,7 @@ class OssUploadUtil {
|
|
|
127
128
|
constructor(props: IOssUploadProps = {}) {
|
|
128
129
|
this.axios = props.axios || axios;
|
|
129
130
|
this.axiosConf = props.axiosConf || {};
|
|
130
|
-
this.serverUrl = props.serverUrl || "/api/v1/
|
|
131
|
+
this.serverUrl = props.serverUrl || getGlobalServeConfig()?.serverUrl || "/api/v1/user/oss/getWebOssConfig";
|
|
131
132
|
this.signatureParams = props.signatureParams || {};
|
|
132
133
|
}
|
|
133
134
|
|
|
@@ -185,7 +186,9 @@ class OssUploadUtil {
|
|
|
185
186
|
return _axios
|
|
186
187
|
.post(ossParams.host, formData, { ...this.axiosConf, ...opt?.axiosConf })
|
|
187
188
|
.then((res) => {
|
|
188
|
-
|
|
189
|
+
if (getGlobalServeConfig()?.showThumbnail) {
|
|
190
|
+
res.data.data.thumbnailUrl = `${res?.data?.data?.fileUrl}${opt?.thumbnailParams ? isPublicMap[opt?.signatureParams?.isPublic] + opt?.thumbnailParams : ""}`;
|
|
191
|
+
}
|
|
189
192
|
resolve(res);
|
|
190
193
|
return res;
|
|
191
194
|
})
|
package/src/url.ts
CHANGED
|
@@ -1,120 +1,123 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 替换 search 字符串
|
|
3
|
-
* @param url
|
|
4
|
-
* @param search
|
|
5
|
-
* @returns
|
|
6
|
-
*/
|
|
7
|
-
export function replaceSearch(url, search) {
|
|
8
|
-
if (!url || typeof url !== "string") {
|
|
9
|
-
console.warn("Warn handleUrlQuery: 请传入正确的 url 字符串");
|
|
10
|
-
return url;
|
|
11
|
-
}
|
|
12
|
-
let _url = fixUrlQuery(url);
|
|
13
|
-
|
|
14
|
-
const hashIdx = _url.indexOf("#");
|
|
15
|
-
const queryIdx = _url.indexOf("?");
|
|
16
|
-
let hash = "";
|
|
17
|
-
let resUrl = _url;
|
|
18
|
-
if (queryIdx >= 0) {
|
|
19
|
-
resUrl = _url.slice(0, queryIdx);
|
|
20
|
-
}
|
|
21
|
-
if (hashIdx >= 0) {
|
|
22
|
-
hash = _url.slice(hashIdx);
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
resUrl += search?.indexOf("?") >= 0 ? "" : "?";
|
|
26
|
-
resUrl += search;
|
|
27
|
-
resUrl += hash;
|
|
28
|
-
|
|
29
|
-
return resUrl;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* 把对象转为 query 参数
|
|
34
|
-
* @param query
|
|
35
|
-
*/
|
|
36
|
-
export function objToSearch(query, hasQuery) {
|
|
37
|
-
if (!query || typeof query !== "object") {
|
|
38
|
-
console.warn("Warn objToSearch: 请传入正确的对象");
|
|
39
|
-
return;
|
|
40
|
-
}
|
|
41
|
-
let search = hasQuery ? "?" : "";
|
|
42
|
-
Object.keys(query).forEach((key) => {
|
|
43
|
-
if (search && search != "?") {
|
|
44
|
-
search += "&";
|
|
45
|
-
}
|
|
46
|
-
search += key;
|
|
47
|
-
search += "=";
|
|
48
|
-
search += query[key];
|
|
49
|
-
});
|
|
50
|
-
return search;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* 修复 query,解决 query 在 hash 后面的情况
|
|
55
|
-
* @param url
|
|
56
|
-
* @returns
|
|
57
|
-
* 源数据:/#/login?a=1&b=2
|
|
58
|
-
* 结果:/?a=1&b=2#/login
|
|
59
|
-
*/
|
|
60
|
-
export function fixUrlQuery(url) {
|
|
61
|
-
if (!url || typeof url !== "string") {
|
|
62
|
-
console.warn("Warn handleUrlQuery: 请传入正确的 url 字符串");
|
|
63
|
-
return url;
|
|
64
|
-
}
|
|
65
|
-
const hashIdx = url.indexOf("#");
|
|
66
|
-
const queryIdx = url.indexOf("?");
|
|
67
|
-
|
|
68
|
-
// 双 query 情况
|
|
69
|
-
if (queryIdx < hashIdx) {
|
|
70
|
-
let hash = url.slice(hashIdx);
|
|
71
|
-
const hashQueryIdx = hash.indexOf("?");
|
|
72
|
-
if (hashQueryIdx >= 0) {
|
|
73
|
-
const _url = url.slice(0, queryIdx);
|
|
74
|
-
const urlQuery = url.slice(queryIdx, hashIdx)?.replace(/\/$/, "");
|
|
75
|
-
const hashQuery = hash.slice(hashQueryIdx)?.replace("?", "")?.replace(/\/$/, "");
|
|
76
|
-
hash = hash.slice(0, hashQueryIdx);
|
|
77
|
-
return `${_url}${urlQuery}&${hashQuery}${hash}`;
|
|
78
|
-
}
|
|
79
|
-
return url;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
// query 在 hash # 之后
|
|
83
|
-
if (queryIdx >= 0 && hashIdx >= 0 && queryIdx > hashIdx) {
|
|
84
|
-
const hash = url.slice(hashIdx, queryIdx);
|
|
85
|
-
const search = url.slice(queryIdx);
|
|
86
|
-
return `${url.slice(0, hashIdx)}${search}${hash}`;
|
|
87
|
-
}
|
|
88
|
-
return url;
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
/**
|
|
92
|
-
* 获取 url 中的 query 值的对象
|
|
93
|
-
* @param query
|
|
94
|
-
*/
|
|
95
|
-
export function searchToObj(url) {
|
|
96
|
-
if (!url || typeof url !== "string") {
|
|
97
|
-
console.warn("Warn getSearchParams: 请传入正确的 url 字符串.");
|
|
98
|
-
return;
|
|
99
|
-
}
|
|
100
|
-
let _url = fixUrlQuery(url)
|
|
101
|
-
// 解决 query 在 /# 之前导致 query 参数多了 / 的问题
|
|
102
|
-
.replace("/#", "#");
|
|
103
|
-
const queryIdx = _url.indexOf("?");
|
|
104
|
-
const hashIdx = _url.indexOf("#");
|
|
105
|
-
if (queryIdx < 0) {
|
|
106
|
-
return;
|
|
107
|
-
}
|
|
108
|
-
// 去除 hash 相关数据及 query 的 "?"
|
|
109
|
-
if (hashIdx >= 0 && queryIdx >= 0) {
|
|
110
|
-
_url = _url.slice(queryIdx + 1, hashIdx);
|
|
111
|
-
} else if (queryIdx >= 0) {
|
|
112
|
-
_url = _url.slice(queryIdx + 1);
|
|
113
|
-
}
|
|
114
|
-
const res = {};
|
|
115
|
-
_url.split("&")?.forEach((it) => {
|
|
116
|
-
const arr = it.split("=");
|
|
117
|
-
res[arr[0]] = arr[1];
|
|
118
|
-
});
|
|
119
|
-
return res;
|
|
120
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* 替换 search 字符串
|
|
3
|
+
* @param url
|
|
4
|
+
* @param search
|
|
5
|
+
* @returns
|
|
6
|
+
*/
|
|
7
|
+
export function replaceSearch(url, search) {
|
|
8
|
+
if (!url || typeof url !== "string") {
|
|
9
|
+
console.warn("Warn handleUrlQuery: 请传入正确的 url 字符串");
|
|
10
|
+
return url;
|
|
11
|
+
}
|
|
12
|
+
let _url = fixUrlQuery(url);
|
|
13
|
+
|
|
14
|
+
const hashIdx = _url.indexOf("#");
|
|
15
|
+
const queryIdx = _url.indexOf("?");
|
|
16
|
+
let hash = "";
|
|
17
|
+
let resUrl = _url;
|
|
18
|
+
if (queryIdx >= 0) {
|
|
19
|
+
resUrl = _url.slice(0, queryIdx);
|
|
20
|
+
}
|
|
21
|
+
if (hashIdx >= 0) {
|
|
22
|
+
hash = _url.slice(hashIdx);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
resUrl += search?.indexOf("?") >= 0 ? "" : "?";
|
|
26
|
+
resUrl += search;
|
|
27
|
+
resUrl += hash;
|
|
28
|
+
|
|
29
|
+
return resUrl;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* 把对象转为 query 参数
|
|
34
|
+
* @param query
|
|
35
|
+
*/
|
|
36
|
+
export function objToSearch(query, hasQuery) {
|
|
37
|
+
if (!query || typeof query !== "object") {
|
|
38
|
+
console.warn("Warn objToSearch: 请传入正确的对象");
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
let search = hasQuery ? "?" : "";
|
|
42
|
+
Object.keys(query).forEach((key) => {
|
|
43
|
+
if (search && search != "?") {
|
|
44
|
+
search += "&";
|
|
45
|
+
}
|
|
46
|
+
search += key;
|
|
47
|
+
search += "=";
|
|
48
|
+
search += query[key];
|
|
49
|
+
});
|
|
50
|
+
return search;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* 修复 query,解决 query 在 hash 后面的情况
|
|
55
|
+
* @param url
|
|
56
|
+
* @returns
|
|
57
|
+
* 源数据:/#/login?a=1&b=2
|
|
58
|
+
* 结果:/?a=1&b=2#/login
|
|
59
|
+
*/
|
|
60
|
+
export function fixUrlQuery(url) {
|
|
61
|
+
if (!url || typeof url !== "string") {
|
|
62
|
+
console.warn("Warn handleUrlQuery: 请传入正确的 url 字符串");
|
|
63
|
+
return url;
|
|
64
|
+
}
|
|
65
|
+
const hashIdx = url.indexOf("#");
|
|
66
|
+
const queryIdx = url.indexOf("?");
|
|
67
|
+
|
|
68
|
+
// 双 query 情况
|
|
69
|
+
if (queryIdx < hashIdx) {
|
|
70
|
+
let hash = url.slice(hashIdx);
|
|
71
|
+
const hashQueryIdx = hash.indexOf("?");
|
|
72
|
+
if (hashQueryIdx >= 0) {
|
|
73
|
+
const _url = url.slice(0, queryIdx);
|
|
74
|
+
const urlQuery = url.slice(queryIdx, hashIdx)?.replace(/\/$/, "");
|
|
75
|
+
const hashQuery = hash.slice(hashQueryIdx)?.replace("?", "")?.replace(/\/$/, "");
|
|
76
|
+
hash = hash.slice(0, hashQueryIdx);
|
|
77
|
+
return `${_url}${urlQuery}&${hashQuery}${hash}`;
|
|
78
|
+
}
|
|
79
|
+
return url;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// query 在 hash # 之后
|
|
83
|
+
if (queryIdx >= 0 && hashIdx >= 0 && queryIdx > hashIdx) {
|
|
84
|
+
const hash = url.slice(hashIdx, queryIdx);
|
|
85
|
+
const search = url.slice(queryIdx);
|
|
86
|
+
return `${url.slice(0, hashIdx)}${search}${hash}`;
|
|
87
|
+
}
|
|
88
|
+
return url;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* 获取 url 中的 query 值的对象
|
|
93
|
+
* @param query
|
|
94
|
+
*/
|
|
95
|
+
export function searchToObj(url) {
|
|
96
|
+
if (!url || typeof url !== "string") {
|
|
97
|
+
console.warn("Warn getSearchParams: 请传入正确的 url 字符串.");
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
let _url = fixUrlQuery(url)
|
|
101
|
+
// 解决 query 在 /# 之前导致 query 参数多了 / 的问题
|
|
102
|
+
.replace("/#", "#");
|
|
103
|
+
const queryIdx = _url.indexOf("?");
|
|
104
|
+
const hashIdx = _url.indexOf("#");
|
|
105
|
+
if (queryIdx < 0) {
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
// 去除 hash 相关数据及 query 的 "?"
|
|
109
|
+
if (hashIdx >= 0 && queryIdx >= 0) {
|
|
110
|
+
_url = _url.slice(queryIdx + 1, hashIdx);
|
|
111
|
+
} else if (queryIdx >= 0) {
|
|
112
|
+
_url = _url.slice(queryIdx + 1);
|
|
113
|
+
}
|
|
114
|
+
const res = {};
|
|
115
|
+
_url.split("&")?.forEach((it) => {
|
|
116
|
+
const arr = it.split("=");
|
|
117
|
+
res[arr[0]] = arr[1];
|
|
118
|
+
});
|
|
119
|
+
return res;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
|