首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >Vue-typescript 打包成app后如何自动更新

Vue-typescript 打包成app后如何自动更新

作者头像
taixingyiji
发布2022-07-25 15:10:12
发布2022-07-25 15:10:12
1.3K0
举报

# Vue typescript 打包成app后如何自动更新

# 前言

项目需要有一个手机app,则通过Hbuilder云打包生成apk。 但是由于没有上架应用商店等问题,所以使用wgt包的方式进行app升级,免去了重新安装的问题。

# 一、前置条件

提示

项目使用的是vue+ts,使用组件 vue-property-decorator 写法,具体请参考链接说明。

使用前,使用者已经知道如何将vue打包成一个app

# 二、代码

话不多说直接上代码

# 1. 如何自动更新

在生命周期函数 created()中调用方法 autoUpdate()

autoUpdate()方法就是检查更新的操作

代码语言:javascript
复制
created() {
  this.autoUpdate()
}

# 2. 更新方法

window 为浏览器的对象,window.plus 为打包成后app可以调用的api,电脑访问是无法调用的。需要进行判断是否可用。

代码语言:javascript
复制
autoUpdate() {
  if ((window as any).plus) {
    this.plusReady()
  } else {
    document.addEventListener('plusready', this.plusReady, false)
  }
}

# 3. 检查更新

检查是否需要更新,我的更新版本为每次都写入 http://xxx.xxxx.xxx/manifest.json 文件中。

通过 axios 进行操作,获取文件中版本内容,若需要更新则下载 http://xxx.xxxx.xxx/wgt/xxxx.wgt 文件。

代码语言:javascript
复制
plusReady() {
  // 获取本地应用资源版本号
  (window as any).plus.runtime.getProperty((window as any).plus.runtime.appid, (inf: any) => {
    this.checkVersion(inf.version)
  })
}

// 检查版本
checkVersion(currentV: any) {
  // 这里是你获取版本号的方式
  const url = 'http://xxx.xxxx.xxx/manifest.json'
  axios.get(url).then((res: any) => {
    if (res.status === 200) {
      const result = res.data ? res.data : ''
      if (result && currentV && currentV !== result.version) {
        // 这里使用了element-ui的组件 你也可以更换别的
        this.$confirm('检测到有新版本, 是否更新?', '更新提示', {
          closeOnClickModal: false,
          confirmButtonText: '更新',
          cancelButtonText: '取消',
          type: 'success'
        }).then(() => {
          this.downloadWgt('http://xxx.xxxx.xxx/wgt/xxxx.wgt')
        }).catch(() => {
          let plus = (window as any).plus
          plus.runtime.restart()
        })
      }
    }
  }).catch(err => {
    console.log(err)
  })
}

# 4. 完整代码

项目中此处是直接写在App.vue中,可根据自己代码习惯及需求自行更改。

代码语言:javascript
复制
<template>
  <div id="app">
    <router-view/>
  </div>
</template>

<script lang="ts">
import {Component, Vue} from 'vue-property-decorator'
import axios from 'axios'

@Component({
  name: 'App',
})
export default class extends Vue {

  created() {
    this.autoUpdate()
  }

  autoUpdate() {
    if ((window as any).plus) {
      this.plusReady()
    } else {
      document.addEventListener('plusready', this.plusReady, false)
    }
  }


  plusReady() {
    // 获取本地应用资源版本号
    (window as any).plus.runtime.getProperty((window as any).plus.runtime.appid, (inf: any) => {
      this.checkVersion(inf.version)
    })
  }

  // 检查版本
  checkVersion(currentV: any) {
    // 这里是你获取版本号的方式
    const url = 'http://xxx.xxx.xxx/manifest.json'
    axios.get(url).then((res: any) => {
      if (res.status === 200) {
        const result = res.data ? res.data : ''
        if (result && currentV && currentV !== result.version) {
          // 这里使用了element-ui的组件 你也可以更换别的
          this.$confirm('检测到有新版本, 是否更新?', '更新提示', {
            closeOnClickModal: false,
            confirmButtonText: '更新',
            cancelButtonText: '取消',
            type: 'success'
          }).then(() => {
            this.downloadWgt('http://xxxx.xxx.xxx/wgt/xxx.wgt')
          }).catch(() => {
            let plus = (window as any).plus
            plus.runtime.restart()
          })
        }
      }
    }).catch(err => {
      console.log(err)
    })
  }

  // 下载wgt包
  downloadWgt(url: any) {
    let plus = (window as any).plus
    plus.nativeUI.showWaiting('下载更新文件中...')
    try {
      let download = plus.downloader.createDownload(url, {filename: '_doc/update/'}, (d: any, status: any) => {
        if (status === 200) {
          this.installWgt(d.filename) // 安装wgt包
        } else {
          plus.nativeUI.alert('下载更新文件失败!')
        }
        plus.nativeUI.closeWaiting()
      })
      download.start()
    } catch (err) {
      this.$message.error(err)
    }

  }

  // 安装wgt包
  installWgt(path: string) {
    let plus = (window as any).plus
    plus.nativeUI.showWaiting('安装更新文件...')
    let runtime = plus.runtime
    runtime.install(path, {}, this.success, this.error)
  }

  success() {
    let plus = (window as any).plus
    plus.nativeUI.closeWaiting()
    plus.nativeUI.alert('应用资源更新完成!', function () {
      plus.runtime.restart()
    })
  }

  error(e: any) {
    let plus = (window as any).plus
    plus.nativeUI.closeWaiting()
    plus.nativeUI.alert('安装更新文件失败[' + e.code + ']:' + e.message)
  }
}
</script>

# 三、打包wgt

# 1. 将你的静态资源拷贝到HBuilder中

此处不给予演示。

注意

切记,请勿覆盖之前打包的 manifest.json 文件。

# 2. 修改你的版本号

提示

打包wgt时需要修改版本号,版本号需要比上一个版本号要高

且必须与前面代码通过axios获取的版本号一致。

# 3.发行wgt

# 4. 上传到你之前代码所写的服务器路径中

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021-11-12,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • # Vue typescript 打包成app后如何自动更新
    • # 前言
    • # 一、前置条件
    • # 二、代码
      • # 1. 如何自动更新
      • # 2. 更新方法
      • # 3. 检查更新
      • # 4. 完整代码
    • # 三、打包wgt
      • # 1. 将你的静态资源拷贝到HBuilder中
      • # 2. 修改你的版本号
      • # 3.发行wgt
      • # 4. 上传到你之前代码所写的服务器路径中
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档