增加企业产品分类
This commit is contained in:
@@ -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()
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user