feat: 产品相关接口

This commit is contained in:
2025-01-24 17:54:04 +08:00
parent a64379f188
commit 050d7cec4b
18 changed files with 787 additions and 0 deletions

View File

@@ -0,0 +1,157 @@
<?php
declare (strict_types = 1);
namespace app\admin\controller\v1;
use app\admin\model\v1\ProductModel;
use app\admin\validate\v1\ProductValidate;
class Product
{
// 分页列表
public function index()
{
$param = request()->get([
'name',
'spu',
'category_id',
'created_at',
'is_sale',
'page/d' => 1,
'size/d' => 10
]);
$validate = new ProductValidate();
if (!$validate->scene('index')->check($param)) {
return error($validate->getError());
}
$products = ProductModel::field([
'id',
'name',
'cover_image',
'spu',
'category_id',
'sort',
'is_new',
'is_hot',
'is_sale',
'status',
'created_at'
])
->with('category')
->language(request()->lang_id)
->withSearch(['name_nullable', 'spu_nullable', 'created_at_nullable'], [
'name_nullable' => $param['name']??null,
'spu_nullable' => $param['spu']??null,
'created_at_nullable' => $param['created_at']??null,
])
->categoryNullable($param['category_id']??null)
->isSaleNullable(isset($param['is_sale']) ? (bool)$param['is_sale'] : null)
->order(['sort' => 'asc', 'id' => 'desc'])
->paginate([
'list_rows' => $param['size'],
'page' => $param['page'],
])
->bindAttr('category', ['category_name' => 'name'])
->hidden(['category_id', 'category']);
return success('获取成功', $products);
}
// 产品详情
public function read()
{
$product = ProductModel::withoutField([
'language_id',
'created_at',
'updated_at',
'deleted_at'
])
->bypk(request()->param('id'))
->find();
if (empty($product)) {
return error('产品不存在');
}
return success('获取成功', $product);
}
// 更新
public function update()
{
$id = request()->param('id');
$put = request()->put([
'category_id',
'spu',
'name',
'short_name',
'cover_image',
'desc',
'video_img',
'video_url',
'is_sale',
'is_new',
'is_hot',
'sort',
'detail',
'status' => 1,
'seo_title',
'seo_keywords',
'seo_desc'
]);
$validate = new ProductValidate();
$check_data = array_merge($put, [
'id' => $id,
'language_id' => request()->lang_id
]);
if (!$validate->scene('update')->check($check_data)) {
return error($validate->getError());
}
$product = ProductModel::bypk($id)->find();
if (is_null($product)) {
return error('请确认操作对象是否存在');
}
if (!$product->save($put)) {
return error('操作失败');
}
return success('操作成功');
}
// 上下架操作
public function updownShelves()
{
$id = request()->param('id');
$product = ProductModel::bypk($id )->find();
if (empty($product)) {
return error('请确认操作对象是否存在');
}
$product->is_sale = (int)!$product->is_sale;
if (!$product->save()) {
return error('操作失败');
}
return success('操作成功');
}
// 删除
public function delete()
{
$product = ProductModel::bypk(request()->param('id'))->find();
if (empty($product)) {
return error('请确认操作对象是否存在');
}
// 软删除
if (!$product->delete()) {
return error('操作失败');
}
return success('操作成功');
}
}

View File

@@ -0,0 +1,15 @@
<?php
declare (strict_types = 1);
namespace app\admin\model\v1;
use app\common\model\ProductCategoryBaseModel;
/**
* 产品分类模型
* @mixin \think\Model
*/
class ProductCategoryModel extends ProductCategoryBaseModel
{
//
}

View File

@@ -0,0 +1,81 @@
<?php
declare (strict_types = 1);
namespace app\admin\model\v1;
use app\common\model\ProductBaseModel;
use think\model\concern\SoftDelete;
/**
* 产品模型
* @mixin \think\Model
*/
class ProductModel extends ProductBaseModel
{
// 启用软件删除
use SoftDelete;
// 软件字段
protected $deleteTime = 'deleted_at';
// 修改自动写入时间格式
protected $autoWriteTimestamp = 'datetime';
// 分类关联查询
public function category()
{
return $this->belongsTo('ProductCategoryModel', 'category_id', 'id');
}
// 搜索器名称
public function searchNameNullableAttr($query, $value, $data)
{
if (is_null($value)) {
return;
}
$query->where('name', 'like', '%' . $value . '%');
}
// 搜索spu
public function searchSpuNullableAttr($query, $value, $data)
{
if (is_null($value)) {
return;
}
$query->where('spu', 'like', '%' . $value . '%');
}
// 搜索发布时间
public function searchCreatedAtNullableAttr($query, $value, $data)
{
if (is_array($value)) {
if (count($value) == 2) {
$query->whereBetweenTime('created_at', $value[0], $value[1]);
} else {
$query->whereTime('created_at', '>=', $value[0]);
}
}
}
// 所属语言查询
public function scopeLanguage($query, $value)
{
$query->where('language_id', '=', $value);
}
// 产品分类查询
public function scopeCategoryNullable($query, $value)
{
if (is_null($value)) {
return;
}
$query->where('category_id', '=', $value);
}
// 上架状态查询
public function scopeIsSaleNullable($query, bool|null $value)
{
if (is_null($value)) {
return;
}
$query->where('is_sale', '=', (int)$value);
}
}

View File

@@ -109,6 +109,30 @@ Route::group('v1', function () {
Route::delete('delete/:id', 'ArticleTrash/delete');
});
});
// 产品模块
Route::group('product', function () {
// 产品分页列表
Route::get('index', 'Product/index');
// 产品详情
Route::get('read/:id', 'Product/read');
// 产品新增
Route::post('save', 'Product/save');
// 产品更新
Route::put('update/:id', 'Product/update');
// 上/下架操作
Route::get('updown_shelves/:id', 'Product/updownShelves');
// 产品删除
Route::delete('delete/:id', 'Product/delete');
// 产品导出
Route::get('export', 'Product/export');
});
})->prefix('v1.');
Route::miss(function() {

View File

@@ -0,0 +1,115 @@
<?php
declare (strict_types = 1);
namespace app\admin\validate\v1;
use think\Validate;
class ProductValidate extends Validate
{
/**
* 定义验证规则
* 格式:'字段名' => ['规则1','规则2'...]
*
* @var array
*/
protected $rule = [
'category_id' => 'integer',
'spu' => 'require|max:255',
'name' => 'require|max:125',
'short_name' => 'max:64',
'cover_image' => 'max:255',
'desc' => 'max:255',
'video_img' => 'max:255',
'video_url' => 'max:255',
'is_sale' => 'in:0,1',
'is_new' => 'in:0,1',
'is_hot' => 'in:0,1',
'sort' => 'integer',
'detail' => 'max:65535',
'status' => 'in:-1,1',
'seo_title' => 'max:255',
'seo_keywords' => 'max:255',
'seo_desc' => 'max:255',
'created_at' => 'checkFormatDatetimeRange'
];
/**
* 定义错误信息
* 格式:'字段名.规则名' => '错误信息'
*
* @var array
*/
protected $message = [
'category_id.integer' => '分类ID必须为整数',
'spu.require' => 'spu不能为空',
'spu.max' => 'spu不能超过255个字符',
'name.require' => '名称不能为空',
'name.max' => '名称不能超过125个字符',
'short_name.max' => '短标题不能超过64个字符',
'cover_image.max' => '封面图不能超过255个字符',
'desc.max' => '描述不能超过255个字符',
'video_img.max' => '视频封面图不能超过255个字符',
'video_url.max' => '视频地址不能超过255个字符',
'is_sale.in' => '上架状态值错误',
'is_new.in' => '新品状态值错误',
'is_hot.in' => '热门状态值错误',
'sort.integer' => '排序值类型错误',
'detail.max' => '详情不能超过65535个字符',
'status.in' => '状态值错误',
'seo_title.max' => 'seo标题不能超过255个字符',
'seo_keywords.max' => 'seo关键字不能超过255个字符',
'seo_desc.max' => 'seo描述不能超过255个字符',
'created_at.checkFormatDatetimeRange' => '添加时间格式错误'
];
/**
* 分页列表验证场景
*/
public function sceneIndex()
{
return $this->only(['created_id', 'is_sale', 'created_at']);
}
/**
* 数据更新验证场景
*/
public function sceneUpdate()
{
return $this->only([
'category_id',
'spu',
'name',
'short_name',
'cover_image',
'desc',
'video_img',
'video_url',
'is_sale',
'is_new',
'is_hot',
'sort',
'detail',
'status',
'seo_title',
'seo_keywords',
'seo_desc',
]);
}
protected function checkFormatDatetimeRange($value, $rule, $data)
{
$val = explode(',', $value);
if (empty($val)) return '时间错误';
$v1 = strtotime($val[0]);
if (!$v1) return '时间错误';
if (count($val) == 2) {
$v2 = strtotime($val[1]);
if (!$v2) {
return '时间错误';
}
}
return true;
}
}

View File

@@ -0,0 +1,31 @@
<?php
declare (strict_types = 1);
namespace app\common\model;
use think\Model;
/**
* @mixin \think\Model
*/
class BaseModel extends Model
{
/**
* 根据主键查询
*/
public function scopeBypk($query, $pk)
{
$query->where((new static)->pk, '=', $pk);
}
/**
* 根据多主键查询
*/
public function scopeBypks($query, $pks)
{
if (!is_array($pks) && (is_string($pks) && false === strpos($pks, ','))) {
throw new \Exception('pks参数为级数或逗号分隔字符串');
}
$query->where((new static)->pk, 'in', $pks);
}
}

View File

@@ -0,0 +1,28 @@
<?php
declare (strict_types = 1);
namespace app\common\model;
/**
* 产品属性模型
* @mixin \think\Model
*/
class ProductAttrBaseModel extends BaseModel
{
// 表名
protected $name = 'product_attr';
// 主键
protected $pk = 'id';
// 字段信息
protected $schema = [
'id' => 'int',
'language_id' => 'int',
'attr_name' => 'string',
'is_system' => 'string',
'created_at' => 'datetime',
'updated_at' => 'datetime',
'deleted_at' => 'datetime',
];
}

View File

@@ -0,0 +1,26 @@
<?php
declare (strict_types = 1);
namespace app\common\model;
/**
* 产品属性特征模型
* @mixin \think\Model
*/
class ProductAttrPropBaseModel extends BaseModel
{
// 表名
protected $name = 'product_attr_prop';
// 主键
protected $pk = 'id';
// 字段信息
protected $schema = [
'id' => 'int',
'prop_name' => 'string',
'prop_value' => 'string',
'created_at' => 'datetime',
'updated_at' => 'datetime',
];
}

View File

@@ -0,0 +1,43 @@
<?php
declare (strict_types = 1);
namespace app\common\model;
/**
* 产品spu模型
* @mixin \think\Model
*/
class ProductBaseModel extends BaseModel
{
// 表名
protected $name = 'product';
// 主键
protected $pk = 'id';
// 字段信息
protected $schema = [
'id' => 'int',
'language_id' => 'int',
'category_id' => 'int',
'spu' => 'string',
'name' => 'string',
'short_name' => 'string',
'cover_image' => 'string',
'desc' => 'string',
'video_img' => 'string',
'video_url' => 'string',
'is_sale' => 'int',
'is_new' => 'int',
'is_hot' => 'int',
'sort' => 'int',
'detail' => 'string',
'status' => 'int',
'seo_title' => 'string',
'seo_keywords' => 'string',
'seo_desc' => 'string',
'created_at' => 'datetime',
'updated_at' => 'datetime',
'deleted_at' => 'datetime'
];
}

View File

@@ -0,0 +1,38 @@
<?php
declare (strict_types = 1);
namespace app\common\model;
/**
* 产品分类模型
* @mixin \think\Model
*/
class ProductCategoryBaseModel extends BaseModel
{
// 表名
protected $name = 'product_category';
// 主键
protected $pk = 'id';
// 字段信息
protected $schema = [
'id' => 'int',
'language_id' => 'int',
'unique_id' => 'string',
'pid' => 'int',
'name' => 'string',
'icon' => 'string',
'desc' => 'string',
'related_tco_category' => 'string',
'sort' => 'int',
'level' => 'int',
'is_show' => 'int',
'seo_title' => 'string',
'seo_keywords' => 'string',
'seo_desc' => 'string',
'created_at' => 'datetime',
'updated_at' => 'datetime',
'deleted_at' => 'datetime',
];
}

View File

@@ -0,0 +1,32 @@
<?php
declare (strict_types = 1);
namespace app\common\model;
/**
* 产品询盘记录模型
* @mixin \think\Model
*/
class ProductInquiryBaseModel extends BaseModel
{
// 表名
protected $name = 'product_inquiry';
// 主键
protected $pk = 'id';
// 字段信息
protected $schema = [
'id' => 'int',
'language_id' => 'int',
'corp_name' => 'string',
'fisrt_name' => 'string',
'last_name' => 'string',
'email' => 'string',
'phone' => 'string',
'country_name' => 'string',
'industry' => 'string',
'message' => 'string',
'created_at' => 'datetime'
];
}

View File

@@ -0,0 +1,27 @@
<?php
declare (strict_types = 1);
namespace app\common\model;
/**
* 产品参数模型
* @mixin \think\Model
*/
class ProductParamsBaseModel extends BaseModel
{
// 表名
protected $name = 'product_params';
// 主键
protected $pk = 'id';
// 字段信息
protected $schema = [
'id' => 'int',
'product_id' => 'int',
'name' => 'string',
'value' => 'string',
'created_at' => 'datetime',
'updated_at' => 'datetime',
];
}

View File

@@ -0,0 +1,28 @@
<?php
declare (strict_types = 1);
namespace app\common\model;
/**
* 产品购买链接模型
* @mixin \think\Model
*/
class ProductPurchaseLinkBaseModel extends BaseModel
{
// 表名
protected $name = 'product_purchase_link';
// 主键
protected $pk = 'id';
// 字段信息
protected $schema = [
'id' => 'int',
'language_id' => 'int',
'product_id' => 'int',
'platform_id' => 'int',
'link' => 'string',
'sort' => 'int',
'created_at' => 'datetime',
];
}

View File

@@ -0,0 +1,27 @@
<?php
declare (strict_types = 1);
namespace app\common\model;
/**
* 产品购买平台模型
* @mixin \think\Model
*/
class ProductPurchasePlatformBaseModel extends BaseModel
{
// 表名
protected $name = 'product_purchase_platform';
// 主键
protected $pk = 'id';
// 字段信息
protected $schema = [
'id' => 'int',
'language_id' => 'int',
'platform' => 'string',
'desc' => 'string',
'sort' => 'int',
'created_at' => 'datetime'
];
}

View File

@@ -0,0 +1,28 @@
<?php
declare (strict_types = 1);
namespace app\common\model;
/**
* 产品 - 关联产品模型
* @mixin \think\Model
*/
class ProductRelatedBaseModel extends BaseModel
{
// 表名
protected $name = 'product_related';
// 主键
protected $pk = 'id';
// 字段信息
protected $schema = [
'id' => 'int',
'product_id' => 'int',
'related_product_id' => 'int',
'desc' => 'string',
'sort' => 'int',
'created_at' => 'datetime',
'updated_at' => 'datetime',
];
}

View File

@@ -0,0 +1,23 @@
<?php
declare (strict_types = 1);
namespace app\common\model;
use think\Model;
/**
* 产品sku属性模型
* @mixin \think\Model
*/
class ProductSkuAttrBaseModel extends Model
{
// 表名
protected $name = 'product_sku_attr';
// 字段信息
protected $schema = [
'sku_id' => 'int',
'attr_id' => 'int',
'prop_id' => 'int',
];
}

View File

@@ -0,0 +1,29 @@
<?php
declare (strict_types = 1);
namespace app\common\model;
/**
* 产品sku模型
* @mixin \think\Model
*/
class ProductSkuBaseModel extends BaseModel
{
// 表名
protected $name = 'product_sku';
// 主键
protected $pk = 'id';
// 字段信息
protected $schema = [
'id' => 'int',
'product_id' => 'int',
'sku' => 'string',
'main_image' => 'string',
'photo_album' => 'string',
'sort' => 'int',
'created_at' => 'datetime',
'updated_at' => 'datetime'
];
}

View File

@@ -0,0 +1,35 @@
<?php
declare (strict_types = 1);
namespace app\common\model;
/**
* 产品 - 产品目录分类同步记录模型
* @mixin \think\Model
*/
class ProductTcoCategoryBaseModel extends BaseModel
{
// 表名
protected $name = 'product_tco_category';
// 主键
protected $pk = 'id';
// 字段信息
protected $schema = [
'id' => 'int',
'language_id' => 'int',
'name' => 'int',
'tco_id' => 'int',
'tco_pid' => 'int',
'tco_path' => 'string',
'erp_id' => 'int',
'erp_pid' => 'int',
'erp_path' => 'string',
'disabled' => 'int',
'sync_time' => 'int',
'created_at' => 'datetime',
'updated_at' => 'datetime',
'deleted_at' => 'datetime',
];
}