122 lines
3.2 KiB
JavaScript
122 lines
3.2 KiB
JavaScript
/**
|
||
* 显示消息提示框
|
||
* @param content { icon:'none/loading/sucess/fail/exception/', title:'(您要显示的文字消息)'} 提示的标题
|
||
*/
|
||
export function toast(content) {
|
||
uni.showToast({
|
||
icon:'none',
|
||
title: content
|
||
})
|
||
}
|
||
|
||
/**
|
||
* 显示模态弹窗
|
||
* @param content 提示的标题
|
||
*/
|
||
export function showConfirm(content) {
|
||
return new Promise((resolve, reject) => {
|
||
uni.showModal({
|
||
title: '提示',
|
||
content: content,
|
||
cancelText: '取消',
|
||
confirmText: '确定',
|
||
success: function(res) {
|
||
resolve(res)
|
||
}
|
||
})
|
||
})
|
||
}
|
||
|
||
/**
|
||
* 解密
|
||
* @param {*} hexString
|
||
* @returns
|
||
*/
|
||
export function decrypto( hexString ) {
|
||
return JSON.parse( Buffer.from(hexString, 'hex').toString('binary') )
|
||
}
|
||
|
||
/**
|
||
* 加密
|
||
* @param {*} jsonString
|
||
* @returns
|
||
*/
|
||
export function encrypto (jsonString) {
|
||
return Buffer.from(JSON.stringify(jsonString), 'binary').toString('hex')
|
||
}
|
||
|
||
/**
|
||
* 参数处理
|
||
* @param params 参数
|
||
*/
|
||
export function tansParams(params) {
|
||
let result = ''
|
||
for (const propName of Object.keys(params)) {
|
||
const value = params[propName]
|
||
var part = encodeURIComponent(propName) + "="
|
||
if (value !== null && value !== "" && typeof (value) !== "undefined") {
|
||
if (typeof value === 'object') {
|
||
for (const key of Object.keys(value)) {
|
||
if (value[key] !== null && value[key] !== "" && typeof (value[key]) !== 'undefined') {
|
||
let params = propName + '[' + key + ']'
|
||
var subPart = encodeURIComponent(params) + "="
|
||
result += subPart + encodeURIComponent(value[key]) + "&"
|
||
}
|
||
}
|
||
} else {
|
||
result += part + encodeURIComponent(value) + "&"
|
||
}
|
||
}
|
||
}
|
||
return result
|
||
}
|
||
|
||
/**
|
||
* 复制粘贴板
|
||
* @param {String} text 需要复制的dom元素
|
||
*/
|
||
export const copyFn = (text) => {
|
||
// 处理 Vue 组件实例(如果 ref 绑定在组件上)
|
||
if (text && text.$el && text.$el instanceof Node) {
|
||
text = text.$el;
|
||
}
|
||
|
||
// 严格验证节点类型
|
||
if (!(text instanceof Node) || typeof text.nodeType !== 'number') {
|
||
uni.showToast({ title: '复制失败:无效内容', icon: 'none' });
|
||
return;
|
||
}
|
||
|
||
let range, selection;
|
||
try {
|
||
if (document.body.createTextRange) {
|
||
range = document.body.createTextRange();
|
||
range.moveToElementText(text);
|
||
range.select();
|
||
} else if (window.getSelection) {
|
||
selection = window.getSelection();
|
||
range = document.createRange();
|
||
// 再次验证节点有效性
|
||
if (text.isConnected) {
|
||
range.selectNodeContents(text);
|
||
selection.removeAllRanges();
|
||
selection.addRange(range);
|
||
} else {
|
||
throw new Error('节点已从DOM中移除');
|
||
}
|
||
} else {
|
||
throw new Error('浏览器不支持复制操作');
|
||
}
|
||
|
||
const success = document.execCommand('Copy');
|
||
uni.showToast({ title: success ? '复制成功' : '复制失败', icon: 'none' });
|
||
} catch (e) {
|
||
uni.showToast({ title: `复制失败:${e.message}`, icon: 'none' });
|
||
console.error('复制错误:', e);
|
||
} finally {
|
||
// 清除选中状态(避免iOS等设备上的视觉残留)
|
||
if (selection && range) {
|
||
setTimeout(() => selection.removeAllRanges(), 100);
|
||
}
|
||
}
|
||
} |