主要用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;}, {});},