471 lines
17 KiB
PHP
471 lines
17 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;
|
|
use App\Model\Online as oModel;
|
|
use App\Model\Post as pModel;
|
|
use App\Model\Role as rModel;
|
|
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\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\HttpServer\Annotation\Controller;
|
|
use Hyperf\HttpServer\Annotation\DeleteMapping;
|
|
use Hyperf\HttpServer\Annotation\GetMapping;
|
|
use Hyperf\HttpServer\Annotation\PostMapping;
|
|
use Hyperf\HttpServer\Annotation\PutMapping;
|
|
|
|
#[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", "");
|
|
if (!$ids) return $this->error("请选择要删除的菜单");
|
|
$res = mModel::del($ids);
|
|
return $res ? $this->success("操作成功") : $this->error("操作失败");
|
|
}
|
|
|
|
#[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", "");
|
|
if (!$ids) return $this->error("请选择要删除的菜单");
|
|
$res = dModel::del($ids);
|
|
return $res ? $this->success("操作成功") : $this->error("操作失败");
|
|
}
|
|
|
|
// 角色列表
|
|
#[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", "");
|
|
if (!$ids) return $this->error("请选择要删除的角色");
|
|
$res = rModel::del($ids);
|
|
return $res ? $this->success("操作成功") : $this->error("操作失败");
|
|
}
|
|
|
|
#[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", "");
|
|
if (!$ids) return $this->error("请选择要删除的岗位");
|
|
$res = pModel::del($ids);
|
|
return $res ? $this->success("操作成功") : $this->error("操作失败");
|
|
}
|
|
|
|
// 账号列表
|
|
#[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 = Online::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']);
|
|
// 设置离线
|
|
Online::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()
|
|
{
|
|
var_dump(cModel::options());
|
|
|
|
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", "");
|
|
if (!$ids) return $this->error("操作失败");
|
|
$res = cModel::del($ids);
|
|
if ($res) {
|
|
return $this->success("操作成功");
|
|
}
|
|
return $this->error("操作失败");
|
|
}
|
|
|
|
#[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']);
|
|
$res = clModel::del($param['ids']);
|
|
return $res ? $this->success("操作成功") : $this->error("操作失败");
|
|
}
|
|
|
|
#[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("操作失败");
|
|
}
|
|
} |