diff options
author | 2019-08-23 11:45:30 +0000 | |
---|---|---|
committer | 2019-08-23 11:45:30 +0000 | |
commit | 5c9f4896c435936f41c54107626de7f700e3a012 (patch) | |
tree | b9fe7b5c0655ecb7c4e85b6b0f77ac365399f013 /usecaseui-portal | |
parent | c048639a46be107578e8ce0d8721bc7b5d4fe791 (diff) | |
parent | 1c5c010542dc53872b7213dbf9343aa92869673a (diff) |
Merge "feat: add ajax request function"
Diffstat (limited to 'usecaseui-portal')
-rw-r--r-- | usecaseui-portal/package.json | 1 | ||||
-rw-r--r-- | usecaseui-portal/src/app/shared/utils/http.ts | 63 |
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)
+ }
+
+ })
+
+ })
+}
+
+
|