chore(uniapp): 增加拷贝文本超时检测提示功能

This commit is contained in:
2026-01-17 15:30:02 +08:00
parent 1ebb94e9e2
commit 7ae7a1d3bd
2 changed files with 101 additions and 38 deletions

View File

@@ -38,6 +38,10 @@ const localDevConfig = ({
uniacid: 1, uniacid: 1,
domain: 'https://test.aigc-quickapp.com', domain: 'https://test.aigc-quickapp.com',
}, },
'local-2': { // 测试平台
uniacid: 2,
domain: 'http://localhost:8050/',
},
})['2811']; // 选择要使用的环境配置 })['2811']; // 选择要使用的环境配置
export default localDevConfig; export default localDevConfig;

View File

@@ -6,13 +6,23 @@
/** /**
* 显示错误信息 * 显示错误信息
* @param {Exception} err * @param {Exception} err
* @param {Boolean} useModal
*/ */
const showError = (err) => { const showError = (err, useModal = false) => {
const content = err?.message || err?.errMsg || err?.toString();
if (!useModal) {
uni.showToast({ uni.showToast({
title: err?.message || err?.errMsg || err?.toString(), title: content,
icon: 'none', icon: 'none',
duration: 2000 duration: 3000
}); });
} else {
uni.showModal({
title: '错误提示',
content,
showCancel: false,
})
}
} }
/** /**
@@ -33,43 +43,92 @@ export const makePhoneCall = (mobile) => {
} }
/** /**
* 拷贝文本 * 拷贝文本(返回 Promise
* @param {*} text * @param {*} text
* @param {*} options * @param {*} options
* @returns {Promise} 返回 Promise成功时 resolve失败时 reject
*/ */
export const copyText = (text, { copySuccess = '', copyFailed = '' } = {}) => { export const copyTextAsync = (text, { copySuccess = '', copyFailed = '' } = {}) => {
return new Promise((resolve, reject) => {
// 输入验证
if (!text && text !== '') {
const error = new Error('复制文本不能为空');
showError(error);
reject(error);
return;
}
// 超时监测
const timeoutId = setTimeout(() => {
let error = new Error('复制操作长时间无响应,请检查相关权限及配置是否正确');
// #ifdef MP-WEIXIN
error = new Error([
'复制操作长时间无响应!',
'原因:',
'1.微信平台->用户隐私保护指引->"剪贴板"功能未添加或审核未通过;',
'2.微信平台对剪贴板API调用频率有限制'
].join('\r\n'));
// #endif
showError(error, true);
reject(error);
}, 5000);
try { try {
console.log('copyText');
uni.setClipboardData({ uni.setClipboardData({
data: `${text}`, data: `${text}`,
success: () => { success: (res) => {
console.error('复制成功'); clearTimeout(timeoutId);
try { try {
if (copySuccess) {
uni.showToast({ uni.showToast({
title: copySuccess, title: copySuccess,
icon: 'success', icon: 'success',
duration: 2000 duration: 2000
}); });
}
} catch (e) { } catch (e) {
showError(e); showError(e);
} }
resolve(res);
}, },
fail: (err) => { fail: (err) => {
console.error('复制失败:', err); clearTimeout(timeoutId);
try { try {
uni.showToast({ uni.showToast({
title: err.message || err.errMsg || copyFailed, title: err.message || err.errMsg || copyFailed || '复制失败',
icon: 'none', icon: 'none',
duration: 2000 duration: 2000
}); });
} catch (e) { } catch (e) {
showError(e); showError(e);
} }
reject(err);
} }
}); });
} catch (err) { } catch (err) {
clearTimeout(timeoutId);
showError(err); showError(err);
reject(err);
} }
});
}
/**
* 拷贝文本(回调形式,兼容旧代码)
* @param {*} text
* @param {*} options
* @param {Function} callback 回调函数,接收 (success, error) 参数
*/
export const copyText = (text, options = {}, callback) => {
copyTextAsync(text, options)
.then(res => {
if (callback) callback(true, null);
})
.catch(err => {
if (callback) callback(false, err);
});
} }
/** /**