214 lines
5.4 KiB
JavaScript
214 lines
5.4 KiB
JavaScript
![]() |
import { baseUrl } from "@/config";
|
|||
|
import { isExternal } from "@/utils/validate";
|
|||
|
|
|||
|
/**
|
|||
|
* 判断是否空字符串
|
|||
|
*/
|
|||
|
export function isBlank(str) {
|
|||
|
return str == null || str === '';
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* 字符串转换为数组
|
|||
|
*/
|
|||
|
export function strToList(str, split = ',') {
|
|||
|
if (isBlank(str)) {
|
|||
|
return [];
|
|||
|
}
|
|||
|
if (str instanceof Array) {
|
|||
|
return str;
|
|||
|
}
|
|||
|
return str.split(split);
|
|||
|
}
|
|||
|
|
|||
|
export function getRealUrl(url) {
|
|||
|
return url.substring(0, url.lastIndexOf('?'))
|
|||
|
}
|
|||
|
|
|||
|
export function isEmpty(obj) {
|
|||
|
return obj == null || obj.length === 0;
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* 转换本地URL,若是本地URL则加前缀,否则直接返回
|
|||
|
* @param url
|
|||
|
*/
|
|||
|
export function parseLocalUrl(url) {
|
|||
|
if (isEmpty(url) || isExternal(url)) {
|
|||
|
return url;
|
|||
|
}
|
|||
|
return baseUrl + url;
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
// 日期格式化
|
|||
|
export function parseTime(time, pattern) {
|
|||
|
if (arguments.length === 0 || !time) {
|
|||
|
return null
|
|||
|
}
|
|||
|
const format = pattern || '{y}-{m}-{d} {h}:{i}:{s}'
|
|||
|
let date
|
|||
|
if (typeof time === 'object') {
|
|||
|
date = time
|
|||
|
} else {
|
|||
|
if ((typeof time === 'string') && (/^[0-9]+$/.test(time))) {
|
|||
|
time = parseInt(time)
|
|||
|
} else if (typeof time === 'string') {
|
|||
|
time = time.replace(new RegExp(/-/gm), '/').replace('T', ' ').replace(new RegExp(/\.[\d]{3}/gm), '');
|
|||
|
}
|
|||
|
if ((typeof time === 'number') && (time.toString().length === 10)) {
|
|||
|
time = time * 1000
|
|||
|
}
|
|||
|
date = new Date(time)
|
|||
|
}
|
|||
|
const formatObj = {
|
|||
|
y: date.getFullYear(),
|
|||
|
m: date.getMonth() + 1,
|
|||
|
d: date.getDate(),
|
|||
|
h: date.getHours(),
|
|||
|
i: date.getMinutes(),
|
|||
|
s: date.getSeconds(),
|
|||
|
a: date.getDay()
|
|||
|
}
|
|||
|
const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
|
|||
|
let value = formatObj[key]
|
|||
|
// Note: getDay() returns 0 on Sunday
|
|||
|
if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value] }
|
|||
|
if (result.length > 0 && value < 10) {
|
|||
|
value = '0' + value
|
|||
|
}
|
|||
|
return value || 0
|
|||
|
})
|
|||
|
return time_str
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* 解析地址信息
|
|||
|
* @param {Object} res - 地址信息对象
|
|||
|
* @returns {Object} 解析后的地址对象
|
|||
|
*/
|
|||
|
export function parseLocation(res) {
|
|||
|
if (!res || !res.address) {
|
|||
|
return {
|
|||
|
province: null,
|
|||
|
country: null,
|
|||
|
city: null,
|
|||
|
address: null
|
|||
|
};
|
|||
|
}
|
|||
|
|
|||
|
const addressBean = {
|
|||
|
province: null,
|
|||
|
country: null,
|
|||
|
city: null,
|
|||
|
address: null
|
|||
|
};
|
|||
|
|
|||
|
// 直辖市和特别行政区匹配
|
|||
|
const directCityRegex = /^(北京市|天津市|重庆市|上海市|香港特别行政区|澳门特别行政区)/;
|
|||
|
const directCityMatch = directCityRegex.exec(res.address);
|
|||
|
|
|||
|
if (directCityMatch) {
|
|||
|
addressBean.province = directCityMatch[1];
|
|||
|
regexAddressBean(res, addressBean);
|
|||
|
} else {
|
|||
|
// 普通省份匹配
|
|||
|
const provinceRegex = /^(.*?(省|自治区))(.*?)$/;
|
|||
|
const provinceMatch = provinceRegex.exec(res.address);
|
|||
|
|
|||
|
if (provinceMatch) {
|
|||
|
addressBean.province = provinceMatch[1];
|
|||
|
regexAddressBean(res, addressBean);
|
|||
|
} else {
|
|||
|
// 无法匹配省份的情况
|
|||
|
regexAddressBean(res, addressBean);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
return addressBean;
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* 正则解析地址信息
|
|||
|
* @param {Object} res - 地址信息对象
|
|||
|
* @param {Object} addressBean - 地址对象
|
|||
|
*/
|
|||
|
function regexAddressBean(res, addressBean) {
|
|||
|
if (!res || !res.address) return;
|
|||
|
|
|||
|
// 移除省份信息后的地址
|
|||
|
const addressWithoutProvince = res.address.replace(addressBean.province, '');
|
|||
|
|
|||
|
const regex = /^(.*?[市]|.*?地区|.*?特别行政区)(.*?[市区县])(.*?)$/g;
|
|||
|
const match = regex.exec(addressWithoutProvince);
|
|||
|
|
|||
|
if (match && match.length >= 4) {
|
|||
|
addressBean.city = match[1];
|
|||
|
addressBean.country = match[2];
|
|||
|
addressBean.address = match[3] + (res.name ? `(${res.name})` : '');
|
|||
|
} else {
|
|||
|
// 如果正则匹配失败,将整个地址作为详细地址
|
|||
|
addressBean.address = res.address + (res.name ? `(${res.name})` : '');
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
// 文件类型
|
|||
|
export const FileType = {
|
|||
|
// 图片
|
|||
|
IMAGE: ['png', 'jpg', 'jpeg', 'gif', 'bmp', 'webp', 'ico'],
|
|||
|
// 办公
|
|||
|
OFFICE: ['doc', 'docx', 'xls', 'xlsx', 'pdf', 'ppt', 'pptx'],
|
|||
|
// 音频
|
|||
|
AUDIO: ['mp3', 'wav', 'm4a', 'ogg', 'flac', 'aac'],
|
|||
|
// 视频
|
|||
|
VIDEO: ['mp4', 'avi', 'mov', 'wmv', 'flv', 'mpeg', 'mpg', 'm4v', 'webm', 'mkv'],
|
|||
|
// 压缩文件
|
|||
|
ZIP: ['zip', 'rar', '7z'],
|
|||
|
// 其他
|
|||
|
OTHER: ['exe'],
|
|||
|
// 全部
|
|||
|
all() {
|
|||
|
return [...this.IMAGE, ...this.OFFICE, ...this.AUDIO, ...this.VIDEO, ...this.OTHER, ...this.ZIP];
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
/**
|
|||
|
* 获取文件扩展名,支持文件名或url
|
|||
|
* @param {string} fileNameOrUrl 文件名或url
|
|||
|
* @returns {string} 扩展名(小写),无扩展名返回空字符串
|
|||
|
*/
|
|||
|
export function getExt(fileNameOrUrl) {
|
|||
|
if (!fileNameOrUrl) return '';
|
|||
|
// 去除url参数
|
|||
|
let cleanName = fileNameOrUrl.split('?')[0].split('#')[0];
|
|||
|
// 只取最后的文件名部分
|
|||
|
cleanName = cleanName.substring(cleanName.lastIndexOf('/') + 1);
|
|||
|
const lastDot = cleanName.lastIndexOf('.');
|
|||
|
if (lastDot > -1 && lastDot < cleanName.length - 1) {
|
|||
|
return cleanName.slice(lastDot + 1).toLowerCase();
|
|||
|
}
|
|||
|
return '';
|
|||
|
}
|
|||
|
|
|||
|
// 是否为办公文件
|
|||
|
export function isOfficeFile(url) {
|
|||
|
const ext = getExt(url);
|
|||
|
return FileType.OFFICE.includes(ext);
|
|||
|
}
|
|||
|
// 是否为音频文件
|
|||
|
export function isAudioFile(url) {
|
|||
|
const ext = getExt(url);
|
|||
|
return FileType.AUDIO.includes(ext);
|
|||
|
}
|
|||
|
|
|||
|
// 是否为图片
|
|||
|
export function isImage(url) {
|
|||
|
const ext = getExt(url);
|
|||
|
return FileType.IMAGE.includes(ext);
|
|||
|
}
|
|||
|
// 是否为视频
|
|||
|
export function isVideo(url) {
|
|||
|
const ext = getExt(url);
|
|||
|
return FileType.VIDEO.includes(ext);
|
|||
|
}
|