使用飞书小程序提供的API预览功能只能预览普通图片
如果想实现打开pdf需要调用下载和打开文件两个方法
html
<view tt:for="{{item.picList}}" tt:for-item="imgItem" class="imgItem">
<image class="imgValue" src="{{imgItem}}" data-img="{{imgItem}}" data-picList="{{item.picList}}" catchtap="_imagePreview"/>
</view>
js
// 预览
_imagePreview(ev) {
console.log('==_imagePreview:', ev.currentTarget.dataset)
let img = ev.currentTarget.dataset.img;
let picList = ev.currentTarget.dataset.picList;
// 判断是不是pdf
const is_pdf = img.indexOf('.pdf') !== -1
console.log('==>is_pdf', is_pdf, img, 'encodeURI:', encodeURI(img))
is_pdf
// 打开文档 针对pdf 先下载到本地
? tt.downloadFile({
// 示例 url
url: encodeURI(img),
success(res) {
console.log('==downloadFile res:', res)
// 拿到本地地址后打开pdf
const filePath = res.tempFilePath;
tt.openDocument({
filePath: filePath,
success(res1) {
console.log('打开文档成功', res1);
}
});
}
})
// 图片预览
: tt.previewImage({
current: img,
urls: picList.filter(imgItem => imgItem.indexOf('.pdf') === -1),
success: res => {
console.log(JSON.stringify(res));
},
fail: res => {
this._showToast(`预览失败: ${res.errMsg}`);
console.log('预览失败', res);
}
});
},
_showToast(msg, icon) {
tt.showToast({
title: msg,
icon: icon || 'error',
duration: 2000
});
console.error(msg);
},
以下是飞书开发文档入口,可以通过右上角搜索关键字功能搜索对应的API
http://open.work.sany.com.cn/document/uYjL24iN/ucDMx4yNwEjL3ATM