小Q
小Q
  • 发布:2018-11-23 09:11
  • 更新:2020-08-19 10:18
  • 阅读:6982

关于nuiapp检测升级app并自动安装的代码分享

分类:uni-app

思路如下,供新手参考

检测升级部分代码

    upapp: function(isup) {  
        //#ifdef APP-PLUS  
        /* 5+环境锁定屏幕方向 */  
        plus.screen.lockOrientation('portrait-primary'); //锁定  
        /* 5+环境升级提示 */  
        var server = ""; //检查更新地址  

        var req = { //升级检测数据  
            "appid": plus.runtime.appid,  
            "version": plus.runtime.version,  
            "imei": plus.device.imei  
        };  

        uni.request({  
            url: server,  
            data: req,  
            success: (res) => {  
                if (res.statusCode == 200 && res.data.state == "yes") {  
                    let openUrl = plus.os.name === 'iOS' ? res.data.iOS : res.data.Android;  
                    var url = res.data.urlapp;  
                    var content = res.data.content;  
                    uni.showModal({ //提醒用户更新  
                        title: '更新提示',  
                        cancelText:'确定',  
                        confirmText:'取消',  
                        content: res.data.content ? res.data.content : '有新版本,是否确定更新?',  
                        success: (res) => {  
                            if (!res.confirm) {  
                                                                //跳转到升级进度页面  
                                uni.navigateTo({  
                                    url: "/pages/upapp/index?url=" + url + "&content=" + content  
                                })  
                            } else {  
                                //取消  
                            }  
                        }  
                    })  
                } else if (res.statusCode == 200) {  
                    uni.setStorageSync("upapp", time);  
                    uni.showToast({  
                        "title": res.data.content ? res.data.content : "已是最新版本",  
                    });  

                } else {  
                    //  
                }  
            }  
        })  
        //#endif  
    }

下载升级进度页面:/pages/upapp/index

<template>  
    <view>  
        <view class="page-body">  
            {{title}} {{content}}  
            <view class="page-section page-section-gap">  
                <view class="progress-box">  
                    <progress :percent="percent" show-info stroke-width="30" />  
                </view>  
            </view>  
        </view>  
    </view>  
</template>  
<script>  
    export default {  
        data() {  
            return {  
                title: '正在下载中,请稍候!',  
                content: "",  
                percent: 0  
            }  
        },  
        onLoad: function (option) {  
            if (option.url) {  
                this.content = option.content;  
                this.up_app(option.url);  
            } else {  
                uni.navigateBack({  
                    delta: 1,  
                });  
                return;  
            }  

        },  
        onHide: function () {  

        },  
        onShow: function () {  

        },  
        methods: {  
            up_app: function (url) {  
                plus.screen.lockOrientation('portrait-primary'); //锁定  
                this.download(url)  
            },  
            download: function (url) {  
                console.log(url)  
                var that = this;  
                const downloadTask = uni.downloadFile({  
                    url: url,   
                    success: function (res) {  
                        if (res.statusCode === 200) {  
                            console.log(JSON.stringify(res))  
                            console.log('下载成功');  
                            var tempFilePaths = res.tempFilePath;  
                            that.install(tempFilePaths);  
                            return;  
                        }  
                    }  
                });  

                downloadTask.onProgressUpdate(function (res) {  
                    that.percent = res.progress;  
                    //console.log('下载进度' + res.progress);  
                    //console.log('已经下载的数据长度' + res.totalBytesWritten);  
                    //console.log('预期需要下载的数据总长度' + res.totalBytesExpectedToWrite);  
                });  
            },  
            install: function (path) {//安装  
                console.log(path)  
                plus.runtime.install(path, {  
                    force: true  
                }, function () {  
                    //uni.hideNavigationBarLoading();  
                    console.log("加载完成!");  
                    uni.navigateBack({  
                        delta: 1,  
                    });  
                }, function (e) {  

                    console.log(JSON.stringify(e));  

                });  
            }  

        }  
    }  
</script>  

<style>  
    progress {  
        width: 100%;  
    }  

    .progress-box {  
        display: flex;  
        height: 50px;  
        margin-bottom: 60px;  
    }  

    .progress-cancel {  
        margin-left: 40px;  
    }  
</style>

后端代码(php):

$version="";//是接收到的版本号
$now_v="1.0.1";//要升级最新版本号
$data_json=["state"=>"not","content"=>"已是最新版本!",];
if ($version !== $now_v) { //这里是示例代码,真实业务上,最新版本号及relase no
$data_json =array(
"state"=>"yes",
"content"=>"增加一些新功能。",
"mark"=>$now_v,
"urlapp"=>"",//下载app地址
);
}

2 关注 分享
CodeIsLaw 1***@qq.com

要回复文章请先登录注册

3***@qq.com

3***@qq.com

在android5.1左右版本的系统里 uni.downloadFile()下载报错,报400,downfile:ok 这个怎么解决啊大佬
2020-08-19 10:18
小Q

小Q (作者)

回复 dyjx2004 :
插件市场里有相关的更新插件,请去里面找
2020-08-08 11:36
dyjx2004

dyjx2004

onProgressUpdate里面监听的下载进度不准确是怎么回事呢 手机上都弹出安装框了 进度才40%
2020-07-29 11:44
maozai

maozai

6666
2020-01-13 15:28