CCNetwork.js 9.35 KB
//修改Network,解决闪屏问题
var Network = require('Network');
cc.Class({
    extends: Network,

    properties: {

    },

    // use this for initialization
    onLoad: function () {

    },


    statics: {
        //_count: 0,

        /*
                    var txt = document.getElementById('txt');
                    //1.创建XMLHttpRequest对象
                    var xhr = null;
                    if(window.XMLHttpRequest) {
                        xhr = new XMLHttpRequest();
                    }else {
                        xhr = new ActiveXObject('Microsoft.XMLHTTP');
                    }
                    //2.打开与服务器的链接
                    xhr.open('get',url,false);
                    //3.发送给服务器
                    xhr.send(null);
                    //4.响应就绪(同步请求)
                    var json = JSON.parse(xhr.responseText);
                    txt.innerHTML = json;
                    cc.log('其他程序');
        */



        //ajax请求函数
        ajax: function (strMethod, strURL, astrContentType, oDataOrPostBody, onSuccess, onError, oScope, oAdditionalData) {
            var xlr = null;
            var oResult = {};



            if (window.XMLHttpRequest) {// code for IE7, Firefox, Opera, etc.
                xlr = new XMLHttpRequest(); //Log.info('XMLHttpRequest');
            } else if (window.ActiveXObject) {// code for IE6, IE5
                xlr = new ActiveXObject("Microsoft.XMLHTTP"); //Log.info('ActiveXObject');
            }

            if (!xlr) {
                cc.log("Your browser does not support XMLHTTP, using iframe instead..");
                //退化到使用iframe 在cocos中无用                                      //这里两个函数名需要字符串输入
                //iframeRequest(strMethod, strURL, astrContentType, oDataOrPostBody, onSuccess, onError, oAdditionalData)
                return;
            }

            function onStateChange() {
                if (xlr.readyState == 4) {// 4 = "loaded"
                    if (xlr.status == 200) {// 200 = OK
                        if (null != oScope) {
                            onSuccess.call(oScope, xlr.responseText, oAdditionalData);
                        } else {
                            onSuccess(xlr.responseText, oAdditionalData);
                        }
                    } else {
                        if (null != oScope) {
                            onError.call(oScope, xlr.responseText, oAdditionalData);
                        } else {
                            onError(xlr.responseText, oAdditionalData);
                        }
                    }
                    xlr = null;
                }
            }



            strMethod = strMethod.toUpperCase();
            var strDataOrPostBody = '';
            if (strMethod == "GET") {

                //以下这段可使用正则表达式改造
                if (oDataOrPostBody) {
                    if (strURL.indexOf('?') > 0) { //有问号
                        if (strURL.indexOf('=') > strURL.indexOf('?') //并且有等号在问号右边
                            && strURL.lastIndexOf('&') != strURL.length - 1) //最后一个不是&
                        {
                            strURL = strURL.concat('&');
                        } else { //光有问号没等号
                            cc.log('Unavailable Request URL!');
                            return;
                        }
                    } else {//没问号
                        strURL = strURL.concat('?');
                    }

                    if (typeof oDataOrPostBody == "string") {
                        strDataOrPostBody = oDataOrPostBody;
                    } else if (oDataOrPostBody instanceof Object) {
                        for (var key in oDataOrPostBody) {
                            strDataOrPostBody = strDataOrPostBody.concat(key).concat('=').concat(oDataOrPostBody[key]).concat('&');
                        }
                        strDataOrPostBody = strDataOrPostBody.substring(0, strDataOrPostBody.length - 1);
                        //strDataOrPostBody.TrimEnd('&');
                    } else {
                        cc.log('Unavailable Parameters!');
                        return;
                    }
                }

                strURL = strURL.concat(strDataOrPostBody);
            }

            xlr.onreadystatechange = onStateChange;

            xlr.open(strMethod, strURL, true);

            var isWWWFormURLEncoded = false;
            if (astrContentType) {
                for (var i = 0; i < astrContentType.length; i++) {
                    xlr.setRequestHeader('Content-type', astrContentType[i]);
                    if (astrContentType[i] == 'application/x-www-form-urlencoded') {
                        isWWWFormURLEncoded = true;
                        break;
                    }
                }
            }
            //xlr.setRequestHeader('Connection', 'close');

            switch (strMethod) {
                case 'GET':
                    xlr.send(null);
                    break;
                case 'POST':
                    if (isWWWFormURLEncoded) {

                    }
                    if (oDataOrPostBody) {
                        /*
                        if (isWWWFormURLEncoded) {
                            var fd=new FormData();
                            if (typeof oDataOrPostBody == "string") {
                                strDataOrPostBody = oDataOrPostBody;
                                
                                var aParameters1=oDataOrPostBody.split('&');
                                for (var i=0;i<aParameters1.length;i++){
                                    var aPairs1=aParameters1.split('=');
                                    if (aPairs1[0] && aPairs1[1]){
                                        fd.append(aPairs1[0],aPairs1[1]);
                                    }
                                }
                                
                            } else if (oDataOrPostBody instanceof Object) {
                                for (var key in oDataOrPostBody) {
                                    fd.append(key,oDataOrPostBody[key]);
                                }
                                //strDataOrPostBody.TrimEnd('&');
                                //strDataOrPostBody = strDataOrPostBody.substring(0, strDataOrPostBody.length - 1);
                            } else {
                                console.log("Unavailable Parameters!");
                                return
                            }
                            xlr.send(fd);
                        }else{*/
                        if (typeof oDataOrPostBody == "string") {
                            strDataOrPostBody = oDataOrPostBody;

                        } else if (oDataOrPostBody instanceof Object) {
                            for (var key in oDataOrPostBody) {
                                strDataOrPostBody = strDataOrPostBody.concat(key).concat('=').concat(oDataOrPostBody[key]).concat('&');
                            }
                            //strDataOrPostBody.TrimEnd('&');
                            strDataOrPostBody = strDataOrPostBody.substring(0, strDataOrPostBody.length - 1);
                        } else {
                            oResult.businessCode = 'success';
                            oResult.description = 'Unavailable Parameters!';
                            return oResult;
                        }
                        xlr.send(strDataOrPostBody);
                        //}
                    } else {
                        xlr.send(null);
                    }

                    break;
            }


            // cc.director.on(cc.Director.EVENT_BEFORE_SCENE_LOADING, function () {
            //     // cc.log("运行新场景之前所触发的事件------------->");
            //     xlr.abort();
            // });

        },
        loadImageInJSRuntime: function (strURL, iRequestId, onSuccess, onError, oScope) {
            cc.loader.load(
                {
                    url: strURL,
                    isCrossOrigin: false,
                    type: 'jpg'  //防止跨域问题
                },
                function (err, texture) {
                    if (!err) {
                        if (null != oScope) {
                            onSuccess.call(oScope, texture, iRequestId);
                        } else {
                            onSuccess(texture);
                        }
                    } else {
                        if (null != oScope) {
                            onError.call(oScope, err, iRequestId);
                        } else {
                            onError(err);
                        }
                    }
                }
            );

        },

        loadImageInNativeRuntime: function (strURL, iRequestId, onSuccess, onError, oScope) {
            cc.loader.load(strURL, function (err, texture) {
                if (null != oScope) {
                    onSuccess.call(oScope, texture, iRequestId);
                } else {
                    onSuccess(texture);
                }
                cc.log('Should load a texture from external url: ' + (texture instanceof cc.Texture2D));
            });
        },

    },


    // called every frame, uncomment this function to activate update callback
    // update: function (dt) {

    // },
});