371 lines
10 KiB
JavaScript
371 lines
10 KiB
JavaScript
import {
|
||
BASE_URL
|
||
} from '@/api/request.js'
|
||
|
||
export function dateformat(time, format = 'yyyy-MM-dd HH:mm:ss') {
|
||
var t = new Date(time);
|
||
var tf = function(i) {
|
||
return (i < 10 ? '0' : '') + i
|
||
};
|
||
return format.replace(/yyyy|MM|dd|HH|mm|ss/g,
|
||
function(a) {
|
||
switch (a) {
|
||
case 'yyyy':
|
||
return tf(t.getFullYear());
|
||
break;
|
||
case 'MM':
|
||
return tf(t.getMonth() + 1);
|
||
break;
|
||
case 'mm':
|
||
return tf(t.getMinutes());
|
||
break;
|
||
case 'dd':
|
||
return tf(t.getDate());
|
||
break;
|
||
case 'HH':
|
||
return tf(t.getHours());
|
||
break;
|
||
case 'ss':
|
||
return tf(t.getSeconds());
|
||
break;
|
||
}
|
||
})
|
||
}
|
||
|
||
|
||
//获取文件访问路径
|
||
export function getRes(url) {
|
||
const reg = /^(http:|https:).*/gi;
|
||
// console.log(11223344, reg.test(url), url)
|
||
return reg.test(url) ? BASE_URL + url : 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 saveNetWorkFileTemp(file) {
|
||
// #ifndef H5
|
||
let fileObj = uni.getStorageSync('localNetWorkFile')
|
||
if (!fileObj) {
|
||
fileObj = {}
|
||
}
|
||
fileObj[file.name] = file.path
|
||
uni.setStorageSync('localNetWorkFile', fileObj)
|
||
// #endif
|
||
}
|
||
|
||
//清除下本地文件(网络下载)
|
||
export function removeNetWorkFileTemp(name) {
|
||
// #ifndef H5
|
||
uni.setStorageSync('localNetWorkFile', {})
|
||
// #endif
|
||
}
|
||
|
||
//记录下本地文件,方便打开的时候直接取本地
|
||
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) {
|
||
//除h5外其他端先看本地有没有,没有就下载到本地再打开,有就直接打开
|
||
// #ifdef H5
|
||
window.open(path)
|
||
// #endif
|
||
|
||
// #ifndef H5
|
||
let fileObj = uni.getStorageSync('localNetWorkFile')
|
||
if (fileObj && fileObj[name]) {
|
||
uni.openDocument({
|
||
filePath: fileObj[name],
|
||
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,
|
||
// });
|
||
//保存好再打开文件
|
||
saveNetWorkFileTemp({
|
||
name: name,
|
||
path: res.savedFilePath
|
||
})
|
||
// 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 openOutsideFile(path) {
|
||
console.log(path)
|
||
// #ifdef H5
|
||
window.open(path)
|
||
// #endif
|
||
|
||
// #ifndef H5
|
||
plus.runtime.openWeb(path)
|
||
// #endif
|
||
}
|
||
//获取表单组件文本值
|
||
export function getFormValText(obj) {
|
||
switch (obj.type) {
|
||
case 'DeptPicker':
|
||
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 getBottomLevelData(arr) {
|
||
let leafData = [];
|
||
|
||
function getLeafDataHelper(subArr) {
|
||
subArr.forEach(item => {
|
||
if (item.list) {
|
||
// 如果存在list属性,则递归调用
|
||
getLeafDataHelper(item.list);
|
||
} else {
|
||
// 如果是最底层数据,将其添加到结果数组中
|
||
leafData.push(item);
|
||
}
|
||
});
|
||
}
|
||
|
||
getLeafDataHelper(arr);
|
||
return leafData;
|
||
}
|
||
|
||
export function exitApp() {
|
||
uni.removeStorageSync('userInfo');
|
||
uni.removeStorageSync('userInfoObj');
|
||
uni.removeStorageSync('projectDetail');
|
||
uni.removeStorageSync('projectDetailObj');
|
||
uni.removeStorageSync('company');
|
||
uni.removeStorageSync('dept');
|
||
uni.removeStorageSync('moduleInfo');
|
||
|
||
// uni.redirectTo({
|
||
// url: '/pages/login/login'
|
||
// })
|
||
uni.redirectTo({
|
||
url: '/pages/projectEnd/projectIndex/projectIndex'
|
||
})
|
||
}
|
||
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);
|
||
}
|
||
|
||
export function isJSON(str) {
|
||
try {
|
||
JSON.parse(str);
|
||
} catch (e) {
|
||
// 转换出错,抛出异常
|
||
return false;
|
||
}
|
||
return true;
|
||
};
|
||
|
||
export function isHttpImage(url, httpName, url_config) {
|
||
const flag = url.includes(httpName);
|
||
console.log(url, httpName);
|
||
return flag ? url : url_config + 'image/' + url;
|
||
};
|
||
|
||
// px转rpx
|
||
export function pxToRpx(px) {
|
||
const screenWidth = uni.getSystemInfoSync().screenWidth
|
||
return (750 * Number.parseInt(px)) / screenWidth
|
||
}
|
||
|
||
// rpx转px
|
||
export function rpxToPx(rpx) {
|
||
const screenWidth = uni.getSystemInfoSync().screenWidth
|
||
return (screenWidth * Number.parseInt(rpx)) / 750
|
||
}
|
||
|
||
// 解码,输出:中文
|
||
export function decodeStr(str) {
|
||
var out, i, len, c;
|
||
var char2, char3;
|
||
out = "";
|
||
len = str.length;
|
||
i = 0;
|
||
while (i < len) {
|
||
c = str.charCodeAt(i++);
|
||
switch (c >> 4) {
|
||
case 0:
|
||
case 1:
|
||
case 2:
|
||
case 3:
|
||
case 4:
|
||
case 5:
|
||
case 6:
|
||
case 7:
|
||
// 0xxxxxxx
|
||
out += str.charAt(i - 1);
|
||
break;
|
||
case 12:
|
||
case 13:
|
||
// 110x xxxx 10xx xxxx
|
||
char2 = str.charCodeAt(i++);
|
||
out += String.fromCharCode(((c & 0x1F) << 6) | (char2 & 0x3F));
|
||
break;
|
||
case 14:
|
||
// 1110 xxxx 10xx xxxx 10xx xxxx
|
||
char2 = str.charCodeAt(i++);
|
||
char3 = str.charCodeAt(i++);
|
||
out += String.fromCharCode(((c & 0x0F) << 12) |
|
||
((char2 & 0x3F) << 6) |
|
||
((char3 & 0x3F) << 0));
|
||
break;
|
||
}
|
||
}
|
||
return out;
|
||
} |