server/app/Controller/Admin/System.php

633 lines
23 KiB
PHP

<?php
/**
* Author: cfn <cfn@leapy.cn>
*/
namespace App\Controller\Admin;
use App\Annotation\Auth;
use App\Model\Account as aModel;
use App\Model\Crontab as cModel;
use App\Model\CrontabLog as clModel;
use App\Model\Dept as dModel;
use App\Model\Menu as mModel;
use App\Model\Online as oModel;
use App\Model\Post as pModel;
use App\Model\Role as rModel;
use App\Model\GenTable as gtModel;
use App\Request\Account as aRequest;
use App\Request\Dept as dRequest;
use App\Request\Menu as mRequest;
use App\Request\Post as pRequest;
use App\Request\Role as rRequest;
use App\Request\GenTable as gtRequest;
use App\Utils\AppInfoHelper;
use App\Utils\CpuHelper;
use App\Utils\DiskInfoHelper;
use App\Utils\Ip;
use App\Utils\MemoryHelper;
use App\Utils\Param;
use App\Utils\RedisInfoHelper;
use App\Utils\SystemHelper;
use Hyperf\Context\ApplicationContext;
use Hyperf\DbConnection\Db;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\DeleteMapping;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\PostMapping;
use Hyperf\HttpServer\Annotation\PutMapping;
use function Hyperf\Config\config;
#[Controller(prefix: "admin")]
class System extends Base
{
#[GetMapping(path: "menu/list")]
#[Auth(needAuth: false, auth: "menu:list")]
public function menuList()
{
return $this->success("菜单列表", mModel::getMenus(1));
}
#[GetMapping(path: "menu/option")]
#[Auth(needAuth: false, auth: "menu:option")]
public function menuOption()
{
// 菜单类型
return $this->success("菜单列表", mModel::getMenus(1));
}
#[PostMapping(path: "menu/add")]
#[Auth(needAuth: false, auth: "menu:add")]
public function menuAdd()
{
$data = Param::only(['pid' => 0, 'title' => '', 'account_type' => 1, 'type' => 0, 'method', 'flag', 'name',
'path', 'icon', 'rank', 'hidden', 'remark'
], $this->request->post());
$request = $this->container->get(mRequest::class);
$request->scene('add')->validateResolved();
$id = mModel::add($data);
return $id ? $this->success("操作成功", ['menu_id' => $id]) : $this->error("操作失败");
}
#[PutMapping(path: "menu/edit")]
#[Auth(needAuth: false, auth: "menu:edit")]
public function menuEdit()
{
$data = Param::only(['pid' => 0, 'title' => '', 'account_type' => 1, 'type' => 0, 'method', 'flag', 'name',
'path', 'icon', 'rank', 'hidden', 'remark', 'menu_id' => 0
], $this->request->post());
$request = $this->container->get(mRequest::class);
$request->scene('edit')->validateResolved();
$res = mModel::edit($data);
return $res ? $this->success("操作成功") : $this->error("操作失败");
}
#[DeleteMapping(path: "menu/del")]
#[Auth(needAuth: false, auth: "menu:del")]
public function menuDel()
{
$ids = $this->request->input("ids");
return $this->toAjax(mModel::del($ids));
}
#[PostMapping(path: "menu/quick")]
#[Auth(needAuth: false, auth: "menu:quick")]
public function menuQuickAdd()
{
$data = Param::only(['pid' => 0, 'title' => '', 'flag', 'name', 'path', 'icon', 'account_type' => 1], $this->request->post());
$request = $this->container->get(mRequest::class);
$request->scene('quick')->validateResolved();
$res = mModel::quickAdd($data);
return $res ? $this->success("操作成功") : $this->error("操作失败");
}
#[GetMapping(path: "dept/list")]
#[Auth(auth: "dept:list")]
public function deptList()
{
$dept_name = $this->request->input("dept_name", "");
return $this->success("菜单列表", dModel::list($this->accountId(), $this->account()['account_type'], $dept_name));
}
#[GetMapping(path: "dept/option")]
#[Auth(needAuth: false)]
public function deptOption()
{
return $this->success("菜单列表", dModel::options($this->account()['belong_id'], $this->account()['account_type']));
}
#[PostMapping(path: "dept/add")]
#[Auth(auth: "dept:add")]
public function deptAdd()
{
$request = $this->container->get(dRequest::class);
$request->scene('add')->validateResolved();
$data = Param::only(['dept_name' => '', 'pid' => 0, 'rank', 'status' => 1]);
$data['belong_id'] = $this->account()['belong_id'];
$data['account_type'] = $this->account()['account_type'];
$res = dModel::add($data);
return $res ? $this->success("操作成功") : $this->error("操作失败");
}
#[PutMapping(path: "dept/edit")]
#[Auth(auth: "dept:edit")]
public function deptEdit()
{
$request = $this->container->get(dRequest::class);
$request->scene('edit')->validateResolved();
$data = Param::only(['dept_id' => '', 'dept_name' => '', 'pid' => 0, 'rank', 'status' => 1]);
$res = dModel::edit($data);
return $res ? $this->success("操作成功") : $this->error("操作失败");
}
#[DeleteMapping(path: "dept/del")]
#[Auth(auth: "dept:del")]
public function deptDel()
{
$ids = $this->request->input("ids");
return $this->toAjax(dModel::del($ids));
}
// 角色列表
#[GetMapping(path: "role/list")]
#[Auth(auth: "role:list")]
public function roleList()
{
$role_name = $this->request->input("role_name", "");
return $this->success("菜单列表", rModel::list($this->account()['belong_id'], $this->account()['account_type'], $role_name));
}
// 角色选择
#[GetMapping(path: "role/option")]
#[Auth(needAuth: false)]
public function roleOption()
{
return $this->success("菜单列表", rModel::options($this->account()['belong_id'], $this->account()['account_type']));
}
// 添加角色
#[PostMapping(path: "role/add")]
#[Auth(auth: "role:add")]
public function roleAdd()
{
$request = $this->container->get(rRequest::class);
$request->scene('add')->validateResolved();
$data = Param::only(['role_name' => '', 'menus' => [], 'status' => 1, 'rank', "checked_menus"]);
$data['belong_id'] = $this->account()['belong_id'];
$data['account_type'] = $this->account()['account_type'];
$res = rModel::add($data);
return $res ? $this->success("操作成功") : $this->error("操作失败");
}
// 修改角色
#[PutMapping(path: "role/edit")]
#[Auth(auth: "role:edit")]
public function roleEdit()
{
$request = $this->container->get(rRequest::class);
$request->scene('edit')->validateResolved();
$data = Param::only(['role_id' => '', 'role_name' => '', 'menus' => [], 'status' => 1, 'rank', "checked_menus"]);
$res = rModel::edit($data);
return $res ? $this->success("操作成功") : $this->error("操作失败");
}
// 删除角色
#[DeleteMapping(path: "role/del")]
#[Auth(auth: "role:del")]
public function roleDel()
{
$ids = $this->request->input("ids");
return $this->toAjax(rModel::del($ids));
}
#[GetMapping(path: "post/list")]
#[Auth(auth: "post:list")]
public function postList()
{
$post_name = $this->request->input("post_name", "");
return $this->success("岗位列表", pModel::list($this->account()['belong_id'], $this->account()['account_type'], $post_name));
}
#[GetMapping(path: "post/option")]
#[Auth(needAuth: false)]
public function postOption()
{
return $this->success("岗位列表", pModel::options($this->account()['belong_id'], $this->account()['account_type']));
}
#[PostMapping(path: "post/add")]
#[Auth(auth: "post:add")]
public function postAdd()
{
$request = $this->container->get(pRequest::class);
$request->scene('add')->validateResolved();
$data = Param::only(['post_name' => '', 'rank', 'status' => 1]);
$data['belong_id'] = $this->account()['belong_id'];
$data['account_type'] = $this->account()['account_type'];
$res = pModel::add($data);
return $res ? $this->success("操作成功") : $this->error("操作失败");
}
#[PutMapping(path: "post/edit")]
#[Auth(auth: "post:edit")]
public function postEdit()
{
$request = $this->container->get(pRequest::class);
$request->scene('edit')->validateResolved();
$data = Param::only(['post_id' => '', 'post_name' => '', 'rank', 'status' => 1]);
$res = pModel::edit($data);
return $res ? $this->success("操作成功") : $this->error("操作失败");
}
#[DeleteMapping(path: "post/del")]
#[Auth(auth: "post:del")]
public function postDel()
{
$ids = $this->request->input("ids");
return $this->toAjax(pModel::del($ids));
}
// 账号列表
#[GetMapping(path: "account/list")]
#[Auth(auth: "account:list")]
public function accountList()
{
$where = Param::only(['username' => '', 'limit' => 1, 'dept_id' => '']);
$account = $this->request->getAttribute("account");
$rows = aModel::list($where, $account['belong_id'], $account['account_type']);
return $this->success("账户列表", $rows['data'], $rows['count']);
}
// 添加账号
#[PostMapping(path: "account/add")]
#[Auth(auth: "account:add")]
public function accountAdd()
{
$request = $this->container->get(aRequest::class);
$request->scene('add')->validateResolved();
$account = $this->request->getAttribute("account");
$data = Param::only(['roles' => [], 'username', 'status' => 1, 'dept_id' => 0, 'posts' => [], 'password' => '123456']);
// 判断数据库是否存在相同的用户名
$admin = aModel::getByUsername($data['username'], ['account_id']);
if (!empty($admin)) {
return $this->error('用户名已存在!');
}
$data['belong_id'] = $account['belong_id'];
$data['account_type'] = $account['account_type'];
$res = aModel::add($data);
return $res ? $this->success("操作成功") : $this->error("操作失败");
}
// 修改账号
#[PutMapping(path: "account/edit")]
#[Auth(auth: "account:edit")]
public function accountEdit()
{
$request = $this->container->get(aRequest::class);
$request->scene('edit')->validateResolved();
$data = Param::only(['roles' => [], 'username', 'status' => 1, 'dept_id' => 0, 'posts' => [], 'password' => '', 'account_id']);
$res = aModel::edit($data);
return $res ? $this->success("操作成功") : $this->error("操作失败");
}
// 删除账号
#[DeleteMapping(path: "account/del")]
#[Auth(auth: "account:del")]
public function accountDel()
{
$id = $this->request->input("ids");
if (!$id) return $this->error("请选择要删除的账号");
$account = aModel::getById($id, ['master_flag']);
if ($account['master_flag'] == 1) return $this->error("系统创建的主账号不允许删除");
if ($account['account_id'] == $id) return $this->error("不能删除自己的账号");
$res = aModel::del($id);
return $res ? $this->success("操作成功") : $this->error("操作失败");
}
// 系统监控
#[GetMapping(path: "monitor/server")]
#[Auth(auth: "monitor:server")]
public function monitorServer()
{
$cores = CpuHelper::getCpuCores();
$usage = CpuHelper::getCpuUsage(1);
$data['cpu'] = [
'cpu_cores' => $cores,
'user_usage' => $usage['user'] . '%',
'system_usage' => $usage['system'] . '%',
'idle' => $usage['idle'] . '%'
];
$mem = MemoryHelper::getMemoryInfo();
$data['mem'] = [
'total' => $mem['total'] . "M",
'used' => $mem['used'] . "M",
'free' => $mem['free'] . "M",
'usage' => $mem['usage'] . '%'
];
$sys = SystemHelper::getServerInfo();
$data['sys'] = [
'server_name' => $sys['server_name'],
'server_ip' => $sys['server_ip'],
'os' => $sys['os'],
'architecture' => $sys['architecture'],
'php_version' => $sys['php_version']
];
$app = AppInfoHelper::getInfo();
$data['app'] = [
'swoole_version' => $app['swoole_version'],
'hyperf_version' => $app['hyperf_version'],
'project_path' => $app['project_path'],
'start_time' => $app['start_time'],
'uptime' => $app['uptime']
];
$disks = DiskInfoHelper::getAllDisks();
$data['disks'] = $disks;
$redis = RedisInfoHelper::getInfo();
$data['redis'] = $redis;
return $this->success($data);
}
#[GetMapping(path: "online/list")]
#[Auth(auth: "online:list")]
public function onlineList()
{
$param = Param::only(['limit' => 10, 'username']);
$paginate = oModel::list($param);
foreach ($paginate->items() as &$item) {
$item['os'] = Ip::getBrowserAndOS($item['ua']);
$item['location'] = ip2region($item['ip']);
}
return $this->success('在线用户', $paginate->items(), $paginate->total());
}
#[GetMapping(path: "online/quit")]
#[Auth(auth: "online:quit")]
public function onlineQuit()
{
$param = Param::only(['session_id' => '']);
// 退出登录状态
$online = oModel::getByUuid($param['session_id']);
if (empty($online) || $online['status'] == 0) {
return $this->success("用户已不在线");
}
$container = ApplicationContext::getContainer();
$redis = $container->get(\Hyperf\Redis\Redis::class);
$redis->del("USER:" . $param['session_id']);
$redis->del("AUTH:" . $online['account_id']);
// 设置离线
oModel::leave($param['session_id']);
return $this->success("强退成功");
}
#[GetMapping(path: "crontab/list")]
#[Auth(auth: "crontab:list")]
public function crontabList()
{
$param = Param::only(['crontab_name', 'enable', 'limit' => 10]);
return $this->success("任务列表", cModel::list($param));
}
#[GetMapping(path: "crontab/option")]
#[Auth(needAuth: false)]
public function crontabOption()
{
return $this->success("任务列表", cModel::options());
}
#[PostMapping(path: "crontab/add")]
#[Auth(auth: "crontab:add")]
public function crontabAdd()
{
$data = Param::only(['crontab_name' => '', 'rule', 'callback', 'memo', 'singleton', 'skip_log', 'enable' => 1]);
$res = cModel::add($data);
if ($res) {
return $this->success("操作成功");
}
return $this->error("操作失败");
}
#[PutMapping(path: "crontab/edit")]
#[Auth(auth: "crontab:edit")]
public function crontabEdit()
{
$data = Param::only(['crontab_id' => '', 'crontab_name' => '', 'rule', 'callback', 'memo', 'singleton', 'skip_log', 'enable' => 1]);
$res = cModel::edit($data);
if ($res) {
return $this->success("操作成功");
}
return $this->error("操作失败");
}
#[DeleteMapping(path: "crontab/del")]
#[Auth(auth: "crontab:del")]
public function crontabDel()
{
$ids = $this->request->input("ids");
return $this->toAjax(cModel::del($ids));
}
#[GetMapping(path: "crontab_log/list")]
#[Auth(auth: "crontab:log:list")]
public function crontabLogList()
{
$param = Param::only(['crontab_id', 'limit' => 10]);
return $this->success("任务日志列表", clModel::list($param));
}
#[DeleteMapping(path: "crontab_log/del")]
#[Auth(auth: "crontab:log:del")]
public function crontabLogDel()
{
$param = Param::only(['ids']);
return $this->toAjax(clModel::del($param['ids']));
}
#[DeleteMapping(path: "crontab_log/remove_all")]
#[Auth(auth: "crontab:log:empty")]
public function crontabLogEmpty()
{
$param = Param::only(['crontab_id']);
$res = clModel::removeAll($param);
return $res ? $this->success("操作成功") : $this->error("操作失败");
}
#[GetMapping(path: "gen_table/list")]
#[Auth(auth: "gen_table:list")]
public function genTableList()
{
$param = Param::only(['table_name', 'table_comment', 'limit' => 10]);
return $this->success("表列表", gtModel::list($param));
}
#[GetMapping(path: "gen_table/info")]
#[Auth(auth: "gen_table:info")]
public function genTableInfo()
{
$id = $this->request->input("id");
return $this->success("详情", gtModel::info($id));
}
#[PostMapping(path: "gen_table/edit")]
#[Auth(auth: "gen_table:edit")]
public function genTableEdit()
{
$request = $this->container->get(gtRequest::class);
$request->scene('edit')->validateResolved();
$param = Param::only(['table_id', 'table_name', 'table_comment', 'controller_name', 'module_name', 'remark', 'gen_table_columns']);
return $this->toAjax(gtModel::editData($param));
}
#[DeleteMapping(path: "gen_table/del")]
#[Auth(auth: "gen_table:del")]
public function genTableDel()
{
$param = Param::only(['ids' => []]);
return $this->toAjax(gtModel::del($param['ids']));
}
#[GetMapping(path: "gen_table/select")]
#[Auth(auth: "gen_table:select")]
public function genTableSelect()
{
$param = Param::only(['table_name', 'table_comment', 'limit' => 10, 'page' => 1]);
$offset = ($param['page'] - 1) * $param['limit'];
// 获取所有表
$rows = Db::select("SHOW TABLE STATUS");
// 获取已经导入的表
$tables = gtModel::pluck("table_name")->toArray();
// 表前缀
$prefix = config("databases.default.prefix");
// 构建表不导入 gen_table gen_table_column
$tables = array_merge([$prefix . "gen_table", $prefix . "gen_table_column"], $tables);
$rows = array_filter($rows, function ($item) use ($tables, $param) {
// 过滤表
if (in_array($item->Name, $tables)) {
return false;
}
// 表名筛选
if (isset($param['table_name']) && $param['table_name'] != "" && !str_contains($item->Name, $param['table_name'])) {
return false;
}
// 表描述筛选
if (isset($param['table_comment']) && $param['table_comment'] != "" && !str_contains($item->Comment, $param['table_comment'])) {
return false;
}
return true;
});
$count = count($rows);
$rows = array_slice($rows, $offset, $param['limit']);
// 过滤返回数据
$data = [];
foreach ($rows as $row) {
$data[] = [
'table_name' => $row->Name,
'table_comment' => $row->Comment,
'create_time' => $row->Create_time,
'update_time' => $row->Update_time
];
}
return $this->success("表列表", $data, $count);
}
#[PostMapping(path: "gen_table/build")]
#[Auth(auth: "gen_table:build")]
public function genTableBuild()
{
// 生成表数据
$param = Param::only(['names' => []]);
if (empty($param['names'])) {
return $this->error("数据表不为空");
}
// 获取已经导入的表
$tables = gtModel::pluck("table_name")->toArray();
// 表前缀
$prefix = config("databases.default.prefix");
// 构建表不导入 gen_table gen_table_column
$tables = array_merge([$prefix . "gen_table", $prefix . "gen_table_column"], $tables);
// 获取所有表
$rows = Db::select("SHOW TABLE STATUS");
// 去重
$rows = array_filter($rows, function ($item) use ($tables, $param) {
// 过滤表
if (in_array($item->Name, $tables)) {
return false;
}
// 表筛选
return in_array($item->Name, $param['names']);
});
if (empty($rows)) {
return $this->error("数据表不为空");
}
// 生成数据
$res = gtModel::genTable($rows);
return $res ? $this->success("操作成功") : $this->error("操作失败");
}
#[PostMapping(path: "gen_table/sync")]
#[Auth(auth: "gen_table:sync")]
public function genTableSync()
{
$table_id = $this->request->input("id");
if (!$table_id) {
return $this->error("同步失败");
}
return $this->toAjax(gtModel::syncTable($table_id));
}
#[GetMapping(path: "gen_table/show")]
#[Auth(auth: "gen_table:show")]
public function genTableShow()
{
$table_id = $this->request->input("id");
if (!$table_id) {
return $this->error("生成失败");
}
$table = gtModel::info($table_id);
if (empty($table)) {
return $this->error("生成失败");
}
// 表前缀
$prefix = config("databases.default.prefix");
// 表名
$table_name = str_replace($prefix, "", $table['table_name']);
$module_name = $table['module_name'];
$controller_name = $table['controller_name'];
// 搜搜字段
$query_fields = [];
$insert_fields = [];
$list_fields = [];
$required_fields = [];
$fields = $table['gen_table_columns'];
foreach ($fields as $column) {
if ($column['is_query']) {
$query_fields[] = $column;
}
if ($column['is_insert'] && !in_array($column['column_name'], ['create_time', 'update_time', 'deleted_at'])) {
$insert_fields[] = $column;
}
if ($column['is_edit'] && !in_array($column['column_name'], ['create_time', 'update_time', 'deleted_at'])) {
$edit_fields[] = $column;
}
if ($column['is_list']) {
$list_fields[] = $column;
}
if ($column['is_required'] && !in_array($column['column_name'], ['create_time', 'update_time', 'deleted_at'])) {
$required_fields[] = $column;
}
}
$data = [
'model.php' => $this->render->getContents('templates/model.php.twig', compact("table_name", "fields", "controller_name", "query_fields", "list_fields")),
'request.php' => $this->render->getContents('templates/request.php.twig', compact("controller_name", "insert_fields", "fields", "required_fields", "edit_fields")),
'controller.php' => $this->render->getContents('templates/controller.php.twig', compact("controller_name", "module_name", "query_fields", "insert_fields", "edit_fields", "table_name")),
'api.ts' => $this->render->getContents('templates/api.ts.twig', compact('table_name')),
'index.vue' => $this->render->getContents('templates/index.vue.twig', compact('table_name', 'list_fields', 'query_fields')),
'save.vue' => $this->render->getContents('templates/save.vue.twig', compact('table_name', 'insert_fields', 'required_fields'))
];
return $this->success("模板信息", $data);
}
}