本插件实现,在填写快递信息的页面,输入大块信息,智能解析到对应的表单元素。
快宝地址:https://open.kuaidihelp.com/api/1020
信息录入页面:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>layui</title> <meta name="renderer" content="webkit"> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"> <link rel="stylesheet" href="http://vip.gb56.net/upload/member/css/layui.css" media="all"> <link rel="stylesheet" type="text/css" href="http://vip.gb56.net/upload/member/admin/style.css" /> <link rel="stylesheet" type="text/css" href="http://vip.gb56.net/upload/member/admin/area.css" /> <!-- 注意:如果你直接复制所有代码到本地,上述css路径需要改成你本地的 --> <script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script> <script src="kuaibao.js"></script> </head> <body> <div style="padding: 20px; background-color: #F2F2F2;"> <div class="layui-row layui-col-space15"> <div class="layui-col-md12"> <div class="layui-card"> <div class="layui-card-header">快递信息录入</div> <div class="layui-card-body"> <form class="layui-form layui-form-pane" action=""> <div class="layui-form-item"> <label class="layui-form-label">日期</label> <div class="layui-input-block"> <input type="text" name="riqi" autocomplete="off" class="layui-input" id="test5" placeholder="yyyy-MM-dd HH:mm:ss"> </div> </div> <div class="layui-form-item"> <label class="layui-form-label">收件人</label> <div class="layui-input-block"> <input type="text" name="name" autocomplete="off" placeholder="收件人 需要导入的信息" class="layui-input"> </div> </div> <div class="layui-form-item"> <label class="layui-form-label">电话</label> <div class="layui-input-block"> <input type="text" name="mobile" autocomplete="off" placeholder="电话 需要导入的信息" class="layui-input"> </div> </div> <div class="layui-form-item"> <label class="layui-form-label">地址</label> <div class="layui-input-block"> <input type="text" name="detail" autocomplete="off" placeholder="地址 需要导入的信息" class="layui-input"> </div> </div> <div class="layui-form-item"> <label class="layui-form-label">数量</label> <div class="layui-input-block"> <input type="text" name="title" autocomplete="off" placeholder="数量" class="layui-input"> </div> </div> <div class="layui-form-item"> <label class="layui-form-label">价格</label> <div class="layui-input-block"> <input type="text" name="title" autocomplete="off" placeholder="价格" class="layui-input"> </div> </div> <div class="layui-form-item"> <label class="layui-form-label">快递公司</label> <div class="layui-input-block"> <select name="interest" lay-filter="aihao"> <option value=""></option> <option value="0">顺丰快递</option> <option value="1" selected="">圆通快递</option> <option value="2">申通快递</option> <option value="3">韵达快递</option> <option value="4">天天快递</option> </select> </div> </div> <div class="layui-form-item"> <label class="layui-form-label">快递单号</label> <div class="layui-input-block"> <input type="text" name="title" autocomplete="off" placeholder="请输入标题" class="layui-input"> </div> </div> <div class="layui-form-item"> <label class="layui-form-label">快递费</label> <div class="layui-input-block"> <input type="text" name="title" autocomplete="off" placeholder="请输入标题" class="layui-input"> </div> </div> <div class="layui-form-item layui-form-text"> <label class="layui-form-label">文本域</label> <div class="layui-input-block"> <textarea placeholder="请输入内容" class="layui-textarea"></textarea> </div> </div> <div class="layui-form-item"> <button class="layui-btn" lay-submit="" lay-filter="demo2">跳转式提交</button> <button class="layui-btn jiexi">智能解析</button> </div> </form> </div> </div> </div> </div> </div> <script src="http://vip.gb56.net/upload/member/layui.js"></script> <script> layui.use(['form', 'layedit', 'laydate'], function(){ var form = layui.form ,layer = layui.layer ,layedit = layui.layedit ,laydate = layui.laydate; //日期时间选择器 laydate.render({ elem: '#test5' ,type: 'datetime' }); }); </script> </body> </html>
对应的js:
$(function(){ $('.jiexi').on('click',function(){ var data={text:$('.layui-textarea').val()}; var url="index.php"; $.post(url,data,function(r){ if(r.msg=='成功'){ $("input[name='mobile']").val(r.data[0].mobile); $("input[name='name']").val(r.data[0].name); $("input[name='detail']").val(r.data[0].province_name+r.data[0].city_name+r.data[0].county_name+r.data[0].detail); } },"json"); return false; }); });
智能解析index.php页面:
<?php $host = "https://kop.kuaidihelp.com/api"; $method = "POST"; $headers = array(); //根据API的要求,定义相对应的Content-Type array_push($headers, "Content-Type".":"."application/x-www-form-urlencoded; charset=UTF-8"); $api_key='**********************************************'; $app_id='******'; $app_name='cloud.address.resolve'; $time=time(); $text=$_POST[text]; //$text='浙江省绍兴市诸暨市浣东街道西子公寓北区电话:13905857430 衣服 食物 '; // 按照规则(md5(app_id + method + ts + api_key))生成的验证合法性签名 $sign=md5($app_id.$app_name.$time.$api_key); //cloud.address.resolve $querys = ""; $bodys = [ "app_id"=>$app_id, "method"=>'cloud.address.resolve', "sign"=>$sign, "ts"=>$time, "data"=>'{"text":"'.$text.'","multimode":false}' ]; $bodys = http_build_query($bodys); $url = $host; $curl = curl_init(); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); curl_setopt($curl, CURLOPT_FAILONERROR, false); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_HEADER, true); if (1 == strpos("$".$host, "https://")) { curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); } curl_setopt($curl, CURLOPT_POSTFIELDS, $bodys); $result=curl_exec($curl); curl_close($curl); echo strstr($result,'{');