Files
income_payments_applet/assets/js/preview.js
T
2021-11-01 11:33:52 +08:00

81 lines
2.1 KiB
JavaScript

import MeetingApi from '../js/http/meetingApi'
import { baseUrl } from '../js/project.config'
/**
* 预览文件的方法
* @param fileId 文件id
* @param fileName 文件名称
*/
// 文件下载的地址 后期上线需要修改地址
const downFileUrl = `${baseUrl}/jero-boot/sys/common/download/`
// 预览
export function previewFile(fileId,fileName) {
// 文件下载的地址
const url = downFileUrl + fileId
// 文件类型 目前是通过文件名称去截取
const fileType = sliceFileTypeByFileName(fileName)
// const fileType =
wx.downloadFile({
url: url,
success: function (res) {
wx.showToast({
title: '正在打开',
icon: 'loading',
mask: true,
duration: 1000
})
const filePath = res.tempFilePath
wx.openDocument({
filePath: filePath,
fileType: fileType,
success: function (res) {
console.log('打开文档成功')
wx.hideToast()
}
})
},
fail(err) {
console.log(err, '文件打开失败');
wx.hideToast()
}
})
}
// 截取文件类型的方法
export function sliceFileTypeByFileName(fileName){
// 获取 . 的索引
let index = fileName.lastIndexOf('.')
// 截取文件类型
return fileName.slice(index+1)
}
// 通过文件id获取文件的相关信息
export function getFileInfo(id) {
return new Promise(resolve => {
let result = {}
MeetingApi.queryFileInfoById({ id: id }).then(res => {
if (res.success) {
result = res.result
}
}).finally(() => {
resolve(result)
})
})
}
// 获取一个或多个文件信息
export async function initFileList(fileIds) {
if (fileIds) {
const fileList = []
let arr = fileIds.split(',')
for (let i = 0; i < arr.length; i++) {
let fileObj = {}
fileObj = await getFileInfo(arr[i])
fileList.push({ id: fileObj.id, name: fileObj.fileName })
}
return fileList
} else{
return []
}
}