uni-app升级方案

升级的js工具类

import { getLatestVersion, updateInstalledCount } from '@/common/api/common/dictionary';
import environment from '../environment/main';
import store from '@/store';

export default {
  platform: 'Android',
  platformCode: 2,
  versionCode: '',
  versionName: '',
  needUpdate: false,
  forceUpdate: false,
  title: '',
  announcement: '',
  confirmText: '',
  cancelText: '',
  address: '',
  userOpenId: '',
  localVersionCode: 61,
  restart: false,
  installId: 0,

  // 初始化
  __init(userOpenId) {
    var self = this;
    if (userOpenId) {
      this.userOpenId = userOpenId;
    }
    switch(uni.getSystemInfoSync().platform) {
      case 'android':
        console.log('运行Android上');
        self.platform = 'Android';
        self.platformCode = 2;
        break;
      case 'ios':
        console.log('运行iOS上');
        self.platform = 'iOS';
        self.platformCode = 1;
        break;
    }

    // #ifdef APP-PLUS
    plus.runtime.getProperty(plus.runtime.appid, function(widgetInfo) {
      console.log(widgetInfo);
      self.versionName = widgetInfo.version;
      if (self.platformCode === 2) {
        self.versionCode = widgetInfo.versionCode;
      } else {
        self.versionCode = self.localVersionCode;
      }
      self.checkVersion();
    });
    // #endif
  },

  // 检查版本更新
  async checkVersion(checkFrom) {
    /* 获取版本更新数据 */
    let params = {
      productOpenId: environment.SYSTEM_OPEN_ID,
      versionCode: this.versionCode,
      platform: this.platformCode,
      userOpenId: this.userOpenId
    };
    console.log(params);

    try {
      let ret = await getLatestVersion(params);
      console.log(ret.data);
      const data = ret.data;
      if(checkFrom === 'mine' && data.update == false) {
        this.title = '检查更新';
        this.announcement = '暂无更新。';
        this.confirmText = '好的';
      }

      if(data.hotUpdate && data.hotUpdate.parentVersion.toString() === this.localVersionCode.toString() && data.hotUpdate.active && !data.update && !data.forceUpdate && !checkFrom) {
        this.address = `${environment.OSS_URL}${environment.ENVIRONMENT}/${data.hotUpdate.address}`;
        this.restart = data.hotUpdate.restart;
        this.installId = data.hotUpdate.id;
        this.hotUpdateDownloadFile();
        return;
      }

      if(data.update || data.forceUpdate) {
        if(this.platform === 'iOS') {
          this.address = `${environment.IOS_APP_STORE_URL}${data.latestVersion.address}?mt=8`;
        } else {
          this.address = `${environment.OSS_URL}${environment.ENVIRONMENT}/${data.latestVersion.address}`;
        }

        let content = data.latestVersion.announcement;
        if(content) {
          content = content.replace(/;/g, ';').replace(//g, ';<br/>');
        }
        this.installId = data.latestVersion.id;
        this.title = '版本更新';
        this.announcement = `已检测到新版本${data.latestVersion.versionName},更新内容:<br/>${content}`;
        this.confirmText = '立即更新';
        this.cancelText = checkFrom=='mine' ? '以后再说' : '忽略该版本';
      }

      if (data.update) {
        const versionFlag = uni.getStorageSync(data.latestVersion.versionName);
        if(!versionFlag) {
          this.needUpdate = true;
        }
      }

      this.forceUpdate = data.forceUpdate;
      let showCancel = true;
      if (this.forceUpdate || this.title === '检查更新') {
        showCancel = false;
      }

      console.log(this.address);
      const model_data = {
        showValue: this.needUpdate || this.forceUpdate || (this.title === '检查更新'),
        title: this.title,
        content: this.announcement,
        showCancel: showCancel,
        cancelText: this.cancelText,
        confirmText: this.confirmText,
      }

      if (this.needUpdate || this.forceUpdate) {
        model_data.contentStyle = this.announcement=='暂无更新。' ? 'center' : undefined;
        model_data.cancelCallback = checkFrom=='mine' ? undefined : this.cancelCallback;
        model_data.confirmCallback = this.announcement=='暂无更新。' ? undefined : this.downloadFile;
        model_data.versionName = data.latestVersion && data.latestVersion.versionName;
        store.dispatch('versionUpdate/update_download_address', this.address);
        store.dispatch('versionUpdate/update_version_id', this.installId);
      }

      store.dispatch('versionUpdate/set_update_model', model_data);
    } catch (e) {
      // this.forceUpdate = false;
      console.log(e);
    }
  },

  cancelCallback() {
    const model_data = {
      showValue: true,
      title: '忽略该版本',
      content: '您可在“我的-在线更新”中,随时获取最新版淘知学堂。',
      showImage: true,
      showCancel: false,
      confirmText: '好的',
      prevent: false,
      confirmCallback: function ingoreVersion() {
        const versionName = store.state.versionUpdate.updateModel.versionName;
        console.log(versionName);
        uni.setStorageSync(versionName, true);
        store.dispatch('versionUpdate/reset_update_callback');
      },
    }

    store.dispatch('versionUpdate/reset_update_callback');
    store.dispatch('versionUpdate/set_update_model', model_data);
  },

  // 下载安装包并安装
  downloadFile() {
    const address = store.state.versionUpdate.downloadModel.address;
    const installId = store.state.versionUpdate.downloadModel.versionId;
    console.log(address);
    console.log('新版本 id:' + installId);

    if(!address) {
      uni.showToast({
        title: '获取下载地址失败 ...',
        icon: 'none',
      });
      return;
    }

    if(uni.getSystemInfoSync().platform === 'ios') {
      // 跳转到 苹果商店
      plus.runtime.openURL(address);
      return;
    }
    console.log(address)
    const downloadTask = uni.downloadFile({
      url: address,
      success: (downloadResult) => {
        if (downloadResult.statusCode === 200) {
          uni.showToast({
            title: '安装包下载成功 ...',
            icon: 'none',
            duration: 2000,
          });
          plus.runtime.install(downloadResult.tempFilePath, {
            force: true
          }, function(e) {
            console.log('install success...');
            const dictionaryApi = require('@/common/api/common/dictionary');
            dictionaryApi.updateInstalledCount(installId);
            uni.showToast({
              title: '版本更新成功 ...',
              icon: 'none',
            });
            setTimeout(function(){
              plus.runtime.restart();
            }, 2000)
          }, function(e) {
            console.error('install fail...');
            uni.showToast({
              title: '版本更新失败 ...',
              icon: 'none',
            });
          });
        }
      },
      fail: (downloadResult) => {
        uni.showToast({
          title: '安装包下载失败 ...',
          icon: 'none',
        });
      },
      complete: (downloadResult) => {
        store.dispatch('versionUpdate/reset_download_model');
      }
    });

    store.dispatch('versionUpdate/reset_update_callback');
    store.dispatch('versionUpdate/set_download_model', {showDownloadModel: true, downloadTask: downloadTask});
    downloadTask.onProgressUpdate((res) => {
      const process = store.state.versionUpdate.downloadModel.process;
      if (process !== res.progress) {
        store.dispatch('versionUpdate/update_download_process', res.progress);
        console.log('下载进度:' + res.progress);
      }
    });
  },

  // 下载热更新包
  hotUpdateDownloadFile() {
    console.log(this.address);
    uni.downloadFile({
      url: this.address,
      success: (downloadResult) => {
        if (downloadResult.statusCode === 200) {
          console.log('热更新包下载成功......');
          this.installHotFile(downloadResult.tempFilePath);
        }
      },
      fail: (res) => {
        console.log(res);
      },
      complete: (res) => {
        console.log(res);
      }
    });
  },

  installHotFile(tempFilePath) {
    const that = this;
    plus.runtime.install(tempFilePath, {
      force: false
    }, function() {
      console.log('install success...');
      updateInstalledCount(that.installId);
      if (that.restart) {
        plus.runtime.restart();
      }
    }, function(e) {
      console.error('install fail...');
    });
  },
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值