聚合数据获取物流信息

<?php

/**
 * User: Eden
 */

namespace Common\Util;

use Vendor\Func\Http;

class ExpressUtil extends CommonUtil
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 获取物流
     */
    public function getExpress($com,$no) {
        $url = 'http://v.juhe.cn/exp/index';
        $params = [
            'com' => $com,
            'no'  => $no,
            'key' => C('EXPRESS_KEY'),
        ];
        $result = Http::newDoGet($url, $params,5);
        $result = json_decode($result, true);
        if ($result['error_code'] == 0) {
            return $result['result'];
        } else {
            return false;
        }
    }

    /**
     * 获取状态
     */
    public function getStatusDetail($status_detail)
    {
        switch ($status_detail) {
            case 'PENDING':
                return '待查询';
                break;
            case 'NO_RECORD':
                return '无记录';
                break;
            case 'ERROR':
                return '查询异常';
                break;
            case 'IN_TRANSIT':
                return '运输中';
                break;
            case 'DELIVERING':
                return '派件中';
                break;
            case 'SIGNED':
                return '已签收';
                break;
            case 'REJECTED':
                return '拒签';
                break;
            case 'PROBLEM':
                return '疑难件';
                break;
            case 'INVALID':
                return '无效件';
                break;
            case 'TIMEOUT':
                return '超时件';
                break;
            case 'FAILED':
                return '派送失败';
                break;
            case 'SEND_BACK':
                return '退回';
                break;
            case 'TAKING':
                return '揽件';
                break;
            default:
                return '无';
        }
    }
}

具体调用

/**
 * 获取订单物流
 */
public function getOrderExpress() {
    if (!$order_id = trim($_POST['order_id'])) {
        $this->json->err('缺少订单信息');
    }
    $order_express      = M('order_express');
    $expressUtil = new ExpressUtil();
    $order_express_info = $order_express->where(['order_id' => $order_id])->find();
    if (!$order_express_info) {
        $this->json->err('物流信息不存在');
    }

    if ($order_express_info['is_end'] == 1 && $order_express_info['result_json']) {
        $res = json_decode($order_express_info['result_json'],true);
        $res['status_detail_str'] = $expressUtil->getStatusDetail($res['status_detail']);
        $this->json->ok($res);
    }

    
    $res = $expressUtil->getExpress($order_express_info['com'],$order_express_info['no']);
    if ($res && $res['status'] == 1) {
        $edit_data = [
            'is_end' => 1,
            'result_json' => json_encode($res,JSON_UNESCAPED_UNICODE)
        ];
        $order_express->where(['id'=>$order_express_info['id']])->save($edit_data);
        $res['status_detail_str'] = $expressUtil->getStatusDetail($res['status_detail']);
    }
    $this->json->ok($res);
}

状态为1的时候,将信息保存到数据库。

posted @ 2020-07-01 10:06  TBHacker  阅读(291)  评论(0编辑  收藏  举报