videoDetail.js 18.5 KB
var Common = require('Common');
var Network = require('Network');
var TVFocus = require('TVFocus');
var CCTVFocus = require('CCTVFocus');
var FocusInfo = require('FocusInfo');
var TVCanvas = require('TVCanvas');
var TVScrollParameter = require('TVScrollParameter');
var ListView = require('ListView');
var BusinessParameter = require('BusinessParameter');
var ListCell = require('ListCell');
var Application = require('Application');

cc.Class({
    extends: TVCanvas,

    properties: {

    },

    onLoad: function () {
        this._super();
        this._oInit = {};
        this._bCollected = false;
        this._oSceneContext.focusPath = "ButtonFullScreen";

        cc.find("FullDesc", this.node).zIndex = 100;

        var nodeButtonFullScreen = cc.find("ButtonFullScreen", this.node);
        var nodeButtonCollect = cc.find("ButtonCollect", this.node);
        nodeButtonFullScreen.getComponent(cc.Sprite).spriteFrame.setRect(cc.rect(0, 0, nodeButtonFullScreen.width, nodeButtonFullScreen.height));
        nodeButtonCollect.getComponent(cc.Sprite).spriteFrame.setRect(cc.rect(0, 0, nodeButtonCollect.width, nodeButtonCollect.height));

        //播放器相关
        let nodePlayCtrl = cc.find("PlayCtrl", this.node);
        this._ComOperation = nodePlayCtrl.getComponent("OperateVideoPlay");
        this._ComOperation.init(this, null);
        let fiPlayCtrl = nodePlayCtrl.addComponent(FocusInfo);
        fiPlayCtrl.init('{"click": [{"action": "PlaySongInFullScreen"}]}', true);
        this._aFocusTargets[0]["PlayCtrl"] = nodePlayCtrl;

        if (this._cApplication.getBackStatus()) {
            //恢复上下文 包括
            //光标位置 focusPath
            let oSceneContext = this._cApplication.popSceneContext();
            if (oSceneContext) {
                this._oSceneContext = oSceneContext;
                cc.log(this._oSceneContext);
            }
            this._cApplication.setBackStatus(false);
        }

        this.checkPfbReadyAndInitPage();
        this.checkDataReadyAndInitFocus();
    },

    //获取节目详情
    checkPfbReadyAndInitPage: function () {
        var self = this;
        Network.ajax("GET", Common.TOPDRAW_API_SERVER + "videoDetail/game_test_detail_list_by_ids.json", null, null,
            function (strResponse) {
                try {
                    var oJSONResult = JSON.parse(strResponse);
                    // var oJSONResult = this.test_detail_json.json;
                    this._oInit = oJSONResult.resultSet[0];
                    if (oJSONResult.businessCode == 'success') {
                        cc.find("Desc/Title", this.node).getComponent(cc.Label).string = oJSONResult.resultSet[0].name;//标题
                        cc.find("Desc/Title", this.node).getComponent(cc.Label)._forceUpdateRenderData();
                        //==========放到大屏显示问题
                        cc.find("FullDesc/Title", this.node).getComponent(cc.Label).string = oJSONResult.resultSet[0].name;
                        cc.find("FullDesc/Desc", this.node).getComponent(cc.Label).string = oJSONResult.resultSet[0].description;
                        //==========end
                        //这里给描述做拦截,防止字数多超出栏目
                        var description = oJSONResult.resultSet[0].description;
                        if (description.length > 80) {
                            description = description.substring(0, 80) + "...";
                        }
                        cc.find("Desc/LabelDesc", this.node).getComponent(cc.Label).string = description;

                        this._strEpisodeImg = oJSONResult.resultSet[0].images.list[0].fileUrl;
                        cc.log("图片地址:" + (Common.TOPDRAW_IMAGE_SERVER + oJSONResult.resultSet[0].images.list[0].fileUrl));
                        // Network.loadImageInNativeRuntime(
                        //     Common.TOPDRAW_IMAGE_SERVER + oJSONResult.resultSet[0].images.list[0].fileUrl, null,
                        //     function (texture, iRequestId) {
                        //         self.node.getChildByName("Pic").getComponent(cc.Sprite).spriteFrame = new cc.SpriteFrame(texture);
                        //     }, function () { }, this
                        // );

                        // this.getTypeList();

                        this.getPlayURLAndPlay();
                    } else {
                        cc.log("Business Error:Get checkPfbReadyAndInitPage..." + oJSONResult.description);
                    }
                } catch (error) {
                    cc.log("Business Exception:Get checkPfbReadyAndInitPage..." + error);
                }
            },
            function (error) {
                cc.log("Business Exception:Get checkPfbReadyAndInitPage..." + error);
            }, this, "uuid");
    },

    getPlayURLAndPlay: function () {
        try {
            this._ComOperation.initPlayer(64, 56, 564, 317, true, true);  //这里采用左右边距对应android坐标       fix jerry
            // this._ComOperation.initPlayer(0, 0, Common.SCREEN_WIDTH, Common.SCREEN_HEIGHT, true, true);     //测试
            let options = {};
            options.playurl = this._oInit.url;
            this._ComOperation.setURL(options);     //测试播放地址
        } catch (error) {
            if (cc.sys.isNative) {
                jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "logInSceneI", "(Ljava/lang/String;)V", "getPlayURLAndPlay Error in SongLibraryPlayer==>" + error);
            }
        }
    },

    checkDataReadyAndInitFocus: function () {
        if (!this._bIsFocusInit) {
            this.scheduleOnce(() => {           //指定0让回调函数在下一帧立即执行
                this.initFocus();
            }, 0);
            this._bIsFocusInit = true;
        }
    },

    //初始化焦点框
    initFocus: function () {
        this._aFocusTargets[1] = [];
        var nodeButtonFullScreen = cc.find("ButtonFullScreen", this.node);
        var nodeButtonCollect = cc.find("ButtonCollect", this.node);

        let fiButtonFullScreen = nodeButtonFullScreen.addComponent(FocusInfo); //
        fiButtonFullScreen.init('', true, null, null, 1.0);
        this._aFocusTargets[0]['node_button_full_screen'] = nodeButtonFullScreen;

        let fiButtonCollect = nodeButtonCollect.addComponent(FocusInfo); //
        fiButtonCollect.init('', true, null, null, 1.0);
        this._aFocusTargets[0]['node_button_collect'] = nodeButtonCollect;

        let fiDesc = cc.find('Desc/LabelDesc', this.node).addComponent(FocusInfo); //
        fiDesc.init('', true, null, null, 1.0);
        this._aFocusTargets[0]['desc'] = cc.find('Desc/LabelDesc', this.node);

        // let fiBanner = cc.find('ButtonVip', this.node).addComponent(FocusInfo); //
        // fiBanner.init('', true, null, null, 1.06);
        // this._aFocusTargets[0]['button_vip'] = cc.find('ButtonVip', this.node);

        var nodeInitFocus = cc.find('ButtonFullScreen', this.node);  //强制写死,不然会闪屏,找不到ButtonOpen
        // cc.log("初始化 " + nodeInitFocus.name);
        var nodeFocus = new cc.Node('nodeFocus');
        this.node.addChild(nodeFocus, 10);
        this._cFocus = this.node.getChildByName('nodeFocus').addComponent(CCTVFocus);
        this._cFocus.init('focusContainer', this,
            nodeInitFocus.getComponent(FocusInfo),
            Common.SCREEN_WIDTH, Common.SCREEN_HEIGHT, 10, 6, 1.1, true);

        if (0 == nodeInitFocus.name.indexOf('Button')) {
            this._cFocus.hide();
            cc.loader.loadRes("VideoDetail/icon_full_screen", cc.Texture2D, function (err, texture) {
                nodeInitFocus.getComponent(cc.Sprite).spriteFrame = new cc.SpriteFrame(texture, cc.rect(0, nodeInitFocus.height, nodeInitFocus.width, nodeInitFocus.height));
            });
        }

    },

    keyDownDirection: function (Direct) {
        var fiFocusTarget = null;
        var fiCurrentFocus = this._fiCurrentFocus;
        var oScrollParameter = null;
        let aCheckResult;
        fiFocusTarget = this._cFocus.findTarget(fiCurrentFocus, this._aFocusTargets, this._iSceneStatus, Direct);
        if (!fiFocusTarget) { return; }
        0 == fiFocusTarget.node.name.indexOf('Button') ? this._cFocus.hide() : this._cFocus.show();
        aCheckResult = this.checkFocusTarget(fiFocusTarget, oScrollParameter);
        fiFocusTarget = aCheckResult[0];
        oScrollParameter = aCheckResult[1];
        this._cFocus.flyFocus(this._fiCurrentFocus, fiFocusTarget, Direct, null, oScrollParameter);
    },

    checkFocusTarget: function (fiFocusTarget, oScrollParameter) {
        if (fiFocusTarget && 0 == fiFocusTarget.node.name.indexOf("TypeListCell")) {
            if (fiFocusTarget.node.x + this._nodeTypeList.x >= this._nodeTypeListWrapper.width) {
                oScrollParameter = this._nodeTypeList.getComponent(TVScrollParameter);
                oScrollParameter.setHasRelation(true);
                oScrollParameter.setStep((fiFocusTarget.node.x + fiFocusTarget.node.width / 2 + this._nodeTypeList.x) - this._nodeTypeListWrapper.width + 50);//打补丁:20,解决放大时Rect遮挡问题
                oScrollParameter.setTargetPosition(this._nodeTypeList.x - oScrollParameter.getStep());
                this._oSceneContext.nodeTypeListX = this._nodeTypeList.x - oScrollParameter.getStep();
            }
            if (fiFocusTarget.node.x + this._nodeTypeList.x < 0) {
                oScrollParameter = this._nodeTypeList.getComponent(TVScrollParameter);
                oScrollParameter.setHasRelation(true);
                oScrollParameter.setStep(-fiFocusTarget.node.x + fiFocusTarget.node.width / 2 - this._nodeTypeList.x + 20);//打补丁:20,解决放大时Rect遮挡问题
                oScrollParameter.setTargetPosition(this._nodeTypeList.x + oScrollParameter.getStep());
                this._oSceneContext.nodeTypeListX = this._nodeTypeList.x + oScrollParameter.getStep();
            }
        }

        return [fiFocusTarget, oScrollParameter];
    },

    onKeyDown: function (event) {
        this._super(event);
        var self = this;
        var fiFocusTarget = null;
        var fiCurrentFocus = this._fiCurrentFocus;
        cc.find("FullDesc", this.node).active = false;
        switch (event.keyCode) {
            case cc.macro.KEY.up:
            case Common.ANDROID_KEY.up:
                this.keyDownDirection(Common.MOVE_DIRECTION_UP);
                break;
            case cc.macro.KEY.right:
            case Common.ANDROID_KEY.right:
                this.keyDownDirection(Common.MOVE_DIRECTION_RIGHT);
                break;
            case cc.macro.KEY.down:
            case Common.ANDROID_KEY.down:
                this.keyDownDirection(Common.MOVE_DIRECTION_DOWN);
                break;
            case cc.macro.KEY.left:
            case Common.ANDROID_KEY.left:
                this.keyDownDirection(Common.MOVE_DIRECTION_LEFT);
                break;
            case cc.macro.KEY.enter:
            case cc.macro.KEY.space:
            case Common.ANDROID_KEY.enter:
                if (0 == this._fiCurrentFocus.node.name.indexOf('Collect')) {
                    this._bCollected = !this._bCollected;
                    if (this._bCollected) {
                        this.commonSimpleTip("恭喜您!收藏成功!", 3);
                    }
                    let icon = this._bCollected ? "GameDetail/be_collected" : "GameDetail/not_collected";
                    cc.loader.loadRes(icon, cc.Texture2D, function (err, texture) {
                        self._fiCurrentFocus.node.getComponent(cc.Sprite).spriteFrame = new cc.SpriteFrame(texture, cc.rect(0, 56, self._fiCurrentFocus.node.width, 56));
                    });
                } else if (0 == this._fiCurrentFocus.node.name.indexOf('LabelDesc')) {
                    cc.find("FullDesc", this.node).active = true;
                } else if (0 == this._fiCurrentFocus.node.name.indexOf('ButtonOpen') || 0 == this._fiCurrentFocus.node.name.indexOf('Pic')) {
                    // this.commonSimpleTip("是否拿到数据: " + this._bInitExistApk);
                    if (!this._bInitExistApk) { //还未拿到判断数据
                        return;
                    }
                    if (this._bIsExistApk == "true") {
                        if (this._oSceneContext._downloadUrl && this._oSceneContext._downloadUrl != "undefined") { //如果有下载地址就启动奥比游戏
                            jsb.reflection.callStaticMethod("com/topdraw/melody/TopdrawSDKWrapper", "startOBiGame", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V", Common.TOPDRAW_API_SERVER + this._oSceneContext._downloadUrl, this._oSceneContext._packageName, this._oSceneContext._className, "");
                        } else {
                            //这里应该转圈圈等待加载游戏
                            // cc.find("LoadingLabel", this.node).opacity = 255;
                            // cc.find("Loading/LoadingIcon", this.node).opacity = 255;
                            // cc.find("Loading/LoadingIcon", this.node).getComponent(cc.Animation).play();
                            jsb.reflection.callStaticMethod("com/topdraw/melody/TopdrawSDKWrapper", "startGame", "(Ljava/lang/String;Ljava/lang/String;)V", this._oSceneContext._packageName, "");
                        }
                    } else {
                        this.doCurrentFocusTVLinkAction(Common.TV_LINK_ACTION_CLICK);
                    }
                    return;
                } else {
                    this.doCurrentFocusTVLinkAction(Common.TV_LINK_ACTION_CLICK);
                }
                break;
            case cc.macro.KEY.backspace:
            case Common.ANDROID_KEY.back:
                this.backAScene();
                break;
        }
    },

    doCurrentFocusTVLinkAction: function (strAction) {
        let strTVLink = this._fiCurrentFocus.getTVLink();
        cc.log("tvlink===============" + strTVLink);
        try {
            let oTVLink = JSON.parse(strTVLink);
            let aOperationList = oTVLink.click;
            for (let i = 0; i < aOperationList.length; i++) {
                switch (aOperationList[i].action) {
                    case "changeLayout":
                    default:
                        this.doTVLinkAction(aOperationList[i]);
                        break;
                }
            }
        } catch (error) {
            cc.log("runTVLinkAction Exception..." + error);
        }
    },

    onBeforeFocusChange: function (event) {

        let fiFrom = event.detail.from;
        let fiTo = event.detail.to;

        if (0 == fiFrom.node.getName().indexOf('ButtonFullScreen')) {
            cc.loader.loadRes("VideoDetail/icon_full_screen", cc.Texture2D, function (err, texture) {
                fiFrom.node.getComponent(cc.Sprite).spriteFrame = new cc.SpriteFrame(texture, cc.rect(0, 0, fiFrom.node.width, fiFrom.node.height));
            });
        }

        if (0 == fiFrom.node.getName().indexOf('ButtonCollect')) {
            cc.loader.loadRes("VideoDetail/icon_collect", cc.Texture2D, function (err, texture) {
                fiFrom.node.getComponent(cc.Sprite).spriteFrame = new cc.SpriteFrame(texture, cc.rect(0, 0, fiFrom.node.width, fiFrom.node.height));
            });
        }

    },

    onAfterFocusChange: function (event) {
        var self = this;
        let fiTo = event.detail.to;
        let fiFrom = event.detail.from;

        if (0 == fiTo.node.getName().indexOf('ButtonFullScreen')) {
            cc.loader.loadRes("VideoDetail/icon_full_screen", cc.Texture2D, function (err, texture) {
                fiTo.node.getComponent(cc.Sprite).spriteFrame = new cc.SpriteFrame(texture, cc.rect(0, fiTo.node.height, fiTo.node.width, fiTo.node.height));
            });
        }

        if (0 == fiTo.node.getName().indexOf('ButtonCollect')) {
            cc.loader.loadRes("VideoDetail/icon_collect", cc.Texture2D, function (err, texture) {
                fiTo.node.getComponent(cc.Sprite).spriteFrame = new cc.SpriteFrame(texture, cc.rect(0, fiTo.node.height, fiTo.node.width, fiTo.node.height));
            });
        }

        if (0 == fiTo.node.getName().indexOf('TypeListCell')) {
            let iIndex = fiTo.node.name.replace("TypeListCell", '');
            if (this._oInit.aListImage[iIndex]) {
                Network.loadImageInNativeRuntime(Common.TOPDRAW_IMAGE_SERVER + this._oInit.aListImage[iIndex].fileUrl, null,
                    function (texture) {
                        cc.find("FullView", self.node).getComponent(cc.Sprite).spriteFrame = new cc.SpriteFrame(texture);
                    }, function () { }, this
                );
            }
        }
    },

    doCurrentFocusTVLinkAction: function (strAction) {
        try {
            let strTVLink = this._fiCurrentFocus.getTVLink();
            var joTVLink = null;
            try {
                joTVLink = JSON.parse(strTVLink);
                let jaOperationList = joTVLink.click;
                //上下文
                let compApplication = cc.find('application').getComponent(Application);
                let aSceneContext = compApplication.getSceneContext();
                let aSceneParameter = compApplication.getSceneParameter();
                for (let i = 0; i < jaOperationList.length; i++) {
                    let strMediaId;
                    switch (jaOperationList[i].action) {
                        case "PlaySongInFullScreen":
                            aSceneParameter.push({
                                "name": this._oInit.name,
                                "url": this._oInit.url,
                                "backSceneName": cc.director.getScene().name,
                            });
                            // this._oSceneContext.focusPath = Common.getNodePath(this._fiCurrentFocus.node);
                            // aSceneContext.push(this._oSceneContext);
                            // this._ComOperation && this._ComOperation.end();
                            // Common.g_strCurrentSceneName = 'sceneKTVPlayer';
                            cc.director.loadScene('sceneVideoPlayer');
                            break;
                        default:
                            this.doTVLinkAction(jaOperationList[i]);
                            break;
                    }
                }
            } catch (err) {
                cc.log("runTVLinkAction Exception: " + err);
            }
        } catch (error) {
            if (cc.sys.isNative) {
                jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "logInSceneI", "(Ljava/lang/String;)V", "doCurrentFocusTVLinkAction Error in SongLibraryPlayer==>" + error);
            }
        }

    },
});