增加企业产品分类

This commit is contained in:
2026-03-21 14:12:21 +08:00
parent 9281c1dc35
commit 2a6b14f698
13 changed files with 1139 additions and 277 deletions
@@ -0,0 +1,206 @@
<?php
declare(strict_types=1);
namespace app\admin\controller\Cms\Products;
use app\admin\BaseController;
use app\model\Cms\ProductsTypes;
use think\exception\ValidateException;
use think\response\Json;
/**
* 产品分类管理控制器
*/
class ProductsTypesController extends BaseController
{
/**
* 获取产品分类列表
* @return Json
*/
public function productsTypesList(): Json
{
try {
$page = (int)$this->request->param('page', 1);
$limit = (int)$this->request->param('limit', 10);
$keyword = (string)$this->request->param('keyword', '');
$query = ProductsTypes::where('delete_time', null)
->where('tid', $this->getTenantId());
if ($keyword !== '') {
$query->where('title', 'like', '%' . $keyword . '%');
}
$total = $query->count();
$list = $query->order('sort', 'asc')
->order('id', 'desc')
->page($page, $limit)
->select()
->toArray();
return json([
'code' => 200,
'msg' => 'success',
'data' => [
'list' => $list,
'total' => $total
]
]);
} catch (\Exception $e) {
return json([
'code' => 500,
'msg' => '获取失败:' . $e->getMessage(),
'data' => [
'list' => [],
'total' => 0
]
]);
}
}
/**
* 添加产品分类
* @return Json
*/
public function addProductsTypes(): Json
{
try {
$data = $this->request->param();
$title = (string)($data['title'] ?? '');
if ($title === '') {
return json([
'code' => 400,
'msg' => '分类名称不能为空'
]);
}
$type = new ProductsTypes();
$type->tid = $this->getTenantId();
$type->pid = isset($data['pid']) ? (int)$data['pid'] : 0;
$type->title = $title;
$type->desc = (string)($data['desc'] ?? '');
$type->sort = isset($data['sort']) ? (int)$data['sort'] : 0;
$type->create_time = date('Y-m-d H:i:s');
$type->update_time = date('Y-m-d H:i:s');
$type->save();
$this->logSuccess('产品分类管理', '添加分类', ['id' => $type->id]);
return json([
'code' => 200,
'msg' => '添加成功',
'data' => $type->toArray()
]);
} catch (ValidateException $e) {
return json([
'code' => 400,
'msg' => $e->getError()
]);
} catch (\Exception $e) {
$this->logFail('产品分类管理', '添加分类', $e->getMessage());
return json([
'code' => 500,
'msg' => '添加失败:' . $e->getMessage()
]);
}
}
/**
* 更新产品分类
* @param int $id
* @return Json
*/
public function editProductsTypes(int $id): Json
{
try {
$data = $this->request->param();
$type = ProductsTypes::where('id', $id)
->where('tid', $this->getTenantId())
->where('delete_time', null)
->find();
if (!$type) {
return json([
'code' => 404,
'msg' => '分类不存在'
]);
}
if (isset($data['title'])) $type->title = (string)$data['title'];
if (isset($data['pid'])) $type->pid = (int)$data['pid'];
if (isset($data['desc'])) $type->desc = (string)$data['desc'];
if (isset($data['sort'])) $type->sort = (int)$data['sort'];
if ((string)$type->title === '') {
return json([
'code' => 400,
'msg' => '分类名称不能为空'
]);
}
$type->update_time = date('Y-m-d H:i:s');
$type->save();
$this->logSuccess('产品分类管理', '更新分类', ['id' => $id]);
return json([
'code' => 200,
'msg' => '更新成功',
'data' => $type->toArray()
]);
} catch (ValidateException $e) {
return json([
'code' => 400,
'msg' => $e->getError()
]);
} catch (\Exception $e) {
$this->logFail('产品分类管理', '更新分类', $e->getMessage());
return json([
'code' => 500,
'msg' => '更新失败:' . $e->getMessage()
]);
}
}
/**
* 删除产品分类(软删除)
* @param int $id
* @return Json
*/
public function deleteProductsTypes(int $id): Json
{
try {
$type = ProductsTypes::where('id', $id)
->where('tid', $this->getTenantId())
->where('delete_time', null)
->find();
if (!$type) {
return json([
'code' => 404,
'msg' => '分类不存在'
]);
}
$type->delete();
$this->logSuccess('产品分类管理', '删除分类', ['id' => $id]);
return json([
'code' => 200,
'msg' => '删除成功'
]);
} catch (\Exception $e) {
$this->logFail('产品分类管理', '删除分类', $e->getMessage());
return json([
'code' => 500,
'msg' => '删除失败:' . $e->getMessage()
]);
}
}
}
+5 -1
View File
@@ -107,7 +107,7 @@ class MenuController extends BaseController
$menus = SystemMenu::where('delete_time', null)
->where('status', 1)
->whereIn('id', $allMenuIds)
->field('id,pid,title,path,component_path,icon,sort')
->field('id,pid,title,path,component_path,icon,sort,is_visible')
->order('sort', 'asc')
->select();
@@ -261,6 +261,7 @@ class MenuController extends BaseController
'title|菜单名称' => 'require|max:50',
'type|菜单类型' => 'require|in:1,2,3',
'status|菜单状态' => 'require|in:0,1',
'is_visible|是否显示' => 'require|in:0,1',
'sort|排序号' => 'integer',
'path|路由路径' => 'max:200',
'icon|菜单图标' => 'max:100',
@@ -274,6 +275,7 @@ class MenuController extends BaseController
'title' => $data['title'],
'type' => $data['type'],
'status' => $data['status'] ?? 1,
'is_visible' => $data['is_visible'] ?? 1,
'sort' => $data['sort'] ?? 0,
'path' => $data['path'] ?? '',
'component_path' => $data['component_path'] ?? '',
@@ -329,6 +331,7 @@ class MenuController extends BaseController
'pid|上级菜单ID' => 'integer',
'type|菜单类型' => 'require|in:1,2,3',
'status|菜单状态' => 'require|in:0,1',
'is_visible|是否显示' => 'require|in:0,1',
'sort|排序号' => 'integer',
'path|路由路径' => 'max:200',
'icon|菜单图标' => 'max:100',
@@ -346,6 +349,7 @@ class MenuController extends BaseController
'icon' => $data['icon'] ?? null,
'sort' => $data['sort'] ?? 0,
'status' => $data['status'],
'is_visible' => $data['is_visible'],
'permission' => $data['permission'] ?? null,
'remark' => $data['remark'] ?? null,
'update_time' => date('Y-m-d H:i:s')