2021-12-14 15:53:30 +08:00
|
|
|
|
import axiosInstance from './axios'
|
2022-07-20 12:19:24 +08:00
|
|
|
|
import {
|
|
|
|
|
RequestHttpEnum,
|
|
|
|
|
ContentTypeEnum,
|
|
|
|
|
RequestBodyEnum,
|
|
|
|
|
RequestDataTypeEnum,
|
|
|
|
|
RequestContentTypeEnum,
|
|
|
|
|
RequestParamsObjType
|
|
|
|
|
} from '@/enums/httpEnum'
|
2022-07-17 21:51:28 +08:00
|
|
|
|
import type { RequestGlobalConfigType, RequestConfigType } from '@/store/modules/chartEditStore/chartEditStore.d'
|
2021-12-14 15:53:30 +08:00
|
|
|
|
|
2022-05-21 17:31:01 +08:00
|
|
|
|
export const get = (url: string, params?: object) => {
|
2021-12-14 15:53:30 +08:00
|
|
|
|
return axiosInstance({
|
2022-03-21 00:57:23 +08:00
|
|
|
|
url: url,
|
2022-03-21 20:56:42 +08:00
|
|
|
|
method: RequestHttpEnum.GET,
|
2022-05-21 17:31:01 +08:00
|
|
|
|
params: params,
|
2021-12-14 15:53:30 +08:00
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-21 17:31:01 +08:00
|
|
|
|
export const post = (url: string, data?: object, headersType?: string) => {
|
2021-12-14 15:53:30 +08:00
|
|
|
|
return axiosInstance({
|
|
|
|
|
url: url,
|
2022-03-21 20:56:42 +08:00
|
|
|
|
method: RequestHttpEnum.POST,
|
2022-05-21 17:31:01 +08:00
|
|
|
|
data: data,
|
2021-12-14 15:53:30 +08:00
|
|
|
|
headers: {
|
2022-03-21 20:56:42 +08:00
|
|
|
|
'Content-Type': headersType || ContentTypeEnum.JSON
|
|
|
|
|
}
|
2021-12-14 15:53:30 +08:00
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-17 21:51:28 +08:00
|
|
|
|
export const patch = (url: string, data?: object, headersType?: string) => {
|
|
|
|
|
return axiosInstance({
|
|
|
|
|
url: url,
|
|
|
|
|
method: RequestHttpEnum.PATCH,
|
|
|
|
|
data: data,
|
|
|
|
|
headers: {
|
|
|
|
|
'Content-Type': headersType || ContentTypeEnum.JSON
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-31 11:18:34 +08:00
|
|
|
|
export const put = (url: string, data?: object, headersType?: ContentTypeEnum) => {
|
2022-05-22 16:38:22 +08:00
|
|
|
|
return axiosInstance({
|
|
|
|
|
url: url,
|
|
|
|
|
method: RequestHttpEnum.PUT,
|
|
|
|
|
data: data,
|
|
|
|
|
headers: {
|
|
|
|
|
'Content-Type': headersType || ContentTypeEnum.JSON
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-21 17:31:01 +08:00
|
|
|
|
export const del = (url: string, params?: object) => {
|
2021-12-14 15:53:30 +08:00
|
|
|
|
return axiosInstance({
|
2022-03-21 00:57:23 +08:00
|
|
|
|
url: url,
|
2022-03-21 20:56:42 +08:00
|
|
|
|
method: RequestHttpEnum.DELETE,
|
|
|
|
|
params
|
2021-12-14 15:53:30 +08:00
|
|
|
|
})
|
2022-03-21 00:57:23 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-21 20:56:42 +08:00
|
|
|
|
// 获取请求函数,默认get
|
|
|
|
|
export const http = (type?: RequestHttpEnum) => {
|
2022-05-22 16:38:22 +08:00
|
|
|
|
switch (type) {
|
|
|
|
|
case RequestHttpEnum.GET:
|
|
|
|
|
return get
|
|
|
|
|
|
|
|
|
|
case RequestHttpEnum.POST:
|
|
|
|
|
return post
|
|
|
|
|
|
2022-07-17 21:51:28 +08:00
|
|
|
|
case RequestHttpEnum.PATCH:
|
|
|
|
|
return patch
|
|
|
|
|
|
2022-05-22 16:38:22 +08:00
|
|
|
|
case RequestHttpEnum.PUT:
|
|
|
|
|
return put
|
|
|
|
|
|
|
|
|
|
case RequestHttpEnum.DELETE:
|
|
|
|
|
return del
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
return get
|
|
|
|
|
}
|
2022-03-21 00:57:23 +08:00
|
|
|
|
}
|
2022-09-19 23:13:34 +08:00
|
|
|
|
const prefix = 'javascript:'
|
|
|
|
|
// 对输入字符进行转义处理
|
|
|
|
|
export const translateStr = (target: string | object) => {
|
|
|
|
|
if (typeof target === 'string') {
|
|
|
|
|
if (target.startsWith(prefix)) {
|
|
|
|
|
const funcStr = target.split(prefix)[1]
|
|
|
|
|
let result;
|
|
|
|
|
try {
|
|
|
|
|
result = new Function(`${funcStr}`)()
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.log(error)
|
|
|
|
|
window['$message'].error('js内容解析有误!')
|
|
|
|
|
}
|
|
|
|
|
return result
|
|
|
|
|
} else {
|
|
|
|
|
return target
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for (const key in target) {
|
|
|
|
|
if (Object.prototype.hasOwnProperty.call(target, key)) {
|
|
|
|
|
const subTarget = (target as any)[key];
|
|
|
|
|
(target as any)[key] = translateStr(subTarget)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return target
|
|
|
|
|
}
|
2022-07-17 21:51:28 +08:00
|
|
|
|
|
2022-07-20 12:19:24 +08:00
|
|
|
|
/**
|
|
|
|
|
* * 自定义请求
|
|
|
|
|
* @param targetParams 当前组件参数
|
|
|
|
|
* @param globalParams 全局参数
|
|
|
|
|
*/
|
|
|
|
|
export const customizeHttp = (targetParams: RequestConfigType, globalParams: RequestGlobalConfigType) => {
|
2022-09-16 12:26:12 +08:00
|
|
|
|
if (!targetParams || !globalParams) {
|
2022-07-20 12:19:24 +08:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 全局
|
|
|
|
|
const {
|
|
|
|
|
// 全局请求源地址
|
|
|
|
|
requestOriginUrl,
|
|
|
|
|
// 全局请求内容
|
|
|
|
|
requestParams: globalRequestParams
|
|
|
|
|
} = globalParams
|
|
|
|
|
|
|
|
|
|
// 目标组件(优先级 > 全局组件)
|
|
|
|
|
const {
|
|
|
|
|
// 请求地址
|
|
|
|
|
requestUrl,
|
|
|
|
|
// 普通 / sql
|
|
|
|
|
requestContentType,
|
|
|
|
|
// 获取数据的方式
|
|
|
|
|
requestDataType,
|
|
|
|
|
// 请求方式 get/post/del/put/patch
|
|
|
|
|
requestHttpType,
|
|
|
|
|
// 请求体类型 none / form-data / x-www-form-urlencoded / json /xml
|
|
|
|
|
requestParamsBodyType,
|
|
|
|
|
// SQL 请求对象
|
|
|
|
|
requestSQLContent,
|
|
|
|
|
// 请求内容 params / cookie / header / body: 同 requestParamsBodyType
|
|
|
|
|
requestParams: targetRequestParams
|
|
|
|
|
} = targetParams
|
|
|
|
|
|
|
|
|
|
// 静态排除
|
|
|
|
|
if (requestDataType === RequestDataTypeEnum.STATIC) return
|
|
|
|
|
|
|
|
|
|
if (!requestUrl) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 处理头部
|
2022-09-19 23:13:34 +08:00
|
|
|
|
let headers: RequestParamsObjType = {
|
2022-07-20 12:19:24 +08:00
|
|
|
|
...globalRequestParams.Header,
|
2022-09-16 12:26:12 +08:00
|
|
|
|
...targetRequestParams.Header
|
2022-07-20 12:19:24 +08:00
|
|
|
|
}
|
2022-09-19 23:13:34 +08:00
|
|
|
|
headers = translateStr(headers)
|
2022-07-20 12:19:24 +08:00
|
|
|
|
|
|
|
|
|
// data 参数
|
|
|
|
|
let data: RequestParamsObjType | FormData | string = {}
|
|
|
|
|
// params 参数
|
2022-09-19 23:13:34 +08:00
|
|
|
|
let params: RequestParamsObjType = { ...targetRequestParams.Params }
|
|
|
|
|
params = translateStr(params)
|
2022-07-20 12:19:24 +08:00
|
|
|
|
// form 类型处理
|
|
|
|
|
let formData: FormData = new FormData()
|
|
|
|
|
formData.set('default', 'defaultData')
|
|
|
|
|
// 类型处理
|
|
|
|
|
|
|
|
|
|
switch (requestParamsBodyType) {
|
|
|
|
|
case RequestBodyEnum.NONE:
|
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
case RequestBodyEnum.JSON:
|
|
|
|
|
headers['Content-Type'] = ContentTypeEnum.JSON
|
2022-09-19 23:13:34 +08:00
|
|
|
|
data = translateStr(JSON.parse(targetRequestParams.Body['json']))
|
2022-07-20 12:19:24 +08:00
|
|
|
|
// json 赋值给 data
|
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
case RequestBodyEnum.XML:
|
|
|
|
|
headers['Content-Type'] = ContentTypeEnum.XML
|
|
|
|
|
// xml 字符串赋值给 data
|
2022-09-19 23:13:34 +08:00
|
|
|
|
data = translateStr(targetRequestParams.Body['xml'])
|
2022-07-20 12:19:24 +08:00
|
|
|
|
break
|
|
|
|
|
|
2022-09-16 12:26:12 +08:00
|
|
|
|
case RequestBodyEnum.X_WWW_FORM_URLENCODED: {
|
2022-07-20 12:19:24 +08:00
|
|
|
|
headers['Content-Type'] = ContentTypeEnum.FORM_URLENCODED
|
|
|
|
|
const bodyFormData = targetRequestParams.Body['x-www-form-urlencoded']
|
2022-09-19 23:13:34 +08:00
|
|
|
|
for (const i in bodyFormData) formData.set(i, translateStr(bodyFormData[i]))
|
2022-07-20 12:19:24 +08:00
|
|
|
|
// FormData 赋值给 data
|
|
|
|
|
data = formData
|
|
|
|
|
break
|
2022-09-16 12:26:12 +08:00
|
|
|
|
}
|
2022-07-20 12:19:24 +08:00
|
|
|
|
|
2022-09-16 12:26:12 +08:00
|
|
|
|
case RequestBodyEnum.FORM_DATA: {
|
2022-07-20 12:19:24 +08:00
|
|
|
|
headers['Content-Type'] = ContentTypeEnum.FORM_DATA
|
|
|
|
|
const bodyFormUrlencoded = targetRequestParams.Body['form-data']
|
|
|
|
|
for (const i in bodyFormUrlencoded) {
|
2022-09-19 23:13:34 +08:00
|
|
|
|
formData.set(i, translateStr(bodyFormUrlencoded[i]))
|
2022-07-20 12:19:24 +08:00
|
|
|
|
}
|
|
|
|
|
// FormData 赋值给 data
|
|
|
|
|
data = formData
|
|
|
|
|
break
|
2022-09-16 12:26:12 +08:00
|
|
|
|
}
|
2022-07-20 12:19:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// sql 处理
|
|
|
|
|
if (requestContentType === RequestContentTypeEnum.SQL) {
|
|
|
|
|
headers['Content-Type'] = ContentTypeEnum.JSON
|
|
|
|
|
data = requestSQLContent
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-19 23:13:34 +08:00
|
|
|
|
try {
|
|
|
|
|
const url = (new Function("return `" + `${requestOriginUrl}${requestUrl}`.trim() + "`"))();
|
|
|
|
|
return axiosInstance({
|
|
|
|
|
url,
|
|
|
|
|
method: requestHttpType,
|
|
|
|
|
data,
|
|
|
|
|
params,
|
|
|
|
|
headers
|
|
|
|
|
})
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.log(error)
|
|
|
|
|
window['$message'].error('URL地址格式有误!')
|
|
|
|
|
}
|
2022-07-17 21:51:28 +08:00
|
|
|
|
}
|