web app升级—带进度条的App自动更新

  带进度条的App自动更新,效果如下图所示:

    技术:vue、vant-ui、5+

  封装独立组件AppProgress.vue:

<template>
  <div>
    <van-dialog
      v-model="showProgress"
      confirm-button-text="后台下载"
      class="app-update"
      @confirm="confirmClick"
    >
      <img src="../../assets/imgs/progress-bar.png" />
      <van-progress :percentage="percentageVal" />
      <div class="msg">版本更新中,请稍后...</div>
    </van-dialog>
  </div>
</template>

<script>
// app下载进度组件
export default {
  props: {
    // 进度值
    percentageVal: {
      type: Number,
      default: 0
    },
    // 是否显示弹窗
    showProgress: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {}
  },
  methods: {
    confirmClick() {
      this.$emit('confirm');
    }
  }
}
</script>

<style lang="scss" scoped>
img {
  width: 270px;
  height: 163px;
  position: fixed;
  top: -35px;
  z-index: 2200;
}
</style>
<style lang="scss">
.app-update.van-dialog {
  overflow: visible;
  width: 270px;
  border-radius: 12px;
  .van-progress {
    margin-top: 124px;
    z-index: 2300;
  }
  .msg {
    font-size: 16px;
    font-weight: 600;
    color: white;
    position: absolute;
    top: 50px;
    z-index: 2300;
    width: 100%;
    text-align: center;
  }
  .van-dialog__footer {
    border-radius: 12px;
    .van-button--default {
      .van-button__text {
        width: 105px;
        height: 26px;
        border-radius: 13px;
        background-color: #006eff;
        color: white;
        font-weight: 600;
        font-size: 12px;
        display: inline-block;
        margin-top: 10px;
        line-height: 26px;
      }
    }
  }
}
</style>

app升级代码,封装独立js文件:appUpdateOptions.js

 

/**
 * IOS 包发布到应用市场后要更新此处的ID,替换掉测试ID:1053012308
 */
/* eslint-disable no-undef */
import { getVersion } from '@/services/login';
import request from '../../api/ajax.js';
import { Dialog } from 'vant';
import expiredStorage from '@/utils/expiredStorage.js';

function sleep(numberMillis) {
  var now = new Date();
  var exitTime = now.getTime() + numberMillis;
  while (true) {
    now = new Date();
    if (now.getTime() > exitTime) return;
  }
}

// Vue继承的基础对象
export default {
  data() {
    return {
      showProgress: false,
      percentageVal: 0
    };
  },
  methods: {
    appUpdate(ismanual) {
      const that = this;
      console.log('appUpdate');
      // 获取5+运行环境的版本号
      console.log('5+ Runtime version:' + plus.runtime.innerVersion);
      plus.runtime.getProperty(plus.runtime.appid, function(inf) {
        const ver = inf.version;
        console.log('ver:' + ver);
        var ua = navigator.userAgent.toLowerCase();
        // 苹果手机
        if (/iphone|ipad|ipod/.test(ua)) {
          // 获取当前上架APPStore版本信息
          request
            .get('https://itunes.apple.com/lookup?id=1053012308', {
              id: 1053012308 // APP唯一标识ID
            })
            .then(data => {
              console.log('data:' + JSON.stringify(data));
              var resultCount = data.resultCount;
              for (var i = 0; i < resultCount; i++) {
                var normItem = data.results[i].version;
                console.log('normItem:' + normItem);
                if (normItem > ver) {
                  var _msg = '发现新版本:V' + normItem;
                  // plus.nativeUI.alert("发现新版本:V" + normItem);
                  Dialog.confirm({
                    title: '升级确认',
                    message: _msg
                  })
                    .then(() => {
                      // on confirm
                      // 执行升级操作
                      document.location.href =
                        'https://itunes.apple.com/cn/app/id1053012308?mt=8'; // 上新APPStore下载地址
                    })
                    .catch(() => {
                      // on cancel
                      expiredStorage.setItem('$upgradeTip', false, 1 / 12); // 1/12天内不再显示升级提示
                    });
                  return;
                }
              }
              if (ismanual) {
                plus.nativeUI.toast('当前版本号已是最新');
              }
            });
        } else if (/android/.test(ua)) {
          getVersion().then(res => {
            console.log('data:' + JSON.stringify(res));
            if ((res.code = 200 && res.data.version > ver)) {
              var _msg = '发现新版本:V' + res.data.version;
              const apkUrl = res.data.redirectUrl;
              Dialog.confirm({
                title: '升级确认',
                message: _msg
              })
                .then(() => {
                  // on confirm
                  // 执行升级操作
                  console.log('apkUrl :', apkUrl);
                  // plus.nativeUI.toast('正在准备环境,请稍后!');
                  that.showProgress = true;
                  var dtask = plus.downloader.createDownload(
                    apkUrl,
                    {},
                    function(d, status) {
                      if (status == 200) {
                        // sleep(1000);
                        var path = d.filename; // 下载apk
                        plus.runtime.install(path); // 自动安装apk文件
                        that.showProgress = false;
                      } else {
                        plus.nativeUI.alert('版本更新失败:' + status);
                        that.showProgress = false;
                      }
                    }
                  );
                  try {
                    dtask.start(); // 开启下载的任务
                    var prg = 0;
                    // var showLoading = plus.nativeUI.showWaiting(
                    //   '版本更新中,请稍后!'
                    // ); // 创建一个showWaiting对象
                    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
                          );
                          // 让百分比 10% 增长,如果这里不这么处理  出现 堆栈内存溢出的问题
                          // if (prg % 10 == 0) {
                          //   // showLoading.setTitle(
                          //   //   '已下载' + prg + '%'
                          //   // );
                          //   that.percentageVal = prg;
                          // }
                          that.percentageVal = prg;
                          break;
                        case 4:
                          // plus.nativeUI.closeWaiting();
                          that.showProgress = false;
                          break;
                      }
                    });
                  } catch (err) {
                    that.showProgress = false;
                    if (ismanual) {
                      plus.nativeUI.toast('网络异常,请稍候再试' + err);
                    }
                  }
                })
                .catch(error => {
                  // on cancel
                  console.log('error :', error);
                  that.showProgress = false;
                  expiredStorage.setItem('$upgradeTip', false, 1 / 12); // 1/12天内不再显示升级提示
                });
            } else {
              console.log('当前版本号已是最新');
              if (ismanual) {
                plus.nativeUI.toast('当前版本号已是最新');
              }
            }
          });
        }
      });
    },
    // 点击确定按钮
    confirmClick() {
      this.showProgress = false;
    }
  }
};

 

调用代码:

import appUpdateOptions from '@/utils/mixins/appUpdateOptions.js'
import AppProgress from '@/components/common/AppProgress.vue';
export default {
  components: { AppProgress },
  props: {},
  mixins: [appUpdateOptions],
  methods: {
    // app更新
    appUpdateFuc() {
      const that = this;
      that.$mui.plusReady(function() {
        that.appUpdate(true);
      });
    },

结束.......

转载于:https://www.cnblogs.com/jiekzou/p/11544116.html

非常精美的h5 进度条 |DEMO_jQuery之家-自由分享jQuery、html5、css3的插件库 <!----> .ClassyCountdownDemo { margin:0 auto 30px auto; max-width:800px; width:calc(100%); padding:30px; display:block } #countdown2 { background:#FFF } #countdown3 { background:rgb(52, 73, 94) } #countdown4 { background:#222 } #countdown5 { background:#222 } #countdown6 { background:#222 } #countdown7 { background:#222 } #countdown8 { background:#222 } #countdown9 { background:#FFF } #countdown10 { background:#3498db } jQuery炫酷图片预览Lightbox插件 A jQuery plugin designed to provide gallery view for images jQuery之家 返回下载页 Example $(document).ready(function() { $('#countdown15').ClassyCountdown({ theme: "flat-colors", end: $.now() + 10000 }); $('#countdown16').ClassyCountdown({ theme: "flat-colors-wide", end: $.now() + 10000 }); $('#countdown17').ClassyCountdown({ theme: "flat-colors-very-wide", end: $.now() + 10000 }); $('#countdown18').ClassyCountdown({ theme: "flat-colors-black", end: $.now() + 10000 }); $('#countdown1').ClassyCountdown({ theme: "white", end: $.now() + 645600 }); $('#countdown5').ClassyCountdown({ theme: "white", end: $.now() + 10000 }); $('#countdown6').ClassyCountdown({ theme: "white-wide", end: $.now() + 10000 }); $('#countdown7').ClassyCountdown({ theme: "white-very-wide", end: $.now() + 10000 }); $('#countdown8').ClassyCountdown({ theme: "white-black", end: $.now() + 10000 }); $('#countdown11').ClassyCountdown({ theme: "black", style: { secondsElement: { gauge: { fgColor: "#F00" } } }, end: $.now() + 10000 }); $('#countdown12').ClassyCountdown({ theme: "black-wide", labels: false, end: $.now() + 10000 }); $('#countdown13').ClassyCountdown({ theme: "black-very-wide", labelsOptions: { lang: { days: 'D', hours: 'H', minutes: 'M', seconds: 'S' }, style: 'font-size:0.5em; text-transform:uppercase;' }, end: $.now() + 10000 }); $('#countdown14').ClassyCountdown({ theme: "black-black", labelsOptions: { style: 'font-size:0.5em; text-transform:uppercase;' }, end: $.now() + 10000 }); $('#countdown4').ClassyCountdown({ end: $.now() + 10000, labels: true, style: { element: "", textResponsive: .5, days: { gauge: { thickness: .03, bgColor: "rgba(255,255,255,0.05)", fgColor: "#1abc9c" }, textCSS: 'font-family:\'Open Sans\'; font-size:25px; font-weight:300; color:#fff;' }, hours: { gauge: { thickness: .03, bgColor: "rgba(255,255,255,0.05)", fgColor: "#2980b9" }, textCSS: 'font-family:\'Open Sans\'; font-size:25px; font-weight:300; color:#fff;' }, minutes: { gauge: { thickness: .03, bgColor: "rgba(255,255,255,0.05)", fgColor: "#8e44ad" }, textCSS: 'font-family:\'Open Sans\'; font-size:25px; font-weight:300; color:#fff;' }, seconds: { gauge: { thickness: .03, bgColor: "rgba(255,255,255,0.05)", fgColor: "#f39c12" }, textCSS: 'font-family:\'Open Sans\'; font-size:25px; font-weight:300; color:#fff;' } }, onEndCallback: function() { console.log("Time out!"); } }); $('#countdown2').ClassyCountdown({ end: '1388468325', now: '1378441323', labels: true, style: { element: "", textResponsive: .5, days: { gauge: { thickness: .01, bgColor: "rgba(0,0,0,0.05)", fgColor: "#1abc9c" }, textCSS: 'font-family:\'Open Sans\'; font-size:25px; font-weight:300; color:#34495e;' }, hours: { gauge: { thickness: .01, bgColor: "rgba(0,0,0,0.05)", fgColor: "#2980b9" }, textCSS: 'font-family:\'Open Sans\'; font-size:25px; font-weight:300; color:#34495e;' }, minutes: { gauge: { thickness: .01, bgColor: "rgba(0,0,0,0.05)", fgColor: "#8e44ad" }, textCSS: 'font-family:\'Open Sans\'; font-size:25px; font-weight:300; color:#34495e;' }, seconds: { gauge: { thickness: .01, bgColor: "rgba(0,0,0,0.05)", fgColor: "#f39c12" }, textCSS: 'font-family:\'Open Sans\'; font-size:25px; font-weight:300; color:#34495e;' } }, onEndCallback: function() { console.log("Time out!"); } }); $('#countdown9').ClassyCountdown({ end: '1388468325', now: '1380501323', labels: true, style: { element: "", textResponsive: .5, days: { gauge: { thickness: .05, bgColor: "rgba(0,0,0,0)", fgColor: "#1abc9c", lineCap: 'round' }, textCSS: 'font-family:\'Open Sans\'; font-size:25px; font-weight:300; color:#34495e;' }, hours: { gauge: { thickness: .05, bgColor: "rgba(0,0,0,0)", fgColor: "#2980b9", lineCap: 'round' }, textCSS: 'font-family:\'Open Sans\'; font-size:25px; font-weight:300; color:#34495e;' }, minutes: { gauge: { thickness: .05, bgColor: "rgba(0,0,0,0)", fgColor: "#8e44ad", lineCap: 'round' }, textCSS: 'font-family:\'Open Sans\'; font-size:25px; font-weight:300; color:#34495e;' }, seconds: { gauge: { thickness: .05, bgColor: "rgba(0,0,0,0)", fgColor: "#f39c12", lineCap: 'round' }, textCSS: 'font-family:\'Open Sans\'; font-size:25px; font-weight:300; color:#34495e;' } }, onEndCallback: function() { console.log("Time out!"); } }); $('#countdown10').ClassyCountdown({ end: '1397468325', now: '1388471324', labels: true, labelsOptions: { lang: { days: 'D', hours: 'H', minutes: 'M', seconds: 'S' }, style: 'font-size:0.5em; text-transform:uppercase;' }, style: { element: "", textResponsive: .5, days: { gauge: { thickness: .02, bgColor: "rgba(255,255,255,0.1)", fgColor: "rgba(255,255,255,1)", lineCap: 'round' }, textCSS: 'font-family:\'Open Sans\'; font-size:25px; font-weight:300; color:rgba(255,255,255,0.7);' }, hours: { gauge: { thickness: .02, bgColor: "rgba(255,255,255,0.1)", fgColor: "rgba(255,255,255,1)", lineCap: 'round' }, textCSS: 'font-family:\'Open Sans\'; font-size:25px; font-weight:300; color:rgba(255,255,255,0.7);' }, minutes: { gauge: { thickness: .02, bgColor: "rgba(255,255,255,0.1)", fgColor: "rgba(255,255,255,1)", lineCap: 'round' }, textCSS: 'font-family:\'Open Sans\'; font-size:25px; font-weight:300; color:rgba(255,255,255,0.7);' }, seconds: { gauge: { thickness: .02, bgColor: "rgba(255,255,255,0.1)", fgColor: "rgba(255,255,255,1)", lineCap: 'round' }, textCSS: 'font-family:\'Open Sans\'; font-size:25px; font-weight:300; color:rgba(255,255,255,0.7);' }, }, onEndCallback: function() { console.log("Time out!"); } }); $('#countdown3').ClassyCountdown({ end: '1390868325', now: '1388461323', labels: true, labelsOptions: { lang: { days: 'Zile', hours: 'Ore', minutes: 'Minute', seconds: 'Secunde' }, style: 'font-size:0.5em; text-transform:uppercase;' }, style: { element: "", textResponsive: .5, days: { gauge: { thickness: .2, bgColor: "rgba(255,255,255,0.2)", fgColor: "rgb(241, 196, 15)" }, textCSS: 'font-family:\'Open Sans\'; font-size:25px; font-weight:300; color:rgba(255,255,255,0.7);' }, hours: { gauge: { thickness: .2, bgColor: "rgba(255,255,255,0.2)", fgColor: "rgb(241, 196, 15)" }, textCSS: 'font-family:\'Open Sans\'; font-size:25px; font-weight:300; color:rgba(255,255,255,0.7);' }, minutes: { gauge: { thickness: .2, bgColor: "rgba(255,255,255,0.2)", fgColor: "rgb(241, 196, 15)" }, textCSS: 'font-family:\'Open Sans\'; font-size:25px; font-weight:300; color:rgba(255,255,255,0.7);' }, seconds: { gauge: { thickness: .2, bgColor: "rgba(255,255,255,0.2)", fgColor: "rgb(241, 196, 15)" }, textCSS: 'font-family:\'Open Sans\'; font-size:25px; font-weight:300; color:rgba(255,255,255,0.7);' } }, onEndCallback: function() { console.log("Time out!"); } }); }); 如果你喜欢这个插件,那么你可能也喜欢: html5+jquery通过鼠标控制的圆形进度条 jQuery和css3旋钮控制按钮-knobKnob
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值