148 lines
3.9 KiB
JavaScript
148 lines
3.9 KiB
JavaScript
/**
|
||
* 将常用的Uniapp提供的函数,存放到这里,按需引用
|
||
*/
|
||
|
||
|
||
/**
|
||
* 显示错误信息
|
||
* @param {Exception} err
|
||
* @param {Boolean} useModal
|
||
*/
|
||
const showError = (err, useModal = false) => {
|
||
const content = err?.message || err?.errMsg || err?.toString();
|
||
if (!useModal) {
|
||
uni.showToast({
|
||
title: content,
|
||
icon: 'none',
|
||
duration: 3000
|
||
});
|
||
} else {
|
||
uni.showModal({
|
||
title: '错误提示',
|
||
content,
|
||
showCancel: false,
|
||
})
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 打电话
|
||
* @param {string} mobile 电话号码
|
||
*/
|
||
export const makePhoneCall = (mobile) => {
|
||
try {
|
||
uni.makePhoneCall({
|
||
phoneNumber: `${mobile}`,
|
||
success(e) {
|
||
console.log(e);
|
||
}
|
||
});
|
||
} catch (err) {
|
||
showError(err);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 拷贝文本(返回 Promise)
|
||
* @param {*} text
|
||
* @param {*} options
|
||
* @returns {Promise} 返回 Promise,成功时 resolve,失败时 reject
|
||
*/
|
||
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 {
|
||
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);
|
||
}
|
||
});
|
||
} catch (err) {
|
||
clearTimeout(timeoutId);
|
||
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);
|
||
});
|
||
}
|
||
|
||
/**
|
||
* 打开定位
|
||
* @param {Object} options
|
||
*/
|
||
export const openLocation = ({ latitude, longitude, name } = {}) => {
|
||
try {
|
||
uni.openLocation({
|
||
latitude,
|
||
longitude,
|
||
name,
|
||
});
|
||
} catch (err) {
|
||
showError(err);
|
||
}
|
||
} |