feat: 开放API产品相关
This commit is contained in:
174
app/openapi/controller/v1/Product.php
Normal file
174
app/openapi/controller/v1/Product.php
Normal file
@@ -0,0 +1,174 @@
|
|||||||
|
<?php
|
||||||
|
declare (strict_types = 1);
|
||||||
|
|
||||||
|
namespace app\openapi\controller\v1;
|
||||||
|
|
||||||
|
use app\openapi\model\ProductModel;
|
||||||
|
|
||||||
|
class Product
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* 产品列表
|
||||||
|
*/
|
||||||
|
public function list()
|
||||||
|
{
|
||||||
|
$params = request()->get([
|
||||||
|
'category_id',
|
||||||
|
'language' => 'zh-cn',
|
||||||
|
'page/d' => 1,
|
||||||
|
'size/d' => 50
|
||||||
|
]);
|
||||||
|
|
||||||
|
if ($params['size'] > 200) {
|
||||||
|
// 每页不超过200条
|
||||||
|
$params['size'] = 200;
|
||||||
|
}
|
||||||
|
|
||||||
|
$list = ProductModel::with([
|
||||||
|
'category' => fn($query) => $query->field(['id', 'name']),
|
||||||
|
])
|
||||||
|
->field([
|
||||||
|
'id',
|
||||||
|
'category_id',
|
||||||
|
'spu',
|
||||||
|
'name',
|
||||||
|
'short_name',
|
||||||
|
'cover_image',
|
||||||
|
'desc',
|
||||||
|
'deleted_at'
|
||||||
|
])
|
||||||
|
->where(function($query) use($params) {
|
||||||
|
$model = $query->getModel();
|
||||||
|
if (!empty($params['category_id'])) {
|
||||||
|
$model->scopeCategoryId($query, $params['category_id']);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
->language($params['language']??'zh-cn')
|
||||||
|
->order(['sort' => 'asc', 'id' => 'desc'])
|
||||||
|
->hidden(['category_id'])
|
||||||
|
->paginate([
|
||||||
|
'list_row' => $params['size'],
|
||||||
|
'page' => $params['page']
|
||||||
|
])
|
||||||
|
->each(function($item) {
|
||||||
|
if (!empty($item['cover_image']) && !str_starts_with($item['cover_image'], 'http')) {
|
||||||
|
$item['cover_image'] = image_domain_concat($item['cover_image']);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $item;
|
||||||
|
});
|
||||||
|
|
||||||
|
return success('success', $list);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 产品详情
|
||||||
|
*/
|
||||||
|
public function detail()
|
||||||
|
{
|
||||||
|
$id = request()->param('id');
|
||||||
|
|
||||||
|
$product = ProductModel::with([
|
||||||
|
// 关联分类
|
||||||
|
'category' => fn($query) => $query->field(['id', 'name']),
|
||||||
|
// 关联属性
|
||||||
|
'params' => fn($query) => $query->field(['product_id', 'name', 'value'])
|
||||||
|
->hidden(['product_id']),
|
||||||
|
// 关联sku
|
||||||
|
'skus' => fn($query) => $query->withoutField(['created_at', 'updated_at'])
|
||||||
|
->with([
|
||||||
|
'sku_attr' => fn($query) => $query->with('attr')->hidden(['sku_id', 'attr_id'])
|
||||||
|
])
|
||||||
|
->hidden(['id', 'product_id']),
|
||||||
|
// 关联购买链接
|
||||||
|
'links' => fn($query) => $query->field(['product_id', 'platform_id', 'link'])
|
||||||
|
->with(['platform' => fn($query) => $query->field(['id', 'platform'])])
|
||||||
|
->hidden(['product_id', 'platform_id']),
|
||||||
|
// 关联相关产品
|
||||||
|
'related' => fn($query) => $query->field(['product_id', 'related_product_id'])
|
||||||
|
->with([
|
||||||
|
'product' => fn($query) => $query->field(['id', 'name', 'spu', 'cover_image'])->withBind([
|
||||||
|
'id',
|
||||||
|
'name',
|
||||||
|
'spu',
|
||||||
|
'cover_image'
|
||||||
|
])
|
||||||
|
])
|
||||||
|
->hidden(['product_id', 'related_product_id'])
|
||||||
|
])
|
||||||
|
->withoutField([
|
||||||
|
'language_id',
|
||||||
|
'stock_qty',
|
||||||
|
'seo_title',
|
||||||
|
'seo_keywords',
|
||||||
|
'seo_desc',
|
||||||
|
'created_at',
|
||||||
|
'updated_at',
|
||||||
|
'deleted_at'
|
||||||
|
])
|
||||||
|
->bypk($id)
|
||||||
|
->hidden(['category_id'])
|
||||||
|
->find();
|
||||||
|
|
||||||
|
// 处理封面图
|
||||||
|
if (!empty($product['cover_image']) && !str_starts_with($product['cover_image'], 'http')) {
|
||||||
|
$product['cover_image'] = image_domain_concat($product['cover_image']);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 处理视频图片
|
||||||
|
if (!empty($product['video_img']) && !str_starts_with($product['video_img'], 'http')) {
|
||||||
|
$product['video_img'] = image_domain_concat($product['video_img']);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 处理视频
|
||||||
|
if (!empty($product['video_url']) && !str_starts_with($product['video_url'], 'http')) {
|
||||||
|
$product['video_url'] = video_domain_concat($product['video_url']);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 处理详情内容中图片
|
||||||
|
if (!empty($product['detail'])) {
|
||||||
|
$product['detail'] = html_image_replace($product['detail']);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 处理sku中图片
|
||||||
|
if (!empty($product['skus'])) {
|
||||||
|
$skus = $product['skus']->toArray();
|
||||||
|
foreach ($skus as $key => $sku) {
|
||||||
|
// sku二级列表图
|
||||||
|
if (!empty($sku['main_image']) && !str_starts_with($sku['main_image'], 'http')) {
|
||||||
|
$skus[$key]['main_image'] = image_domain_concat($sku['main_image']);
|
||||||
|
}
|
||||||
|
// sku相册图
|
||||||
|
if (!empty($sku['photo_album'])) {
|
||||||
|
$photo_album = json_decode($sku['photo_album'], true);
|
||||||
|
foreach ($photo_album as $idx => $photo) {
|
||||||
|
if (!empty($photo) && !str_starts_with($photo, 'http')) {
|
||||||
|
$photo_album[$idx] = image_domain_concat($photo);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$skus[$key]['photo_album'] = $photo_album;
|
||||||
|
}
|
||||||
|
// sku属性图片
|
||||||
|
foreach ($sku['sku_attr'] as $idx => $attr) {
|
||||||
|
if (!empty($attr['attr_value']) && !str_starts_with($attr['attr_value'], 'http')) {
|
||||||
|
$skus[$key]['sku_attr'][$idx]['attr_value'] = image_domain_concat($attr['attr_value']);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$product['skus'] = $skus;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 处理相关产品中图片
|
||||||
|
if (!empty($product['related'])) {
|
||||||
|
$related = $product['related']->toArray();
|
||||||
|
foreach ($related as $key => $item) {
|
||||||
|
if (!empty($item['cover_image']) && !str_starts_with($item['cover_image'], 'http')) {
|
||||||
|
$related[$key]['cover_image'] = image_domain_concat($item['cover_image']);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$product['related'] = $related;
|
||||||
|
}
|
||||||
|
|
||||||
|
return success('success', $product);
|
||||||
|
}
|
||||||
|
}
|
||||||
51
app/openapi/controller/v1/ProductCategory.php
Normal file
51
app/openapi/controller/v1/ProductCategory.php
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
<?php
|
||||||
|
declare (strict_types = 1);
|
||||||
|
|
||||||
|
namespace app\openapi\controller\v1;
|
||||||
|
|
||||||
|
use app\openapi\model\ProductCategoryModel;
|
||||||
|
|
||||||
|
class ProductCategory
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* 产品分类列表
|
||||||
|
*/
|
||||||
|
public function list()
|
||||||
|
{
|
||||||
|
$params = request()->get([
|
||||||
|
'parent_id',
|
||||||
|
'language' => 'zh-cn',
|
||||||
|
'page' => 1,
|
||||||
|
'size' => 50
|
||||||
|
]);
|
||||||
|
|
||||||
|
if ($params['size'] > 200) {
|
||||||
|
// 每页不超过200条
|
||||||
|
$params['size'] = 200;
|
||||||
|
}
|
||||||
|
|
||||||
|
$categories = ProductCategoryModel::withoutField([
|
||||||
|
'language_id',
|
||||||
|
'unique_id',
|
||||||
|
'related_tco_category',
|
||||||
|
'created_at',
|
||||||
|
'updated_at',
|
||||||
|
'deleted_at'
|
||||||
|
])
|
||||||
|
->language($params['language']??'zh-cn')
|
||||||
|
->parent($params['parent_id']??null)
|
||||||
|
->paginate([
|
||||||
|
'list_rows' => $params['size'],
|
||||||
|
'page' => $params['page']
|
||||||
|
])
|
||||||
|
->each(function($item) {
|
||||||
|
if (!empty($item['icon']) && !str_starts_with($item['icon'], 'http')) {
|
||||||
|
$item['icon'] = image_domain_concat($item['icon']);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $item;
|
||||||
|
});
|
||||||
|
|
||||||
|
return success('success', $categories);
|
||||||
|
}
|
||||||
|
}
|
||||||
19
app/openapi/model/LanguageModel.php
Normal file
19
app/openapi/model/LanguageModel.php
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
<?php
|
||||||
|
declare (strict_types = 1);
|
||||||
|
|
||||||
|
namespace app\openapi\model;
|
||||||
|
|
||||||
|
use app\common\model\LanguageBaseModel;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 语言模型
|
||||||
|
* @mixin \think\Model
|
||||||
|
*/
|
||||||
|
class LanguageModel extends LanguageBaseModel
|
||||||
|
{
|
||||||
|
// 所属code范围查询
|
||||||
|
public function scopeCode($query, $code)
|
||||||
|
{
|
||||||
|
$query->where('code', '=', $code);
|
||||||
|
}
|
||||||
|
}
|
||||||
33
app/openapi/model/ProductCategoryModel.php
Normal file
33
app/openapi/model/ProductCategoryModel.php
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
<?php
|
||||||
|
declare (strict_types = 1);
|
||||||
|
|
||||||
|
namespace app\openapi\model;
|
||||||
|
|
||||||
|
use app\common\model\ProductCategoryBaseModel;
|
||||||
|
use think\facade\Db;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 产品分类模型
|
||||||
|
* @mixin \think\Model
|
||||||
|
*/
|
||||||
|
class ProductCategoryModel extends ProductCategoryBaseModel
|
||||||
|
{
|
||||||
|
// 所属语言
|
||||||
|
public function scopeLanguage($query, $language)
|
||||||
|
{
|
||||||
|
$query->whereExists(function($subquery) use($language) {
|
||||||
|
$lang_model = new LanguageModel;
|
||||||
|
$subquery->model($lang_model)
|
||||||
|
->name($lang_model->getName())
|
||||||
|
->where('id', '=', Db::Raw($this->getTable() . '.language_id'))
|
||||||
|
->where('code', '=', $language);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// 所属上级范围查询
|
||||||
|
public function scopeParent($query, $parent_id)
|
||||||
|
{
|
||||||
|
if (is_null($parent_id)) return;
|
||||||
|
$query->where('pid', '=', $parent_id);
|
||||||
|
}
|
||||||
|
}
|
||||||
62
app/openapi/model/ProductModel.php
Normal file
62
app/openapi/model/ProductModel.php
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
<?php
|
||||||
|
declare (strict_types = 1);
|
||||||
|
|
||||||
|
namespace app\openapi\model;
|
||||||
|
|
||||||
|
use app\common\model\ProductBaseModel;
|
||||||
|
use think\facade\Db;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 产品模型
|
||||||
|
* @mixin \think\Model
|
||||||
|
*/
|
||||||
|
class ProductModel extends ProductBaseModel
|
||||||
|
{
|
||||||
|
// 关联分类
|
||||||
|
public function category()
|
||||||
|
{
|
||||||
|
return $this->belongsTo(ProductCategoryModel::class, 'category_id', 'id');
|
||||||
|
}
|
||||||
|
|
||||||
|
// 关联产品参数
|
||||||
|
public function params()
|
||||||
|
{
|
||||||
|
return $this->hasMany(ProductParamsModel::class, 'product_id', 'id');
|
||||||
|
}
|
||||||
|
|
||||||
|
// 关联sku
|
||||||
|
public function skus()
|
||||||
|
{
|
||||||
|
return $this->hasMany(ProductSkuModel::class, 'product_id', 'id');
|
||||||
|
}
|
||||||
|
|
||||||
|
// 关联购买链接
|
||||||
|
public function links()
|
||||||
|
{
|
||||||
|
return $this->hasMany(ProductPurchaseLinkModel::class, 'product_id', 'id');
|
||||||
|
}
|
||||||
|
|
||||||
|
// 关联相关产品
|
||||||
|
public function related()
|
||||||
|
{
|
||||||
|
return $this->hasMany(ProductRelatedModel::class, 'product_id', 'id');
|
||||||
|
}
|
||||||
|
|
||||||
|
// 根据所属语言范围查询
|
||||||
|
public function scopeLanguage($query, $language)
|
||||||
|
{
|
||||||
|
$query->whereExists(function($subquery) use($language) {
|
||||||
|
$lang_model = new LanguageModel;
|
||||||
|
$subquery->model($lang_model)
|
||||||
|
->name($lang_model->getName())
|
||||||
|
->where('id', '=', Db::Raw($this->getTable() . '.language_id'))
|
||||||
|
->where('code', '=', $language);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// 根据category_id范围查询
|
||||||
|
public function scopeCategoryId($query, $category_id)
|
||||||
|
{
|
||||||
|
$query->where('category_id', '=', $category_id);
|
||||||
|
}
|
||||||
|
}
|
||||||
15
app/openapi/model/ProductParamsModel.php
Normal file
15
app/openapi/model/ProductParamsModel.php
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
<?php
|
||||||
|
declare (strict_types = 1);
|
||||||
|
|
||||||
|
namespace app\openapi\model;
|
||||||
|
|
||||||
|
use app\common\model\ProductParamsBaseModel;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 产品参数模型
|
||||||
|
* @mixin \think\Model
|
||||||
|
*/
|
||||||
|
class ProductParamsModel extends ProductParamsBaseModel
|
||||||
|
{
|
||||||
|
//
|
||||||
|
}
|
||||||
19
app/openapi/model/ProductPurchaseLinkModel.php
Normal file
19
app/openapi/model/ProductPurchaseLinkModel.php
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
<?php
|
||||||
|
declare (strict_types = 1);
|
||||||
|
|
||||||
|
namespace app\openapi\model;
|
||||||
|
|
||||||
|
use app\common\model\ProductPurchaseLinkBaseModel;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 产品购买链接模型
|
||||||
|
* @mixin \think\Model
|
||||||
|
*/
|
||||||
|
class ProductPurchaseLinkModel extends ProductPurchaseLinkBaseModel
|
||||||
|
{
|
||||||
|
// 关联购买平台
|
||||||
|
public function platform()
|
||||||
|
{
|
||||||
|
return $this->belongsTo(ProductPurchasePlatformModel::class, 'platform_id', 'id')->bind(['platform']);
|
||||||
|
}
|
||||||
|
}
|
||||||
15
app/openapi/model/ProductPurchasePlatformModel.php
Normal file
15
app/openapi/model/ProductPurchasePlatformModel.php
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
<?php
|
||||||
|
declare (strict_types = 1);
|
||||||
|
|
||||||
|
namespace app\openapi\model;
|
||||||
|
|
||||||
|
use app\common\model\ProductPurchasePlatformBaseModel;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 产品购买链接平台模型
|
||||||
|
* @mixin \think\Model
|
||||||
|
*/
|
||||||
|
class ProductPurchasePlatformModel extends ProductPurchasePlatformBaseModel
|
||||||
|
{
|
||||||
|
//
|
||||||
|
}
|
||||||
19
app/openapi/model/ProductRelatedModel.php
Normal file
19
app/openapi/model/ProductRelatedModel.php
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
<?php
|
||||||
|
declare (strict_types = 1);
|
||||||
|
|
||||||
|
namespace app\openapi\model;
|
||||||
|
|
||||||
|
use app\common\model\ProductRelatedBaseModel;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 产品 - 相关产品模型
|
||||||
|
* @mixin \think\Model
|
||||||
|
*/
|
||||||
|
class ProductRelatedModel extends ProductRelatedBaseModel
|
||||||
|
{
|
||||||
|
// 关联产品
|
||||||
|
public function product()
|
||||||
|
{
|
||||||
|
return $this->belongsTo(ProductModel::class, 'related_product_id', 'id');
|
||||||
|
}
|
||||||
|
}
|
||||||
19
app/openapi/model/ProductSkuAttrModel.php
Normal file
19
app/openapi/model/ProductSkuAttrModel.php
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
<?php
|
||||||
|
declare (strict_types = 1);
|
||||||
|
|
||||||
|
namespace app\openapi\model;
|
||||||
|
|
||||||
|
use app\common\model\ProductSkuAttrBaseModel;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 产品sku属性模型
|
||||||
|
* @mixin \think\Model
|
||||||
|
*/
|
||||||
|
class ProductSkuAttrModel extends ProductSkuAttrBaseModel
|
||||||
|
{
|
||||||
|
// 关联属性
|
||||||
|
public function attr()
|
||||||
|
{
|
||||||
|
return $this->belongsTo(\app\common\model\ProductAttrBaseModel::class, 'attr_id', 'id')->bind(['attr_name']);
|
||||||
|
}
|
||||||
|
}
|
||||||
19
app/openapi/model/ProductSkuModel.php
Normal file
19
app/openapi/model/ProductSkuModel.php
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
<?php
|
||||||
|
declare (strict_types = 1);
|
||||||
|
|
||||||
|
namespace app\openapi\model;
|
||||||
|
|
||||||
|
use app\common\model\ProductSkuBaseModel;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 产品sku模型
|
||||||
|
* @mixin \think\Model
|
||||||
|
*/
|
||||||
|
class ProductSkuModel extends ProductSkuBaseModel
|
||||||
|
{
|
||||||
|
// 关联属性
|
||||||
|
public function skuAttr()
|
||||||
|
{
|
||||||
|
return $this->hasMany(ProductSkuAttrModel::class, 'sku_id', 'id');
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -12,10 +12,10 @@ Route::group('v1', function() {
|
|||||||
Route::get('products', 'v1.Product/list');
|
Route::get('products', 'v1.Product/list');
|
||||||
Route::group('product', function() {
|
Route::group('product', function() {
|
||||||
// 获取产品信息
|
// 获取产品信息
|
||||||
Route::get(':id', 'v1.Product/detail');
|
Route::get(':id', 'v1.Product/detail')->when('id', 'number');
|
||||||
|
|
||||||
// 获取产品分类
|
// 获取产品分类
|
||||||
Route::get('categories', 'v1.Product/categories');
|
Route::get('categories', 'v1.ProductCategory/list');
|
||||||
});
|
});
|
||||||
|
|
||||||
// 获取新闻动态
|
// 获取新闻动态
|
||||||
|
|||||||
Reference in New Issue
Block a user