初始化 ui.
This commit is contained in:
134
orion-ops-ui/src/utils/file.ts
Normal file
134
orion-ops-ui/src/utils/file.ts
Normal file
@@ -0,0 +1,134 @@
|
||||
import { isEmptyStr } from '@/utils/index';
|
||||
|
||||
/**
|
||||
* 获取 base64 实际数据
|
||||
*/
|
||||
export function getBase64Data(e: string) {
|
||||
return e.substring(e.indexOf(',') + 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取文件 base64 返回 promise
|
||||
*/
|
||||
export function readFileBase64(e: any) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const reader = new FileReader();
|
||||
reader.readAsDataURL(e);
|
||||
reader.onload = res => {
|
||||
resolve(res.target?.result);
|
||||
};
|
||||
reader.onerror = err => {
|
||||
reject(err);
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析路径类型
|
||||
*/
|
||||
type PathAnalysis = {
|
||||
name: string;
|
||||
path: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取解析路径
|
||||
*/
|
||||
export function getPathAnalysis(analysisPath: string, paths: PathAnalysis[] = []): PathAnalysis[] {
|
||||
const lastSymbol = analysisPath.lastIndexOf('/');
|
||||
if (lastSymbol === -1) {
|
||||
paths.unshift({
|
||||
name: '/',
|
||||
path: '/'
|
||||
});
|
||||
return paths;
|
||||
}
|
||||
const name = analysisPath.substring(lastSymbol, analysisPath.length);
|
||||
if (!isEmptyStr(name) && name !== '/') {
|
||||
paths.unshift({
|
||||
name: name.substring(1, name.length),
|
||||
path: analysisPath
|
||||
});
|
||||
}
|
||||
return getPathAnalysis(analysisPath.substring(0, lastSymbol), paths);
|
||||
}
|
||||
|
||||
/**
|
||||
* 替换路径
|
||||
*/
|
||||
export function getPath(path: string) {
|
||||
return path.replace(new RegExp('\\\\+', 'g'), '/')
|
||||
.replace(new RegExp('/+', 'g'), '/');
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取父级路径
|
||||
*/
|
||||
export function getParentPath(path: string) {
|
||||
const paths = getPath(path).split('/');
|
||||
const len = paths.length;
|
||||
if (len <= 2) {
|
||||
return '/';
|
||||
}
|
||||
let parent = '';
|
||||
for (let i = 0; i < len - 1; i++) {
|
||||
parent += paths[i];
|
||||
if (i !== len - 2) {
|
||||
parent += '/';
|
||||
}
|
||||
}
|
||||
return parent;
|
||||
}
|
||||
|
||||
/**
|
||||
* 下载文件
|
||||
*/
|
||||
export function downloadFile(res: any, fileName: string) {
|
||||
const blob = new Blob([res.data]);
|
||||
const tempLink = document.createElement('a');
|
||||
const blobURL = window.URL.createObjectURL(blob);
|
||||
tempLink.style.display = 'none';
|
||||
tempLink.href = blobURL;
|
||||
if (!fileName) {
|
||||
fileName = res.headers['content-disposition']
|
||||
? res.headers['content-disposition'].split(';')[1].split('=')[1]
|
||||
: new Date().getTime();
|
||||
}
|
||||
tempLink.download = decodeURIComponent(fileName);
|
||||
if (typeof tempLink.download === 'undefined') {
|
||||
tempLink.target = '_blank';
|
||||
}
|
||||
document.body.appendChild(tempLink);
|
||||
tempLink.click();
|
||||
document.body.removeChild(tempLink);
|
||||
window.URL.revokeObjectURL(blobURL);
|
||||
}
|
||||
|
||||
/**
|
||||
* 10进制权限 转 字符串权限
|
||||
*/
|
||||
export function permission10toString(permission: number) {
|
||||
const ps = (permission + '');
|
||||
let res = '';
|
||||
for (let i = 0; i < ps.length; i++) {
|
||||
const per = ps.charAt(i) as any;
|
||||
if ((per & 4) === 0) {
|
||||
res += '-';
|
||||
} else {
|
||||
res += 'r';
|
||||
}
|
||||
if ((per & 2) === 0) {
|
||||
res += '-';
|
||||
} else {
|
||||
res += 'w';
|
||||
}
|
||||
if ((per & 1) === 0) {
|
||||
res += '-';
|
||||
} else {
|
||||
res += 'x';
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
export default null;
|
||||
@@ -1,5 +1,11 @@
|
||||
import { Md5 } from 'ts-md5';
|
||||
import { Ref } from 'vue';
|
||||
|
||||
type TargetContext = '_self' | '_parent' | '_blank' | '_top';
|
||||
|
||||
/**
|
||||
* 打开新窗口
|
||||
*/
|
||||
export const openWindow = (
|
||||
url: string,
|
||||
opts?: { target?: TargetContext; [key: string]: any }
|
||||
@@ -9,17 +15,197 @@ export const openWindow = (
|
||||
url,
|
||||
target,
|
||||
Object.entries(others)
|
||||
.reduce((preValue: string[], curValue) => {
|
||||
const [key, value] = curValue;
|
||||
return [...preValue, `${key}=${value}`];
|
||||
}, [])
|
||||
.join(',')
|
||||
.reduce((preValue: string[], curValue) => {
|
||||
const [key, value] = curValue;
|
||||
return [...preValue, `${key}=${value}`];
|
||||
}, [])
|
||||
.join(',')
|
||||
);
|
||||
};
|
||||
|
||||
/**
|
||||
* url 正则
|
||||
*/
|
||||
export const regexUrl = new RegExp(
|
||||
'^(?!mailto:)(?:(?:http|https|ftp)://)(?:\\S+(?::\\S*)?@)?(?:(?:(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}(?:\\.(?:[0-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))|(?:(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)(?:\\.(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)*(?:\\.(?:[a-z\\u00a1-\\uffff]{2,})))|localhost)(?::\\d{2,5})?(?:(/|\\?|#)[^\\s]*)?$',
|
||||
'i'
|
||||
);
|
||||
|
||||
/**
|
||||
* 触发鼠标事件
|
||||
*/
|
||||
export const triggerMouseEvent = (ref: Ref, e = 'click') => {
|
||||
const event = new MouseEvent(e, {
|
||||
view: window,
|
||||
bubbles: true,
|
||||
cancelable: true,
|
||||
});
|
||||
ref.value.dispatchEvent(event);
|
||||
};
|
||||
|
||||
/**
|
||||
* md5
|
||||
*/
|
||||
export function md5(plain: string): string {
|
||||
return Md5.hashStr(plain);
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断值是否非空
|
||||
*/
|
||||
export function isEmptyStr(val: any) {
|
||||
return typeof (val) === 'undefined' || val == null || val === '';
|
||||
}
|
||||
|
||||
/**
|
||||
* 复制到剪切板
|
||||
*/
|
||||
// export function copyToClipboard(content: any) {
|
||||
// const clipboardData = window.clipboardData;
|
||||
// if (clipboardData) {
|
||||
// clipboardData.clearData();
|
||||
// clipboardData.setData('Text', content);
|
||||
// return true;
|
||||
// } else if (document.execCommand) {
|
||||
// const el = document.createElement('textarea');
|
||||
// el.value = content;
|
||||
// el.setAttribute('readonly', '');
|
||||
// el.style.position = 'absolute';
|
||||
// el.style.left = '-9999px';
|
||||
// document.body.appendChild(el);
|
||||
// el.select();
|
||||
// document.execCommand('copy');
|
||||
// document.body.removeChild(el);
|
||||
// return true;
|
||||
// }
|
||||
// return false;
|
||||
// }
|
||||
|
||||
/**
|
||||
* 获取剪切板内容 返回 promise
|
||||
*/
|
||||
export function getClipboardText() {
|
||||
return navigator.clipboard.readText();
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化时间
|
||||
*/
|
||||
export function dateFormat(date = new Date(), pattern = 'yyyy-MM-dd HH:mm:ss') {
|
||||
const o = {
|
||||
'M+': date.getMonth() + 1,
|
||||
'd+': date.getDate(),
|
||||
'H+': date.getHours(),
|
||||
'm+': date.getMinutes(),
|
||||
's+': date.getSeconds(),
|
||||
'q+': Math.floor((date.getMonth() + 3) / 3),
|
||||
'S+': date.getMilliseconds()
|
||||
};
|
||||
let reg = /(y+)/;
|
||||
if (reg.test(pattern)) {
|
||||
// @ts-ignore
|
||||
const match = reg.exec(pattern)[1];
|
||||
pattern = pattern.replace(match, (date.getFullYear() + '').substring(4 - match.length));
|
||||
}
|
||||
for (const k in o) {
|
||||
let reg = new RegExp('(' + k + ')');
|
||||
if (reg.test(pattern)) {
|
||||
// @ts-ignore
|
||||
const match = reg.exec(pattern)[1];
|
||||
// @ts-ignore
|
||||
pattern = pattern.replace(match, (match.length === 1) ? o[k] : ('00' + o[k]).substring(('' + o[k]).length));
|
||||
}
|
||||
}
|
||||
return pattern;
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化秒
|
||||
*/
|
||||
export function formatSecond(second: number, p = 'HH:mm') {
|
||||
return dateFormat(new Date(~~second * 1000), p);
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化数字为 ,分割
|
||||
*/
|
||||
export function formatNumber(value: number = 0) {
|
||||
const list = (value + '').split('.');
|
||||
const prefix = list[0].charAt(0) === '-' ? '-' : '';
|
||||
let num = prefix ? list[0].slice(1) : list[0];
|
||||
let result = '';
|
||||
while (num.length > 3) {
|
||||
result = `,${num.slice(-3)}${result}`;
|
||||
num = num.slice(0, num.length - 3);
|
||||
}
|
||||
if (num) {
|
||||
result = num + result;
|
||||
}
|
||||
return `${prefix}${result}${list[1] ? `.${list[1]}` : ''}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断是否为数字
|
||||
*/
|
||||
export function isNumber(value: any, decimal = true, negative = true) {
|
||||
let reg;
|
||||
if (decimal && negative) {
|
||||
reg = /^-?[0-9]*(\.[0-9]*)?$/;
|
||||
} else if (!decimal && negative) {
|
||||
reg = /^-?[0-9]*$/;
|
||||
} else if (decimal && !negative) {
|
||||
reg = /^[0-9]*(\.[0-9]*)?$/;
|
||||
} else if (!decimal && !negative) {
|
||||
reg = /^[0-9]*$/;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
return (!isNaN(value) && reg.test(value)) || value === '';
|
||||
}
|
||||
|
||||
/**
|
||||
* 替换数字
|
||||
*/
|
||||
export function replaceNumber(value: string) {
|
||||
const s = value.charAt(value.length - 1);
|
||||
if (s === '.' || s === '-') {
|
||||
return s.substring(0, value.length - 1);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前页面的缩放值
|
||||
*/
|
||||
export function detectZoom() {
|
||||
let ratio = 0;
|
||||
if (window.devicePixelRatio !== undefined) {
|
||||
ratio = window.devicePixelRatio;
|
||||
} else if (window.outerWidth !== undefined && window.innerWidth !== undefined) {
|
||||
ratio = window.outerWidth / window.innerWidth;
|
||||
}
|
||||
if (ratio) {
|
||||
ratio = Math.round(ratio * 100);
|
||||
}
|
||||
return ratio;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取页面路由
|
||||
*/
|
||||
export function getRoute(url = location.href) {
|
||||
return url.substring(url.lastIndexOf('#') + 1).split('?')[0];
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取唯一的 UUID
|
||||
*/
|
||||
export function getUUID() {
|
||||
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
|
||||
const r = Math.random() * 16 | 0;
|
||||
const v = c === 'x' ? r : (r & 0x3 | 0x8);
|
||||
return v.toString(16);
|
||||
});
|
||||
}
|
||||
|
||||
export default null;
|
||||
|
||||
@@ -9,11 +9,11 @@ export const successResponseWrap = (data: unknown) => {
|
||||
data,
|
||||
status: 'ok',
|
||||
msg: '请求成功',
|
||||
code: 20000,
|
||||
code: 200,
|
||||
};
|
||||
};
|
||||
|
||||
export const failResponseWrap = (data: unknown, msg: string, code = 50000) => {
|
||||
export const failResponseWrap = (data: unknown, msg: string, code = 5000) => {
|
||||
return {
|
||||
data,
|
||||
status: 'fail',
|
||||
|
||||
Reference in New Issue
Block a user