Skip to content

Commit 1ea0fbb

Browse files
committed
chore(Example): 新增启动时检查新版本的功能
1 parent 234f070 commit 1ea0fbb

2 files changed

Lines changed: 41 additions & 3 deletions

File tree

examples/app.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
//app.js
1+
import versionUtil from './utils/version-util'
22
App({
33
onLaunch: function() {
4-
4+
// 检查更新
5+
versionUtil.checkUpdate()
56
},
67

78
globalData: {
89

910
}
10-
});
11+
});

examples/utils/version-util.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// 小程序启动时检查版本
2+
class VersionUtil {
3+
/**
4+
* 检查更新
5+
*/
6+
checkUpdate(){
7+
const updateManager = wx.getUpdateManager()
8+
updateManager.onCheckForUpdate((hasUpdate)=>{
9+
if(hasUpdate){
10+
updateManager.onUpdateReady(()=>{
11+
wx.showModal({
12+
title:'更新提示',
13+
content:'有新版本啦!要更新看看吗',
14+
success(res){
15+
if(res.confirm){
16+
updateManager.applyUpdate()
17+
}
18+
}
19+
})
20+
})
21+
22+
updateManager.onUpdateFailed(function () {
23+
// 新版本下载失败
24+
wx.showModal({
25+
title: '更新提示',
26+
content: '有新版本啦!删除当前小程序,重新打开就能更新啦!'
27+
})
28+
})
29+
}
30+
})
31+
32+
33+
}
34+
}
35+
36+
const versionUtil = new VersionUtil()
37+
export default versionUtil

0 commit comments

Comments
 (0)