From 44b2c522ad321e5a6b7c2d7737e7e9c60a343130 Mon Sep 17 00:00:00 2001 From: cyuamber Date: Fri, 30 Aug 2019 10:32:04 +0800 Subject: feat: optimization request function Change-Id: I9382aaabbc6d96ff405ef1bf3e1f96bacfbc1e0b Issue-ID: USECASEUI-307 Signed-off-by: cyuamber --- usecaseui-portal/src/app/shared/utils/http.ts | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) (limited to 'usecaseui-portal/src/app') diff --git a/usecaseui-portal/src/app/shared/utils/http.ts b/usecaseui-portal/src/app/shared/utils/http.ts index f68ea5e5..c0fc3f29 100644 --- a/usecaseui-portal/src/app/shared/utils/http.ts +++ b/usecaseui-portal/src/app/shared/utils/http.ts @@ -6,7 +6,6 @@ export default function http (url:string,data:object = {},method:any = 'get',que method = method.trim().toLocaleLowerCase() let promise:any; if(method === 'get' || method === 'delete'){ - // 若无参数则不传递data let options:object; if(JSON.stringify(data) === '{}'){ options = { method, url }; @@ -14,7 +13,6 @@ export default function http (url:string,data:object = {},method:any = 'get',que options = { method, url, params: data}; } - // 若请求方式为delete请求,则携带请求头 if(method === 'delete'){ } @@ -23,13 +21,13 @@ export default function http (url:string,data:object = {},method:any = 'get',que }else if (method === 'post' || method === 'put') { if(method === 'post' && query){ - let params:string; + let params:string = ''; if(query){ query = JSON.parse((query)); } - for(let key in query){ - params = '&' + key + '=' + query[key]; - } + Object.keys(query).forEach(item => { + params += '&' + item + '=' + query[item]; + }) params = params.slice(1); url += '?' + params; } @@ -41,7 +39,6 @@ export default function http (url:string,data:object = {},method:any = 'get',que } promise .then((response) => { - // 请求成功返回携带成功状态及响应数据的promise对象 if(response.status === 200 || 304){ resolve(response.data) }else{ @@ -49,12 +46,7 @@ export default function http (url:string,data:object = {},method:any = 'get',que } }) .catch((error) => { - if(error.status === 404){ - reject('请求资源不存在') - }else{ - reject(error.message) - } - + reject(error.message) }) }) -- cgit 1.2.3-korg