parent
9d04c1b71e
commit
69454a3b04
@ -1,16 +1,70 @@
|
|||||||
import { ElMessage } from './element-plus';
|
import { ElMessage } from './element-plus';
|
||||||
|
/**
|
||||||
export const copy = (content) => {
|
* 复制文本
|
||||||
const el = document.createElement('textarea');
|
* @param {string} content 要复制的内容
|
||||||
el.style.position = 'absolute';
|
*/
|
||||||
el.style.zIndex = '-9999';
|
export const copy = async (content) => {
|
||||||
el.value = content;
|
if (typeof navigator?.clipboard?.writeText === 'function') {
|
||||||
document.body.appendChild(el);
|
try {
|
||||||
el.select();
|
await navigator.clipboard.writeText(content);
|
||||||
document.execCommand('copy');
|
ElMessage.success('复制成功');
|
||||||
el.remove();
|
} catch (e) {
|
||||||
ElMessage.success('复制成功');
|
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();
|
||||||
|
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) => {
|
export default (app) => {
|
||||||
app.config.globalProperties.$copy = copy;
|
app.config.globalProperties.$copy = copy;
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in new issue