由於商品池暫未開通,於是先寫地址對接模塊,由於在提交訂單的時候,京東收到的是一二三四級地址的代碼(例如北京代碼1),一二三四級分別是省市縣街道等, 而我們商城有一套自己的地址省份圖,切儲存在js -_-||,不在數據庫,難度增大一級,總的思路


接下來准備需要的數據
先准備對四級地址進行for循環寫入數據庫代碼如下
控制器層:
/** * 更新1234級地址 * */ public function update(){ global $_W; global $_GPC; $accesstoken=pdo_fetch('select access_token from '.tablename('ewei_shop_jdvop').' where id =1'); $address=new AddressM(); for ($i=1;$i<=4;$i++){ $res=$address->updateAddress($accesstoken['access_token'],4); if ($res['error']==0){ continue; }else{ return $res; } }
model層
public function updateAddress($accesstoken,$type=1){ global $_W; switch ($type){ case 1: $this->url='https://bizapi.jd.com/api/area/getProvince'; $data=[ 'token'=>$accesstoken, ]; $resA=json_decode($this->postApi($this->url,$data),true); if (isset($resA['success'])&&$resA['success']!=true){ $res['error']=40001; $res['errorMsg']='獲取一級地址失敗'; }else{ $res['result']=$resA['result']; asort($res['result']); $res= $this->update($res['result'],$type); } return $res; break; case 2: $findOne=pdo_fetchall('select address_id from '.tablename('ewei_shop_jdvop_address').' where level=1 and uniacid=:uniacid',array(':uniacid'=>$_W['uniacid'])); if ($findOne){ $this->url='https://bizapi.jd.com/api/area/getCity'; foreach ($findOne as $key=>$value){ $data=[ 'token'=>$accesstoken, 'id'=>$value['address_id'], ]; $resA=json_decode($this->postApi($this->url,$data),true); if (isset($resA['success'])&&$resA['success']!=true){ $res['error']=40002; $res['errorMsg']='獲取二級地址失敗'; }else{ $res['result']=$resA['result']; asort($res['result']); $res= $this->update($res['result'],$type,$value['address_id']); } } }else{ $res['error']=40002; $res['errorMsg']='查詢一級地址失敗'; } return $res; break; case 3: $findTwo=pdo_fetchall('select address_id from '.tablename('ewei_shop_jdvop_address').' where level=2 and uniacid=:uniacid',array(':uniacid'=>$_W['uniacid'])); if ($findTwo){ $this->url='https://bizapi.jd.com/api/area/getCounty'; foreach ($findTwo as $key=>$value){ $data=[ 'token'=>$accesstoken, 'id'=>$value['address_id'], ]; $resA=json_decode($this->postApi($this->url,$data),true); if (isset($resA['success'])&&$resA['success']!=true){ $res['error']=40002; $res['errorMsg']='獲取三級地址失敗'; }else{ $res['result']=$resA['result']; asort($res['result']); $res= $this->update($res['result'],$type,$value['address_id']); } } }else{ $res['error']=40002; $res['errorMsg']='查詢二級地址失敗'; } return $res; break; case 4: $findTwo=pdo_fetchall('select address_id from '.tablename('ewei_shop_jdvop_address').' where level=3 and uniacid=:uniacid',array(':uniacid'=>$_W['uniacid'])); if ($findTwo){ $this->url='https://bizapi.jd.com/api/area/getTown'; foreach ($findTwo as $key=>$value){ $data=[ 'token'=>$accesstoken, 'id'=>$value['address_id'], ]; $resA=json_decode($this->postApi($this->url,$data),true); if (isset($resA['success'])&&$resA['success']!=true){ continue; }else{ $res['result']=$resA['result']; asort($res['result']); $res= $this->update($res['result'],$type,$value['address_id']); } } }else{ $res['error']=40002; $res['errorMsg']='查詢三級地址失敗'; } return $res; break; default: $res['error']=4000; $res['errorMsg']='傳入地址級別錯誤,請檢查'; return $res; break; }
但是在實際使用中卻發現,效率很慢,其中三級和四級導入運行時間很長,即使把數據庫相關字段加入索引,依舊很慢,經排查是在往京東發送查詢信息的時候,以及返回耗時較長,這個就沒辦法優化了, 等待四級地址同步完成,
其中一級地址 34個 二級地址:457個 三級地址 5171個 四級地址 :43000 條 正在同步 ,簡單觀察了兩個數據


按照這樣的話,兩個地址出現名稱不一樣的情況,后面就很難做到匹配, 於是嘗試了一下,京東自動匹配地址,發現匹配准確度挺高,這樣的話,就准備把京東自動匹配的地址作為首選項,如下圖

接下來准備使用這種自動匹配的地址,然后導入到本地的地址作為備用。
