#
whycq
2023-12-02 178d0ee433ecee57bbd57804cc29c593f3de7829
App.vue
@@ -1,13 +1,139 @@
<script>
   export default {
      globalData: {
         baseHttp: '',
         baseUrll: '',
         upVersion: 0,
      },
      onLaunch: function() {
         console.log('App Launch')
      },
      onShow: function() {
         // this.getVersion();
         // this.baseUrll = uni.getStorageSync("baseUrl")
         console.log('App Show')
      },
      onHide: function() {
         console.log('App Hide')
      },
      methods: {
         //检测当前平台,如果是安卓则启动安卓更新
         getVersion() {
            let that = this;
            uni.getSystemInfo({
               success: (res) => {
                  if (res.platform == "android") {
                     that.AndroidCheckUpdate();
                  }
               }
            })
         },
         // 获取当前版本号
         AndroidCheckUpdate() {
            let that = this;
            plus.runtime.getProperty(plus.runtime.appid, (wgtinfo) => {
               that.version = wgtinfo.version //客户端版本号
            })
            setTimeout(()=>{
               that.getUpdateVersion()
            },100)
         },
         // 校验版本
         getUpdateVersion() {
            let that = this
            let type = 0
            if (that.baseUrl == 'http://undefined:undefined/undefined') {
               return
            }
            let url = that.baseUrl + '/appVersion/checkUpdate/' + that.version + '/' + type
            uni.request({
               url: url,
               method: 'GET',
               success(res) {
                  console.log(res);
                  var res = res.data
                  if (res.data) {
                     that.filename = res.data.path
                     that.dialogContent = '发现新版本:' + res.data.version + ', 是否立即更新'
                     that.$refs.upVersion.open()
                  } else {
                     uni.showToast({
                        title: res.msg,
                        icon: "none",
                        position: 'top'
                     })
                  }
               }
            })
         },
         dialogConfirm() {
            this.$refs.upVersion.close()
            this.downWgt()
         },
         dialogClose() {
            this.$refs.upVersion.close()
         },
         downWgt() {
            let that = this;
            const downloadUrl = that.baseUrl + "/appVersion/downloadApp/" + that.filename
            uni.showLoading({
               title: '更新中……'
            })
            const downloadTask = uni.downloadFile({ //执行下载
               url: downloadUrl, //下载地址
               timeout: 1000 * 30, //30秒超时时间
               success: downloadResult => { //下载成功
                  console.log(downloadResult);
                  that.showdownLine = false
                  uni.hideLoading();
                  if (downloadResult.statusCode == 200) {
                     uni.showModal({
                        title: '',
                        content: '更新成功,确定现在重启吗?',
                        confirmText: '重启',
                        confirmColor: '#EE8F57',
                        success: function(res) {
                           if (res.confirm == true) {
                              plus.runtime.install( //安装
                                 downloadResult.tempFilePath, {
                                    force: true
                                 },
                                 function(res) {
                                    utils.showToast('更新成功,重启中');
                                    plus.runtime.restart();
                                 }
                              );
                           }
                        }
                     });
                  } else {
                     uni.hideLoading();
                     that.showdownLine = false
                     uni.showToast({
                        title:'请先上传安装包',
                        icon: 'error'
                     })
                  }
               },
               fail: err => {
                  uni.hideLoading();
                  that.showdownLine = false
                  that.$u.toast(downloadResult.errMsg)
               },
               complete: com => {
                  console.log(com)
               }
            });
            // 下载进度
            downloadTask.onProgressUpdate(res => {
               that.downloadNum = res.progress
               console.log('下载进度' + that.downloadNum);
            });
         },
      }
   }
</script>