ikoncept/product-manager-adapter

与产品注册API结合使用

0.1.0 2024-07-03 06:45 UTC

This package is auto-updated.

Last update: 2024-09-03 07:09:18 UTC


README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status

安装

您可以通过composer安装此包

composer require ikoncept/product-manager-adapter
php artisan vendor:publish --tag="product-manager-adapter-config"

这是发布配置文件的内容

return [
    'api_key' => env('PRODUCT_MANAGER_ADAPTER_KEY'),
    'endpoint' => env('PRODUCT_MANAGER_ADAPTER_ENDPOINT', 'https://products.malmsten.com')
];

使用方法

路由

此包包含一些不同的控制器方法,以便更容易与产品API交互。

注意:您需要自己注册路由

推荐公共路由

// Get a tree node by ID. Usually when retrieving navigation on the front end
Route::get('/tree-nodes/{id}', [TreeNodeController::class, 'show']);

// Get a tree node via slug, usefull when retrieving a tree node from an url parameter
Route::get('/tree-nodes/{slug}/slug', [TreeNodeSlugsController::class, 'show']);

// Get a product with a specific SKU
Route::get('/products/{sku}', [ProductController::class, 'show']);

推荐私有路由

// Get a list of all available product trees
Route::get('/product-trees', [ProductTreeController::class, 'index']);

// Get a tree representation of a specific node
Route::get('/node-trees/{id}', [NodeTreeController::class, 'index']);

指定语言

产品API支持多种语言。所需语言通过 X-LOCALE 头设置。以下为示例

const payload = {
    params: {
        include: 'content',
    },
    headers: {
        'X-LOCALE': this.$i18n.locale,
    },
}
const { data } = await this.$axios.get('/api/products', payload)

过滤活动产品树中的产品

要防止其他树的产品包含在各种API调用中,可以使用 X-PRODUCT-TREES

const payload = {
    params: {
        include: 'content',
    },
    headers: {
        'X-PRODUCT-TREES':  3
    },
}
const { data } = await this.$axios.get('/api/products', payload)

使用搜索过滤器完整示例

const payload = {
    params: {
        include: 'content',
        number: 10,
        'filter[nodeIds]': this.nodeRootIds.join(','),
        'filter[search]': this.searchQuery,
    },
    headers: {
        'X-LOCALE': this.$i18n.locale,
        'X-PRODUCT-TREES': this.$store.getters['nav/productTreeRootIds'].join(',')
    },
}
const { data } = await this.$axios.get('/api/products', payload)

测试

composer test