feat: 新增产品分类增/删/改/查接口
This commit is contained in:
189
app/admin/controller/v1/ProductCategory.php
Normal file
189
app/admin/controller/v1/ProductCategory.php
Normal file
@@ -0,0 +1,189 @@
|
||||
<?php
|
||||
declare (strict_types = 1);
|
||||
|
||||
namespace app\admin\controller\v1;
|
||||
|
||||
use app\admin\model\v1\ProductCategoryModel;
|
||||
use app\admin\validate\v1\ProductCategoryValidate;
|
||||
|
||||
class ProductCategory
|
||||
{
|
||||
/**
|
||||
* 获取商品分类列表
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$params = request()->param([
|
||||
'keywords' => '',
|
||||
]);
|
||||
|
||||
$ret = ProductCategoryModel::field([
|
||||
'id',
|
||||
'pid',
|
||||
'name',
|
||||
'level',
|
||||
'is_show'
|
||||
])
|
||||
->withSearch(['name_nullable'], [
|
||||
'name_nullable' => $params['keywords']
|
||||
])
|
||||
->select();
|
||||
if ($ret->isEmpty()) {
|
||||
return success('获取成功!');
|
||||
}
|
||||
|
||||
return success('获取成功!', array_to_tree($ret->toArray(), 0, 'pid', 1));
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取商品分类详情
|
||||
*/
|
||||
public function read()
|
||||
{
|
||||
$category = ProductCategoryModel::withoutField([
|
||||
'language_id',
|
||||
'created_at',
|
||||
'updated_at',
|
||||
'deleted_at',
|
||||
])
|
||||
->bypk(request()->param('id'))
|
||||
->find();
|
||||
if (empty($category)) {
|
||||
return error('分类不存在!');
|
||||
}
|
||||
|
||||
return success('获取成功!', $category);
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增商品分类
|
||||
*/
|
||||
public function save()
|
||||
{
|
||||
$post = request()->post([
|
||||
'unique_id',
|
||||
'pid' => 0,
|
||||
'name',
|
||||
'icon',
|
||||
'desc',
|
||||
'related_tco_category',
|
||||
'sort',
|
||||
'level' => 1,
|
||||
'is_show' => 1,
|
||||
'seo_title',
|
||||
'seo_keywords',
|
||||
'seo_desc'
|
||||
]);
|
||||
if (empty($post['unique_id'])) {
|
||||
$post['unique_id'] = md5(uniqid());
|
||||
}
|
||||
$data = array_merge($post, ['language_id' => request()->lang_id]);
|
||||
|
||||
$validate = new ProductCategoryValidate;
|
||||
if (!$validate->check($data)) {
|
||||
return error($validate->getError());
|
||||
}
|
||||
|
||||
$category = new ProductCategoryModel;
|
||||
|
||||
// 处理层级
|
||||
if ($data['pid'] > 0) {
|
||||
$parent = $category->bypk($data['pid'])->find();
|
||||
if (!empty($parent)) {
|
||||
$data['level'] = $parent->level + 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (!$category->save($data)) {
|
||||
return error('操作失败!');
|
||||
}
|
||||
return success('操作成功!');
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新商品分类
|
||||
*/
|
||||
public function update()
|
||||
{
|
||||
$id = request()->param('id');
|
||||
$put = request()->put([
|
||||
'unique_id',
|
||||
'pid' => 0,
|
||||
'name',
|
||||
'icon',
|
||||
'desc',
|
||||
'related_tco_category',
|
||||
'sort',
|
||||
'level' => 1,
|
||||
'is_show' => 1,
|
||||
'seo_title',
|
||||
'seo_keywords',
|
||||
'seo_desc'
|
||||
]);
|
||||
$data = array_merge($put, [
|
||||
'id' => $id,
|
||||
'language_id' => request()->lang_id
|
||||
]);
|
||||
|
||||
$validate = new ProductCategoryValidate;
|
||||
if (!$validate->check($data)) {
|
||||
return error($validate->getError());
|
||||
}
|
||||
|
||||
$category = ProductCategoryModel::bypk($id)->find();
|
||||
if (empty($category)) {
|
||||
return error('请确认操作对对象是否存在!');
|
||||
}
|
||||
|
||||
// 处理层级
|
||||
$updated_level = false;
|
||||
if ($data['pid'] != $category->pid) {
|
||||
$parent = ProductCategoryModel::bypk($data['pid'])->find();
|
||||
if (!empty($parent)) {
|
||||
$data['level'] = $parent->level + 1;
|
||||
$updated_level = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (!$category->save($data)) {
|
||||
return error('操作失败!');
|
||||
}
|
||||
|
||||
// 处理子分类层级
|
||||
if ($updated_level) {
|
||||
$this->handle_children($category->id, $data['level']);
|
||||
}
|
||||
|
||||
return success('操作成功!');
|
||||
}
|
||||
private function handle_children($pid, $level)
|
||||
{
|
||||
$children = ProductCategoryModel::pid($pid)->select();
|
||||
if ($children->isEmpty()) {
|
||||
return;
|
||||
}
|
||||
foreach ($children as $child) {
|
||||
$child->level = $level + 1;
|
||||
if ($child->save()) {
|
||||
$this->handle_children($child->id, $child->level);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除商品分类
|
||||
*/
|
||||
public function delete()
|
||||
{
|
||||
$id = request()->param('id');
|
||||
$category = ProductCategoryModel::bypk($id)->find();
|
||||
if (empty($category)) {
|
||||
return error('请确认操作对对象是否存在!');
|
||||
}
|
||||
|
||||
if (!$category->delete()) {
|
||||
return error('操作失败!');
|
||||
}
|
||||
return success('操作成功!');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user