@hzab/utils 1.0.9 → 1.0.10
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 +4 -0
- package/package.json +1 -1
- 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
|
@@ -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
|
+
|