接口生成

原创
2017/07/30 20:49
阅读数 18
/**
 * Created by liuhu on 2017/7/29.
 * 意图:根据接口路径生成调用方法
 * 建议路径三层以内,如需三层以上则需要自行调整
 */
let interfaceArr = [
    "salary/list",
    "part/list",
    "part/detail",
    "salary/management/add"
];
/**
 * faceExport statement interface methods
 */
var faceExport = ( function(arr){
    var obj = {},sec=[];
    arr.forEach( function( x ){
        var arr = (/^\//.test(x))
            ? x.replace(/\//,"").split("/")
            : x.split("/");
        /*check up everyone for array list*/
        for( var i = 0, n = arr.length; i < n; i++){
            if( !arr[i] ){
                throw new Error("Error:there is a null or undefined, you should check up.Such as:'"+arr.join("/")+"'");
            }
        }
        sec.push( arr );
    });

    sec.forEach( function(x){
        var o = {};
        var h = {};
        if( x.length ){
            var path = x.join("/");
            if( x.length > 2 ){
                for( var i = x.length-1; i > 0; --i ){
                    if(i !== x.length-1){
                        o[ x[i] ] = (i === x.length-2)? h : o;
                    }else{
                        h[ x[i] ] = function( callback ) {
                            callback( path );
                        };
                    }
                }
                if(obj[ x[0] ]){
                    if(obj[ x[0] ][ x[1] ]){
                        obj[ x[0] ][ x[1] ][ x[2] ] = function (callback) {
                            callback( path );
                        };
                    }else{
                        console.log(o)
                        obj[ x[0] ][ x[1] ] = o[x[1]];
                    }
                }else{
                    obj[ x[0] ] = o;
                }
            }else{
                if( x.length === 2 ){
                    if( obj[ x[0] ] ){
                        obj[ x[0] ][ x[1] ] = function( callback ){
                            callback( path );
                        };
                    }else{
                        var m = {};
                            m[ x[1] ] = function( callback ){
                                callback( path );
                            };
                        obj[ x[0] ] = m;
                    }
                }else{
                    obj[ x[0] ] = function(callback){
                        callback( path );
                    };
                }
            }
        }
    });
    /**
     * 解析地址栏参数
     * @param v
     * @returns {*}
     */
    obj["dataUrlParse"] =  function(v){
        var s;
        s = v? (function(v){
                var m = "";
                if(v.indexOf("?")>-1){
                    m = v.substr(v.indexOf("?")+1);
                }else{
                    m = v;
                }
                return m;
            })(v):location.search.replace(/[?]/g,"");/*如果没有外部传入,直接去浏览器地址栏取值*/
        if(s.indexOf("&")>-1){
            s = s.split("&");
        }else{
            if(s.indexOf("=")<0){
                return s;
            }else{
                s = [s];
            }
        }
        var obj = {},
            m = null;
        try{
            for(var i =0,n = s.length;i<n;i++){
                m = s[i].split("=");
                if(decodeURI(m[1]) !== m[1]){/*判断是否经历过编码处理*/
                    obj[m[0]] = decodeURI(m[1]);
                }else{
                    obj[m[0]] = m[1];
                }
            }
        }catch (e){
            obj = {};
            throw new Error("拆分地址栏数据异常,需要检查数据源");
        }
        return obj;
    };
    return obj;
})( interfaceArr );
展开阅读全文
加载中

作者的其它热门文章

打赏
0
0 收藏
分享
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部