Application.js 18.2 KB
var Common = require('Common');
var Network = require('Network');
//var ListView = require('ListView');
cc.Class({
    extends: cc.Component,

    properties: {
        //键值由具体场景决定
        _aSceneContext: { default: [] },
        _aSceneParameter: { default: [] },
        _bBackStatus: false,
        _iDefaultCollectionId: -1,
        _oScene: null,
        _strSceneName: "",
    },

    // use this for initialization
    onLoad: function () {
        this._aSceneContext = [];
        this._aSceneParameter = [];
        //探针相关
        this._oProbe = this.initProbe();
    },
    setScene: function (oScene) {
        this._strSceneName = cc.director.getScene().name;
        this._oScene = oScene;
    },
    getScene: function () {
        return this._oScene;
    },
    getSceneContext: function () {
        return this._aSceneContext;
    },
    popSceneContext: function () {
        return this._aSceneContext.pop();
    },
    getTopSceneContext: function () {
        return this._aSceneContext[this._aSceneContext.length - 1];
    },
    setSceneContext: function (aSceneContext) {
        this._aSceneContext = aSceneContext;
    },
    getSceneParameter: function () {
        return this._aSceneParameter;
    },
    popSceneParameter: function () {
        return this._aSceneParameter.pop();
    },
    getTopSceneParameter: function () {
        if (this._aSceneParameter.length) {
            return this._aSceneParameter[this._aSceneParameter.length - 1];
        } else {
            return null;
        }

    },
    setSceneParameter: function (aSceneParameter) {
        this._aSceneParameter = aSceneParameter;
    },
    getBackStatus: function () {
        return this._bBackStatus;
    },

    setBackStatus: function (bBackStatus) {
        this._bBackStatus = bBackStatus;
    },
    getDefaultCollectionId: function () {
        return this._iDefaultCollectionId;
    },

    setDefaultCollectionId: function (iCollectionId) {
        this._iDefaultCollectionId = iCollectionId;
    },
    initProbe: function () {
        try {
            let _this = this;
            //对于ajax进行拦截
            let fnTempAjax = Network.ajax;
            Network.ajax = function (strMethod, strURL, astrContentType, oDataOrPostBody, onSuccess, onError, oScope, oAdditionalData) {
                if (_this._oScene && _this._oScene._bIsFocusInit) {
                    cc.log("strURL==>" + strURL);
                    if (-1 != strURL.indexOf("User/AddMedia2Collection")) {
                        console.log(strURL);
                        console.log(oDataOrPostBody);
                        _this._oProbe.requestProbe("MediaCollect", {
                            "mediaId": oDataOrPostBody.mediaId,
                            "mediaCode": oAdditionalData.code
                        });
                    }
                    if (-1 != strURL.indexOf("User/RemoveMediaFromCollection")) {
                        console.log(strURL);
                        console.log(oDataOrPostBody);
                        _this._oProbe.requestProbe("MediaUncollect", {
                            "mediaId": oDataOrPostBody.mediaId,
                            "mediaCode": oAdditionalData.code
                        });
                    }
                }
                fnTempAjax.call(Network, strMethod, strURL, astrContentType, oDataOrPostBody, onSuccess, onError, oScope, oAdditionalData);
            }
            return {
                oSceneUIMap: {
                    "sceneMain": "index"
                },
                oCommonParam: {
                    product: "ForFun",
                    sessionId: "",
                    version: Common.VERSION,
                    userId: Common.USER_ID,
                    action: "",
                },
                loginEntrance: "",
                lastActiveTime: "",
                uuid: "",
                inited: false,
                jumpCode: "",
                productId: "",
                productPrice: "",
                aInterActive: [],
                aCollectActive: [],
                oActionEvoker: null,
                oActionList: null,
                aDataShowScene: ["sceneAudioPlayer", "sceneCategoryList", "sceneCollectionList", "sceneFreeList", "sceneHistoryList", "sceneHotCategoryWithScreen",
                    "sceneKTVPlayer", "sceneRankList", "sceneSearch", "sceneShop", "sceneSongLibraryInPlayer", "sceneStarPersonalSection", "sceneSubIndex", "sceneSubjectList", "sceneTagList"],
                init: function () {
                    try {
                        this.lastActiveTime = this.lastActiveTime || 0;
                        this.updateActive();
                        this.uuid = this.uuid || Common.uuid(32, 16);
                        this.sessionId = this.uuid || Common.uuid(32, 16);
                        this.inited = true;
                    } catch (err) {
                        jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "logInSceneI", "(Ljava/lang/String;)V", "init in Application Error==>" + err);
                    }
                },
                updateActive: function () {//更新active,每次动作更新lastActiveTime
                    try {
                        let iCurrentTime = new Date().getTime();
                        if (iCurrentTime - this.lastActiveTime >= 20 * 60 * 1000) {//超过20分钟时 2分钟用来测试来着 todo:
                            this.lastActiveTime = iCurrentTime;//更新lastActiveTime时间
                            this.uuid = Common.uuid(32, 16);//更新uuid
                            this.oCommonParam.sessionId = this.uuid;
                        }
                        this.lastActiveTime = iCurrentTime;//更新动作时间
                    } catch (err) {
                        jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "logInSceneI", "(Ljava/lang/String;)V", "updateActive in Application Error==>" + err);
                    }

                },
                requestProbe: function (strType, oExtra) {
                    try {
                        this.updateActive();
                        let oParam = Common.assign({}, this.oCommonParam);
                        oParam.action = strType;
                        let obj = {};
                        switch (strType) {
                            case "Login"://登录
                                obj = {
                                    "productId": Common.AUTH_PRODUCT,//鉴权成功的产品Id
                                }
                                break;
                            case "UIEnter"://页面进入
                                cc.log("UIEnter...");
                                if (!this.inited) {
                                    this.init();
                                    //对按键进行拦截
                                    let self = this;
                                    let fnKeyDown = _this._oScene.onKeyDown;
                                    _this._oScene.onKeyDown = function (event) {
                                        self.updateActive();
                                        fnKeyDown.call(_this._oScene, event);
                                    }
                                }

                                obj = {
                                    "parameters": _this.getTopSceneParameter(),
                                    "ui": this.oSceneUIMap[_this._strSceneName] || _this._strSceneName,
                                    "jumpCode": this.jumpCode || "-"
                                }
                                break;
                            case "UILeave"://页面离开
                                this.jumpCode = Common.uuid(32, 16);
                                obj = {
                                    "context": _this._oScene._oSceneContext,
                                    "ui": this.oSceneUIMap[_this._strSceneName] || _this._strSceneName,
                                    "jumpCode": this.jumpCode,//页面离开进来jumpcode对应
                                }
                                Common.assign(obj, _this._oScene._fiCurrentFocus && _this._oScene._fiCurrentFocus.getFocusProbeInfo() || {});
                                break;
                            case "interActive"://页面交互
                                obj = {
                                    "ui": this.oSceneUIMap[_this._strSceneName] || _this._strSceneName,
                                    "context": this.oActionContext || _this._oScene._oSceneContext,
                                    "actionTime": this.lastActiveTime
                                }
                                break;
                            case "MediaPlay":
                            case "MediaPause":
                            case "MediaStop":
                            case "MediaPlayAvailable":
                            case "MediaSeek"://播控相关
                                obj = {
                                    "mediaId": parseInt(_this._oScene._strRequestMediaId),
                                    "mediaCode": _this._oScene._strRequestMediaCode,
                                    "appId": _this._oScene._strAppId,
                                    "actionTime": new Date().getTime()
                                }
                                break
                            case "MediaCollect"://收藏
                            case "MediaUncollect"://取消收藏
                                obj = {
                                    "ui": this.oSceneUIMap[_this._strSceneName] || _this._strSceneName,
                                    "mediaId": parseInt(_this._oScene._strRequestMediaId),
                                    "mediaCode": _this._oScene._strRequestMediaCode,
                                    "appId": _this._oScene._strAppId
                                }
                                break;
                            case "Subscribing"://订购发起
                            case "Subscribed"://订购发起
                            case "Unsubscribing"://退订发起
                            case "Unsubscribed"://退订结果
                                obj = {
                                    "productId": Common.AUTH_PRODUCT,
                                    "transactionId": "",
                                    "actionTime": new Date().getTime()
                                }
                                break;
                            case "mediaSearch"://搜索行为
                                obj = {
                                    "ui": this.oSceneUIMap[_this._strSceneName] || _this._strSceneName,
                                }
                                break;
                            case "mediaSearchAccept"://搜索采纳
                                let olink = _this._oScene._fiCurrentFocus && _this._oScene._fiCurrentFocus.getFocusProbeInfo() || {};
                                let oLinkPoint = olink.linkPoint;
                                obj = {
                                    "ui": this.oSceneUIMap[_this._strSceneName] || _this._strSceneName,
                                    "keyword": _this._oScene._oSceneContext.currentKeyword
                                }
                                obj[oLinkPoint.type + "Id"] = oLinkPoint.id;
                                obj[oLinkPoint.type + "Code"] = oLinkPoint.code;
                                break;
                            case "playGame"://
                                obj = {
                                    "ui": this.oSceneUIMap[_this._strSceneName] || _this._strSceneName,
                                    "context": _this._oScene._oSceneContext,
                                    "parameters": _this.getTopSceneParameter(),
                                }
                                break;
                            case "endGame"://
                                obj = {
                                    "ui": this.oSceneUIMap[_this._strSceneName] || _this._strSceneName,
                                    "context": _this._oScene._oSceneContext,
                                    "parameters": _this.getTopSceneParameter(),
                                }
                                break;
                            case "error"://页面报错了
                                obj = {
                                    "ui": this.oSceneUIMap[_this._strSceneName] || _this._strSceneName,
                                    "context": _this._oScene._oSceneContext,
                                }
                                break;
                        }
                        if (oExtra) {
                            Common.assign(obj, oExtra);
                        }
                        Common.assign(oParam, obj);
                        var aProbeArray = [];
                        if (strType == "UILeave" && this.aInterActive.length) {
                            aProbeArray.push(oParam);
                            // fnTempAjax('GET',BI_BASE_PATH,null,this.aInterActive,function(responseText){

                            //     // Log.info("probe Business::");
                            // },function(responseText){
                            //     // Log.info("probe Communication::");
                            // });
                            // fnTempAjax('GET',BI_BASE_PATH,null,this.aCollectActive,function(responseText){

                            //     // Log.info("probe Business::");
                            // },function(responseText){
                            //     // Log.info("probe Communication::");
                            // });
                            this.aInterActive = [];
                            this.aCollectActive = [];
                        } else if (strType == "interActive") {
                            if (_this._oScene && _this._oScene._bIsFocusInit) {
                                this.aInterActive.push([oParam]);
                                if (this.aInterActive.length < 3) {
                                    return;
                                }
                                aProbeArray = this.aInterActive;
                                this.aInterActive = [];
                            }
                        } else if (strType == "MediaCollect" || strType == "MediaUncollect") {
                            this.aCollectActive.push([oParam]);
                            if (this.aCollectActive.length < 4) {
                                return;
                            }
                            aProbeArray = this.aCollectActive;
                            this.aCollectActive = [];
                        } else {
                            aProbeArray.push(oParam);
                        }
                        console.log("============================");
                        console.log(aProbeArray);
                        var oArgs = {
                            'ray': encodeURIComponent(JSON.stringify(aProbeArray))//数组作为参数值传到ray参数里边
                        }
                        fnTempAjax('GET', Common.BI_BASE_PATH, null, oArgs, function (responseText) {   //打开发送探针
                            // Log.info("probe Business::");
                            cc.log("UIEnter success..." + responseText);
                        }, function (responseText) {
                            // Log.info("probe Communication::");
                        });
                    } catch (err) {
                        jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "logInSceneI", "(Ljava/lang/String;)V", "requestProbe in Application Error==>" + err);
                    }

                }
            }
        } catch (err) {
            jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "logInSceneI", "(Ljava/lang/String;)V", "initProbe in Application Error==>" + err);
        }

    },
    probeInterceptScrollStart: function (oList) {
        try {
            let _this = this;
            let fnTemp = this._oScene.onListScrollStart;
            this._oScene.onListScrollStart = function () {
                if (_this._oScene && _this._oScene._bIsFocusInit) {
                    _this.oActionEvoker = _this._oScene._fiCurrentFocus;
                    _this.oActionList = oList;
                    cc.log("=====================================");
                    cc.log(oList);
                    cc.log(_this.oActionEvoker.node.name);
                }

                fnTemp.call(_this._oScene);
            }
        } catch (err) {
            jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "logInSceneI", "(Ljava/lang/String;)V", "probeInterceptScrollStart in Application Error==>" + err);
        }

    },
    probeInterceptScrollEnd: function (oList) {
        try {
            let _this = this;
            let fnTemp = this._oScene.onListScrollEnd;
            this._oScene.onListScrollEnd = function () {
                if (_this._oScene && _this._oScene._bIsFocusInit) {
                    let strModuleName = oList.node.name;
                    let strEvoke = "UIButton";
                    if (_this.oActionEvoker && -1 != Common.getNodePath(_this.oActionEvoker.node).indexOf(oList.node.name)) {

                    }
                    if (_this.oActionEvoker && _this.oActionEvoker.node && _this.oActionEvoker.node.name == "ScrollBarBlock") {
                        strEvoke = "ScrollBar";
                    }
                    this._oProbe.requestProbe("interActive", {
                        "action": "DataShow",
                        "module": strModuleName,
                        "context": {
                            "pageIndex": Math.ceil((oList._iRecordIndexOfFirstCellInPage + oList._iCellCountEachRow * oList._iShowCellRows) / oList._iCellCountEachRow / oList._iShowCellRows),
                            "evoke": strEvoke
                        }
                    });
                }

                fnTemp.call(_this._oScene);
            }
        } catch (err) {
            jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "logInSceneI", "(Ljava/lang/String;)V", "probeInterceptScrollEnd in Application Error==>" + err);
        }
    },
});