|
@ -32,7 +32,11 @@ export type ApiException = "invalidToken" | "serverError"; |
|
|
const exceptionSub = new Subject<ApiException>(); |
|
|
const exceptionSub = new Subject<ApiException>(); |
|
|
export const exceptionOb = exceptionSub.asObservable(); |
|
|
export const exceptionOb = exceptionSub.asObservable(); |
|
|
|
|
|
|
|
|
function extHandle(res: BaseResponse) { |
|
|
|
|
|
|
|
|
function extHandle(res: BaseResponse, url:string) { |
|
|
|
|
|
//下载文件时,接口返回的是数据流,直接返回res
|
|
|
|
|
|
if(url.includes("download")){ |
|
|
|
|
|
return res; |
|
|
|
|
|
} |
|
|
return { |
|
|
return { |
|
|
...res, |
|
|
...res, |
|
|
success: res.status === 0, |
|
|
success: res.status === 0, |
|
@ -44,11 +48,12 @@ export default async function httpRequest<T>({ url, method = "GET", params = {}, |
|
|
if (token) { |
|
|
if (token) { |
|
|
headers = { Authorization: token, ...headers }; |
|
|
headers = { Authorization: token, ...headers }; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (method === "GET") { |
|
|
if (method === "GET") { |
|
|
const query = urlEncode(params); |
|
|
const query = urlEncode(params); |
|
|
const _url = query ? url + "?" + query : url; |
|
|
const _url = query ? url + "?" + query : url; |
|
|
const res = await fetch(_url, { headers }); |
|
|
const res = await fetch(_url, { headers }); |
|
|
return res.json().then(res => extHandle(res) as T); |
|
|
|
|
|
|
|
|
return res.json().then(res => extHandle(res, url) as T); |
|
|
} else { |
|
|
} else { |
|
|
const body = encode === "json" ? JSON.stringify(params) : urlEncode(params); |
|
|
const body = encode === "json" ? JSON.stringify(params) : urlEncode(params); |
|
|
const _headers = |
|
|
const _headers = |
|
@ -56,7 +61,8 @@ export default async function httpRequest<T>({ url, method = "GET", params = {}, |
|
|
? { "Content-Type": "application/json; charset=utf-8", ...headers } |
|
|
? { "Content-Type": "application/json; charset=utf-8", ...headers } |
|
|
: { "Content-Type": "application/x-www-form-urlencoded; charset=utf-8", ...headers }; |
|
|
: { "Content-Type": "application/x-www-form-urlencoded; charset=utf-8", ...headers }; |
|
|
const res = await fetch(url, { method, headers: _headers, body }); |
|
|
const res = await fetch(url, { method, headers: _headers, body }); |
|
|
return res.json().then(res => extHandle(res) as T); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return res.json().then(res => extHandle(res, url) as T); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
export function urlEncode(params?: Record<string, any>) { |
|
|
export function urlEncode(params?: Record<string, any>) { |
|
|