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) => {
uni.showToast({ const content = err?.message || err?.errMsg || err?.toString();
title: err?.message || err?.errMsg || err?.toString(), if (!useModal) {
icon: 'none', uni.showToast({
duration: 2000 title: content,
}); icon: 'none',
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 = '' } = {}) => {
try { return new Promise((resolve, reject) => {
console.log('copyText'); // 输入验证
uni.setClipboardData({ if (!text && text !== '') {
data: `${text}`, const error = new Error('复制文本不能为空');
success: () => { showError(error);
console.error('复制成功'); reject(error);
try { return;
uni.showToast({ }
title: copySuccess,
icon: 'success', // 超时监测
duration: 2000 const timeoutId = setTimeout(() => {
}); let error = new Error('复制操作长时间无响应,请检查相关权限及配置是否正确');
} catch (e) { // #ifdef MP-WEIXIN
showError(e); error = new Error([
'复制操作长时间无响应!',
'原因:',
'1.微信平台->用户隐私保护指引->"剪贴板"功能未添加或审核未通过;',
'2.微信平台对剪贴板API调用频率有限制'
].join('\r\n'));
// #endif
showError(error, true);
reject(error);
}, 5000);
try {
uni.setClipboardData({
data: `${text}`,
success: (res) => {
clearTimeout(timeoutId);
try {
if (copySuccess) {
uni.showToast({
title: copySuccess,
icon: 'success',
duration: 2000
});
}
} catch (e) {
showError(e);
}
resolve(res);
},
fail: (err) => {
clearTimeout(timeoutId);
try {
uni.showToast({
title: err.message || err.errMsg || copyFailed || '复制失败',
icon: 'none',
duration: 2000
});
} catch (e) {
showError(e);
}
reject(err);
} }
}, });
fail: (err) => { } catch (err) {
console.error('复制失败:', err); clearTimeout(timeoutId);
try { showError(err);
uni.showToast({ reject(err);
title: err.message || err.errMsg || copyFailed, }
icon: 'none', });
duration: 2000 }
});
} catch (e) { /**
showError(e); * 拷贝文本(回调形式,兼容旧代码)
} * @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);
}); });
} catch (err) {
showError(err);
}
} }
/** /**