业务场景:
在ios手机去预览pdf文件,本身该项目是基于uniapp开发的h5页面,使用了飞书的网页式鉴权,飞书的api方法内自带预览文件功能,本来我使用的是风生水起;
奈何,测试提出的了一个bug,在" ios系统 " 苹果p7、p8 手机预览时,没有反应,也没有报错,Σσ(・Д・;)我我我什么都没做!!!,哎哎哎哎哎 ,挠头挠头~
既然这个方法无法解决,此时又没有时间能让我去研究它究竟是为什么,我只能另辟蹊径;就不借助飞书自带的api去实现了,直接新打开窗口使用 window.open(),同时测试反映安卓手机没有问题,那我就只处理ios系统,真是机智如我!┗( ▔, ▔ )┛
好家伙,还有问题,好几个页面用得都是这个方法,其他都好好的,但偏偏其中一个按钮点击还是在ios手机就是没反应,主要问题是没有报错o((⊙﹏⊙))o ,,,,,,
左思右想到底是为啥啊,就开始查找上下文,对比其他成功的页面到底是什么影响到了,后发现,单单这个有问题的window.open(),前执行了个异步请求接口;后把接口注释,直接打开新窗口,发现是可行的,难道真的是异步的问题???????
既然如此那我就进入页面的时候直接请求获取这个链接存到data里,再在用户点击的时候按钮时,判断data里值的长度,进行操作,发现是可行的,皆大欢喜(σ゚∀゚)σ…:*☆哎哟不错哦;
data() {
return {
contractFileUrl: ''
}
},
methods: {
async showContaractPdf() {
if (this.contractFileUrl !== '') {
await uni.showLoading({
mask: true,
title: '加载中'
})
await showFile(this.contractFileUrl)
} else {
await uni.showToast({
title: '请联系管理员添加预览文件',
icon: "error",
duration: 2000,
})
}
},
},
async created() {
await getTemplateSample().then(async res => {
const { code, data } = res
this.contractFileUrl = code == 200 ? data.url : ''
})
}
该方法同时使用了飞书客户端网页应用api,如果不需要删除就好
// 预览文件 该方法同时使用了飞书客户端网页应用api,如果不需要删除就好
export async function showFile(url) {
console.log('预览附件')
if (!window.h5sdk) {
console.log('window.h5sdk 找不到!!!!')
await uni.hideLoading()
return false
}
var u = navigator.userAgent
if(u.indexOf('iPhone') > -1) { // ios手机
window.open(url)
} else { // 安卓手机
window.h5sdk.ready(() => {
console.log('走进来了啦')
const task = tt.downloadFile({ // 先下载到本地,只是临时的文件
url,
success: async (res) => {
console.log('downloadFile 成功')
await tt.openDocument({ // 打开文件
filePath: res.tempFilePath,
showMenu: true,
...normalCallbacks('toast', 'openDocument')
})
await uni.hideLoading()
},
fail: async (err) => {
console.log('downloadFile 失败', err)
await uni.hideLoading()
}
});
task.onProgressUpdate(res => {
console.log(`下载进度为${res.progress, this, Vue}%;`)
})
})
}
}
后发现有个别的博客使用的别的方法实现的,值得参考哦: https://blog.csdn.net/weixin_34406061/article/details/91460923