福州網站建設>網站新聞>uni-app使用問題

        關于nuiapp檢測升級app并自動安裝的代碼分享

        發布日期:2020-01-13瀏覽次數:2500 來源:福州網站建設

        思路如下,供新手參考

        檢測升級部分代碼

        復制代碼 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地址
        );
        }

        以上是由福州網站建設的小編為你分享了"關于nuiapp檢測升級app并自動安裝的代碼分享"文章,如果你在這方面有什么問題,隨時聯系我們

        uni-app使用問題有關的文章
        如果您有什么問題,歡迎咨詢我們客服! 點擊QQ咨詢