2024-04-28 10:10:03 +08:00

204 lines
5.1 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { BASE_URL } from '@/api/request.js'
//获取文件访问路径
export function getRes(url) {
const reg = /^(http:|https:).*/gi
return reg.test(url) ? url : BASE_URL + '/image/' + url
}
/**
* 去抖动函数,多次触发只执行一次
* @param call 回调函数
* @param cycle 校验周期
* @returns {(function(): void)|*}
*/
export function debounce(call, cycle = 800) {
var timer = null; // 创建一个用来存放定时器的变量
let func = call
return function(...args) {
clearTimeout(timer); //只要触发就清除
timer = setTimeout(() => {
func.apply(this, args);
}, cycle);
};
}
export function getFileSize(size) {
if (size > 1048576) {
return (size / 1048576).toFixed(1) + 'MB'
} else if (size > 1024) {
return (size / 1024).toFixed(1) + 'KB'
} else {
return size + 'B'
}
}
export function $deepCopy(obj) {
return JSON.parse(JSON.stringify(obj))
}
export function $nEmpty(obj) {
return obj && (obj || '') !== ''
}
export function parseNumber(val, precision) {
if (typeof(val) === 'string') {
return precision > 0 ? parseFloat(parseFloat(val).toFixed(precision)) : parseInt(val)
}
return val
}
//常用文件格式
const fileTypes = {
pdf: ['pdf'],
img: ['bmp', 'jpg', 'png', 'tif', 'gif', 'svg', 'psd', 'webp', 'apng'],
video: ['mp4', 'avi', 'mpeg', 'mkv', 'mov', 'rmvb', 'flv', '3gp', 'wav'],
word: ['doc', 'docx'],
excel: ['xls', 'xlsx'],
ppt: ['ppt', 'pptx'],
txt: ['txt'],
zip: ['zip', 'rar', '7z', 'iso'],
}
const fileTypeMap = new Map()
for (const key of Object.keys(fileTypes)) {
fileTypes[key].forEach(v => fileTypeMap.set(v, key))
}
/**
* 从文件名获取文件对应图标
* @param {Object} fileName 文件名
*/
export function getFileImgByType(fileName) {
const type = fileTypeMap.get(fileName.split('.').pop())
return `/static/image/filetype/${(type || '') === '' ? 'file': type}.png`
}
//记录下本地文件,方便打开的时候直接取本地
export function saveFileTemp(file) {
// #ifndef H5
let fileObj = uni.getStorageSync('localFile')
if (!fileObj) {
fileObj = {}
}
fileObj[file.name] = file
uni.setStorageSync('localFile', fileObj)
// #endif
}
//记录下本地文件,方便打开的时候直接取本地
export function removeFileTemp(name) {
// #ifndef H5
let fileObj = uni.getStorageSync('localFile')
delete fileObj[name]
uni.setStorageSync('localFile', fileObj)
// #endif
}
//下载并打开文件
export function openLocalFile(name, path) {
console.log(path)
//除h5外其他端先看本地有没有没有就下载到本地再打开有就直接打开
// #ifdef H5
window.open(path)
// #endif
// #ifndef H5
let fileObj = uni.getStorageSync('localFile')
if (fileObj && fileObj[name]) {
uni.openDocument({
filePath: fileObj.path,
showMenu: true,
fileType: getFileImgByType(name),
success: function(res) {
console.log('打开文档成功');
}
});
return
}
uni.downloadFile({
url: path,
success: function(res) {
uni.showToast({
icon: 'none',
title: '文件下载成功'
});
//ios中文文件名会报错需要编码下
let filePath = uni.getSystemInfoSync().platform === 'ios' ? res.tempFilePath : escape(res
.tempFilePath)
uni.saveFile({
tempFilePath: filePath, //临时路径
success: function(res) {
uni.showToast({
icon: 'none',
mask: true,
title: '保存在:' + res.savedFilePath, //保存路径
duration: 2000,
});
//保存好再打开文件
setTimeout(() => {
uni.openDocument({
filePath: res.savedFilePath,
showMenu: true,
fileType: getFileImgByType(name),
success: function(res) {
console.log('打开文档成功');
}
});
}, 1000)
}
})
uni.openDocument({
filePath: filePath,
showMenu: true,
fileType: getFileImgByType(name),
success: function(res) {
console.log('打开文档成功');
}
});
}
});
// #endif
}
//获取表单组件文本值
export function getFormValText(obj) {
switch (obj.type) {
case 'DeptPicker':
case 'GroupPicker':
case 'UserPicker':
case 'FileUpload':
case 'ImageUpload':
return (obj.value || []).map(v => v.name).join('、');
case 'TimeRangePicker':
case 'DateTimeRange':
case 'SelectPlus':
case 'MultipleSelect':
return (obj.value || []).join('、');
case 'ProcessIndex':
return (obj.value || []).map(v => v.startUser.name + '-' + v.name).join('、');
default:
return obj.value;
}
}
export function showItem(item, val) {
return !(item.perm === 'R' && isEm(val)) || item.perm === 'H'
}
function isEm(val) {
return !$nEmpty(val) ||
((val instanceof String) && val.trim() === '') ||
(Array.isArray(val) && val.length === 0)
}
export function isVideoLink(url) {
const videoExtensions = ['mp4', 'webm', 'ogg', 'mov', 'avi', 'flv', 'wmv'];
const extension = url.split('.').pop().toLowerCase();
return videoExtensions.includes(extension);
}
export function isImageLink(url) {
// 正则表达式匹配常见的图片文件扩展名
const imageExtensionsRegex = /\.(jpg|jpeg|png|gif|webp)$/i;
return imageExtensionsRegex.test(url);
}