public function follow_waybill(){
// 传运单接口 follow_waybill
$order_num = $this->request->request('order_num');
$tokenInfo = $this->getXcxAccessToken();
$access_token = $tokenInfo['access_token'];
$follow_waybill_url = 'https://api.weixin.qq.com/cgi-bin/express/delivery/open_msg/follow_waybill?access_token='.$access_token;
$order_info = db('goods_order')->where('order_num',$order_num)->find();
$user_info = db('user')->where('id',$order_info['userid'])->find();
$follow_waybill_data['openid'] = $user_info['xcx_openid'];//用户openid
// $follow_waybill_data['sender_phone'] = '15666666666';//寄件人手机号(非必填)
$follow_waybill_data['receiver_phone'] = $order_info['use_tel'];//收件人手机号
$follow_waybill_data['waybill_id'] = $order_info['coupon_code'];//运单号
$follow_waybill_data['goods_info'] = [
'detail_list'=>[
[
'goods_name' => $order_info['goods_name'],
'goods_img_url' => $order_info['goods_img'],
]
],
];//商品信息
//$follow_waybill_data['trans_id'] = $order_info['wx_order'];//交易单号(微信支付生成的交易单号,一般以420开头)
$follow_waybill_res = $this->curlPostJson($follow_waybill_url,$follow_waybill_data);
$res_arr = json_decode($follow_waybill_res,true);
if($res_arr['errcode'] == 0 && $res_arr['errcode'] == 'ok'){
$waybill_token = $res_arr['waybill_token'];//查询id
// 查运单接口query_follow_trace
$query_follow_trace_url = 'https://api.weixin.qq.com/cgi-bin/express/delivery/open_msg/query_follow_trace?access_token='.$access_token;
$query_follow_trace_data['openid'] = $user_info['xcx_openid'];//用户openid
$query_follow_trace_data['waybill_token'] = $waybill_token;//查询id
$query_follow_trace_res = $this->curlPostContents($query_follow_trace_url,$query_follow_trace_data);
$query_follow_trace_res_arr = json_decode($query_follow_trace_res,true);
if($query_follow_trace_res_arr['errcode'] == 0 && $query_follow_trace_res_arr['errmsg'] == 'ok'){
$waybill_info = $query_follow_trace_res_arr['waybill_info'];
if($waybill_info['status'] == 0){
$waybill_info['status_explain'] = '运单不存在或者未揽收';
}else if($waybill_info['status'] == 1){
$waybill_info['status_explain'] = '已揽件';
}else if($waybill_info['status'] == 2){
$waybill_info['status_explain'] = '运输中';
}else if($waybill_info['status'] == 3){
$waybill_info['status_explain'] = '派件中';
}else if($waybill_info['status'] == 4){
$waybill_info['status_explain'] = '已签收';
}else if($waybill_info['status'] == 5){
$waybill_info['status_explain'] = '异常';
}else if($waybill_info['status'] == 6){
$waybill_info['status_explain'] = '代签收';
}
$this->success('success',$waybill_info);
}else{
$this->error('信息有误!');
}
}else{
$this->error('信息有误!');
}
}
public function getXcxAccessToken(){
$wechat = Config::get('site.wechatapp');
$appid = $wechat['app_id'];
$secret = $wechat['app_secret'];
$url = "https://api.weixin.qq.com/cgi-bin/token? grant_type=client_credential&appid=".$appid."&secret=".$secret;
$response = $this->curlGet($url);
$ret = (array)json_decode($response, true);
return $ret ? $ret : [];
}
public function curlGet($url){
$headerArray = array("Content-type:application/json;","Accept:application/json");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch,CURLOPT_HTTPHEADER,$headerArray);
$output = curl_exec($ch);
curl_close($ch);
//$output = json_decode($output,true);
return $output;
}
public function curlPostJson($url, $data = array(), $timeout=10){
$data_string = json_encode($data,JSON_UNESCAPED_UNICODE);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS,$data_string);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_TIMEOUT,$timeout);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$result = curl_exec($ch);
if ($no = curl_errno($ch)) {
$error = curl_error($ch);
curl_close($ch);
if(in_array(intval($no), [7, 28], true)){
throw new Exception('连接或请求超时' . $error, $no);
}
}
curl_close($ch);
return $result;
}
微信小程序物流消息组件
于 2023-03-08 13:37:55 首次发布