ibiz-http.js 7.2 KB
Newer Older
ibiz's avatar
ibiz committed
1 2 3 4 5 6 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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 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 91 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 163 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
"use strict";
/**
 * IBizHttp net 对象
 * 调用 getInstance() 获取实例
 *
 * @class IBizHttp
 */
var IBizHttp = /** @class */ (function () {
    /**
     * Creates an instance of IBizHttp.
     * 私有构造,拒绝通过 new 创建对象
     *
     * @memberof IBizHttp
     */
    function IBizHttp() {
        /**
         * 是否正在加载中
         *
         * @type {boolean}
         * @memberof IBizHttp
         */
        this.isLoading = false;
        /**
         * 统计加载
         *
         * @type {number}
         * @memberof IBizHttp
         */
        this.loadingCount = 0;
    }
    /**
     * 获取 IBizHttp 单例对象
     *
     * @static
     * @returns {IBizHttp}
     * @memberof IBizHttp
     */
    IBizHttp.getInstance = function () {
        if (!IBizHttp.iBizHttp) {
            IBizHttp.iBizHttp = new IBizHttp();
        }
        return this.iBizHttp;
    };
    /**
     * post请求
     *
     * @param {string} url 请求路径
     * @param {*} [params={}] 请求参数
     * @returns {Subject<any>} 可订阅请求对象
     * @memberof IBizHttp
     */
    IBizHttp.prototype.post = function (url, params) {
        if (params === void 0) { params = {}; }
        var _this = this;
        var subject = new rxjs.Subject();
        var win = window;
        var iBizApp = win.getIBizApp();
        if (!Object.is(iBizApp.getAppData(), '')) {
            Object.assign(params, { srfappdata: iBizApp.getAppData() });
        }
        var _strParams = _this.transformationOpt(params);
        var _url = url;
        _this.beginLoading();
        axios({
            baseURL: IBizEnvironment.Proxy,
            method: 'post',
            url: _url,
            data: _strParams,
            headers: { 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8', 'Accept': 'application/json' },
            transformResponse: [function (data) {
                    var _data = null;
                    try {
                        _data = JSON.parse(JSON.parse(JSON.stringify(data)));
                    }
                    catch (error) {
                    }
                    return _data;
                }],
        }).then(function (data) {
            _this.endLoading();
            if (data && data.notlogin) {
                return;
            }
            if (data.ret !== 0) {
                data.failureType = 'CLIENT_INVALID';
                data.info = data.info ? data.info : '本地网络异常,请重试';
                data.info = data.errorMessage ? data.errorMessage : '本地网络异常,请重试';
            }
            IBizUtil.processResult(data);
            subject.next(data);
        }).catch(function (error) {
            _this.endLoading();
            subject.error(error);
        });
        return subject;
    };
    /**
     * post请求,不处理loading加载
     *
     * @param {string} url
     * @param {*} [params={}]
     * @returns {Subject<any>}
     * @memberof IBizHttp
     */
    IBizHttp.prototype.post2 = function (url, params) {
        if (params === void 0) { params = {}; }
        var subject = new rxjs.Subject();
        var iBizApp = IBizApp.getInstance();
        if (!Object.is(iBizApp.getAppData(), '')) {
            Object.assign(params, { srfappdata: iBizApp.getAppData() });
        }
        var _strParams = this.transformationOpt(params);
        var _url = url;
        axios({
            baseURL: IBizEnvironment.Proxy,
            method: 'post',
            url: _url,
            data: _strParams,
            headers: { 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8', 'Accept': 'application/json' },
            transformResponse: [function (data) {
                    var _data = null;
                    try {
                        _data = JSON.parse(JSON.parse(JSON.stringify(data)));
                    }
                    catch (error) {
                    }
                    return _data;
                }],
        }).then(function (data) {
            if (data && data.notlogin) {
                return;
            }
            if (data.ret !== 0) {
                data.failureType = 'CLIENT_INVALID';
                data.info = data.info ? data.info : '本地网络异常,请重试';
                data.info = data.errorMessage ? data.errorMessage : '本地网络异常,请重试';
            }
            IBizUtil.processResult(data);
            subject.next(data);
        }).catch(function (error) {
            subject.error(error);
        });
        return subject;
    };
    /**
     * get请求
     *
     * @param {string} url 请求路径
     * @param {*} [params={}] 请求参数
     * @returns {Subject<any>} 可订阅请求对象
     * @memberof IBizHttp
     */
    IBizHttp.prototype.get = function (url, params) {
        if (params === void 0) { params = {}; }
        var _this = this;
        var subject = new rxjs.Subject();
        var iBizApp = IBizApp.getInstance();
        if (!Object.is(iBizApp.getAppData(), '')) {
            Object.assign(params, { srfappdata: iBizApp.getAppData() });
        }
        if (Object.keys(params).length > 0) {
            var _strParams = _this.transformationOpt(params);
            url = url.indexOf('?') ? url + "&" + _strParams : url + "?&" + _strParams;
        }
        var _url = url;
        _this.beginLoading();
        axios.get(_url, { baseURL: IBizEnvironment.Proxy, }).then(function (response) {
            _this.endLoading();
            subject.next(response);
        }).catch(function (error) {
            _this.endLoading();
            subject.error(error);
        });
        return subject;
    };
    /**
     * 请求参数转义处理
     *
     * @param {*} [opt={}]
     * @returns {string}
     * @memberof IBizHttp
     */
    IBizHttp.prototype.transformationOpt = function (opt) {
        if (opt === void 0) { opt = {}; }
        var params = {};
        var postData = [];
        Object.assign(params, opt);
        var keys = Object.keys(params);
        keys.forEach(function (key) {
            var val = params[key];
            if (val instanceof Array || val instanceof Object) {
                postData.push(key + "=" + encodeURIComponent(JSON.stringify(val)));
            }
            else {
                postData.push(key + "=" + encodeURIComponent(val));
            }
        });
        return postData.join('&');
    };
    /**
     * 开始加载
     *
     * @memberof IBizHttp
     */
    IBizHttp.prototype.beginLoading = function () {
        var _this_1 = this;
        if (this.loadingCount === 0) {
            setTimeout(function () {
                _this_1.isLoading = true;
            });
        }
        this.loadingCount++;
    };
    /**
     * 加载结束
     *
     * @memberof IBizHttp
     */
    IBizHttp.prototype.endLoading = function () {
        var _this_1 = this;
        if (this.loadingCount > 0) {
            this.loadingCount--;
        }
        setTimeout(function () {
            if (_this_1.loadingCount === 0) {
                _this_1.isLoading = false;
            }
        }, 500);
    };
    /**
     * 单例变量声明
     *
     * @static
     * @type {IBizHttp}
     * @memberof IBizHttp
     */
    IBizHttp.iBizHttp = null;
    return IBizHttp;
}());