All files / packages/interface-proxy index.js

96.89% Statements 374/386
84.13% Branches 228/271
96.15% Functions 75/78
97.3% Lines 361/371

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904            8x 8x 8x 8x 8x     8x 44x 44x 44x   44x         8x 44x 44x 44x   44x   44x   44x 44x 1x   44x 1x     44x 5x 5x 5x     44x 44x   1x     44x   44x 43x   1x         8x         8x                   47x 78x   47x                                                                                                                           8x         48x       50x 50x 12x 12x 8x   12x 12x 14x 13x   1x     14x   2x   12x         50x       48x 48x 48x 48x 48x 48x 48x         48x                 48x   48x 48x 48x 48x 48x 48x     48x 79x 79x             1x 1x   78x 1x   78x 1x 1x   77x   48x 11x 9x   11x           37x 37x 37x   23x 5x 3x 3x   2x 2x 2x       18x 16x 16x     18x 2x 2x 3x 3x 2x 2x 2x               48x 48x 45x 1x   44x     48x 48x 1x 1x 1x         48x 48x       48x   1x     48x 48x 48x 48x 48x   48x 4x   48x                                     200x 48x   48x 48x 48x 34x 11x 11x   23x       48x     48x                   49x 3x 3x 3x         49x 46x         49x 45x         48x     48x 46x 40x 40x 38x 38x     46x     15x 14x     1x 1x 1x     1x     46x       48x   6x 6x       48x 2x 2x 2x 1x 1x   2x 1x 1x   2x 2x       48x     48x 1x 1x 2x   1x       48x 48x     48x     200x 800x 5x 5x 5x 5x 5x     200x 600x 6x 6x 6x 6x     6x   6x     200x     48x   48x   47x   47x 46x   43x           1x             2x     46x     47x 44x         47x 2x       47x 4x 4x   1x       1x       46x 2x 2x   44x       48x 48x   47x 1x     48x         47x 1x   47x                           44x     44x 44x 44x 44x 44x     44x 3x                 44x 44x 44x   38x 38x 38x                         44x 1x 1x 1x             43x 2x     41x   33x 33x       8x 6x     2x 2x                         2x                   4x 4x 4x                   4x 4x 1x   3x               8x 8x 8x 1x   8x 63x   8x 59x   49x 49x 49x 53x 52x       46x 46x 46x   1x 1x 1x 1x     49x   48x   48x 2x     48x 2x       2x   1x 1x 1x 1x 1x 1x     1x 1x         1x       47x           47x   47x   7x 7x 7x 5x   2x 2x 2x     7x 2x 2x   2x 2x 1x 1x 1x     2x 1x 1x 1x     2x 2x 2x 2x     2x 2x               47x   1x     8x       8x 8x     3x             4x 4x         4x   3x 3x 3x 3x 2x                                         8x 8x     8x   8x   8x   8x     8x               8x   8x   8x 200x             200x 200x 200x                             8x     8x 8x   8x       8x      
/**
 * 接口代理模块
 * @author wdzxc
 */
/*===============================================================================================*/
 
import Hashes from './src/vendor/sha1';
import AjaxJsonp from './src/jsonp.js';
import axios from 'axios';
import {isObject, isArray, isFunction, isString, isUndefined} from "../utils/isWhat";
import {objClone, getObjAttr, objCover, objEqual} from "../utils/objectOpt";
 
function bind(fn, thisArg) {
    return function wrap() {
        var args = new Array(arguments.length);
        for (var i = 0; i < args.length; i++) {
            args[i] = arguments[i];
        }
        return fn.apply(thisArg, args);
    };
}
 
function ExtendAxios() {
    return bind(function (options) {
        let axiosOptions = objClone(options);
        let callback = options.success;
        let errCallback = options.error;
        // method & type
        axiosOptions.method = axiosOptions.method || axiosOptions.type;
        // responseType & dataType
        axiosOptions.responseType = axiosOptions.responseType || axiosOptions.dataType;
        // headers
        axiosOptions.headers = axiosOptions.headers || {};
        if (axiosOptions.responseType === 'html') {
            axiosOptions.headers["Accept"] = "text/html, */*";
        }
        if (axiosOptions.responseType === 'text') {
            axiosOptions.headers["Accept"] = "text/plain, */*";
        }
        // options polyfill
        if (axiosOptions.method === 'post') {
            axiosOptions.data = axiosOptions.data || axiosOptions.params;
            axiosOptions.params = null;
            axiosOptions.headers["Content-Type"] = axiosOptions.headers["Content-Type"]
                || "application/json";
        }
        Eif (axiosOptions.beforeSend) {
            let fakeXhr = {
                setRequestHeader(key, value) {
                    axiosOptions.headers[key] = value;
                }
            };
            axiosOptions.beforeSend(fakeXhr);
        }
        axios(axiosOptions).then((response) => {
            callback(response.data, response.status);
        }).catch((error) => {
            errCallback(error);
        });
    }, axios);
}
 
const extendAxios = new ExtendAxios();
 
//=========================================================
//  参数说明 & 默认参数
//=========================================================
let defaultOptions = {
    //----------------------------------
    //  接口数据默认配置
    //----------------------------------
    // 默认域名
    origin: null,
    // xhr send 前钩子函数
    beforeSend: new Function,
    // 参数过滤 - 默认过滤掉null字段
    paramsFilter(params) {
        Object.keys(params).forEach(key => {
            if (params[key] === null) delete params[key];
        });
        return params;
    },
    // 成功回调的数据预处理
    dataFilter: null,
    // 默认失败回调
    callback: new Function,
    errCallback: new Function,
    // ajax default
    type: "get",
    dataType: "json",
    contentType: "application/json; charset=utf-8",
    traditional: true,
    jsonp: null,        // jsonpCallback
    // modify header
    fakeHeaders: null,
};
 
/**
 * 接口配置项
 * @typedef {Object}    OriginAjaxConfig
 * // 基础配置
 * @property {string}   [pathname=]             - 接口路径 路径可以是相对路径也可以是绝对路径,按书写方式区分
 * @property {string}   [url=]                  - url 通常是绝对路径
 * @property {string}   [origin=""]             - 接口域名 如果一个接口存在origin配置,则会和pathname进行拼接作为具体路径。
 * @property {string}   [type="GET"]            - 操作方法 GET|POST
 * @property {string}   [dataType="json"]       - 数据类型 json|jsonp
 * @property {string}   [jsonp="jsonpCallback"] - jsonp 回调函数名
 * @property {string}   [paramsType=""]         - 参数类型
 * @property {function} [paramsFilter=""]       - 参数预处理
 * @property {string}   [dataPath=""]           - 数据路径
 * @property {function} [dataFilter=""]         - 数据预处理
 * @property {object}   [pathParams=]           - 加载中提示
 * @property {boolean}  [merge=false]           - 合并
 * @property {boolean}  [queue=false]           - 排队
 * @property {boolean}  [single=false]          - 单一最后有效
 * @property {number}   [validTime=0]           - 有效时间,单位为秒,可以为浮点数
 * @property {*}        [params=null]           - 接口查询参数,{} [] "" , 如果没有接口调用时没有对应参数,会使用这里配置的默认配置进行补充
 * @property {string}   [name=""]               - 描述接口名称,参考使用
 * @property {*}        [returns=null]          - 接口成功状态返回数据的参考格式,只是针对数据部分。例如: 返回数据:{ success:true,data:{} } => returns : {} (为data对象)
 * // 高级配置
 * @property {function} [errCallback=]          - 默认失败回调函数
 * @property {function} [ajax=]                 - ajax 调用
 * @property {function} [beforeSend=]           - 发送前钩子函数,可以用于修改header等
 * @property {function} [model=]                - 接口模型
 * @property {function} [successHandler=]       - 返回数据成功状态判断过滤器
 *
 * @example
 * {
 *      getGoodsList: {
 *           name: "GoodsList",
 *           pathname: "/goods/findAll",
 *           params: {type:1},
 *           returns: [{
 *              "id": "001",
 *              "name": "Iphone4"
 *           }]
 *      }
 *      // ...
 * }
 */
 
// 接口请求装配工具箱
const toolkit = {
 
    // 生成接口最终配置
    getInterfaceConfig(key, proxyObj) {
        // 接口配置列表
        const configList = objClone(proxyObj.$configMap);
 
        // 预处理optionsList
        function doMixin(config) {
            let result = config;
            if (config.mixins) {
                var mixinTarget = config.mixins;
                if (isString(mixinTarget) || isObject(mixinTarget)) {
                    mixinTarget = [mixinTarget];
                }
                Eif (isArray(mixinTarget)) {
                    mixinTarget.reverse().map(mixin => {
                        if (isString(mixin)) {
                            return configList[mixin];
                        }
                        Eif (isObject(mixin)) return mixin;
                        // console.error("InterfaceProxy Mixin ERROR.");
                    }).forEach(mixin => {
                        if (mixin.mixins) {
                            // 循环mixin处理
                            result = objCover(doMixin(mixin), result);
                        } else {
                            result = objCover(objClone(mixin), objClone(result));
                        }
                    });
                }
            }
            return result;
        }
 
        // 拼装配置列表
        var unitOptions = doMixin(configList[key]);
        var troopOptions = proxyObj.$additionalOptions;
        var finalConfig = objCover(objCover(objClone(defaultOptions), troopOptions), unitOptions);
        finalConfig.$unitOptions = unitOptions;
        finalConfig.$troopOptions = troopOptions;
        finalConfig.$defaultOptions = defaultOptions;
        return finalConfig;
    },
    // 生成Ajax最终配置
    getAjaxConfig(args, interfaceConfig, baseConfig) {
        // 基础AJAX配置
        let ajaxConfig = objCover(objClone(interfaceConfig), baseConfig);
        /*----------------------------------------------------
         支持参数传入格式
         > callback
         > callback,errCallback
         > params,callback,errCallback
         > [pArg1,pArg2...],callback,errCallback
         > 依照Res配置默认构造方法填充未传入参数值
         ----------------------------------------------------*/
        let defaultParams = ajaxConfig.params;
        // 捕获参数
        let captureParams = {};
        let captureCallback = null;
        let captureErrCallback = null;
        let _Symbol = null;
        Eif (typeof Symbol !== 'undefined') {
            _Symbol = Symbol;
        }
        // 默认参数值 由接口配置生成 如果默认参数配置的类型为函数,则获取函数执行结果
        Object.keys(defaultParams || {}).forEach(key => {
            let value = defaultParams[key];
            if (value === Number
                || value === String
                || value === Array
                || value === Boolean
                || value === Function
                || value === _Symbol
                || value === Object) {
                captureParams[key] = null;
                return;
            }
            if (isObject(value)) {
                value = value.default || null;
            }
            if (isFunction(value)) {
                captureParams[key] = value();
                return;
            }
            captureParams[key] = value;
        });
        if (baseConfig) {
            if (isObject(interfaceConfig.params)) {
                objCover(captureParams, ajaxConfig.params);
            }
            Iif (isObject(interfaceConfig.data)) {
                ajaxConfig.data = objCover(interfaceConfig.data, ajaxConfig.data);
            }
        } else {
            // 根据args捕获参数
            // 分析函数传入参数列表,提取 params,callback,errCallback
            let multiParamPattern = true;
            let multiParamNum = 0;
            [].forEach.call(args, function (param) {
                // 提取成功与失败回调函数
                if (isFunction(param)) {
                    if (!captureCallback) {
                        captureCallback = param;
                        return;
                    }
                    Eif (!captureErrCallback) {
                        captureErrCallback = param;
                        return;
                    }
                }
                // 提取参数列表  单独 {} 配置
                if (isObject(param)) {
                    objCover(captureParams, param);
                    multiParamPattern = false;
                }
                // 多参数配置
                if (multiParamPattern && !isFunction(param)) {
                    let attrNum = 0;
                    for (let an in defaultParams) {
                        Iif (!defaultParams.hasOwnProperty(an)) continue;
                        if (attrNum++ == multiParamNum) {
                            captureParams[an] = param;
                            multiParamNum++;
                            break;
                        }
                    }
                }
            });
        }
 
        // URL处理基本参数处理
        let url = (function () {
            if (ajaxConfig.url) return ajaxConfig.url;
            if (ajaxConfig.origin) {
                return ajaxConfig.origin + ajaxConfig.pathname;
            }
            else return ajaxConfig.pathname;
        })(ajaxConfig);
        // url path重构 pathParams
        let pathParams = ajaxConfig.pathParams;
        if (pathParams) {
            Object.keys(pathParams).forEach(key => {
                let value = pathParams[key];
                url = url.replace(new RegExp(`:${key}`), value);
            });
        }
 
        // ! 参数过滤器 -------------------------------------------------
        Eif (isFunction(ajaxConfig.paramsFilter)) {
            captureParams = ajaxConfig.paramsFilter(captureParams);
        }
        // --------------------------------------------------------------
        // post 处理 将data转化为json字符串
        if (ajaxConfig.type.toLocaleLowerCase() == "post"
            && ajaxConfig.contentType != "application/x-www-form-urlencoded") {
            captureParams = JSON.stringify(captureParams);
        }
 
        ajaxConfig.url = url;
        ajaxConfig.data = ajaxConfig.data || captureParams;
        ajaxConfig.params = captureParams;
        ajaxConfig.callback = captureCallback;
        ajaxConfig.errCallback = captureErrCallback;
        // 生成请求标记
        if (ajaxConfig.validTime > 0) {
            ajaxConfig.requestMark = this.generateDataMark(ajaxConfig);
        }
        return ajaxConfig;
    },
    /**
     * 生成接口数据交换操作函数
     * @this module:InterfaceProxy
     * @param key 单个接口配置
     * @param proxyObj 接口代理对象配置
     * @returns {Function}
     * @ignore
     */
    buildInterfaceMethod: function (key, proxyObj) {
        // extendAxios
        /**
         * 构造接口调用函数
         * @param {*=} params        - 查询参数
         * @param {*=} callback      - 成功回调
         * @param {*=} errCallback   - 失败回调
         * @param {*=} methodConfig  - 代理方法配置
         */
        let proxyMethod = function (params, callback, errCallback, methodConfig) {
            let args = [].slice.call(arguments);
            // 根据各级配置生成最终配置
            let interfaceConfig = toolkit.getInterfaceConfig(key, proxyObj);
            let baseConfig = null;
            args = args.filter(function (arg) {
                if (isObject(arg) && arg['_proxy_method_config_mark_666_']) {
                    baseConfig = arg;
                    return false;
                }
                return true;
            });
 
            // 根据参数和接口配置生成ajaxConfig
            let ajaxConfig = toolkit.getAjaxConfig(args, interfaceConfig, baseConfig);
 
            // 重新包装callback
            let wrapCallback = function () {
                /**
                 * 默认失败回调
                 * 1. 如果没有配置通用回调和专用回调,则使用默认回调
                 * 2. 如果配置了通用回调,没有专用回调,则使用通用回调
                 * 3. 如果配置了通用回调和专用回调,则先调用专用回调,专用回调返回true则继续调用通用回调
                 * 4. 如果配置了通用回调和专用回调,则先调用专用回调,专用回调返回false,则过程结束
                 * 专用回调[cb,then] -> 通用回调[unitOptions,troopOptions,defaultOptions]
                 */
                // 在存在配置回调 和 默认回调的情况下,包装新的回调函数
                if (ajaxConfig.errCallback && interfaceConfig.errCallback && (ajaxConfig.errCallback !== interfaceConfig.errCallback)) {
                    ajaxConfig.errCallback = (function (callback, defaultCallback) {
                        return function () {
                            return callback.apply(this, arguments) && defaultCallback.apply(this, arguments);
                        };
                    })(ajaxConfig.errCallback, interfaceConfig.errCallback);
                }
                // 无参数回调 则先使用默认回调作为ajax回调
                if (!ajaxConfig.errCallback) {
                    ajaxConfig.errCallback = interfaceConfig.errCallback;
                }
                /**
                 * 默认成功回调
                 */
                if (!ajaxConfig.callback) {
                    ajaxConfig.callback = interfaceConfig.callback;
                }
            };
 
            // Promise形式封装
            let agent = {};
 
            // .then
            agent.then = function (callback, errCallback) {
                if (callback) {
                    var _captureCallback = ajaxConfig.callback;
                    ajaxConfig.callback = function () {
                        _captureCallback && _captureCallback.apply(this, arguments);
                        callback.apply(this, arguments);
                    };
                }
                if (errCallback) {
                    // ajax回调 = 默认回调 则 参数回调不存在
                    // ajax回调替换为 链式回调
                    if (ajaxConfig.errCallback === interfaceConfig.errCallback) {
                        ajaxConfig.errCallback = errCallback;
                    } else {
                        // 参数回调 默认回调 都存在则先执行参数回调再执行链式回调
                        var _captureErrCallback = ajaxConfig.errCallback;
                        ajaxConfig.errCallback = function () {
                            return (_captureErrCallback && _captureErrCallback.apply(this, arguments))
                                || errCallback.apply(this, arguments);
                        };
                        wrapCallback();
                    }
                }
                return agent;
            };
 
            // .catch .error
            agent.catch = agent.error = function (errCallback) {
                // catch方法是 .then(null,rejection)的包装
                agent.then(null, errCallback);
                return agent;
            };
 
            // .finally
            agent.finally = function (callback) {
                var _captureCallback = ajaxConfig.callback;
                var _captureErrCallback = ajaxConfig.errCallback;
                ajaxConfig.callback = function () {
                    _captureCallback && _captureCallback.apply(this, arguments);
                    callback.apply(this, arguments);
                };
                ajaxConfig.errCallback = function () {
                    _captureErrCallback && _captureErrCallback.apply(this, arguments);
                    callback.apply(this, arguments);
                };
                ajaxConfig.finallyCallback = callback;
                return {};
            };
 
            // errCallback
            wrapCallback();
 
            // .loadingSwitch
            agent.loadingSwitch = function (target, key) {
                let _target = target, _key = key;
                interfaceConfig.optLoadingSwitch = function (value) {
                    _target[_key] = value;
                };
                return agent;
            };
 
            // do ajax => async start
            setTimeout(function () {
                toolkit.doAjax(interfaceConfig, ajaxConfig);
            }, 1);
 
            return agent;
        };
        // wrap axios methods with base config
        ['delete', 'get', 'head', 'options'].forEach(function (method) {
            proxyMethod[method] = function (params, config) {
                let methodConfig = config || {};
                methodConfig['_proxy_method_config_mark_666_'] = true;
                methodConfig['method'] = method;
                methodConfig['params'] = objCover(methodConfig['params'] || {}, params || {});
                return proxyMethod(methodConfig);
            };
        });
        ['post', 'put', 'patch'].forEach(function (method) {
            proxyMethod[method] = function (data, config) {
                let methodConfig = config || {};
                methodConfig['_proxy_method_config_mark_666_'] = true;
                methodConfig['method'] = method;
                Iif (isObject(methodConfig['data']) && isObject(data)) {
                    methodConfig['data'] = objCover(methodConfig['data'] || {}, data || {});
                } else {
                    methodConfig['data'] = methodConfig['data'] || data;
                }
                return proxyMethod(methodConfig);
            };
        });
        return proxyMethod;
    },
    // 发起请求
    doAjax(interfaceConfig, ajaxConfig) {
        // Ajax 开始前 interceptor
        if (this.beforeAjax(interfaceConfig, ajaxConfig)) return;
        // 请求响应统一处理
        let onResponse = (type, data) => {
            // Ajax 结束后 interceptor
            if (this.afterAjax(interfaceConfig, ajaxConfig)) return;
            let resMap = {
                success: data => {
                    this.AjaxGetSuccessProcess({
                        data: data.data,
                        status: data.status,
                    }, interfaceConfig, ajaxConfig);
                },
                localSuccess: data => {
                    this.AjaxGetSuccessProcess({
                        data: data.data,
                        status: data.status,
                        isLocalObtain: true
                    }, interfaceConfig, ajaxConfig);
                },
                error: error => {
                    this.AjaxGetFailProcess(error, interfaceConfig, ajaxConfig);
                }
            };
            resMap[type] && resMap[type](data);
        };
        // 绑定请求处理函数
        ajaxConfig.success = function (data, status) {
            onResponse('success', {
                data: data,
                status: status
            });
        };
        ajaxConfig.error = function (status) {
            onResponse('error', status);
        };
        // ------------------------------ 开始请求数据 ----------------------------
        // 尝试搜索本地数据
        if (interfaceConfig.validTime) {
            let localData = this.getAlreadyObtain(ajaxConfig.requestMark);
            if (localData) {
                // 匹配到本地数据
                onResponse('localSuccess', {
                    data: localData,
                    status: 200
                });
                return;
            }
        }
        // ajax 获取数据
        if (ajaxConfig.dataType === 'jsonp') {
            AjaxJsonp(objClone(ajaxConfig));
            return;
        }
        (interfaceConfig.ajax || extendAxios)(objClone(ajaxConfig));
    },
    // 请求前
    beforeAjax(interfaceConfig, ajaxConfig) {
        let flag = this.requestManager.start(interfaceConfig, ajaxConfig);
        if (!flag) {
            // 加载中开关
            if (isFunction(interfaceConfig.optLoadingSwitch)) {
                interfaceConfig.optLoadingSwitch(true);
            }
        }
        return flag;
    },
    // 请求后
    afterAjax(interfaceConfig, ajaxConfig) {
        // 加载中开关
        if (isFunction(interfaceConfig.optLoadingSwitch)) {
            interfaceConfig.optLoadingSwitch(false);
        }
        return this.requestManager.finish(interfaceConfig, ajaxConfig);
    },
    /**
     * Ajax数据预处理
     * 200+>接收数据处理:成功
     * @param response
     * @param {} response.data
     * @param interfaceConfig
     * @param ajaxConfig
     * @this module:InterfaceProxy
     * @ignore
     */
    AjaxGetSuccessProcess: function (response, interfaceConfig, ajaxConfig) {
        // 基础配置
        let opt = objCover({}, response);
 
        // 配置项转化
        let data = opt.data;
        let status = opt.status;
        let callback = ajaxConfig.callback;
        let errCallback = ajaxConfig.errCallback;
        let isLocalObtain = opt.isLocalObtain;
 
        // 数据缓存处理
        if (!isLocalObtain && interfaceConfig.validTime) {
            this.repository.push({
                key: ajaxConfig.requestMark,
                timeStamp: +new Date(),
                data: data,
                validTime: interfaceConfig.validTime
            });
        }
 
        // callback
        let callbackEx = null;
        Eif (callback) {
            callbackEx = function (extractData) {
                // 提取数据
                arguments[0] = interfaceConfig.dataPath ? getObjAttr(extractData, interfaceConfig.dataPath) : arguments[0];
                arguments[0] = isFunction(interfaceConfig.dataFilter) ? interfaceConfig.dataFilter(arguments[0], interfaceConfig.dataType) : arguments[0];
                return callback.apply(null, arguments);
            };
        }
 
        /*----------------------------------------------------
         接口返回值类型(200)
         > 空 : 视为成功
         > {success:true,...}
         判断后进行成功或失败回调
         > data | msg  : 成功时尝试提取data,失败时尝试提取msg
         ----------------------------------------------------*/
 
        // 自定义成功状态判断机制
        if (isFunction(interfaceConfig.successHandler)) {
            let result = interfaceConfig.successHandler(data) || {};
            Eif (result.success === true) {
                return callbackEx && callbackEx(result.data);
            }
            if (result.success === false) {
                return errCallback(result.msg, data);
            }
        }
        // 当返回数据为空 > 直接当做成功
        if (!data) {
            return callbackEx && callbackEx(null);
        }
        // 依据success标识提取返回数据
        if (data.success === true) {
            // 提取data数据
            Eif (!isUndefined(data.data)) {
                return callbackEx && callbackEx(data.data, data, status);
            }
            return callbackEx && callbackEx(data, data, status);
        }
        if (data.success === false) {
            return errCallback && errCallback(data.msg, data);
        }
        // 当返回数据不包含success字段 > 视为成功
        Eif (isUndefined(data.success)) {
            return callbackEx && callbackEx(data, data);
        }
    },
    /**
     * 200X>接收数据处理:失败
     * @param error
     * @param interfaceConfig
     * @param ajaxConfig
     * @this module:InterfaceProxy
     * @ignore
     */
    AjaxGetFailProcess: function (error, interfaceConfig, ajaxConfig) {
        // AJAX 失败
        ajaxConfig.errCallback && ajaxConfig.errCallback(error.message, error);
    },
    /**
     * 生成数据标记
     * @param ajaxConfig
     * @returns {string}
     * @this module:InterfaceProxy
     * @ignore
     */
    generateDataMark: function (ajaxConfig) {
        let dataMark = JSON.stringify(ajaxConfig);
        dataMark = Hashes.hex_hmac(dataMark, '888');
        return dataMark;
    },
    /**
     * 获得数据
     * @param key
     * @returns {*}
     * @this module:InterfaceProxy
     * @ignore
     */
    getAlreadyObtain: function (key) {
        let data = this.repository.get(key);
        if (data) {
            return data.responseData;
        }
        return null;
    },
    // 请求队列
    requestManager: (function () {
        function RequestManager() {
            // 写点啥呢...
        }
 
        RequestManager.prototype = [];
        RequestManager.prototype.constructor = RequestManager;
        RequestManager.prototype.requestQueryEqual = function (a, b) {
            return this.interfaceConfigEqual(a.interfaceConfig, b.interfaceConfig) && objEqual(a.ajaxConfig.data, b.ajaxConfig.data);
        };
        RequestManager.prototype.interfaceConfigEqual = function (a, b) {
            return JSON.stringify(a) === JSON.stringify(b);
        };
        RequestManager.prototype.requestEqual = function (a, b) {
            return this.interfaceConfigEqual(a.interfaceConfig, b.interfaceConfig) && a.ajaxConfig.data == b.ajaxConfig.data;
        };
        RequestManager.prototype.removeItem = function (interfaceConfig, ajaxConfig) {
            let removed = null;
            this.find((r, index) => {
                if (interfaceConfig && ajaxConfig) {
                    if (this.requestEqual(r, {
                        interfaceConfig: interfaceConfig,
                        ajaxConfig: ajaxConfig
                    })) {
                        removed = r;
                        this.splice(index, 1);
                        return true;
                    }
                } else Eif (interfaceConfig && this.interfaceConfigEqual(interfaceConfig, interfaceConfig)) {
                    removed = r;
                    this.splice(index, 1);
                    return true;
                }
            });
            return removed;
        };
        RequestManager.prototype.start = function (interfaceConfig, ajaxConfig) {
            // 唯一
            if (interfaceConfig.single) {
                this.removeItem(interfaceConfig);
            }
            // 合并
            if (interfaceConfig.merge) {
                const sameRequest = this.find(r => this.requestQueryEqual(r, {
                    interfaceConfig: interfaceConfig,
                    ajaxConfig: ajaxConfig
                }));
                if (sameRequest) {
                    // 异步执行包装回调的代码 Reason : .then .error .finally
                    const callback = sameRequest.ajaxConfig.callback,
                        errCallback = sameRequest.ajaxConfig.errCallback;
                    Eif (callback) {
                        sameRequest.ajaxConfig.callback = function () {
                            callback.apply(this, arguments);
                            ajaxConfig.callback.apply(this, arguments);
                        }
                    }
                    Eif (errCallback) {
                        sameRequest.ajaxConfig.errCallback = function () {
                            errCallback.apply(this, arguments);
                            ajaxConfig.errCallback.apply(this, arguments);
                        }
                    }
                    return true;
                }
            }
            // 入队
            this.push.call(this, {
                interfaceConfig: interfaceConfig,
                ajaxConfig: ajaxConfig,
                timeStamp: new Date()
            });
        };
        RequestManager.prototype.finish = function (interfaceConfig, ajaxConfig) {
            // 排队
            if (interfaceConfig.queue) {
                // 找到同接口存在的前一个请求
                let r = null;
                this.find(request => {
                    if (this.requestEqual(request, {interfaceConfig: interfaceConfig, ajaxConfig: ajaxConfig})) {
                        return false;
                    }
                    Eif (this.interfaceConfigEqual(request.interfaceConfig, interfaceConfig) && request.ajaxConfig != ajaxConfig) {
                        r = request;
                        return true;
                    }
                });
                if (r) {
                    const callback = r.ajaxConfig.callback, errCallback = r.ajaxConfig.errCallback;
                    const nextCallback = ajaxConfig.callback, nextErrCallback = ajaxConfig.errCallback;
                    // 将回调附加到next函数上,等待队列前的请求调用next进行回调触发
                    let next;
                    ajaxConfig.callback = function () {
                        const arg = objClone(arguments);
                        next = function () {
                            nextCallback.apply(this, arg);
                        };
                    };
                    ajaxConfig.errCallback = function () {
                        const arg = objClone(arguments);
                        next = function () {
                            nextErrCallback.apply(this, arg);
                        };
                    };
                    Eif (callback) {
                        r.ajaxConfig.callback = function () {
                            callback.apply(this, arguments);
                            next && next();
                        };
                    }
                    Eif (errCallback) {
                        r.ajaxConfig.errCallback = function () {
                            errCallback.apply(this, arguments);
                            next && next();
                        };
                    }
                }
            }
            // 清除
            if (!this.removeItem(interfaceConfig, ajaxConfig)) {
                // single : 如果请求已从队列中移除,则终止处理流程
                Eif (interfaceConfig.single) return true;
            }
        };
        return new RequestManager();
    })(),
    // 持久化存储
    repository: (function () {
        let ls = window.localStorage;
        return {
            push: function (params) {
                // 保存新的数据
                ls[params.key] = JSON.stringify({
                    timeStamp: params.timeStamp,
                    validTime: params.validTime,
                    responseData: params.data
                });
            },
            get: function (key) {
                this.clean();
                return JSON.parse(ls.getItem(key) || 'null');
            },
            // 清理过期缓存
            clean: function () {
                // 清除过期缓存
                for (let i = 0; i < ls.length; i++) {
                    // 42db81629a 3582822d6b b6b83bbe1d 2378e6aeeb
                    Eif (/\w{40}/.test(ls.key(i))) {
                        let storeData = JSON.parse(ls.getItem(ls.key(i)) || 'null');
                        Eif (storeData) {
                            if ((new Date().getTime() - storeData.timeStamp) / 1000 > storeData.validTime) {
                                ls.removeItem(ls.key(i));
                            }
                        }
                    }
                }
            }
        };
    })()
};
 
 
/**
 * 接口管理器
 * new InterfaceProxy(interfaceConfig,additionalOptions) 来生成接口操作对象(return)
 * @module InterfaceProxy
 * @param {object}              configMap 接口描述配置信息unitInfo[]
 * @param {OriginAjaxConfig|Array}    additionalOptions 接口基础配置信息
 * @constructor
 */
function InterfaceProxy(configMap, additionalOptions) {
    // 基础配置信息
    this.$additionalOptions = (isArray(additionalOptions) ? additionalOptions : [additionalOptions || {}]).reduce((obj1, obj2) => {
        return objCover(objClone(obj1), obj2);
    }, {});
    // 接口配置JSON
    this.$configMap = configMap;
    // 装配工具箱
    this.$toolkit = toolkit;
    // model
    this.$model = this.$additionalOptions.model || null;
    // 初始化接口 - 自动生成所有接口操作方法
    this.__init__();
}
 
InterfaceProxy.prototype = /**@lends module:InterfaceProxy# */{
    /**
     * 初始化
     * @this module:InterfaceProxy
     * @ignore
     */
    __init__: function () {
        // 变量交换
        let configList = this.$configMap;
        // 提供 iProxy[name] 操作方法 | 将 构造的getData在调用时的this指向getData[pn]本体
        let iProxy = this;
        // 动态生成接口代理对象操作方法
        for (let pn in configList) {
            Iif (!configList.hasOwnProperty(pn)) continue;
            /**
             * @typedef {function} interfaceMethod          - 自动生成的接口操作方法
             * @property {OriginAjaxConfig} interfaceInfo   - 接口信息,来源接口配置
             * @property {OriginAjaxConfig} info            - 接口信息,来源接口配置
             * @property {string} url                       - 接口URL
             */
            iProxy[pn] = toolkit.buildInterfaceMethod(pn, this);
            iProxy[pn].$options = iProxy[pn].info = configList[pn];
            iProxy[pn].url = iProxy[pn].info.url || (iProxy[pn].info.origin || '') + iProxy[pn].info.pathname;
            // iProxy[pn].getUrl = (proxyObj => {
            //     const key = pn;
            //     return function (query) {
            //         const options = toolkit.getInterfaceConfig(key, proxyObj);
            //         var url = options.url || options.origin + options.pathname;
            //         var params = objCover(objClone(options.params) || {}, query);
            //         return buildURL({url: url, params: params})
            //     }
            // })(this);
        }
    }
};
 
// 导出Hashes
InterfaceProxy.Hashes = Hashes;
// 导出Ajax
// InterfaceProxy.Ajax = Ajax;
InterfaceProxy.Ajax = extendAxios;
InterfaceProxy.axios = axios;
// 修改全局默认配置API
InterfaceProxy.setDefault = options => {
    objCover(defaultOptions, options);
};
 
window.InterfaceProxy = InterfaceProxy;
export default InterfaceProxy;