parent
9d04c1b71e
commit
69454a3b04
@ -1,16 +1,70 @@
|
||||
import { ElMessage } from './element-plus';
|
||||
|
||||
export const copy = (content) => {
|
||||
/**
|
||||
* 复制文本
|
||||
* @param {string} content 要复制的内容
|
||||
*/
|
||||
export const copy = async (content) => {
|
||||
if (typeof navigator?.clipboard?.writeText === 'function') {
|
||||
try {
|
||||
await navigator.clipboard.writeText(content);
|
||||
ElMessage.success('复制成功');
|
||||
} catch (e) {
|
||||
ElMessage.error('复制失败');
|
||||
console.warn('复制失败', e);
|
||||
}
|
||||
} else {
|
||||
const el = document.createElement('textarea');
|
||||
el.style.position = 'absolute';
|
||||
el.style.zIndex = '-9999';
|
||||
el.value = content;
|
||||
document.body.appendChild(el);
|
||||
el.select();
|
||||
document.execCommand('copy');
|
||||
el.remove();
|
||||
if (document.execCommand('copy')) {
|
||||
ElMessage.success('复制成功');
|
||||
} else {
|
||||
ElMessage.erorr('复制失败');
|
||||
}
|
||||
el.remove();
|
||||
}
|
||||
};
|
||||
/**
|
||||
* 下载指定地址的文件
|
||||
* @param {*} fileName 文件名称
|
||||
* @param {*} href 文件地址
|
||||
*/
|
||||
export const download = (fileName, href) => {
|
||||
if ('download' in document.createElement('a')) {
|
||||
const elink = document.createElement('a');
|
||||
elink.download = fileName;
|
||||
elink.style.display = 'none';
|
||||
elink.href = href;
|
||||
document.body.appendChild(elink);
|
||||
elink.click();
|
||||
document.body.removeChild(elink);
|
||||
} else {
|
||||
window.open(href, '_blank');
|
||||
}
|
||||
};
|
||||
/**
|
||||
* 导出为excel并下载
|
||||
* @param {*} fileName 文件名称
|
||||
* @param {*} data excel数据
|
||||
*/
|
||||
export const excel = (fileName, data) => {
|
||||
const blob = new Blob([data], { type: 'text/plain,charset=UTF-8' });
|
||||
if ('download' in document.createElement('a')) {
|
||||
const elink = document.createElement('a');
|
||||
elink.download = fileName;
|
||||
elink.href = URL.createObjectURL(blob);
|
||||
document.body.appendChild(elink);
|
||||
elink.click();
|
||||
URL.revokeObjectURL(elink.href);
|
||||
document.body.removeChild(elink);
|
||||
} else {
|
||||
navigator.msSaveBlob(blob, fileName);
|
||||
}
|
||||
};
|
||||
|
||||
export default (app) => {
|
||||
app.config.globalProperties.$copy = copy;
|
||||
};
|
||||
|
Loading…
Reference in new issue