微信小程序物流消息组件

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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

木子李0531

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值