summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorcyuamber <xuranyjy@chinamobile.com>2019-08-23 17:55:21 +0800
committercyuamber <xuranyjy@chinamobile.com>2019-08-23 17:55:31 +0800
commit1c5c010542dc53872b7213dbf9343aa92869673a (patch)
tree23539dcacd907187fd75837e06a84121549a7ff2
parent1ed096510209590d1656489a74692eb5ebcdeee4 (diff)
feat: add ajax request function
Change-Id: Ib52fbe8c39f5d1e37dc4880c36f5a68171530d38 Issue-ID: USECASEUI-307 Signed-off-by: cyuamber <xuranyjy@chinamobile.com>
-rw-r--r--usecaseui-portal/package.json1
-rw-r--r--usecaseui-portal/src/app/shared/utils/http.ts63
2 files changed, 64 insertions, 0 deletions
diff --git a/usecaseui-portal/package.json b/usecaseui-portal/package.json
index e85f2365..9bfae90f 100644
--- a/usecaseui-portal/package.json
+++ b/usecaseui-portal/package.json
@@ -28,6 +28,7 @@
"@angular/router": "^5.2.0",
"@ngx-translate/core": "^9.1.1",
"@ngx-translate/http-loader": "^2.0.1",
+ "axios": "^0.19.0",
"core-js": "^2.4.1",
"d3": "^3.5.17",
"echarts": "^4.1.0",
diff --git a/usecaseui-portal/src/app/shared/utils/http.ts b/usecaseui-portal/src/app/shared/utils/http.ts
new file mode 100644
index 00000000..f68ea5e5
--- /dev/null
+++ b/usecaseui-portal/src/app/shared/utils/http.ts
@@ -0,0 +1,63 @@
+import axios from 'axios';
+
+
+export default function http (url:string,data:object = {},method:any = 'get',query?:string | object):any {
+ return new Promise((resolve,reject) => {
+ method = method.trim().toLocaleLowerCase()
+ let promise:any;
+ if(method === 'get' || method === 'delete'){
+ // 若无参数则不传递data
+ let options:object;
+ if(JSON.stringify(data) === '{}'){
+ options = { method, url };
+ }else{
+ options = { method, url, params: data};
+ }
+
+ // 若请求方式为delete请求,则携带请求头
+ if(method === 'delete'){
+
+ }
+
+ promise = axios(options);
+
+ }else if (method === 'post' || method === 'put') {
+ if(method === 'post' && query){
+ let params:string;
+ if(<string>query){
+ query = JSON.parse((<string>query));
+ }
+ for(let key in <object>query){
+ params = '&' + key + '=' + query[key];
+ }
+ params = params.slice(1);
+ url += '?' + params;
+ }
+ promise = axios({
+ method,
+ url,
+ data,
+ })
+ }
+ promise
+ .then((response) => {
+ // 请求成功返回携带成功状态及响应数据的promise对象
+ if(response.status === 200 || 304){
+ resolve(response.data)
+ }else{
+ reject(response)
+ }
+ })
+ .catch((error) => {
+ if(error.status === 404){
+ reject('请求资源不存在')
+ }else{
+ reject(error.message)
+ }
+
+ })
+
+ })
+}
+
+