vue扫码下载APP安装包

这是一个关于如何根据用户代理判断设备类型并相应地引导用户下载APP的 Vue 组件代码示例。代码中包含了在微信、QQ、Android、iOS 和华为设备上的不同处理方式,并提供了生成二维码的功能。在 Android 和 iOS 设备上直接跳转到下载页面,其他情况下展示引导页。同时,还包含了下载文件的方法。
摘要由CSDN通过智能技术生成

把appDownload.vue地址生成二维码即可

在这里插入图片描述

临时跳转页 appDownload.vue


<template>
  <div class="guide-wrap" v-show="guideShow"><img src="@/assets/images/downloadapp/openinbrowser.png"/></div>
</template>
<script>

export default {
  name: 'AppDownload',
  components: {
  },
  data() {
    return {
      guideShow:false,
      userAgent: navigator.userAgent.toLowerCase()
    }
  },
  created() {
    if (this.isWeChatOrQQ(this.userAgent)) {
      // 是微信或者QQ,使页面变为引导页,引导用户在浏览器中打开该链接
      this.guideShow = true
    } else if (this.isAndroid(this.userAgent) || this.isHuaWei(this.userAgent)) {
      //直接跳转app下载页面
      this.$router.push('/download')
    } else if (this.isIOS(this.userAgent)) {
      //直接跳转app下载页面
      this.$router.push('/download')
    } else {
      // alert('无法判断终端操作系统类型')
      document.write('userAgent: \t', this.userAgent)
    }
  },
  methods: {
    isIOS() {
      return !!this.userAgent.match(/\(i[^;]+;( U;)? cpu.+mac os x/)
    },
    isAndroid() {
      return this.userAgent.indexOf('android') > -1 || this.userAgent.indexOf('linux') > -1
    },
    isHuaWei() {
      return !!(this.userAgent.indexOf('windows nt') > -1)
    },
    isWeChatOrQQ() {
      const ua = this.userAgent.toLowerCase()
      if (ua.indexOf('micromessenger') > -1) {
        // 如果是微信
        return true
      } else if (/mqqbrowser[\S|\s]*qq/.test(ua) || / qq/.test(ua)) {
        // 如果是QQ
        return true
      }
      return false
    },
    async downloadFile(url, fileName) {
      await fetch(url, {
        method: 'Get',
        mode: 'cors',
        headers: {
        },
        responseType: 'blob'
      }).then((response) => {
        response.blob().then(blob => {
          if (window.navigator.msSaveOrOpenBlob) {
            navigator.msSaveBlob(blob, fileName)
          } else {
            var link = document.createElement('a')
            link.href = window.URL.createObjectURL(blob)
            link.download = fileName
            link.click()
            window.URL.revokeObjectURL(link.href)
          }
        })
      })
    }
  }
}
</script>
<style type="text/css">
  body, div, span, i {
    font-size: 100%;
    box-sizing: border-box;
  }
  .guide-wrap {
    position: fixed;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    background: white;
  }
  .guide-item {
    display: block;
    background: #fff;
  }
  .guide-text {
    width: 15rem;
    position: absolute;
    top: 10%;
    left: 60%;
    transform: translate(-50%, -50%);
    padding: 0.4rem 1rem;
    border-radius: 5rem;
    border-top-right-radius: 1rem;
    overflow: hidden;
  }
  .gudde-btn {
    width: 10rem;
    text-align: center;
    position: absolute;
    bottom: 10%;
    left: 50%;
    transform: translate(-50%, 0%);
    padding: 0.4rem 1rem;
    border-radius: 5rem;
    overflow: hidden;
  }
  @keyframes fade {
    from {top: 0;}
    to {top: 100vh;}
  }
  @-webkit-keyframes fade {
    from {top: 0;}
    to {top: 100vh;}
  }
  .wrap-fade {
    animation: fade 3s ease-in;
  }
</style>

安装包下载页 download.vue


<template>
  <div class="downloadwrap">
    <div style="text-align: center;padding-top: 38%"><img src="@/assets/images/downloadapp/bim+logo.png"/></div>
    <div class="downloadbtn" @click="download">下载App</div>
  </div>
</template>
<script>
import { latest } from '@/api/appDownload'

export default {
  name: 'AppDownload',
  components: {
  },
  data() {
    return {
      baseUrl: '',
      // iosItemService: 'itms-services://?action=download-manifest&url=',
      // iosPlist: 'static/app-download-helper/tzmIos.plist',
      // androidFileName: 'static/app-download-helper/app/com.bonc.tzs.app_3.1.0.apk',
      iosItemService: '',
      iosPlist: '',
      androidFile: '',
      androidFileName: '',
      userAgent: navigator.userAgent.toLowerCase()
    }
  },
  created(){
  	//用于调用后端接口获取app包信息。
    this.getLatest();
  },
  methods: {
    download(){
      console.log(this.isAndroid(this.userAgent))
      if (this.isAndroid(this.userAgent) || this.isHuaWei(this.userAgent)) {
        window.open(this.androidFile)
      } else if (this.isIOS(this.userAgent)) {
		//TODO ios跳转APP STORE应用商店下载
		window.location.replace(iosItemService + baseUrl + iosPlist)
      } else {
        // alert('无法判断终端操作系统类型')
        document.write('userAgent: \t', this.userAgent)
      }
    },
    async getLatest() {
      const result = await latest()
      if (result.code === 10000) {
        console.log(result.data.downloadLink)
        this.androidFile = result.data.downloadLink
        this.androidFileName = result.data.fileUrlList[0].name
      }
    },
    getIOS() {
      console.log('下载ios安装包')
      window.open(this.iosItemService + this.baseUrl + this.iosPlist)
    },
    getAndroid() {
      console.log('下载android安装包')
      // window.open(this.androidFile, '_blank')
      this.downloadFile(this.androidFile, this.androidFileName)
    },
    isIOS() {
      return !!this.userAgent.match(/\(i[^;]+;( U;)? cpu.+mac os x/)
    },
    isAndroid() {
      return this.userAgent.indexOf('android') > -1 || this.userAgent.indexOf('linux') > -1
    },
    isHuaWei() {
      return !!(this.userAgent.indexOf('windows nt') > -1)
    },
    isWeChatOrQQ() {
      const ua = this.userAgent.toLowerCase()
      if (ua.indexOf('micromessenger') > -1) {
        // 如果是微信
        return true
      } else if (/mqqbrowser[\S|\s]*qq/.test(ua) || / qq/.test(ua)) {
        // 如果是QQ
        return true
      }
      return false
    },
    async downloadFile(url, fileName) {
      await fetch(url, {
        method: 'Get',
        mode: 'cors',
        headers: {
        },
        responseType: 'blob'
      }).then((response) => {
        response.blob().then(blob => {
          if (window.navigator.msSaveOrOpenBlob) {
            navigator.msSaveBlob(blob, fileName)
          } else {
            var link = document.createElement('a')
            link.href = window.URL.createObjectURL(blob)
            link.download = fileName
            link.click()
            window.URL.revokeObjectURL(link.href)
          }
        })
      })
    }
  }
}
</script>
<style type="text/css">
  body, div, span, i {
    font-size: 100%;
    box-sizing: border-box;
  }
  @keyframes fade {
    from {top: 0;}
    to {top: 100vh;}
  }
  @-webkit-keyframes fade {
    from {top: 0;}
    to {top: 100vh;}
  }
  .downloadwrap{
    background-image: url("./assets/images/downloadapp/bimbg2.png");
    background-repeat: no-repeat;
    background-size: 100%;
  }
  .downloadbtn{
    width: 260px;
    height: 40px;
    color: white;
    background: linear-gradient(270deg,#37a0ff, #2b6eef);
    text-align: center;
    font-size: 0.4rem;
    line-height: 1rem;
    margin: 15% auto;
    border-radius: 0.2rem;
  }
</style>

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值