废话不多说,直接上代码。
1.判断是不是企业微信打开的
const ua = navigator.userAgent.toLowerCase()
if (/micromessenger/.test(ua)) {}
2.复制功能
navigator.clipboard.writeText(newsUrl).then(() => {this.$message({message: '您已复制文件链接,可在浏览器打开链接下载文件。',type: 'success'})})
3.消息弹窗提示
offset Message 距离窗口顶部的偏移量
duration:显示时间, 毫秒。设为 0 则不会自动关闭
Message({ type: 'success', message: '您已复制文件链接,可在浏览器打开链接下载文件。', offset: 200, duration: 3500 })
4.完整代码
// 文件------查看下载文件handlePreview(file) {const reqObj = { filePath: file.url, fileName: file.name }// 判断是不是企业微信const ua = navigator.userAgent.toLowerCase()if (/micromessenger/.test(ua)) {const newsUrl = 'https://' + window.location.host + '/eal-ec-base/fileApi/download?filePath=' + file.url + '&fileName=' + file.namenavigator.clipboard.writeText(newsUrl).then(() => {this.$message({message: '您已复制文件链接,可在浏览器打开链接下载文件。',type: 'success'})})window.open(newsUrl, '_blank')} else {this.downloadInterface(reqObj, file.name)}},
// 文件下载async downloadInterface(reqObj, fileName) {uploadFile(reqObj).then(res => {this.loadingShow = falseconst resData = res.data// 返回格式是文件流格式// 在请求拿到文件流res以后,使用a标签下载const pdfData = resDataconst blob = new Blob([pdfData], {type: `${this.fileBlobType};charset=utf-8;`})var a = document.createElement('a')document.body.appendChild(a)a.style = 'display: none'var url = window.URL.createObjectURL(blob)a.href = urla.setAttribute('download', fileName)a.click()a.remove()window.URL.revokeObjectURL(url)})},