http.ts 7.9 KB
Newer Older
1 2 3 4
import axios from 'axios';
import { Loading } from 'element-ui';
import { ElLoadingComponent } from 'element-ui/types/loading';
import qs from 'qs';
5 6
import { Subject } from 'rxjs';

7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
/**
 * Http net 对象
 * 调用 getInstance() 获取实例
 *
 * @class Http
 */
export class Http {

    /**
     * 单例变量声明
     *
     * @private
     * @static
     * @type {Http}
     * @memberof Http
     */
    private static Http: Http;

    /**
     * 统计加载
     *
     * @type {number}
     * @memberof Http
     */
    private loadingCount: number = 0;

33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
    /**
     * 数据传递对象
     *
     * @type {Subject}
     * @memberof Http
     */
    private subject:Subject<any> = new Subject<any>();

    /**
     * 获取 Http 单例对象
     *
     * @static
     * @returns {Http}
     * @memberof Http
     */
    public static getInstance(): Http {
        if (!Http.Http) {
            Http.Http = new Http();
        }
        return this.Http;
    }

55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
    /**
     * load状态管理器
     *
     * @private
     * @type {(ElLoadingComponent | any)}
     * @memberof Http
     */
    private elLoadingComponent: ElLoadingComponent | any;

    /**
     * Creates an instance of Http.
     * 私有构造,拒绝通过 new 创建对象
     * 
     * @memberof Http
     */
    private constructor() { }

    /**
     * post请求
     *
     * @param {string} url
     * @param {*} [params={}]
     * @param {boolean} [isloading]
     * @param {number} [serialnumber]
     * @returns {Promise<any>}
     * @memberof Http
     */
    public post(url: string, params: any = {}, isloading?: boolean, serialnumber?: number): Promise<any> {
        if (isloading) {
            this.beginLoading();
        }
        params = this.handleRequestData(params);
        return new Promise((resolve: any, reject: any) => {
            axios({
                method: 'post',
                url: url,
91
                data: params,
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
                headers: { 'Content-Type': 'application/json;charset=UTF-8', 'Accept': 'application/json' },
                // transformResponse: [(data: any) => {
                //     let _data: any = null;
                //     try {
                //         _data = JSON.parse(JSON.stringify(data));
                //     } catch (error) {
                //     }
                //     return _data;
                // }],
            }).then((response: any) => {
                this.doResponseRresult(response, resolve, isloading, serialnumber);
            }).catch((response: any) => {
                this.doResponseRresult(response, reject, isloading, serialnumber);
            });
        });
    }

    /**
     * 获取
     *
     * @param {string} url
     * @param {*} [params={}]
     * @param {boolean} [isloading]
     * @param {number} [serialnumber]
     * @returns {Promise<any>}
     * @memberof Http
     */
    public get(url: string,params: any = {}, isloading?: boolean, serialnumber?: number): Promise<any> {
        params = this.handleRequestData(params);
        if(params.srfparentdata){
            Object.assign(params,params.srfparentdata);
            delete params.srfparentdata;
        }
        if((Object.keys(params)).length>0){
            let tempParam:any = {};
            let sort:any = null;
            Object.keys(params).forEach((item:any) =>{
                if( params[item] || Object.is(params[item],0) ){
                    if (Object.is(item,'sort')){
                      sort = params[item];
                    }else{
                      tempParam[item] = params[item];
                    }
                }
            })
            url += `?${qs.stringify(tempParam)}`;
            if(sort){
                url += '&sort='+sort;
            }
        }  
        if (isloading) {
            this.beginLoading();
        }
        return new Promise((resolve: any, reject: any) => {
            axios.get(url).then((response: any) => {
                this.doResponseRresult(response, resolve, isloading, serialnumber);
            }).catch((response: any) => {
                this.doResponseRresult(response, reject, isloading, serialnumber);
            });
        });
    }

    /**
     * 删除
     *
     * @param {string} url
     * @param {boolean} [isloading]
     * @param {number} [serialnumber]
     * @returns {Promise<any>}
     * @memberof Http
     */
tony001's avatar
tony001 committed
163
    public delete(url: string, data?:any,isloading?: boolean, serialnumber?: number): Promise<any> {
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
        if (isloading) {
            this.beginLoading();
        }
        return new Promise((resolve: any, reject: any) => {
            if(!data){
                axios.delete(url).then((response: any) => {
                    this.doResponseRresult(response, resolve, isloading, serialnumber);
                }).catch((response: any) => {
                    this.doResponseRresult(response, reject, isloading, serialnumber);
                });
            }else{
                axios.delete(url,{data:data}).then((response: any) => {
                    this.doResponseRresult(response, resolve, isloading, serialnumber);
                }).catch((response: any) => {
                    this.doResponseRresult(response, reject, isloading, serialnumber);
                });
            }
        });
    }

    /**
     * 修改数据
     *
     * @param {string} url
     * @param {*} data
     * @param {boolean} [isloading]
     * @param {number} [serialnumber]
     * @returns {Promise<any>}
     * @memberof Http
     */
    public put(url: string, data: any, isloading?: boolean, serialnumber?: number): Promise<any> {
        if (isloading) {
            this.beginLoading();
        }
        data = this.handleRequestData(data);
        return new Promise((resolve: any, reject: any) => {
            axios.put(url, data).then((response: any) => {
                this.doResponseRresult(response, resolve, isloading, serialnumber);
            }).catch((response: any) => {
                this.doResponseRresult(response, reject, isloading, serialnumber);
            });
        });
    }

    /**
     * 处理响应结果
     *
     * @private
     * @param {*} response
     * @param {Function} funt
     * @param {boolean} [isloading]
     * @param {number} [serialnumber]
     * @memberof Http
     */
    private doResponseRresult(response: any, funt: Function, isloading?: boolean, serialnumber?: number): void {
        if (isloading) {
            this.endLoading();
        }
        if (serialnumber) {
            Object.assign(response, { serialnumber: serialnumber });
        }
        funt(response);
    }

    /**
     * 开始加载
     *
     * @private
     * @memberof Http
     */
    private beginLoading(): void {
        if (this.loadingCount === 0) {
            this.elLoadingComponent = Loading.service({
                body: true,
                fullscreen: true,
            });
240
            this.notifyLoadState(true);
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
        }
        this.loadingCount++;
    }

    /**
     * 加载结束
     *
     * @private
     * @memberof Http
     */
    private endLoading(): void {
        if (this.loadingCount > 0) {
            this.loadingCount--;
        }
        setTimeout(() => {
            if (this.loadingCount === 0) {
                this.elLoadingComponent.close();
258
                this.notifyLoadState(false);
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
            }
        }, 500);
    }

    /**
     * 处理请求数据
     * 
     * @private
     * @param data 
     * @memberof Http
     */
    private handleRequestData(data:any){
        if(data.srfsessionkey){
            delete data.srfsessionkey;
        }
        if(data.srfsessionid){
            delete data.srfsessionid;
        }
        return data;
    }

280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
    /**
     * 获取通知对象
     * 
     * @public
     * @memberof Http
     */
    public  getNotifyObject(){
        return this.subject;
    }

    /**
     * 通知loadding状态
     * 
     * @private
     * @memberof Http
     */
    private notifyLoadState(loadingState:boolean){
        this.subject.next({action:'setloadstate',state:loadingState});
    }

300
}