Commit 2e4d861901fbca73bb937c88e484fe2e695482b8
1 parent
1dc4b8ec
fix(src/api/external): 修改传post请求body体里携带空字符串问题
Showing
1 changed file
with
23 additions
and
7 deletions
1 | -import { RequestBodyEnum, RequestParams } from "@/enums/httpEnum" | |
1 | +import { RequestBodyEnum, RequestHttpEnum, RequestParams } from "@/enums/httpEnum" | |
2 | 2 | import { ExtraRequestConfigType } from "@/store/external/modules/extraComponentInfo.d" |
3 | 3 | import { RequestConfigType } from "@/store/modules/chartEditStore/chartEditStore.d" |
4 | 4 | import { isShareMode } from "@/views/share/hook" |
... | ... | @@ -115,6 +115,22 @@ const handleParams = (Params: Recordable) => { |
115 | 115 | return Object.keys(Params).filter(Boolean).reduce((prev, next) => ({ ...prev, [next]: Params[next] }), {}) |
116 | 116 | } |
117 | 117 | |
118 | +//post请求动态追加query参数 | |
119 | +const objConvertQuery= (data:Recordable)=> { | |
120 | + const _result = []; | |
121 | + for (const key in data) { | |
122 | + const value = data[key]; | |
123 | + if (value.constructor == Array) { | |
124 | + value.forEach(function(_value) { | |
125 | + _result.push(key + "=" + _value); | |
126 | + }); | |
127 | + } else { | |
128 | + _result.push(key + '=' + value); | |
129 | + } | |
130 | + } | |
131 | + return _result.join('&'); | |
132 | +} | |
133 | + | |
118 | 134 | export const customRequest = async (request: RequestConfigType) => { |
119 | 135 | const { requestHttpType, requestParams, requestParamsBodyType, requestOriginUrl } = request as ExtraRequestConfigType |
120 | 136 | let { requestUrl } = request as ExtraRequestConfigType |
... | ... | @@ -130,18 +146,18 @@ export const customRequest = async (request: RequestConfigType) => { |
130 | 146 | const body = transformBodyValue(Body, requestParamsBodyType) |
131 | 147 | |
132 | 148 | /** |
133 | - * ft 修改post请求针对这种接口(/api/yt/device,设备列表分页),是post请求,body体一定要携带内容,空内容也可以 | |
149 | + * ft 修改post请求针对这种接口(/api/yt/device,设备列表分页),是post请求,page和pageSize是query参数,body可以不传内容 | |
134 | 150 | * 修改后的代码在注释之间,并标注好源代码和修改后代码,方便回溯 |
135 | - * 源代码 data: body, | |
136 | - * 修改后代码 data: !body? {"":null} :body, | |
151 | + * 源代码 url: requestUrl | |
152 | + * 修改后代码 requestHttpType === RequestHttpEnum.GET.toUpperCase() ? requestUrl: `${requestUrl}?${objConvertQuery(Params)} | |
137 | 153 | */ |
138 | 154 | Params = handleParams(Params) |
139 | 155 | return customHttp.request<any>({ |
140 | - url: requestUrl, | |
156 | + url: requestHttpType === RequestHttpEnum.GET.toUpperCase() ? requestUrl: `${requestUrl}?${objConvertQuery(Params)}`, | |
141 | 157 | baseURL: getOriginUrl(requestOriginUrl!), |
142 | 158 | method: requestHttpType, |
143 | - params: Params, | |
144 | - data: !body? {"":null} :body, | |
159 | + params: requestHttpType === RequestHttpEnum.GET.toUpperCase() ? Params: null, | |
160 | + data: body, | |
145 | 161 | headers: extraValue(Header) |
146 | 162 | }, { |
147 | 163 | joinPrefix: false, | ... | ... |