export function downloadByPath(url, fileName) { const $http = this.$http || (window as any)?.HttpUtil $http && $http({ url: `/api/file/iot-base-attachment/getDownUrlByName`, method: 'get', params: { fileName: encodeURI(fileName), oldUrl: url } }) .then(res => { if (res.code === 800 && res.data) { downloadIemag(res.data) } else { downloadIemag(url, fileName) } }) .catch(err => { console.log(err, '下载失败') downloadIemag(url, fileName) }) } export function updateFileSourceName(fileId: string, fileName: string) { //修改线上存储文件的文件名称 const $http = this.$http || (window as any)?.HttpUtil return ( $http && $http({ url: `/api/file/iot-base-attachment/update-source-name`, method: 'post', params: { fileName: encodeURI(fileName), fileId } }) ) } export function getBussinessFile(businessId, businessCode) { //根据业务编码和id查询存储的文件信息 const $http = this.$http || (window as any)?.HttpUtil return ( $http && $http({ url: `/api/file/iot-base-attachment/query-attachment-info-param`, method: 'get', params: { businessId, businessTableName: businessCode } }) ) } export function saveBussinessFileInfo({ businessId, businessCode, fileIds }) { //批量存储文件详情 const $http = this.$http || (window as any)?.HttpUtil const Form = new FormData() Form.append('businessId', businessId) //业务主键 Form.append('businessTableName', businessCode) //业务标识 Form.append('fileIds', fileIds) //文件id,多个逗号隔开 return ( $http && $http({ url: `api/file/iot-base-attachment/update-more-business-info`, method: 'post', data: Form }) ) } export const downloadIemag = async (url: string, name?: string) => { const link = document.createElement('a') link.href = url if (name) { link.download = name } link.style.display = 'none' document.body.appendChild(link) link.click() document.body.removeChild(link) } export function uploadAttachment(file, menuId = '') { const $http = this.$http || (window as any)?.HttpUtil const ismenuId = menuId || '' if (!$http) { return } return new Promise(resolve => { const form = new FormData() form.append('file', file, file.name) const config = { headers: { 'Access-Control-Allow-Origin': '*', 'Content-Type': 'multipart/form-data' } } if (ismenuId !== '') { $http .post(`/api/file/iot-base-attachment/single/file-upload?funcCode=${menuId}`, form, config) .then(response => { resolve(response) }) .catch(error => { console.log(error, '上传失败') }) } else { $http .post(`/api/file/iot-base-attachment/single/file-upload`, form, config) .then(response => { const resFile = { ...response } if (menuId) { $http .get(`/api/file/iot-base-attachment/getUrl?url=${response.data.filePath}`) .then(res => { if (res.code === 200 || res.code === 800) { resolve(res.data) } }) .catch(err => { console.log(err, '上传失败') }) } else { resolve(response) } }) .catch(err => { console.log(err, '上传失败') }) } }) } export const fileToBase64 = (file: File) => { return new Promise((resolve, reject) => { try { const Reader = new FileReader() Reader.readAsDataURL(file) Reader.onload = e => { if (e?.target?.result) { resolve(e.target.result) } else { reject('file转换base64异常') } } Reader.onerror = e => { reject(e || 'file转换base64异常') } } catch (err) { reject(err) } }) }