主要用uni.getClipboardData(OBJECT),更多信息可以到uniapp官网查看
以下实现方式 1利用api, 2针对判断
优化方案,在线API
handleConfirm2(){
let that = this;
promisRequest({
url: 'https://wangzc.wang/smAddress',
data: {
"address": that.inputValueAnalysis
},
method: "POST"
}).then(res => {
//赋值
that.receiveName=res.data.name;
that.receiveTell=res.data.phone;
that.receiveAddress=res.data.address;
that.addressData=res.data.province+res.data.city+res.data.county;
uni.showToast({
title:"内容已识别~",
icon:"none"
})
console.log(res)
this.handleCancel2();
}).finally(()=>{
this.handleCancel2();
})
},
主函数
openPaste(){
let that=this;
uni.getClipboardData({
success: function (res) {
let inputStr=res.data;
console.log("剪切板内容",res.data);
if(inputStr.includes("详细地址")&&!inputStr.includes("收件人")
&&!inputStr.includes("手机号码")&&!inputStr.includes("所在地区")
){
//是拼多多复制的地址
let obj=that.parsePinduoduoInfo(inputStr);
if(
obj !== undefined&&
obj["手机号码"] !== undefined&&
obj["所在地区"] !== undefined&&
obj["详细地址"] !== undefined&&
obj["收件人"] !== undefined
){
//当前新增地址赋值
uni.showModal({
title: "提示",
content: "检测到当前有地址,是否添加地址?",
success: (res) => {
if (res.confirm) {
//当前数据赋值
that.receiveName=obj["收件人"];
that.receiveTell=obj["手机号码"];
that.receiveAddress=obj["详细地址"];
that.addressData=obj["所在地区"];
}
}
})
}
}
if(
inputStr.includes("详细地址")&&
inputStr.includes("所在地区")
){
let obj;
if(
inputStr.includes("收货人")&&
inputStr.includes("手机号")
){
//是淘宝复制的地址
obj=that.parseDeliveryInfo(inputStr);
}else{
//是京东复制的地址
obj=that.parseInfoString(inputStr);
}
if(
obj !== undefined&&
obj["所在地区"] !== undefined&&
obj["详细地址"] !== undefined
){
//当前新增地址赋值
uni.showModal({
title: "提示",
content: "检测到当前有地址,是否添加地址?",
success: (res) => {
if (res.confirm) {
//当前数据赋值,对淘宝,京东判断赋值
if(obj["收件人"] !== undefined){
//淘宝
that.receiveName=obj["收件人"];
that.receiveTell=obj["手机号码"];
}else{
//京东
that.receiveName=obj["收货人"];
that.receiveTell=obj["手机号"];
}
that.receiveAddress=obj["详细地址"];
that.addressData=obj["所在地区"];
}
}
})
}
}
},
fail(e){
console.log(e);
}
});
},
辅助函数
parsePinduoduoInfo(inputStr) {
// 定义正则表达式模式
const pattern = /(\S+)\s+(\d+)\s+([^\s]+?)\s+详细地址:(.+)/;
// 执行正则匹配
const matchResult = inputStr.match(pattern);
// console.log("解析的结果",matchResult);
if (matchResult) {
// 解构匹配结果
const [, name, phone, region, address] = matchResult;
// 返回结构化数据
return {
"收件人": name,
"手机号码": phone,
"所在地区": region,
"详细地址": address
};
} else {
// 可选:抛出错误或静默返回undefined
// throw new Error("解析失败,请检查输入格式");
return undefined; // 或根据需求修改为其他默认值
}
},
parseDeliveryInfo(str) {
return str.split('\n').reduce((acc, line) => {
// 分割每行的键值对(兼容中文冒号)
const [key, value] = line.split(':').map(item => item.trim());
if (key && value !== undefined) {
acc[key] = value;
}
return acc;
}, {});
},
parseInfoString(str) {
return str.split('\n').reduce((acc, line) => {
const [key, value] = line.split(':').map(item => item.trim());
if (key && value) {
acc[key] = value;
}
return acc;
}, {});
},