index.ts
4.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import { RequestBodyEnum, RequestParams } from "@/enums/httpEnum"
import { ExtraRequestConfigType } from "@/store/external/modules/extraComponentInfo.d"
import { RequestConfigType } from "@/store/modules/chartEditStore/chartEditStore.d"
import { isShareMode } from "@/views/share/hook"
import { customHttp } from "./http"
import { SelectTimeAggregationFieldEnum } from "@/views/chart/ContentConfigurations/components/ChartData/external/components/SelectTImeAggregation"
export enum ParamsType {
REQUIRED,
OPTIONAL
}
export const getOriginUrl = (originUrl: string) => {
const locationUrl = 'localhost'
return originUrl === locationUrl ? location.origin : originUrl
}
const regOptionalParams = /(?={\?)/g
const regDynamicParams = /(?={).+?(?<=})/g
/**
* @description 判断是否是动态参数
* @param url
*/
export const isDynamicUrl = (url: string) => {
regDynamicParams.lastIndex = 0
return regDynamicParams.test(url)
}
/**
* @description 解析动态参数
* @param url
*/
export const decomposeDynamicParams = (url: string) => {
regDynamicParams.lastIndex = 0
regOptionalParams.lastIndex = 0
return Array.from((url || '').matchAll(regDynamicParams), ([item]) => {
return {
type: regOptionalParams.test(item) ? ParamsType.OPTIONAL : ParamsType.REQUIRED,
originValue: item,
value: item.replaceAll(/[{}?]/g, '').split(',')
}
})
}
/**
* @description 正则替换url中的动态参数
* @param requestUrl
* @param Params
*/
const getDynamicRequestUrl = (requestUrl = '', Params: Recordable) => {
requestUrl = decodeURI(requestUrl || '')
const paramsList = decomposeDynamicParams(requestUrl)
requestUrl = paramsList.reduce((prev, next) => {
const { type, value, originValue } = next
if (type === ParamsType.REQUIRED) {
value.forEach(key => {
prev = prev.replace(key, Reflect.get(Params, key))
Reflect.deleteProperty(Params, key)
})
}
if (type === ParamsType.OPTIONAL) {
prev = prev.replace(originValue, '')
}
return prev
}, requestUrl)
return requestUrl.replaceAll(/[{}?]/g, '')
}
const transformBodyValue = (body: RequestParams['Body'], requestParamsBodyType: RequestBodyEnum) => {
let value = Reflect.get(body, requestParamsBodyType)
if (requestParamsBodyType === RequestBodyEnum.FORM_DATA) {
const formData = new FormData()
Object.keys(value as RequestParams['Body']['form-data']).forEach(key => {
formData.append(key, value[key])
})
value = formData
}
return value
}
const extraValue = (object: Recordable) => {
return Object.keys(object).reduce((prev, next) => {
return { ...prev, ...(object[next] ? { [next]: object[next] } : {}) }
}, {})
}
const handleParams = (Params: Recordable) => {
if (Params.keys && Params?.keys?.length) {
/** ft 修改select联动下拉框选择了属性(单个)报错问题
* 源代码 Params.keys = (Params.keys || [] as any).join(',')
*/
if(!Array.isArray(Params.keys)){
Params.keys = ([Params.keys] || [] as any).join(',')
}else{
Params.keys = (Params.keys || [] as any).join(',')
}
//ft
}
const timePeriod = Params[SelectTimeAggregationFieldEnum.TIME_PERIOD]
if (timePeriod) {
Params.startTs = Date.now() - timePeriod
Params.endTs = Date.now()
Reflect.deleteProperty(Params, SelectTimeAggregationFieldEnum.TIME_PERIOD)
}
return Object.keys(Params).filter(Boolean).reduce((prev, next) => ({ ...prev, [next]: Params[next] }), {})
}
export const customRequest = async (request: RequestConfigType) => {
const { requestHttpType, requestParams, requestParamsBodyType, requestOriginUrl } = request as ExtraRequestConfigType
let { requestUrl } = request as ExtraRequestConfigType
const { Header, Body } = requestParams
let { Params } = requestParams
Params = JSON.parse(JSON.stringify(Params))
const isDynamicUrlFlag = isDynamicUrl(requestUrl || '')
if (isDynamicUrlFlag) {
requestUrl = getDynamicRequestUrl(requestUrl, Params)
}
const body = transformBodyValue(Body, requestParamsBodyType)
Params = handleParams(Params)
return customHttp.request<any>({
url: requestUrl,
baseURL: getOriginUrl(requestOriginUrl!),
method: requestHttpType,
params: Params,
data: body,
headers: extraValue(Header)
}, {
joinPrefix: false,
apiUrl: '',
withShareToken: isShareMode()
})
}