js启动模块代码,使用说明
Showing
2 changed files
with
325 additions
and
0 deletions
jsTestFile/JsAppStartUp.html
0 → 100644
| 1 | <!DOCTYPE html> | ||
| 2 | <html lang="en"> | ||
| 3 | <head> | ||
| 4 | <meta charset="UTF-8"> | ||
| 5 | <meta name="page-view-size" content="1280*720"/> | ||
| 6 | <title>startUpPage</title> | ||
| 7 | </head> | ||
| 8 | <body style="background-color: black"> | ||
| 9 | <a style="color: azure">JsApp 启动模块</a> | ||
| 10 | <div id="console" style="color: azure;"></div> | ||
| 11 | <script> | ||
| 12 | //当前是否已经安装基座 | ||
| 13 | let isBaseAppExists = false; | ||
| 14 | //安装重试次数 | ||
| 15 | let tryCount = 0; | ||
| 16 | //单次安装超时 | ||
| 17 | let times = 0; | ||
| 18 | |||
| 19 | //JsApp启动参数 | ||
| 20 | let ExtraParam = { | ||
| 21 | UserID: "", | ||
| 22 | UserToken: "", | ||
| 23 | TokenExpireTime: "", | ||
| 24 | UserGroup: "", | ||
| 25 | EPGGroup: "", | ||
| 26 | AreaCode: "", | ||
| 27 | Province: "", | ||
| 28 | City: "", | ||
| 29 | County: "", | ||
| 30 | Cellphone: "", | ||
| 31 | STBType: "", | ||
| 32 | STPID: "", | ||
| 33 | STBMAC: "", | ||
| 34 | STPIP: "", | ||
| 35 | Partner: "", | ||
| 36 | SessionServer: "", | ||
| 37 | SessionID: "", | ||
| 38 | EPGDomain: "",//EPG home页 | ||
| 39 | ReturnURL: "http://192.168.12.98:8080/JsAppLaunch/JsAppStartUp.html", | ||
| 40 | JsAppID: "", | ||
| 41 | 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地址 目前需要传递视九的地址 | ||
| 42 | JSAppParam:"", | ||
| 43 | ChannelList:"[]"//Channel频道播放数据 | ||
| 44 | } | ||
| 45 | |||
| 46 | //基座最新版本配置 | ||
| 47 | const BaseAppConfig = { | ||
| 48 | force: 1, | ||
| 49 | versionCode: 3, | ||
| 50 | versionName: "0.0.3", | ||
| 51 | packageName: "com.lh.jsapp", | ||
| 52 | className:"MainActivity", | ||
| 53 | path: "http://192.168.12.98:8080/JsAppLaunch/newJsBaseApp.apk", | ||
| 54 | md5: "", | ||
| 55 | size: 0, | ||
| 56 | desc: "测试安装" | ||
| 57 | }; | ||
| 58 | |||
| 59 | /** | ||
| 60 | * 基座应用入口参数 | ||
| 61 | */ | ||
| 62 | let StartUpParam = { | ||
| 63 | appName: BaseAppConfig.packageName, | ||
| 64 | className: BaseAppConfig.className, | ||
| 65 | action: "", | ||
| 66 | category: "", | ||
| 67 | data: "", | ||
| 68 | extra: []//启动参数 | ||
| 69 | } | ||
| 70 | |||
| 71 | /** | ||
| 72 | * 通用工具 | ||
| 73 | */ | ||
| 74 | const CommonUtil = { | ||
| 75 | initParam: function () { | ||
| 76 | const that = this; | ||
| 77 | that.printLn(JSON.stringify(BaseAppConfig)); | ||
| 78 | if (that.checkAuthFunc()) { | ||
| 79 | ExtraParam.EPGDomain = Authentication.CTCGetConfig("EPGDomain"); | ||
| 80 | that.printLn("EPGDomain="+EPGDomain); | ||
| 81 | ExtraParam.UserToken = Authentication.CTCGetConfig("accessToken"); | ||
| 82 | ExtraParam.EPGGroup = Authentication.CTCGetConfig("EPGGroupNMB"); | ||
| 83 | ExtraParam.ChannelList = Authentication.CTCGetConfig("Channel"); | ||
| 84 | let sjParams = { | ||
| 85 | userId:that.getUrlParams('userId') || that.getCookie('userId') || '', | ||
| 86 | platformAccount:that.getPlatformAccount(), | ||
| 87 | } | ||
| 88 | let params = {sj:sjParams}; | ||
| 89 | ExtraParam.JSAppParam = JSON.stringify(params); | ||
| 90 | } | ||
| 91 | }, | ||
| 92 | getUrlParams:function (name){ | ||
| 93 | const that = this; | ||
| 94 | let url = window.location.href.split('?')[1] || ''; | ||
| 95 | let obj = {}; // 声明参数对象 | ||
| 96 | let arr = url.split('&') ;// 以&符号分割为数组 | ||
| 97 | for (let i = 0; i < arr.length; i++) { | ||
| 98 | let arrNew = arr[i].split('='); // 以"="分割为数组 | ||
| 99 | obj[arrNew[0]] = arrNew[1]; | ||
| 100 | } | ||
| 101 | return obj[name]; | ||
| 102 | }, | ||
| 103 | getCookie:function (name){ | ||
| 104 | const that = this; | ||
| 105 | const value = name.replace(/([\.$?*|{}\(\)\[\]\\\/\+^])/g, '\\$1'); | ||
| 106 | let regex = new RegExp('(^| )' + value + '=([^;]*)'); | ||
| 107 | let result = regex.exec(document.cookie); | ||
| 108 | return result ? decodeURIComponent(result[2]) : null; | ||
| 109 | }, | ||
| 110 | getPlatformAccount() { | ||
| 111 | const that = this; | ||
| 112 | let platformAccount = | ||
| 113 | top.userId || | ||
| 114 | that.getUrlParams('platformAccount') || | ||
| 115 | that.getUrlParams('platformUserId') || | ||
| 116 | that.getCookie('platformAccount') || | ||
| 117 | that.getCookie('platformUserId') || | ||
| 118 | 'topdraw' | ||
| 119 | return platformAccount | ||
| 120 | }, | ||
| 121 | checkAuthFunc: function () { | ||
| 122 | const that = this; | ||
| 123 | if (typeof Authentication === 'undefined') { | ||
| 124 | console.log("this webview isn't support Authentication!"); | ||
| 125 | that.printLn("this webview isn't support Authentication!"); | ||
| 126 | return false; | ||
| 127 | } | ||
| 128 | that.printLn("check Authentication success!"); | ||
| 129 | return true; | ||
| 130 | }, | ||
| 131 | checkSTBFunc: function () { | ||
| 132 | const that = this; | ||
| 133 | if (typeof STBAppManager === 'undefined') { | ||
| 134 | console.log("this webview isn't support STBAppManger!"); | ||
| 135 | that.printLn("this webview isn't support STBAppManger!"); | ||
| 136 | return false; | ||
| 137 | } | ||
| 138 | that.printLn("check STBAppManger success!"); | ||
| 139 | return true; | ||
| 140 | }, | ||
| 141 | checkFileIsExists:function (url){ | ||
| 142 | const that = this; | ||
| 143 | return fetch(url) | ||
| 144 | .then(response => { | ||
| 145 | if (response.status === 200) { | ||
| 146 | that.printLn('文件存在'); | ||
| 147 | return true; | ||
| 148 | } else { | ||
| 149 | that.printLn('文件不存在或请求出错'); | ||
| 150 | return false; | ||
| 151 | } | ||
| 152 | }) | ||
| 153 | .catch(error => { | ||
| 154 | that.printLn('请求出错:', error); | ||
| 155 | return false; | ||
| 156 | }); | ||
| 157 | }, | ||
| 158 | printLn: function (string) { | ||
| 159 | document.getElementById("console").innerHTML += `<a>${string}</a><br>`; | ||
| 160 | } | ||
| 161 | }; | ||
| 162 | |||
| 163 | /** | ||
| 164 | * 启动模块 | ||
| 165 | */ | ||
| 166 | const StartUpHelper = { | ||
| 167 | /** | ||
| 168 | * 启动epg home | ||
| 169 | */ | ||
| 170 | goEpgHome: function () { | ||
| 171 | if (CommonUtil.checkAuthFunc()) { | ||
| 172 | if (ExtraParam.EPGDomain.length > 0) { | ||
| 173 | window.open(ExtraParam.EPGDomain); | ||
| 174 | } | ||
| 175 | } | ||
| 176 | }, | ||
| 177 | /** | ||
| 178 | * 启动基座 | ||
| 179 | */ | ||
| 180 | goBaseJsApp: function () { | ||
| 181 | const that = this; | ||
| 182 | if (CommonUtil.checkSTBFunc()) { | ||
| 183 | const extraArray = Object.keys(ExtraParam).map(key => ({ | ||
| 184 | name: key, | ||
| 185 | value: ExtraParam[key] | ||
| 186 | })); | ||
| 187 | StartUpParam.extra = extraArray; | ||
| 188 | CommonUtil.printLn("启动参数="+JSON.stringify(StartUpParam)); | ||
| 189 | STBAppManager.startAppByIntent(JSON.stringify(StartUpParam)); | ||
| 190 | } | ||
| 191 | } | ||
| 192 | }; | ||
| 193 | /** | ||
| 194 | *升级模块 | ||
| 195 | */ | ||
| 196 | const UpdateHelper = { | ||
| 197 | Common: { | ||
| 198 | errorCount: 3,//安装错误阈值 | ||
| 199 | timeout: 60,//安装后检测状态超时 单位s | ||
| 200 | }, | ||
| 201 | /** | ||
| 202 | * 判断基座是否需要安装或者升级 | ||
| 203 | */ | ||
| 204 | needUpdateBaseApp: function () { | ||
| 205 | const that = this; | ||
| 206 | if (CommonUtil.checkSTBFunc()) { | ||
| 207 | if (that.isAppExists(BaseAppConfig.packageName)) { | ||
| 208 | isBaseAppExists = true; | ||
| 209 | CommonUtil.printLn("基座已安装"); | ||
| 210 | if (BaseAppConfig.force !== 1) {//不强制则不升级 | ||
| 211 | return false; | ||
| 212 | } | ||
| 213 | let versionName = STBAppManager.getAppVersion(BaseAppConfig.packageName); | ||
| 214 | let versionCode = that.getVersionCode(versionName); | ||
| 215 | CommonUtil.printLn("versionCode = "+versionCode); | ||
| 216 | CommonUtil.printLn("BaseAppConfig versionCode = "+BaseAppConfig.versionCode); | ||
| 217 | return BaseAppConfig.versionCode > versionCode; | ||
| 218 | } | ||
| 219 | } | ||
| 220 | return false; | ||
| 221 | }, | ||
| 222 | /** | ||
| 223 | * 升级基座 | ||
| 224 | * @param callback 安装结果回调 | ||
| 225 | */ | ||
| 226 | updateBaseApp: function (callback) { | ||
| 227 | const that = this; | ||
| 228 | if (CommonUtil.checkSTBFunc()) { | ||
| 229 | if (tryCount > that.Common.errorCount) { | ||
| 230 | CommonUtil.printLn("install failed more than 3 times!"); | ||
| 231 | callback(false); | ||
| 232 | return; | ||
| 233 | } | ||
| 234 | if (!CommonUtil.checkFileIsExists(BaseAppConfig.path)) { | ||
| 235 | callback(false); | ||
| 236 | return; | ||
| 237 | } | ||
| 238 | STBAppManager.installApp(BaseAppConfig.path); | ||
| 239 | const checkInstallStatusInterval = setInterval(function () { | ||
| 240 | CommonUtil.printLn("安装检查 time="+times); | ||
| 241 | if (times > that.Common.timeout) {//安装超时 | ||
| 242 | clearInterval(checkInstallStatusInterval); | ||
| 243 | if (tryCount < that.Common.errorCount) {//安装次数未超过阈值 | ||
| 244 | tryCount++; | ||
| 245 | STBAppManager.installApp(BaseAppConfig.path); | ||
| 246 | return; | ||
| 247 | } | ||
| 248 | callback(false); | ||
| 249 | return; | ||
| 250 | } | ||
| 251 | if (!isBaseAppExists) {//之前没安装过 | ||
| 252 | if (that.isAppExists(BaseAppConfig.packageName)) { | ||
| 253 | clearInterval(checkInstallStatusInterval); | ||
| 254 | callback(true); | ||
| 255 | return; | ||
| 256 | } | ||
| 257 | } else { | ||
| 258 | let versionName = STBAppManager.getAppVersion(BaseAppConfig.packageName); | ||
| 259 | CommonUtil.printLn("已安装包版本信息="+that.getVersionCode(versionName)); | ||
| 260 | CommonUtil.printLn("安装包版本信息="+BaseAppConfig.versionCode); | ||
| 261 | if (that.getVersionCode(versionName) === BaseAppConfig.versionCode) { | ||
| 262 | clearInterval(checkInstallStatusInterval); | ||
| 263 | callback(true); | ||
| 264 | return; | ||
| 265 | } | ||
| 266 | } | ||
| 267 | times++; | ||
| 268 | }, 1000); | ||
| 269 | } | ||
| 270 | }, | ||
| 271 | /** | ||
| 272 | * 判断应用是否存在 | ||
| 273 | * @param packageName | ||
| 274 | */ | ||
| 275 | isAppExists: function (packageName) { | ||
| 276 | const that = this; | ||
| 277 | if (CommonUtil.checkSTBFunc()) { | ||
| 278 | return (STBAppManager.checkPackage(packageName) || STBAppManager.isAppInstalled(packageName)); | ||
| 279 | } | ||
| 280 | }, | ||
| 281 | /** | ||
| 282 | * 转化versionName 为 versionCode | ||
| 283 | * @param versionName | ||
| 284 | */ | ||
| 285 | getVersionCode: function (versionName) { | ||
| 286 | const regex = /^\d{1,2}\.\d{1,2}\.\d{1,2}$/; | ||
| 287 | if (!regex.test(versionName)) { | ||
| 288 | return 0; | ||
| 289 | } | ||
| 290 | let numArr = versionName.split("."); | ||
| 291 | return Number(numArr[0] * 10000 + numArr[1] * 100 + numArr[2]); | ||
| 292 | } | ||
| 293 | }; | ||
| 294 | |||
| 295 | window.addEventListener("load",function () { | ||
| 296 | CommonUtil.printLn("页面加载完成"); | ||
| 297 | CommonUtil.initParam(); | ||
| 298 | if (UpdateHelper.needUpdateBaseApp()) { | ||
| 299 | CommonUtil.printLn("需要升级"); | ||
| 300 | tryCount = 0; | ||
| 301 | times = 0; | ||
| 302 | UpdateHelper.updateBaseApp(status => { | ||
| 303 | if (!status) { | ||
| 304 | if (isBaseAppExists) { | ||
| 305 | StartUpHelper.goBaseJsApp(); | ||
| 306 | return | ||
| 307 | } | ||
| 308 | StartUpHelper.goEpgHome(); | ||
| 309 | return; | ||
| 310 | } | ||
| 311 | StartUpHelper.goBaseJsApp(); | ||
| 312 | }); | ||
| 313 | return; | ||
| 314 | } | ||
| 315 | CommonUtil.printLn("不需要升级"); | ||
| 316 | StartUpHelper.goBaseJsApp(); | ||
| 317 | }); | ||
| 318 | </script> | ||
| 319 | </body> | ||
| 320 | </html> |
readme.md
0 → 100644
-
Please register or sign in to post a comment