/
HotUpdateTest.js
379 lines (335 loc) · 14.9 KB
/
HotUpdateTest.js
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
import {
Platform,
} from 'react-native';
import {Tools} from "./Tools";
import {Alert} from "./Alert";
import RNFS from "react-native-fs";
import {
packageVersion,
currentVersion,
mainBundleFilePath,
HotUpdate,
build,
} from "react-native-update-js";
import DeviceInfo from "react-native-device-info";
import {LocalStorage} from "./LocalStorage";
import {ProgressPerApi} from "./ProgressPerApi";
HotUpdate.tag = "lx_yyt";//热更新的标志 与后台配置一致
HotUpdate.host = "http://yyt.yyy.com:8081/XXX/XXX";//数据请求接口
/**
* 热更新,提供热更新各种方法,自己配置服务器
* **/
export class HotUpdateCus{
static appID = null;//当前给app指定(分配)的id
static wwwDownloadDir = Platform.OS == "ios"
? `${RNFS.DocumentDirectoryPath}/wwwRoot`
: `${RNFS.ExternalStorageDirectoryPath}/wwwRoot`;//下载目录
static update = {
code1:777,//777、立刻更新;888、立刻强制更新;999、立刻静默更新
code2:888,//777、立刻更新;888、立刻强制更新;999、立刻静默更新
code3:999,//777、立刻更新;888、立刻强制更新;999、立刻静默更新
reboot1:555,//666、强制使用更新;555、用户决定是否使用更新;333、下次启用更新 默认是555
reboot2:666,//666、强制使用更新;555、用户决定是否使用更新;333、下次启用更新 默认是555
reboot3:333,//666、强制使用更新;555、用户决定是否使用更新;333、下次启用更新 默认是555
execute:false,//是否监听更新
version:currentVersion,//当前已更新的版本号
}
static timer = null;
/**
* 持续检测是否有更新
* **/
static checkUpdateLoop(){
setInterval(()=>{
if(HotUpdateCus.update.execute){
// console.info("HotUpdate","HotUpdate");
HotUpdateCus.checkUpdate();
}
},10000);
}
/**
* 检查更新
* @Param cd func,//回调函数
* @Param cdUpdate func,//更新回调函数
* **/
static checkUpdate = (cd,cdUpdate) => {
if(true){
HotUpdate.checkUpdate()
.then(info=>{
// alert(JSON.stringify(info))
info.metaInfo.code = typeof info.metaInfo.code == 'number'
? info.metaInfo.code
: HotUpdateCus.update.code1;
info.metaInfo.reboot = typeof info.metaInfo.reboot == 'number'
? info.metaInfo.reboot
: HotUpdateCus.update.reboot1;
if(build == null){
info.metaInfo.code = 888;
info.metaInfo.reboot = 666;
}
if (info.expired) {
cdUpdate&&cdUpdate();
if(HotUpdateCus.isHasUpdate(info)){
HotUpdateCus.update.execute = false;
switch (info.metaInfo.code)
{
case HotUpdateCus.update.reboot1:{
Alert.alert('检查到新的静态版本'+info.packageVersion+'\n是否下载?',
info.description, [
{text: '确定', onPress: ()=>{
HotUpdateCus.updateDelay(false);
HotUpdate.downloadUpdate();
}
},
{text: '取消', onPress: ()=>{
cd&&cd();
HotUpdateCus.updateDelay();
}
},
]);
break;
}
default:{
HotUpdateCus.updateDelay(false);
HotUpdate.downloadUpdate();
break;
}
}
}
}
else if(info.update){
HotUpdateCus.checkHasUpate(info,(info)=>{
info.metaInfo = info.metaInfo ? info.metaInfo : {};
info.metaInfo.code = typeof info.metaInfo.code == 'number'
? info.metaInfo.code
: HotUpdateCus.update.code1;
info.metaInfo.reboot = typeof info.metaInfo.reboot == 'number'
? info.metaInfo.reboot
: HotUpdateCus.update.reboot1;
// info.metaInfo.code = 888;
// info.metaInfo.reboot = 666;
// if(!HotUpdateCus.update.version){
if(build == null){
info.metaInfo.code = 888;
info.metaInfo.reboot = 666;
}
HotUpdateCus.update.execute = false;
switch (info.metaInfo.code)
{
case HotUpdateCus.update.code1: {
cdUpdate&&cdUpdate();
if(build < info.build){
Alert.alert('检查到新的版本'+info.version+'\n是否下载?',
info.description, [
{text: '是', onPress: ()=>{
HotUpdateCus.doUpdate(info,cd,info.metaInfo.reboot);
}},
{text: '否', onPress:()=>{
HotUpdateCus.updateDelay();
cd&&cd();
}
},
]);
}
break;
}
case HotUpdateCus.update.code2:{
cdUpdate&&cdUpdate();
HotUpdateCus.doUpdate(info,cd,info.metaInfo.reboot);
break;
}
case HotUpdateCus.update.code3:{
if(info.metaInfo.reboot !== HotUpdateCus.update.reboot3){
cdUpdate&&cdUpdate();
}
HotUpdateCus.doUpdate(info,cd,info.metaInfo.reboot);
break;
}
}
},cd);
}
});
}
};
/**
* 是否有更新版本
* @prama info json;//后台返回的数据
* @prama resolve func;//有更新时的回调函数 回传与info一样的数据格式
* @prama reject func;//没有更新时的回调函数 回传与info一样的数据格式
* @prama index int;info.publishJS的下标 可不传
* **/
static checkHasUpate(info,resolve:Function,reject:Function,index=0){
if(info.build > build){
if(this.isHasUpdate(info)){
resolve&&resolve(info);
}
else
{
if(++index < info.publishJS.length){
let updateInfo = Object.assign({},info,info.publishJS[index]);
this.checkHasUpate(updateInfo,resolve,reject,index);
}
else
{
reject&&reject();
}
}
}
else
{
reject&&reject();
}
}
/**
* 是否有更新版本
* @prama info json;//后台返回的数据
* return boolean;//有:返回true,反之false
* **/
static isHasUpdate(info){
let update = false;
if(info.metaInfo.updateList){
info.metaInfo.updateList.forEach((v)=>{
if(this.appID == v){
update = true;
}
});
}
else {
//更新全部
update = true;
}
if(info.metaInfo.updateNoList){
info.metaInfo.updateNoList.forEach((v)=>{
if(this.appID == v){
update = false;
}
});
}
return update;
}
/**
* 更新延迟
* @parma toast bool,//是否提示信息
* **/
static updateDelay(toast=true){
if(!HotUpdateCus.timer){
toast?Tools.toast("更新询问延迟1分钟!"):null;
HotUpdateCus.timer = setTimeout(()=>{
HotUpdateCus.update.execute = true;
HotUpdateCus.timer = null;
},60000);
}
}
/**
* 更新应用
* @Param cd func,//回调函数
* **/
static doUpdate = (info,cd,reboot) =>{
HotUpdate.downloadUpdate(info,(per)=>{
ProgressPerApi.show(per);
})
.then(info => {
ProgressPerApi.hide();
LocalStorage.save(Tools.app_config.versionkey,
{
version:info.version,
rnUpdate:false
})
.then((dataSave)=>{
switch (reboot)
{
case HotUpdateCus.update.reboot1:{
Alert.alert('提示', '下载完毕,是否重启应用?', [
{text: '是', onPress: ()=>{
HotUpdate.setPreferData("rnUpdate","false");
Tools.cutLogin = true;
if(!Tools.isCurStruct){
LocalStorage.save(DeviceInfo.getVersion(),
DeviceInfo.getVersion())
.then((dataSave)=>{
HotUpdate.doUpdate(info);
});
}
else
{
HotUpdate.doUpdate(info);
}
}},
{text: '否', onPress:()=>{
LocalStorage.save(Tools.app_config.versionkey,
{
version:Tools.app_config.version,
rnUpdate:false
});
HotUpdateCus.updateDelay();
cd&&cd();
}
},
{text: '下次启动时更新', onPress: ()=>{
HotUpdate.setPreferData("rnUpdate","false");
HotUpdateCus.update.version = info.version;
HotUpdateCus.update.execute = true;
if(!Tools.isCurStruct){
LocalStorage.save(DeviceInfo.getVersion(),
DeviceInfo.getVersion())
.then((dataSave)=>{
HotUpdate.doUpdate(info,false);
cd&&cd();
});
}
else
{
HotUpdate.doUpdate(info,false);
cd&&cd();
}
}
},
]);
break;
}
case HotUpdateCus.update.reboot2:{
HotUpdate.setPreferData("rnUpdate","false");
Tools.cutLogin = true;
if(!Tools.isCurStruct){
LocalStorage.save(DeviceInfo.getVersion(),
DeviceInfo.getVersion())
.then((dataSave)=>{
HotUpdate.doUpdate(info);
});
}
else
{
HotUpdate.doUpdate(info);
}
break;
}
case HotUpdateCus.update.reboot3:{
HotUpdate.setPreferData("rnUpdate","false");
if(info.metaInfo.finishInfo){
Alert.alert("更新完成",info.metaInfo.finishInfo+"");
}
HotUpdateCus.update.version = info.version;
HotUpdateCus.update.execute = true;
if(!Tools.isCurStruct){
LocalStorage.save(DeviceInfo.getVersion(),
DeviceInfo.getVersion())
.then((dataSave)=>{
cd&&cd();
HotUpdate.doUpdate(info,false);
});
}
else
{
cd&&cd();
HotUpdate.doUpdate(info,false);
}
break;
}
}
});
}).catch(err => {
// Tools.toast('更新失败!');
cd&&cd();
});
}
}
// RNFS.mkdir(HotUpdateCus.wwwDownloadDir);