JsAppStartUp.html
11.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
<!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>