cshenfu
2024-06-27 58aa6e282bb343503be3cb198948328ea3ed7b79
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
var updateManager = {};
/**
 * 本API返回全局唯一的版本更新管理器对象: updateManager,用于管理小程序更新。
 * vk.updateManager.updateReady();
 */
updateManager.updateReady = function(obj) {
    // #ifdef MP
    updateManagerByMP(obj);
    // #endif
};
 
 
export default updateManager;
 
 
function updateManagerByMP (obj={}){
    let {
        title = "更新提示",
        content = "新版本已经准备好,点击更新!",
        autoUpdate = true,
        showCancel = false,
        confirmText = "一键更新"
    } = obj;
    const updateManager = uni.getUpdateManager();
    
    updateManager.onCheckForUpdate(function (res) {
        // 请求完新版本信息的回调
        // console.log(res.hasUpdate);
    });
    
    updateManager.onUpdateReady(function (res) {
        uni.showModal({
            title,
            content,
            showCancel,
            confirmText,
            success(res) {
                if (res.confirm) {
                    if(typeof obj.success === "function"){
                        obj.success({
                            applyUpdate : updateManager.applyUpdate
                        });
                    }
                    if(typeof obj.complete === "function") obj.complete();
                    if(autoUpdate){
                        // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
                        updateManager.applyUpdate();
                    }
                }
            },
            
        });
    });
    
    updateManager.onUpdateFailed(function (res) {
        // 新的版本下载失败
        console.error("onUpdateFailed",res);
        if(typeof obj.fail === "function") obj.fail(res);
        if(typeof obj.complete === "function") obj.complete();
    });
}