Commit fd0e8cfe by Sweet Zhang

解决点击复制的工具类方法,新增出账时默认关联保单

parent 03497a45
import { ElMessage } from 'element-plus'
export function copyToClipboard(text) {
// 方案1:使用现代 Clipboard API
if (navigator.clipboard && navigator.clipboard.writeText) {
return navigator.clipboard.writeText(text).catch((err) => {
console.error('Clipboard API 写入失败:', err);
ElMessage.error('复制失败')
// 降级到传统方案
fallbackCopyText(text);
});
}
// 方案2:降级使用传统方法
else {
fallbackCopyText(text);
}
}
// 传统复制方法
// 传统降级复制方法
function fallbackCopyText(text, typeName = '内容') {
const textarea = document.createElement('textarea');
textarea.value = text;
// 防止页面滚动或闪烁
textarea.style.position = 'fixed';
textarea.style.opacity = '0';
textarea.style.left = '-9999px';
document.body.appendChild(textarea);
try {
textarea.select();
textarea.setSelectionRange(0, 99999); // 对于移动设备
document.execCommand('copy');
console.log('传统方法复制成功');
ElMessage.success(`${typeName}已复制`)
textarea.setSelectionRange(0, 99999); // 兼容移动端
const successful = document.execCommand('copy');
if (successful) {
ElMessage.success(`${typeName}已复制`);
} else {
throw new Error('execCommand 返回失败');
}
} catch (err) {
console.error('传统方法复制失败:', err)
ElMessage.error('复制失败');
console.error('传统方法复制失败:', err);
ElMessage.error('复制失败,请手动复制');
} finally {
document.body.removeChild(textarea);
}
}
export default {
copyToClipboard
}
// 主导出函数,接收 text 和 typeName 两个参数
export default function copyToClipboard(text, typeName = '内容') {
// 1. 判断是否在安全上下文(HTTPS/localhost) 且 浏览器支持 Clipboard API
if (window.isSecureContext && navigator.clipboard && navigator.clipboard.writeText) {
navigator.clipboard.writeText(text).then(() => {
ElMessage.success(`${typeName}已复制`);
}).catch((err) => {
console.error('Clipboard API 写入失败,降级处理:', err);
// API 调用失败(如权限被拒),自动降级到传统方案
fallbackCopyText(text, typeName);
});
} else {
// 2. 非安全环境(如 HTTP)或不支持新 API,直接使用传统降级方案
fallbackCopyText(text, typeName);
}
}
\ No newline at end of file
......@@ -316,7 +316,7 @@ const searchConfig = ref([
const payRecordDialogTableVisible = ref(false)
// 新增出账记录
const addPayRecordFormModel = ref({
fortuneBizType: 'U',
fortuneBizType: 'R',
})
const addPayRecordDialogVisible = ref(false)
const addPayRecordFormRef = ref()
......@@ -375,7 +375,8 @@ const addPayRecordFormConfig = [
type: 'select',
prop: 'currency',
label: '出账币种',
dictType: 'bx_currency_type'
dictType: 'bx_currency_type',
defaultValue:'HKD'
}, {
type: 'input',
prop: 'defaultExchangeRate',
......@@ -411,7 +412,8 @@ const addPayRecordFormConfig = [
type: 'select',
prop: 'status',
label: '出账状态',
dictType: 'csf_expected_fortune_status'
dictType: 'csf_expected_fortune_status',
defaultValue:0
}
]
const handleConfirmAddPayRecord = async () => {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment