uniapp_app应用添加应用更新使用

1、版本更新写成mixin函数,getUpdateApk.js
export default {
  data () {
    return {
      appInfo: {
        appid: plus.runtime.appid,
        version: plus.runtime.version
      }
    }
  },
  methods: {
    compareVersionAndUpdate (wgtUrl) {
      // console.log('开始更新', wgtUrl)
      // return
      console.log(wgtUrl)
      this.updateapp2(wgtUrl)
    },
    updateapp2 (wgtUrl) {
      let that = this
      uni.showModal({
        title: '版本更新',
        content: `确认要更新到${that.appInfo.appid}${that.appInfo.version}`,
        confirmText: "更新",
        showCancel: false,
        success: function (e) {
          if (e.confirm) {
            if (plus.os.name.toLowerCase() == 'ios') {
              // 跳转到下载页面
              plus.runtime.openURL(wgtUrl)
            } else {
              var dtask = plus.downloader.createDownload(wgtUrl, {}, function (d, status) {
                uni.showToast({
                  title: '下载完成',
                  mask: false,
                  duration: 1000
                });
                if (status == 200) {// 下载完成
                  plus.runtime.install(plus.io.convertLocalFileSystemURL(d.filename), {}, e => e, function (error) {
                    uni.showToast({
                      title: '安装失败-01',
                      mask: false,
                      duration: 1500
                    });
                  })
                } else {
                  uni.showToast({
                    title: '更新失败-02',
                    mask: false,
                    duration: 1500
                  });
                }
              });
              try {
                dtask.start()
                var prg = 0;
                 //创建一个showWaiting对象 
                var showLoading = plus.nativeUI.showWaiting("正在下载", { "back": "none" }); 
                dtask.addEventListener('statechanged', function (task, status) {
                  // 给下载任务设置一个监听 并根据状态  做操作
                  switch (task.state) {
                    case 1:
                      showLoading.setTitle("正在下载");
                      break;
                    case 2:
                      showLoading.setTitle("已连接到服务器");
                      break;
                    case 3:
                      prg = parseInt(
                        (parseFloat(task.downloadedSize) /
                          parseFloat(task.totalSize)) *
                        100
                      );
                      showLoading.setTitle("  正在下载" + prg + "%  ");
                      break;
                    case 4:
                      plus.nativeUI.closeWaiting();
                      //下载完成
                      break;
                  }
                });
              } catch (err) {
                plus.nativeUI.closeWaiting();
                uni.showToast({
                  title: '更新失败-03',
                  mask: false,
                  duration: 1500
                });
              }
            }
          } else {
            //取消
          }
        }
      });
    },
    updateapp (wgtUrl) {
      uni.showLoading({
        title: '更新中……'
      })
      //执行下载
      uni.downloadFile({
        url: wgtUrl,
        success: downloadResult => {//下载成功
          if (downloadResult.statusCode === 200) {
            uni.showModal({
              title: '',
              content: '更新成功,确定现在重启吗?',
              confirmText: '重启',
              confirmColor: '#EE8F57',
              success: function (res) {
                if (res.confirm) {
                  plus.runtime.install(//安装
                    downloadResult.tempFilePath, {
                    force: true
                  },
                    function () {
                      // utils.showToast('更新成功,重启中');
                      plus.runtime.restart();
                    },
                    function (e) {
                      // utils.showToast('更新失败');
                    }
                  );
                }
              }
            });
          }
        },
        complete: () => {
          uni.hideLoading();
        }
      });
    }
  }
}
2、vuex中actions
actions: {
	compareVersionGetUrl ({ commit, state }, params) {
		return new Promise((resolve, reject) => {
			//此处写接口获取app更新链接url与当前app版本号比较
			setTimeout(() => {
				let version = '2.0.0'
				console.log(version != params.version)
				if (version != params.version) {
					resolve({ url: `http:.../__UNI__E3CD4E2_0216140622.apk`, version: '2.0.0' })
				} else {
					reject({ url: null, msg: '已经是最新版本' })
				}
			}, 500)
		})
	}
}
3、在关于系统的页面中使用如下
<template>
  <view class="app-container">
    <view class="nav">
      <view style="display:flex;justify-content:center;margin:50rpx 0;">
        <view style="width:400rpx">
          <u-button type="primary"
                    @click="checkIsLatest">检查更新</u-button>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
import updateApp from '@/common/utils/getUpdateApk'
export default {
  mixins: [updateApp],
  data () {
    return {
      version: plus.runtime.version,
      titleName: '关于应用',
    }
  },
  methods: {
    checkIsLatest () {
      // {version:'2.0.0',appid:'__UNI__E3CD4E2'}
      this.$store.dispatch("compareVersionGetUrl", { version: '1.0.0' }).then(res => {
        this.compareVersionAndUpdate(res.url)
      })
    }
  }
}
</script>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值