JsAppStartUp.html 11.5 KB
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="page-view-size" content="1280*720"/>
    <title>startUpPage</title>
</head>
<body style="background-color: black">
<a style="color: azure">JsApp 启动模块</a>
<div id="console" style="color: azure;"></div>
<script>
    //当前是否已经安装基座
    let isBaseAppExists = false;
    //安装重试次数
    let tryCount = 0;
    //单次安装超时
    let times = 0;

    //JsApp启动参数
    let ExtraParam = {
        UserID: "",
        UserToken: "",
        TokenExpireTime: "",
        UserGroup: "",
        EPGGroup: "",
        AreaCode: "",
        Province: "",
        City: "",
        County: "",
        Cellphone: "",
        STBType: "",
        STPID: "",
        STBMAC: "",
        STPIP: "",
        Partner: "",
        SessionServer: "",
        SessionID: "",
        EPGDomain: "",//EPG home页
        ReturnURL: "http://192.168.12.98:8080/JsAppLaunch/JsAppStartUp.html",
        JsAppID: "",
        JsAppURL: "http://139.196.145.150:8060/AppEngine/apps/jsviewSCepg/js/main.jsv.mjs?coreVersionRange=1021848&engineUrl=http://139.196.145.150:8060/AppEngine/apps/shijiuBase/JsViewES6_js2c_r963.jsv.4b3a9429.js&updateUrl=http://139.196.145.150:8060/AppEngine/apps/shijiuBase/",//js app地址 目前需要传递视九的地址
        JSAppParam:"",
        ChannelList:"[]"//Channel频道播放数据
    }

    //基座最新版本配置
    const BaseAppConfig = {
        force: 1,
        versionCode: 3,
        versionName: "0.0.3",
        packageName: "com.lh.jsapp",
        className:"MainActivity",
        path: "http://192.168.12.98:8080/JsAppLaunch/newJsBaseApp.apk",
        md5: "",
        size: 0,
        desc: "测试安装"
    };

    /**
     * 基座应用入口参数
     */
    let StartUpParam = {
        appName: BaseAppConfig.packageName,
        className: BaseAppConfig.className,
        action: "",
        category: "",
        data: "",
        extra: []//启动参数
    }

    /**
     * 通用工具
     */
    const CommonUtil = {
        initParam: function () {
            const that = this;
            that.printLn(JSON.stringify(BaseAppConfig));
            if (that.checkAuthFunc()) {
                ExtraParam.EPGDomain = Authentication.CTCGetConfig("EPGDomain");
                that.printLn("EPGDomain="+EPGDomain);
                ExtraParam.UserToken = Authentication.CTCGetConfig("accessToken");
                ExtraParam.EPGGroup = Authentication.CTCGetConfig("EPGGroupNMB");
                ExtraParam.ChannelList = Authentication.CTCGetConfig("Channel");
                let sjParams = {
                    userId:that.getUrlParams('userId') || that.getCookie('userId') || '',
                    platformAccount:that.getPlatformAccount(),
                }
                let params = {sj:sjParams};
                ExtraParam.JSAppParam = JSON.stringify(params);
            }
        },
        getUrlParams:function (name){
            const that = this;
            let url = window.location.href.split('?')[1] || '';
            let obj = {}; // 声明参数对象
            let arr = url.split('&') ;// 以&符号分割为数组
            for (let i = 0; i < arr.length; i++) {
                let arrNew = arr[i].split('='); // 以"="分割为数组
                obj[arrNew[0]] = arrNew[1];
            }
            return obj[name];
        },
        getCookie:function (name){
            const that = this;
            const value = name.replace(/([\.$?*|{}\(\)\[\]\\\/\+^])/g, '\\$1');
            let regex = new RegExp('(^| )' + value + '=([^;]*)');
            let result = regex.exec(document.cookie);
            return result ? decodeURIComponent(result[2]) : null;
        },
        getPlatformAccount() {
            const that = this;
            let platformAccount =
                top.userId ||
                that.getUrlParams('platformAccount') ||
                that.getUrlParams('platformUserId') ||
                that.getCookie('platformAccount') ||
                that.getCookie('platformUserId') ||
                'topdraw'
            return platformAccount
        },
        checkAuthFunc: function () {
            const that = this;
            if (typeof Authentication === 'undefined') {
                console.log("this webview isn't support Authentication!");
                that.printLn("this webview isn't support Authentication!");
                return false;
            }
            that.printLn("check Authentication success!");
            return true;
        },
        checkSTBFunc: function () {
            const that = this;
            if (typeof STBAppManager === 'undefined') {
                console.log("this webview isn't support STBAppManger!");
                that.printLn("this webview isn't support STBAppManger!");
                return false;
            }
            that.printLn("check STBAppManger success!");
            return true;
        },
        checkFileIsExists:function (url){
            const that = this;
            return fetch(url)
                .then(response => {
                    if (response.status === 200) {
                        that.printLn('文件存在');
                        return true;
                    } else {
                        that.printLn('文件不存在或请求出错');
                        return false;
                    }
                })
                .catch(error => {
                    that.printLn('请求出错:', error);
                    return false;
                });
        },
        printLn: function (string) {
            document.getElementById("console").innerHTML += `<a>${string}</a><br>`;
        }
    };

    /**
     * 启动模块
     */
    const StartUpHelper = {
        /**
         * 启动epg home
         */
        goEpgHome: function () {
            if (CommonUtil.checkAuthFunc()) {
                if (ExtraParam.EPGDomain.length > 0) {
                    window.open(ExtraParam.EPGDomain);
                }
            }
        },
        /**
         * 启动基座
         */
        goBaseJsApp: function () {
            const that = this;
            if (CommonUtil.checkSTBFunc()) {
                const extraArray = Object.keys(ExtraParam).map(key => ({
                    name: key,
                    value: ExtraParam[key]
                }));
                StartUpParam.extra = extraArray;
                CommonUtil.printLn("启动参数="+JSON.stringify(StartUpParam));
                STBAppManager.startAppByIntent(JSON.stringify(StartUpParam));
            }
        }
    };
    /**
     *升级模块
     */
    const UpdateHelper = {
        Common: {
            errorCount: 3,//安装错误阈值
            timeout: 60,//安装后检测状态超时 单位s
        },
        /**
         * 判断基座是否需要安装或者升级
         */
        needUpdateBaseApp: function () {
            const that = this;
            if (CommonUtil.checkSTBFunc()) {
                if (that.isAppExists(BaseAppConfig.packageName)) {
                    isBaseAppExists = true;
                    CommonUtil.printLn("基座已安装");
                    if (BaseAppConfig.force !== 1) {//不强制则不升级
                        return false;
                    }
                    let versionName = STBAppManager.getAppVersion(BaseAppConfig.packageName);
                    let versionCode = that.getVersionCode(versionName);
                    CommonUtil.printLn("versionCode = "+versionCode);
                    CommonUtil.printLn("BaseAppConfig versionCode = "+BaseAppConfig.versionCode);
                    return BaseAppConfig.versionCode > versionCode;
                }
            }
            return false;
        },
        /**
         * 升级基座
         * @param callback 安装结果回调
         */
        updateBaseApp: function (callback) {
            const that = this;
            if (CommonUtil.checkSTBFunc()) {
                if (tryCount > that.Common.errorCount) {
                    CommonUtil.printLn("install failed more than 3 times!");
                    callback(false);
                    return;
                }
                if (!CommonUtil.checkFileIsExists(BaseAppConfig.path)) {
                    callback(false);
                    return;
                }
                STBAppManager.installApp(BaseAppConfig.path);
                const checkInstallStatusInterval = setInterval(function () {
                    CommonUtil.printLn("安装检查 time="+times);
                    if (times > that.Common.timeout) {//安装超时
                        clearInterval(checkInstallStatusInterval);
                        if (tryCount < that.Common.errorCount) {//安装次数未超过阈值
                            tryCount++;
                            STBAppManager.installApp(BaseAppConfig.path);
                            return;
                        }
                        callback(false);
                        return;
                    }
                    if (!isBaseAppExists) {//之前没安装过
                        if (that.isAppExists(BaseAppConfig.packageName)) {
                            clearInterval(checkInstallStatusInterval);
                            callback(true);
                            return;
                        }
                    } else {
                        let versionName = STBAppManager.getAppVersion(BaseAppConfig.packageName);
                        CommonUtil.printLn("已安装包版本信息="+that.getVersionCode(versionName));
                        CommonUtil.printLn("安装包版本信息="+BaseAppConfig.versionCode);
                        if (that.getVersionCode(versionName) === BaseAppConfig.versionCode) {
                            clearInterval(checkInstallStatusInterval);
                            callback(true);
                            return;
                        }
                    }
                    times++;
                }, 1000);
            }
        },
        /**
         * 判断应用是否存在
         * @param packageName
         */
        isAppExists: function (packageName) {
            const that = this;
            if (CommonUtil.checkSTBFunc()) {
                return (STBAppManager.checkPackage(packageName) || STBAppManager.isAppInstalled(packageName));
            }
        },
        /**
         * 转化versionName 为 versionCode
         * @param versionName
         */
        getVersionCode: function (versionName) {
            const regex = /^\d{1,2}\.\d{1,2}\.\d{1,2}$/;
            if (!regex.test(versionName)) {
                return 0;
            }
            let numArr = versionName.split(".");
            return Number(numArr[0] * 10000 + numArr[1] * 100 + numArr[2]);
        }
    };

    window.addEventListener("load",function () {
        CommonUtil.printLn("页面加载完成");
        CommonUtil.initParam();
        if (UpdateHelper.needUpdateBaseApp()) {
            CommonUtil.printLn("需要升级");
            tryCount = 0;
            times = 0;
            UpdateHelper.updateBaseApp(status => {
                if (!status) {
                    if (isBaseAppExists) {
                        StartUpHelper.goBaseJsApp();
                        return
                    }
                    StartUpHelper.goEpgHome();
                    return;
                }
                StartUpHelper.goBaseJsApp();
            });
            return;
        }
        CommonUtil.printLn("不需要升级");
        StartUpHelper.goBaseJsApp();
    });
</script>
</body>
</html>