sergeevpasha/laravel-pecom

Laravel 的 Pecom API 封装器

v1.4.1 2023-12-17 09:23 UTC

README

Maintainability Test Coverage CodeFactor Generic badge Generic badge

Laravel PECOM API 封装器

允许您

  • 通过查询字符串查找城市
  • 通过城市 ID 查找城市中的所有终端
  • 计算配送价格

预要求

您需要获取 Pecom API 密钥和登录名。密钥可以在您的个人面板中获取,请访问 https://kabinet.pecom.ru/profile

安装

composer require sergeevpasha/laravel-pecom

配置

此包有一些配置值

'key'        => env('PECOM_KEY', null),
'user'       => env('PECOM_USER', null),
'prefix'     => 'pecom',
'middleware' => ['web']

如果您只需使用 PecomClient,您可以完全跳过此配置。否则,您可以使用默认选项,并在 .env 文件中指定 PECOM_KEY 和 PECOM_USER。要充分利用预定义的路由,您需要发布配置

php artisan vendor:publish --provider="SergeevPasha\Pecom\Providers\PecomServiceProvider" --tag="config"

现在您可以更改路由前缀和中间件,使其满足您的需求

用例 #1

安装后,您可以简单地导入客户端

use SergeevPasha\Pecom\Libraries\PecomClient;

现在您需要初始化它

$client = new PecomClient('user', 'key');

现在我们可以使用这些方法

$client->findCity(string $query)
$client->getCityTerminals(int $cityId)
/* This one requires a Delivery Object, see next to see how to build it */
$client->getPrice(Delivery $delivery)

配送对象

要构建配送对象,您需要像这样传递一个数组给 fromArray() 方法

Delivery::fromArray([
    'arrival_city_id'                 => '123', // Arrival City ID, can be found using findCity() method
    'derival_city_id'                 => '123456', // Derival City ID, can be found using findCity() method
    'arrival_open_car'                => '1', // Boolean. Removable Curtains for arrival car
    'derival_open_car'                => '1, // Boolean. Removable Curtains for derival car
    'arrival_distance_type'           => '2', // Distance Type, Moscow ONLY
                                                  0 - NONE,
                                                  1 - Require transportation by Sadovoe Koltso
                                                  2 - Require transportation by Moscow district railway
                                                  3 - Require transportation by Third Transport Ring
    'derival_distance_type'           => '0', // Same as arrival
    'one_day_delivery'                => '1', // Boolean, day by day delivery
    'is_shop'                         => '0', // Boolean, sender is a shop
    'pay_date'                        => '2020-10-10', // Payment date
    'arrival_address'                 => '1', // Boolean, if delivery is required (means you are not using terminal)
    'derival_address'                 => '1', // Boolean, if pickup is required
    /* Next fields are not required */
    'require_insurance'               => '1', // Boolean, if you need to insure a cargo
    'insurance_price'                 => '100.50', // Total cargo cost to insure
    'arrival_service'                 => [
        'enabled'                     => '1', // Enable additional service on arrival        
        'arrival_service.floor'       => '10', // Floor to deliver
        'arrival_service.distance'    => '10', // Distance in Meters to deliver
        'arrival_service.elevator'    => '1', // Boolean, if there is an elevator
    ],
    'derival_service'                 => [    
        'enabled'                     => '0', // Enable additional service on derival
        'floor'                       => '10', // Pickup floor
        'distance'                    => '10', // Pickup Distance in Meters
        'elevator'                    => '1', // Boolean, if there is an elevator
    ]
    /* --- */
    'cargo'                           => [ // It's an array of arrays with cargo data
      [
          'width'                     => '1', // Width in Meters
          'height'                    => '1', // Height in Meters
          'weight'                    => '1', // Weight in KG
          'volume'                    => '1', // Weight in M3
          'max_size'                  => '1', // Max dimension size in Meters
          /* Next fileds are not required */
          'protective_package'        => '1', // Boolean, if you need a protective package
          'total_sealing_positions'   => '4', // Total sealing positions
          'oversized'                 => '1', // Boolean, if cargo is oversized
          /* --- */
      ]
    ]
])

用例 #2

有一些预定义的路由,它们将与您的路由合并。您可以使用以下方式检查它

php artisan routes:list

它实际上向路由暴露了相同的方法,所以使用方法应该很清楚。有关如何使用它的更多信息,请参阅 src/ 文件夹。