更新架构
This commit is contained in:
@@ -0,0 +1,352 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace app\admin\controller;
|
||||
|
||||
use app\admin\BaseController;
|
||||
use think\response\Json;
|
||||
use app\model\AdminModules;
|
||||
|
||||
class ModulesController extends BaseController
|
||||
{
|
||||
/**
|
||||
* 获取所有模块列表
|
||||
* @return Json
|
||||
*/
|
||||
public function getList()
|
||||
{
|
||||
try {
|
||||
$modules = AdminModules::where('delete_time', null)
|
||||
->order('sort', 'asc')
|
||||
->order('id', 'asc')
|
||||
->select()
|
||||
->toArray();
|
||||
|
||||
$this->logSuccess('模块管理', '获取模块列表', ['count' => count($modules)]);
|
||||
|
||||
return json([
|
||||
'code' => 200,
|
||||
'msg' => '获取成功',
|
||||
'data' => [
|
||||
'list' => $modules,
|
||||
'total' => count($modules)
|
||||
]
|
||||
]);
|
||||
} catch (\Exception $e) {
|
||||
$this->logFail('模块管理', '获取模块列表', $e->getMessage());
|
||||
return json([
|
||||
'code' => 500,
|
||||
'msg' => '获取失败:' . $e->getMessage(),
|
||||
'data' => []
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取模块详情
|
||||
* @return Json
|
||||
*/
|
||||
public function getDetail(int $id)
|
||||
{
|
||||
try {
|
||||
$module = AdminModules::where('id', $id)
|
||||
->where('delete_time', null)
|
||||
->find();
|
||||
|
||||
if (!$module) {
|
||||
return json([
|
||||
'code' => 404,
|
||||
'msg' => '模块不存在',
|
||||
'data' => null
|
||||
]);
|
||||
}
|
||||
|
||||
$this->logSuccess('模块管理', '获取模块详情', ['id' => $id]);
|
||||
|
||||
return json([
|
||||
'code' => 200,
|
||||
'msg' => '获取成功',
|
||||
'data' => $module
|
||||
]);
|
||||
} catch (\Exception $e) {
|
||||
$this->logFail('模块管理', '获取模块详情', $e->getMessage());
|
||||
return json([
|
||||
'code' => 500,
|
||||
'msg' => '获取失败:' . $e->getMessage(),
|
||||
'data' => null
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加模块
|
||||
* @return Json
|
||||
*/
|
||||
public function add()
|
||||
{
|
||||
try {
|
||||
$data = [
|
||||
'name' => input('post.name', '', 'trim'),
|
||||
'code' => input('post.code', '', 'trim'),
|
||||
'description' => input('post.description', '', 'trim'),
|
||||
'icon' => input('post.icon', '', 'trim'),
|
||||
'path' => input('post.path', '', 'trim'),
|
||||
'sort' => (int)input('post.sort', 0),
|
||||
'status' => (int)input('post.status', 1),
|
||||
'is_show' => (int)input('post.is_show', 1),
|
||||
'create_time' => date('Y-m-d H:i:s'),
|
||||
'update_time' => date('Y-m-d H:i:s')
|
||||
];
|
||||
|
||||
if (empty($data['name']) || empty($data['code'])) {
|
||||
return json([
|
||||
'code' => 400,
|
||||
'msg' => '模块名称和编码不能为空',
|
||||
'data' => null
|
||||
]);
|
||||
}
|
||||
|
||||
$exists = AdminModules::where('code', $data['code'])
|
||||
->where('delete_time', null)
|
||||
->find();
|
||||
|
||||
if ($exists) {
|
||||
return json([
|
||||
'code' => 400,
|
||||
'msg' => '模块编码已存在',
|
||||
'data' => null
|
||||
]);
|
||||
}
|
||||
|
||||
$id = AdminModules::insertGetId($data);
|
||||
|
||||
$this->logSuccess('模块管理', '添加模块', ['id' => $id, 'name' => $data['name']]);
|
||||
|
||||
return json([
|
||||
'code' => 200,
|
||||
'msg' => '添加成功',
|
||||
'data' => ['id' => $id]
|
||||
]);
|
||||
} catch (\Exception $e) {
|
||||
$this->logFail('模块管理', '添加模块', $e->getMessage());
|
||||
return json([
|
||||
'code' => 500,
|
||||
'msg' => '添加失败:' . $e->getMessage(),
|
||||
'data' => null
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 编辑模块
|
||||
* @return Json
|
||||
*/
|
||||
public function edit()
|
||||
{
|
||||
try {
|
||||
$id = (int)input('post.id', 0);
|
||||
|
||||
if (empty($id)) {
|
||||
return json([
|
||||
'code' => 400,
|
||||
'msg' => '参数错误',
|
||||
'data' => null
|
||||
]);
|
||||
}
|
||||
|
||||
$data = [
|
||||
'name' => input('post.name', '', 'trim'),
|
||||
'code' => input('post.code', '', 'trim'),
|
||||
'description' => input('post.description', '', 'trim'),
|
||||
'icon' => input('post.icon', '', 'trim'),
|
||||
'path' => input('post.path', '', 'trim'),
|
||||
'sort' => (int)input('post.sort', 0),
|
||||
'status' => (int)input('post.status', 1),
|
||||
'is_show' => (int)input('post.is_show', 1),
|
||||
'update_time' => date('Y-m-d H:i:s')
|
||||
];
|
||||
|
||||
if (empty($data['name']) || empty($data['code'])) {
|
||||
return json([
|
||||
'code' => 400,
|
||||
'msg' => '模块名称和编码不能为空',
|
||||
'data' => null
|
||||
]);
|
||||
}
|
||||
|
||||
$exists = AdminModules::where('code', $data['code'])
|
||||
->where('id', '<>', $id)
|
||||
->where('delete_time', null)
|
||||
->find();
|
||||
|
||||
if ($exists) {
|
||||
return json([
|
||||
'code' => 400,
|
||||
'msg' => '模块编码已存在',
|
||||
'data' => null
|
||||
]);
|
||||
}
|
||||
|
||||
AdminModules::where('id', $id)->update($data);
|
||||
|
||||
$this->logSuccess('模块管理', '编辑模块', ['id' => $id, 'name' => $data['name']]);
|
||||
|
||||
return json([
|
||||
'code' => 200,
|
||||
'msg' => '编辑成功',
|
||||
'data' => null
|
||||
]);
|
||||
} catch (\Exception $e) {
|
||||
$this->logFail('模块管理', '编辑模块', $e->getMessage());
|
||||
return json([
|
||||
'code' => 500,
|
||||
'msg' => '编辑失败:' . $e->getMessage(),
|
||||
'data' => null
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除模块
|
||||
* @return Json
|
||||
*/
|
||||
public function delete(int $id)
|
||||
{
|
||||
try {
|
||||
if (empty($id)) {
|
||||
return json([
|
||||
'code' => 400,
|
||||
'msg' => '参数错误',
|
||||
'data' => null
|
||||
]);
|
||||
}
|
||||
|
||||
AdminModules::where('id', $id)->update([
|
||||
'delete_time' => date('Y-m-d H:i:s')
|
||||
]);
|
||||
|
||||
$this->logSuccess('模块管理', '删除模块', ['id' => $id]);
|
||||
|
||||
return json([
|
||||
'code' => 200,
|
||||
'msg' => '删除成功',
|
||||
'data' => null
|
||||
]);
|
||||
} catch (\Exception $e) {
|
||||
$this->logFail('模块管理', '删除模块', $e->getMessage());
|
||||
return json([
|
||||
'code' => 500,
|
||||
'msg' => '删除失败:' . $e->getMessage(),
|
||||
'data' => null
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量删除模块
|
||||
* @return Json
|
||||
*/
|
||||
public function batchDelete()
|
||||
{
|
||||
try {
|
||||
$ids = input('post.ids', []);
|
||||
$ids = array_map('intval', $ids);
|
||||
|
||||
if (empty($ids)) {
|
||||
return json([
|
||||
'code' => 400,
|
||||
'msg' => '请选择要删除的模块',
|
||||
'data' => null
|
||||
]);
|
||||
}
|
||||
|
||||
AdminModules::whereIn('id', $ids)
|
||||
->update([
|
||||
'delete_time' => date('Y-m-d H:i:s')
|
||||
]);
|
||||
|
||||
$this->logSuccess('模块管理', '批量删除模块', ['ids' => $ids]);
|
||||
|
||||
return json([
|
||||
'code' => 200,
|
||||
'msg' => '批量删除成功',
|
||||
'data' => null
|
||||
]);
|
||||
} catch (\Exception $e) {
|
||||
$this->logFail('模块管理', '批量删除模块', $e->getMessage());
|
||||
return json([
|
||||
'code' => 500,
|
||||
'msg' => '批量删除失败:' . $e->getMessage(),
|
||||
'data' => null
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改状态
|
||||
* @return Json
|
||||
*/
|
||||
public function changeStatus()
|
||||
{
|
||||
try {
|
||||
$id = (int)input('post.id', 0);
|
||||
$status = (int)input('post.status', 0);
|
||||
|
||||
if (empty($id)) {
|
||||
return json([
|
||||
'code' => 400,
|
||||
'msg' => '参数错误',
|
||||
'data' => null
|
||||
]);
|
||||
}
|
||||
|
||||
AdminModules::where('id', $id)->update([
|
||||
'status' => $status,
|
||||
'update_time' => date('Y-m-d H:i:s')
|
||||
]);
|
||||
|
||||
$this->logSuccess('模块管理', '修改状态', ['id' => $id, 'status' => $status]);
|
||||
|
||||
return json([
|
||||
'code' => 200,
|
||||
'msg' => '状态修改成功',
|
||||
'data' => null
|
||||
]);
|
||||
} catch (\Exception $e) {
|
||||
$this->logFail('模块管理', '修改状态', $e->getMessage());
|
||||
return json([
|
||||
'code' => 500,
|
||||
'msg' => '状态修改失败:' . $e->getMessage(),
|
||||
'data' => null
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取模块选择列表
|
||||
* @return Json
|
||||
*/
|
||||
public function getSelectList()
|
||||
{
|
||||
try {
|
||||
$modules = AdminModules::where('delete_time', null)
|
||||
->where('status', 1)
|
||||
->where('is_show', 1)
|
||||
->order('sort', 'asc')
|
||||
->column('id, name, code');
|
||||
|
||||
return json([
|
||||
'code' => 200,
|
||||
'msg' => '获取成功',
|
||||
'data' => $modules
|
||||
]);
|
||||
} catch (\Exception $e) {
|
||||
return json([
|
||||
'code' => 500,
|
||||
'msg' => '获取失败:' . $e->getMessage(),
|
||||
'data' => []
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user