app得版本檢查更新

1 使用方式

import checkAppUpdate from '@/common/utils/appUpdate.js';

setTimeout(() => {
     checkAppUpdate(false);
 }, 1000);

2 源碼

// #ifdef APP-PLUS 
import componentConfig from "@/common/service/config.service.js"
const platform = uni.getSystemInfoSync().platform;

// ========== 1.項(xiàng)目專屬配置 ==========
const $mainColor = componentConfig.appUpdateColor ? componentConfig.appUpdateColor : "#FF5B78";
const $iconUrl = componentConfig.appUpdateIcon ? componentConfig.appUpdateIcon : "/static/ic_ar.png";
const APPID = '__UNI__E50E9FE';
const BASE_API_URL = componentConfig.apiUrl || 'https://你的后端域名/';

// 獲取當(dāng)前應(yīng)用的版本號(hào)
export const getCurrentNo = function(callback) {
    plus.runtime.getProperty(plus.runtime.appid, function(inf) {
        callback && callback({
            versionCode: inf.versionCode,
            versionName: inf.version
        });
    });
}

// ========== 2. 從服務(wù)器下載應(yīng)用資源包 ==========
const getDownload = function(data) {
    let dtask;
    if (data.updateType == 'forcibly' || data.updateType == 'solicit') {
        let popupData = {
            progress: true,
            buttonNum: 2
        };
        if (data.updateType == 'forcibly') {
            popupData.buttonNum = 0; // 強(qiáng)制更新時(shí)下載彈窗無按鈕
        }
        let lastProgressValue = 0;
        let popupObj = downloadPopup(popupData);

        const fullDownloadUrl = data.downloadUrl.startsWith('http') ? data.downloadUrl :
            `${BASE_API_URL}${data.downloadUrl}`;
        console.info("下載地址===" + fullDownloadUrl)
        dtask = plus.downloader.createDownload(fullDownloadUrl, {
            filename: "_doc/update/"
        }, function(download, status) {
            if (status == 200) {
                popupObj.change({
                    progressValue: 100,
                    progressTip: "正在安裝文件...",
                    progress: true,
                    buttonNum: 0
                });
                // 安裝APK/WGT
                plus.runtime.install(download.filename, {}, function() {
                    popupObj.change({
                        contentText: "應(yīng)用資源更新完成!",
                        buttonNum: 1,
                        progress: false
                    });
                }, function(e) {
                    popupObj.cancel();
                    plus.nativeUI.alert("安裝文件失敗[" + e.code + "]:" + e.message);
                    // 強(qiáng)制更新安裝失敗后,退出App
                    if (data.updateType == 'forcibly') {
                        plus.runtime.quit();
                    }
                });
            } else {
                popupObj.change({
                    contentText: "文件下載失敗...",
                    buttonNum: 1,
                    progress: false
                });
                // 強(qiáng)制更新下載失敗后,退出App
                if (data.updateType == 'forcibly') {
                    plus.runtime.quit();
                }
            }
        });
        dtask.start();
        dtask.addEventListener("statechanged", function(task, status) {
            switch (task.state) {
                case 1:
                    popupObj.change({
                        progressValue: 0,
                        progressTip: "準(zhǔn)備下載...",
                        progress: true
                    });
                    break;
                case 2:
                    popupObj.change({
                        progressValue: 0,
                        progressTip: "開始下載...",
                        progress: true
                    });
                    break;
                case 3:
                    const progress = parseInt(task.downloadedSize / task.totalSize * 100);
                    if (progress - lastProgressValue >= 2) {
                        lastProgressValue = progress;
                        popupObj.change({
                            progressValue: progress,
                            progressTip: "已下載" + progress + "%",
                            progress: true
                        });
                    }
                    break;
            }
        });
        // 強(qiáng)制更新時(shí)禁用取消下載
        popupObj.cancelDownload = function() {
            if (data.updateType != 'forcibly') {
                dtask && dtask.abort();
                uni.showToast({
                    title: "已取消下載",
                    icon: "none"
                });
            }
        }
        popupObj.reboot = function() {
            plus.runtime.restart();
        }
    } else if (data.updateType == "silent") {
        const fullDownloadUrl = data.downloadUrl.startsWith('http') ? data.downloadUrl :
            `${BASE_API_URL}${data.downloadUrl}`;

        dtask = plus.downloader.createDownload(fullDownloadUrl, {
            filename: "_doc/update/"
        }, function(download, status) {
            if (status == 200) {
                plus.runtime.install(download.filename, {}, function() {
                    console.log("應(yīng)用資源更新完成");
                }, function(e) {
                    plus.nativeUI.alert("安裝文件失敗[" + e.code + "]:" + e.message);
                });
            } else {
                plus.nativeUI.alert("文件下載失敗...");
            }
        });
        dtask.start();
    }
}

// ========== 3. 文字換行處理 ==========
function drawtext(text, maxWidth) {
    let textArr = text.split("");
    let len = textArr.length;
    let previousNode = 0;
    let nodeWidth = 0;
    let rowText = [];
    let letterWidth = 0;
    let chineseWidth = 14;
    let otherWidth = 7;
    for (let i = 0; i < len; i++) {
        if (/[\u4e00-\u9fa5]|[\uFE30-\uFFA0]/g.test(textArr[i])) {
            if (letterWidth > 0) {
                if (nodeWidth + chineseWidth + letterWidth * otherWidth > maxWidth) {
                    rowText.push({
                        type: "text",
                        content: text.substring(previousNode, i)
                    });
                    previousNode = i;
                    nodeWidth = chineseWidth;
                    letterWidth = 0;
                } else {
                    nodeWidth += chineseWidth + letterWidth * otherWidth;
                    letterWidth = 0;
                }
            } else {
                if (nodeWidth + chineseWidth > maxWidth) {
                    rowText.push({
                        type: "text",
                        content: text.substring(previousNode, i)
                    });
                    previousNode = i;
                    nodeWidth = chineseWidth;
                } else {
                    nodeWidth += chineseWidth;
                }
            }
        } else {
            if (/\n/g.test(textArr[i])) {
                rowText.push({
                    type: "break",
                    content: text.substring(previousNode, i)
                });
                previousNode = i + 1;
                nodeWidth = 0;
                letterWidth = 0;
            } else if (textArr[i] == "\\" && textArr[i + 1] == "n") {
                rowText.push({
                    type: "break",
                    content: text.substring(previousNode, i)
                });
                previousNode = i + 2;
                nodeWidth = 0;
                letterWidth = 0;
            } else if (/[a-zA-Z0-9]/g.test(textArr[i])) {
                letterWidth += 1;
                if (nodeWidth + letterWidth * otherWidth > maxWidth) {
                    rowText.push({
                        type: "text",
                        content: text.substring(previousNode, i + 1 - letterWidth)
                    });
                    previousNode = i + 1 - letterWidth;
                    nodeWidth = letterWidth * otherWidth;
                    letterWidth = 0;
                }
            } else {
                if (nodeWidth + otherWidth > maxWidth) {
                    rowText.push({
                        type: "text",
                        content: text.substring(previousNode, i)
                    });
                    previousNode = i;
                    nodeWidth = otherWidth;
                } else {
                    nodeWidth += otherWidth;
                }
            }
        }
    }
    if (previousNode < len) {
        rowText.push({
            type: "text",
            content: text.substring(previousNode, len)
        });
    }
    return rowText;
}

// ========== 4. 是否更新彈窗(修復(fù) removeEventListener 錯(cuò)誤) ==========
function updatePopup(data, callback) {
    // 彈窗遮罩層
    let maskLayer = new plus.nativeObj.View("maskLayer", {
        top: '0px',
        left: '0px',
        height: '100%',
        width: '100%',
        backgroundColor: 'rgba(0,0,0,0.5)'
    });

    const screenWidth = plus.screen.resolutionWidth;
    const screenHeight = plus.screen.resolutionHeight;
    const popupViewWidth = screenWidth * 0.7;
    const viewContentPadding = 20;
    const viewContentWidth = parseInt(popupViewWidth - (viewContentPadding * 2));
    const descriptionList = drawtext(data.updateContent || data.versionInfo, viewContentWidth);
    let popupViewHeight = 80 + 20 + 20 + 90 + 10;

    let popupViewContentList = [{
            src: $iconUrl,
            id: "logo",
            tag: "img",
            position: {
                top: "0px",
                left: (popupViewWidth - 124) / 2 + "px",
                width: "124px",
                height: "80px",
            }
        },
        {
            tag: 'font',
            id: 'title',
            text: "發(fā)現(xiàn)新版本" + (data.versionName || data.version),
            textStyles: {
                size: '18px',
                color: "#333",
                weight: "bold",
                whiteSpace: "normal"
            },
            position: {
                top: '90px',
                left: viewContentPadding + "px",
                width: viewContentWidth + "px",
                height: "30px",
            }
        }
    ];
    const textHeight = 18;
    let contentTop = 130;
    descriptionList.forEach((item, index) => {
        if (index > 0) {
            popupViewHeight += textHeight;
            contentTop += textHeight;
        }
        popupViewContentList.push({
            tag: 'font',
            id: 'content' + index + 1,
            text: item.content,
            textStyles: {
                size: '14px',
                color: "#666",
                lineSpacing: "50%",
                align: "left"
            },
            position: {
                top: contentTop + "px",
                left: viewContentPadding + "px",
                width: viewContentWidth + "px",
                height: textHeight + "px",
            }
        });
        if (item.type == "break") {
            contentTop += 10;
            popupViewHeight += 10;
        }
    });

    // 強(qiáng)制更新彈窗(立即升級(jí) + 退出App)
    if (data.updateType == "forcibly" || data.isForce) {
        // 增加彈窗高度(容納退出按鈕)
        popupViewHeight += viewContentPadding;

        // 退出App按鈕
        popupViewContentList.push({
            tag: 'rect',
            rectStyles: {
                radius: "6px",
                color: "#999999"
            },
            position: {
                bottom: viewContentPadding + 'px',
                left: viewContentPadding + "px",
                left: viewContentPadding + "px",
                width: (viewContentWidth - viewContentPadding) / 2 + "px",
                height: "30px"
            }
        });
        popupViewContentList.push({
            tag: 'font',
            id: 'quitText',
            text: "退出應(yīng)用",
            textStyles: {
                size: '14px',
                color: "#FFF",
                lineSpacing: "0%",
            },
            position: {
                bottom: viewContentPadding + 'px',

                left: viewContentPadding + "px",
                width: (viewContentWidth - viewContentPadding) / 2 + "px",
                height: "30px"
            }
        });


        // 立即升級(jí)按鈕
        popupViewContentList.push({
            tag: 'rect',
            rectStyles: {
                radius: "6px",
                color: $mainColor
            },
            position: {
                bottom: (viewContentPadding) + 'px',

                left: ((viewContentWidth - viewContentPadding) / 2 + viewContentPadding * 2) + "px",
                width: (viewContentWidth - viewContentPadding) / 2 + "px",
                height: "30px"
            }
        });
        popupViewContentList.push({
            tag: 'font',
            id: 'confirmText',
            text: "立即升級(jí)",
            textStyles: {
                size: '14px',
                color: "#FFF",
                lineSpacing: "0%",
            },
            position: {
                bottom: (viewContentPadding) + 'px',
                left: ((viewContentWidth - viewContentPadding) / 2 + viewContentPadding * 2) + "px",
                width: (viewContentWidth - viewContentPadding) / 2 + "px",
                height: "30px"
            }
        });

    } else {
        // 非強(qiáng)制更新保留原有邏輯
        popupViewContentList.push({
            tag: 'rect',
            id: 'cancelBox',
            rectStyles: {
                radius: "3px",
                borderColor: "#f1f1f1",
                borderWidth: "1px",
            },
            position: {
                bottom: viewContentPadding + 'px',
                left: viewContentPadding + "px",
                width: (viewContentWidth - viewContentPadding) / 2 + "px",
                height: "30px",
            }
        });
        popupViewContentList.push({
            tag: 'rect',
            id: 'confirmBox',
            rectStyles: {
                radius: "3px",
                color: $mainColor,
            },
            position: {
                bottom: viewContentPadding + 'px',
                left: ((viewContentWidth - viewContentPadding) / 2 + viewContentPadding * 2) + "px",
                width: (viewContentWidth - viewContentPadding) / 2 + "px",
                height: "30px",
            }
        });
        popupViewContentList.push({
            tag: 'font',
            id: 'cancelText',
            text: "暫不升級(jí)",
            textStyles: {
                size: '14px',
                color: "#666",
                lineSpacing: "0%",
                whiteSpace: "normal"
            },
            position: {
                bottom: viewContentPadding + 'px',
                left: viewContentPadding + "px",
                width: (viewContentWidth - viewContentPadding) / 2 + "px",
                height: "30px",
            }
        });
        popupViewContentList.push({
            tag: 'font',
            id: 'confirmText',
            text: "立即升級(jí)",
            textStyles: {
                size: '14px',
                color: "#FFF",
                lineSpacing: "0%",
                whiteSpace: "normal"
            },
            position: {
                bottom: viewContentPadding + 'px',
                left: ((viewContentWidth - viewContentPadding) / 2 + viewContentPadding * 2) + "px",
                width: (viewContentWidth - viewContentPadding) / 2 + "px",
                height: "30px",
            }
        });
    }

    let popupView = new plus.nativeObj.View("popupView", {
        tag: "rect",
        top: (screenHeight - popupViewHeight) / 2 + "px",
        left: '15%',
        height: popupViewHeight + "px",
        width: "70%"
    });

    popupView.drawRect({
        color: "#FFFFFF",
        radius: "8px"
    }, {
        top: "40px",
        height: popupViewHeight - 40 + "px",
    });

    popupView.draw(popupViewContentList);

    // 彈窗點(diǎn)擊事件
    popupView.addEventListener("click", function(e) {
        let maxTop = popupViewHeight - viewContentPadding;
        let maxLeft = popupViewWidth - viewContentPadding;
        let buttonWidth = (viewContentWidth - viewContentPadding) / 2;

        if (data.updateType == "forcibly" || data.isForce) {

            if (e.clientY > maxTop - 30 && e.clientY < maxTop) { // 點(diǎn)擊退出App
                if (e.clientX > viewContentPadding && e.clientX < maxLeft - buttonWidth - viewContentPadding) {
                    maskLayer.hide();
                    popupView.hide();
                    plus.runtime.quit(); // 退出應(yīng)用
                } else if (e.clientX > maxLeft - buttonWidth && e.clientX < maxLeft) { // 點(diǎn)擊立即升級(jí)
                    maskLayer.hide();
                    popupView.hide();
                    callback && callback();
                }
            }

        } else {
            // 非強(qiáng)制更新原有邏輯
            if (e.clientY > maxTop - 30 && e.clientY < maxTop) {
                if (e.clientX > viewContentPadding && e.clientX < maxLeft - buttonWidth - viewContentPadding) {
                    maskLayer.hide();
                    popupView.hide();
                } else if (e.clientX > maxLeft - buttonWidth && e.clientX < maxLeft) {
                    maskLayer.hide();
                    popupView.hide();
                    callback && callback();
                }
            }
        }
    });


    // 只有非強(qiáng)制更新(solicit)時(shí),才給遮罩層加點(diǎn)擊關(guān)閉事件
    if (data.updateType == "solicit") {
        maskLayer.addEventListener("click", function() {
            maskLayer.hide();
            popupView.hide();
        });
    }

    maskLayer.show();
    popupView.show();
}

// ========== 5. 下載彈窗繪圖 ==========
function downloadPopupDrawing(data) {
    const screenWidth = plus.screen.resolutionWidth;
    const screenHeight = plus.screen.resolutionHeight;
    const popupViewWidth = screenWidth * 0.7;
    const viewContentPadding = 20;
    const viewContentWidth = popupViewWidth - (viewContentPadding * 2);
    let popupViewHeight = viewContentPadding * 3 + 60;
    let progressTip = data.progressTip || "準(zhǔn)備下載...";
    let contentText = data.contentText || "正在為您更新,請(qǐng)耐心等待";
    let elementList = [{
            tag: 'rect',
            color: '#FFFFFF',
            rectStyles: {
                radius: "8px"
            }
        },
        {
            tag: 'font',
            id: 'title',
            text: "升級(jí)APP",
            textStyles: {
                size: '16px',
                color: "#333",
                weight: "bold",
                verticalAlign: "middle",
                whiteSpace: "normal"
            },
            position: {
                top: viewContentPadding + 'px',
                height: "30px",
            }
        },
        {
            tag: 'font',
            id: 'content',
            text: contentText,
            textStyles: {
                size: '14px',
                color: "#333",
                verticalAlign: "middle",
                whiteSpace: "normal"
            },
            position: {
                top: viewContentPadding * 2 + 30 + 'px',
                height: "20px",
            }
        }
    ];
    if (data.progress) {
        popupViewHeight += viewContentPadding + 40;
        elementList = elementList.concat([{
                tag: 'font',
                id: 'progressValue',
                text: progressTip,
                textStyles: {
                    size: '14px',
                    color: $mainColor,
                    whiteSpace: "normal"
                },
                position: {
                    top: viewContentPadding * 4 + 20 + 'px',
                    height: "30px"
                }
            },
            {
                tag: 'rect',
                id: 'progressBg',
                rectStyles: {
                    radius: "4px",
                    borderColor: "#f1f1f1",
                    borderWidth: "1px",
                },
                position: {
                    top: viewContentPadding * 4 + 60 + 'px',
                    left: viewContentPadding + "px",
                    width: viewContentWidth + "px",
                    height: "8px"
                }
            },
        ]);
    }
    if (data.buttonNum == 2) {
        popupViewHeight += viewContentPadding + 30;
        elementList = elementList.concat([{
                tag: 'rect',
                rectStyles: {
                    radius: "3px",
                    borderColor: "#f1f1f1",
                    borderWidth: "1px",
                },
                position: {
                    bottom: viewContentPadding + 'px',
                    left: viewContentPadding + "px",
                    width: (viewContentWidth - viewContentPadding) / 2 + "px",
                    height: "30px"
                }
            },
            {
                tag: 'rect',
                rectStyles: {
                    radius: "3px",
                    color: $mainColor
                },
                position: {
                    bottom: viewContentPadding + 'px',
                    left: ((viewContentWidth - viewContentPadding) / 2 + viewContentPadding * 2) + "px",
                    width: (viewContentWidth - viewContentPadding) / 2 + "px",
                    height: "30px"
                }
            },
            {
                tag: 'font',
                id: 'cancelText',
                text: "取消下載",
                textStyles: {
                    size: '14px',
                    color: "#666",
                    lineSpacing: "0%",
                    whiteSpace: "normal"
                },
                position: {
                    bottom: viewContentPadding + 'px',
                    left: viewContentPadding + "px",
                    width: (viewContentWidth - viewContentPadding) / 2 + "px",
                    height: "30px",
                }
            },
            {
                tag: 'font',
                id: 'confirmText',
                text: "后臺(tái)下載",
                textStyles: {
                    size: '14px',
                    color: "#FFF",
                    lineSpacing: "0%",
                    whiteSpace: "normal"
                },
                position: {
                    bottom: viewContentPadding + 'px',
                    left: ((viewContentWidth - viewContentPadding) / 2 + viewContentPadding * 2) + "px",
                    width: (viewContentWidth - viewContentPadding) / 2 + "px",
                    height: "30px",
                }
            }
        ]);
    }
    if (data.buttonNum == 1) {
        popupViewHeight += viewContentPadding + 40;
        elementList = elementList.concat([{
                tag: 'rect',
                rectStyles: {
                    radius: "6px",
                    color: $mainColor
                },
                position: {
                    bottom: viewContentPadding + 'px',
                    left: viewContentPadding + "px",
                    width: viewContentWidth + "px",
                    height: "40px"
                }
            },
            {
                tag: 'font',
                id: 'confirmText',
                text: "關(guān)閉",
                textStyles: {
                    size: '14px',
                    color: "#FFF",
                    lineSpacing: "0%",
                },
                position: {
                    bottom: viewContentPadding + 'px',
                    left: viewContentPadding + "px",
                    width: viewContentWidth + "px",
                    height: "40px"
                }
            }
        ]);
    }
    return {
        popupViewHeight: popupViewHeight,
        popupViewWidth: popupViewWidth,
        screenHeight: screenHeight,
        viewContentWidth: viewContentWidth,
        viewContentPadding: viewContentPadding,
        elementList: elementList
    };
}

// ========== 6. 下載彈窗 ==========
function downloadPopup(data) {
    let maskLayer = new plus.nativeObj.View("maskLayer", {
        top: '0px',
        left: '0px',
        height: '100%',
        width: '100%',
        backgroundColor: 'rgba(0,0,0,0.5)'
    });
    let popupViewData = downloadPopupDrawing(data);
    let popupView = new plus.nativeObj.View("popupView", {
        tag: "rect",
        top: (popupViewData.screenHeight - popupViewData.popupViewHeight) / 2 + "px",
        left: '15%',
        height: popupViewData.popupViewHeight + "px",
        width: "70%",
    });
    let progressValue = 0;
    let progressTip = 0;
    let contentText = 0;
    let buttonNum = 2;
    if (data.buttonNum >= 0) {
        buttonNum = data.buttonNum;
    }
    popupView.draw(popupViewData.elementList);
    let callbackData = {
        change: function(res) {
            let progressElement = [];
            if (res.progressValue) {
                progressValue = res.progressValue;
                progressElement.push({
                    tag: 'rect',
                    id: 'progressValueBg',
                    rectStyles: {
                        radius: "4px",
                        color: $mainColor
                    },
                    position: {
                        top: popupViewData.viewContentPadding * 4 + 60 + 'px',
                        left: popupViewData.viewContentPadding + "px",
                        width: popupViewData.viewContentWidth * (res.progressValue / 100) + "px",
                        height: "8px"
                    }
                });
            }
            if (res.progressTip) {
                progressTip = res.progressTip;
                progressElement.push({
                    tag: 'font',
                    id: 'progressValue',
                    text: res.progressTip,
                    textStyles: {
                        size: '14px',
                        color: $mainColor,
                        whiteSpace: "normal"
                    },
                    position: {
                        top: popupViewData.viewContentPadding * 4 + 20 + 'px',
                        height: "30px"
                    }
                });
            }
            if (res.contentText) {
                contentText = res.contentText;
                progressElement.push({
                    tag: 'font',
                    id: 'content',
                    text: res.contentText,
                    textStyles: {
                        size: '16px',
                        color: "#333",
                        whiteSpace: "normal"
                    },
                    position: {
                        top: popupViewData.viewContentPadding * 2 + 30 + 'px',
                        height: "30px",
                    }
                });
            }
            if (res.buttonNum >= 0 && buttonNum != res.buttonNum) {
                buttonNum = res.buttonNum;
                popupView.reset();
                popupViewData = downloadPopupDrawing(Object.assign({
                    progressValue: progressValue,
                    progressTip: progressTip,
                    contentText: contentText,
                }, res));
                let newElement = [];
                popupViewData.elementList.map((item, index) => {
                    let have = false;
                    progressElement.forEach((childItem, childIndex) => {
                        if (item.id == childItem.id) {
                            have = true;
                        }
                    });
                    if (!have) {
                        newElement.push(item);
                    }
                });
                progressElement = newElement.concat(progressElement);
                popupView.setStyle({
                    tag: "rect",
                    top: (popupViewData.screenHeight - popupViewData.popupViewHeight) / 2 + "px",
                    left: '15%',
                    height: popupViewData.popupViewHeight + "px",
                    width: "70%",
                });
                popupView.draw(progressElement);
            } else {
                popupView.draw(progressElement);
            }
        },
        cancel: function() {
            maskLayer.hide();
            popupView.hide();
        }
    }
    //事件點(diǎn)擊
    popupView.addEventListener("click", function(e) {
        let maxTop = popupViewData.popupViewHeight - popupViewData.viewContentPadding;
        let maxLeft = popupViewData.popupViewWidth - popupViewData.viewContentPadding;
        if (e.clientY > maxTop - 40 && e.clientY < maxTop) {
            if (buttonNum == 1) {
                if (e.clientX > popupViewData.viewContentPadding && e.clientX < maxLeft) {
                    maskLayer.hide();
                    popupView.hide();
                    callbackData.reboot();
                }
            } else if (buttonNum == 2) {
                let buttonWidth = (popupViewData.viewContentWidth - popupViewData.viewContentPadding) / 2;
                if (e.clientX > popupViewData.viewContentPadding && e.clientX < maxLeft - buttonWidth -
                    popupViewData.viewContentPadding) {
                    maskLayer.hide();
                    popupView.hide();
                    callbackData.cancelDownload();
                } else if (e.clientX > maxLeft - buttonWidth && e.clientX < maxLeft) {
                    maskLayer.hide();
                    popupView.hide();
                }
            }
        }
    });
    maskLayer.show();
    popupView.show();
    return callbackData;
}

// ========== 7. 主入口方法 ==========
const checkAppUpdateApp = function(isPrompt = false) {
    console.log('進(jìn)入更新檢查函數(shù),當(dāng)前環(huán)境:', plus ? 'App端' : '非App端');
    console.info("checkUpdate地址===" + `${BASE_API_URL}/appVersion/checkUpdate`)
    getCurrentNo(versionInfo => {
        uni.request({
            url: `${BASE_API_URL}/appVersion/checkUpdate`,
            method: 'GET',
            data: {
                platform: platform,
                versionCode: versionInfo.versionName,
                versionName: versionInfo.versionName,
                packageName: plus.runtime.packageName || 'com.mht',
                appid: APPID
            },
            success: (res) => {
                console.info("checkUpdate地址===" + JSON.stringify(res))
                if (res.statusCode !== 200 || !res.data.success || res.data.code !== 200) {
                    if (isPrompt) uni.showToast({
                        title: '版本檢查失敗',
                        icon: 'none'
                    });
                    return;
                }

                const resData = res.data.result;
                const updateData = {
                    versionName: resData.versionCode,
                    versionCode: resData.versionCode,
                    updateContent: resData.updateContent,
                    downloadUrl: '/sys/common/static/' + resData.downloadUrl,
                    updateType: resData.isForce ? 'forcibly' : 'solicit',
                    hasUpdate: resData.hasUpdate
                };

                if (!updateData.hasUpdate) {
                    if (isPrompt) uni.showToast({
                        title: '當(dāng)前已是最新版本',
                        icon: 'success'
                    });
                    return;
                }

                // 執(zhí)行更新邏輯
                if (updateData.updateType == "forcibly") {
                    // 強(qiáng)制更新:先彈更新彈窗(只有立即升級(jí)/退出)
                    updatePopup(updateData, function() {
                        if (/\.wgt$/i.test(updateData.downloadUrl)) {
                            getDownload(updateData);
                        } else if (/\.html$/i.test(updateData.downloadUrl)) {
                            plus.runtime.openURL(updateData.downloadUrl);
                        } else {
                            if (platform == "android") {
                                getDownload(updateData);
                            } else {
                                plus.runtime.openURL(updateData.downloadUrl);
                            }
                        }
                    });
                } else if (updateData.updateType == "silent") {
                    // 靜默更新:直接下載
                    if (/\.wgt$/i.test(updateData.downloadUrl)) {
                        getDownload(updateData);
                    } else if (/\.html$/i.test(updateData.downloadUrl)) {
                        plus.runtime.openURL(updateData.downloadUrl);
                    } else {
                        if (platform == "android") {
                            getDownload(updateData);
                        } else {
                            plus.runtime.openURL(updateData.downloadUrl);
                        }
                    }
                } else if (updateData.updateType == "solicit") {
                    // 非強(qiáng)制更新:彈彈窗(暫不升級(jí)/立即升級(jí))
                    updatePopup(updateData, function() {
                        if (/\.wgt$/i.test(updateData.downloadUrl)) {
                            getDownload(updateData);
                        } else if (/\.html$/i.test(updateData.downloadUrl)) {
                            plus.runtime.openURL(updateData.downloadUrl);
                        } else {
                            if (platform == "android") {
                                getDownload(updateData);
                            } else {
                                plus.runtime.openURL(updateData.downloadUrl);
                            }
                        }
                    });
                }
            },
            fail: () => {
                if (isPrompt) uni.showToast({
                    title: '版本檢查失敗,請(qǐng)稍后重試',
                    icon: 'none'
                });
            }
        });
    });
}
// #endif

// ========== 兜底導(dǎo)出 ==========
export default function checkAppUpdate(isPrompt = false) {
    // #ifdef APP-PLUS
    checkAppUpdateApp(isPrompt);
    // #endif
    // #ifndef APP-PLUS
    if (isPrompt) {
        uni.showToast({
            title: '當(dāng)前環(huán)境不支持應(yīng)用更新',
            icon: 'none'
        });
    }
    // #endif
}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容