Skip to content
liaofei edited this page Jan 20, 2021 · 1 revision

物流

物流为驱动类加载,可以二开加入不同驱动类型

配置文件\config\express.php 配置文件中参数default用以设置默认驱动类型,不同驱动类型参数在stores下配置

<?php

return [
    //默认查询模式
    'default' => 'express',
    //驱动模式
    'stores' => [
        //快递100
        'express' => [
            //配置参数
        ],
        //快递鸟
        'kuaidiniao' => [
             //配置参数
        ]
    ]
];

入口

\crmeb\services\express\Express.php物流驱动入口类,继承 \crmeb\basic\BaseManager

<?php

namespace crmeb\services\express;


use crmeb\basic\BaseManager;
use crmeb\services\AccessTokenServeService;
use think\Container;
use think\facade\Config;

/**
 * Class Express
 * @package crmeb\services\express
 * @mixin \crmeb\services\express\storage\Express
 */
class Express extends BaseManager
{
    /**空间名
     * @var string
     */
    protected $namespace = '\\crmeb\\services\\express\\storage\\';

    protected $handleAccessToken;
    /**
     * 默认驱动
     * @return mixed
     */
    protected function getDefaultDriver()
    {
        return Config::get('express.default', 'express');
    }
    /**
     * 获取类的实例
     * @param $class
     * @return mixed|void
     */
    protected function invokeClass($class)
    {
        if (!class_exists($class)) {
            throw new \RuntimeException('class not exists: ' . $class);
        }
        $this->getConfigFile();

        if (!$this->config) {
            $this->config = Config::get($this->configFile . '.stores.' . $this->name, []);
        }

        if (!$this->handleAccessToken) {
            $this->handleAccessToken = new AccessTokenServeService($this->config, $this->name, $this->configFile);
        }

        $handle = Container::getInstance()->invokeClass($class, [$this->name, $this->handleAccessToken, $this->configFile]);
        $this->config = [];
        return $handle;
    }
}

具体功能实现类

目录\crmeb\services\express\storage\ 必需继承\crmeb\basic\BaseExpress.php,并实现以下方法:

/**
 * 开通服务
 * @return mixed
 */
abstract public function open();

/**物流追踪
 * @return mixed
 */
abstract public function query($com, $num);

/**电子面单
 * @return mixed
 */
abstract public function dump(array $data);

/**快递公司
 * @return mixed
 */
abstract public function express(int $type, int $page, int $limit);

/**面单模板
 * @return mixed
 */
abstract public function temp($com, int $page, int $limit);

使用

实例化物流类:

$express = new Express('驱动名称', ['配置参数'])

也可以使用:

$express = app()->make(Express::class, ['驱动名称', ['配置参数']]);

查询物流,直接使用$express调用query方法传入参数就可以了:

$express->query('物流公司编号','快递单号');
Clone this wiki locally