*/ 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\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\Model\AssetCategory as acModel; use App\Request\AssetCategory as acRequest; use App\Model\SystemConfig as scModel; use App\Request\SystemConfig as scRequest; use App\Model\Asset as asModel; use App\Model\Dict as dtModel; use App\Request\Dict as dtRequest; use App\Model\DictData as ddModel; use App\Request\DictData as ddRequest; use App\Model\Translation as tsModel; use App\Request\Translation as tsRequest; use App\Service\ConfigCacheService; 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\Str; use App\Utils\SystemHelper; use Hyperf\Filesystem\FilesystemFactory; 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"); 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("用户已不在线"); } $this->cache->delete("USER:" . $param['session_id']); $this->cache->delete("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: "asset_category/list")] #[Auth(needAuth: false)] public function assetCategoryList() { $param = Param::only(["type" => "image"]); $param['belong_id'] = $this->account()['belong_id']; $param['account_type'] = $this->account()['account_type']; return $this->success("资源分类列表", acModel::list($param)); } #[PostMapping(path: "asset_category/add")] #[Auth(needAuth: false)] public function assetCategoryAdd() { $request = $this->container->get(acRequest::class); $request->scene('add')->validateResolved(); $data = Param::only(["category_name", "pid" => 0, "rank", "type" => "image"]); $data['belong_id'] = $this->account()['belong_id']; $data['account_type'] = $this->account()['account_type']; return $this->toAjax(acModel::add($data)); } #[PutMapping(path: "asset_category/edit")] #[Auth(needAuth: false)] public function assetCategoryEdit() { $request = $this->container->get(acRequest::class); $request->scene('edit')->validateResolved(); $data = Param::only(["category_id", "category_name", "pid" => 0, "rank", "type" => "image"]); return $this->toAjax(acModel::edit($data)); } #[DeleteMapping(path: "asset_category/del")] #[Auth(needAuth: false)] public function assetCategoryDel() { $ids = $this->request->input("ids"); return $this->toAjax(acModel::del($ids)); } #[GetMapping(path: "asset/list")] #[Auth(needAuth: false)] public function assetList() { $param = Param::only(["category_id", "ori_name", "type", "limit" => 10]); $param['belong_id'] = $this->account()['belong_id']; $param['account_type'] = $this->account()['account_type']; $param['account_id'] = $this->accountId(); return $this->success("资源列表", asModel::list($param)); } #[PutMapping(path: "asset/move")] #[Auth(needAuth: false)] public function assetMove() { $data = Param::only(["category_id", "ids" => []]); return $this->toAjax(asModel::move($data)); } #[PutMapping(path: "asset/edit")] #[Auth(needAuth: false)] public function assetEdit() { $data = Param::only(["asset_id", "ori_name", "category_id"]); return $this->toAjax(asModel::edit($data)); } #[DeleteMapping(path: "asset/del")] #[Auth(needAuth: false)] public function assetDel() { $ids = $this->request->input("ids"); return $this->toAjax(asModel::del($ids)); } // 文件上传 #[PostMapping(path: "asset/upload")] #[Auth(needAuth: false)] public function assetUpload(FilesystemFactory $factory) { // 判断文件是否存在 if (!$this->request->hasFile('file')) { return $this->error('文件不存在!'); } $file = $this->request->file('file'); // 移动文件到文件夹 $path = 'upload' . DIRECTORY_SEPARATOR . date("Ym") . DIRECTORY_SEPARATOR . date("d") . DIRECTORY_SEPARATOR; // 文件二进制信息 $stream = fopen($file->getRealPath(), 'r'); // 文件名称 $filename = md5(date("YmdHis") . rand(1000, 9999)) . "." . $file->getExtension(); // 获取文件存储位置信息 $local = $factory->get('local'); $local->writeStream($path . $filename, $stream); fclose($stream); $fileData = [ 'file_name' => $filename, 'ori_name' => $file->getClientFilename(), 'url' => $path . $filename, 'type' => Str::fileType($file->getExtension()), 'mime' => $file->getMimeType(), 'size' => $file->getSize(), 'create_time' => date("Y-m-d H:i:s") ]; $category_id = $this->request->post('category_id', 0); $fileData['account_type'] = $this->account()['account_type']; $fileData['belong_id'] = $this->account()['belong_id']; $fileData['category_id'] = $category_id; $fileData['account_id'] = $this->accountId(); // 上传成功 记录 $res = asModel::add($fileData); $fileData['asset_id'] = $res; if ($res) { return $this->success("上传成功", $fileData); } return $this->error("上传失败"); } #[GetMapping(path: "system_config/list")] #[Auth(auth: "system_config:list")] public function systemConfigList() { $param = Param::only(["config_name", "config_key", "limit" => 10]); return $this->success("列表接口", scModel::list($param)); } #[GetMapping(path: "system_config/option")] #[Auth(needAuth: false)] public function systemConfigOption() { return $this->success(scModel::options()); } #[GetMapping(path: "system_config/info")] #[Auth(auth: "system_config:info")] public function systemConfigInfo() { $id = $this->request->input("id"); return $this->success(scModel::getById($id)); } #[PostMapping(path: "system_config/add")] #[Auth(auth: "system_config:add")] public function systemConfigAdd() { $request = $this->container->get(scRequest::class); $request->scene('add')->validateResolved(); $data = Param::only(["config_name", "config_key", "config_value", "remark"]); $res = scModel::add($data); if (!$res) { return $this->error(); } (new ConfigCacheService())->setItem($data['config_key'], $data['config_value']); return $this->success(); } #[PutMapping(path: "system_config/edit")] #[Auth(auth: "system_config:edit")] public function systemConfigEdit() { $request = $this->container->get(scRequest::class); $request->scene('edit')->validateResolved(); $data = Param::only(["config_id", "config_name", "config_key", "config_value", "remark"]); // 删除原来的缓存 $info = scModel::getById($data['config_id'],['config_key']); (new ConfigCacheService())->removeItem($info['config_key']); // 变更数据 $res = scModel::edit($data); if (!$res) { return $this->error(); } (new ConfigCacheService())->setItem($data['config_key'], $data['config_value']); return $this->success(); } #[DeleteMapping(path: "system_config/del")] #[Auth(auth: "system_config:del")] public function systemConfigDel() { $ids = $this->request->input("ids"); $data = scModel::whereIn("config_id", $ids)->pluck("config_key")->toArray(); (new ConfigCacheService())->multipleRemoveItem($data); return $this->toAjax(scModel::del($ids)); } #[GetMapping(path: "dict/list")] #[Auth(auth: "dict:list")] public function dictList() { $param = Param::only(["dict_name", "dict_type", "limit"=>10]); return $this->success("列表接口", dtModel::list($param)); } #[GetMapping(path: "dict/option")] #[Auth(needAuth: false)] public function dictOption() { return $this->success(dtModel::options()); } #[GetMapping(path: "dict/info")] #[Auth(auth: "dict:info")] public function dictInfo() { $id = $this->request->input("id"); return $this->success(dtModel::getById($id)); } #[PostMapping(path: "dict/add")] #[Auth(auth: "dict:add")] public function dictAdd() { $request = $this->container->get(dtRequest::class); $request->scene('add')->validateResolved(); $data = Param::only(["dict_name", "dict_type", "remark"]); return $this->toAjax(dtModel::add($data)); } #[PutMapping(path: "dict/edit")] #[Auth(auth: "dict:edit")] public function dictEdit() { $request = $this->container->get(dtRequest::class); $request->scene('edit')->validateResolved(); $data = Param::only(["dict_id", "dict_name", "dict_type", "remark"]); return $this->toAjax(dtModel::edit($data)); } #[DeleteMapping(path: "dict/del")] #[Auth(auth: "dict:del")] public function dictDel() { $ids = $this->request->input("ids"); return $this->toAjax(dtModel::del($ids)); } #[GetMapping(path: "dict_data/list")] #[Auth(auth: "dict_data:list")] public function dictDataList() { $param = Param::only(["dict_id", "dict_label", "status", "limit"=>10]); return $this->success("列表接口", ddModel::list($param)); } #[GetMapping(path: "dict_data/option")] #[Auth(needAuth: false)] public function dictDataOption() { $param = Param::only(["key"=>""]); return $this->success(ddModel::options($param)); } #[GetMapping(path: "dict_data/info")] #[Auth(auth: "dict_data:info")] public function dictDataInfo() { $id = $this->request->input("id"); return $this->success(ddModel::getById($id)); } #[PostMapping(path: "dict_data/add")] #[Auth(auth: "dict_data:add")] public function dictDataAdd() { $request = $this->container->get(ddRequest::class); $request->scene('add')->validateResolved(); $data = Param::only(["dict_id", "dict_label", "dict_value", "is_default", "rank", "status", "remark"]); return $this->toAjax(ddModel::add($data)); } #[PutMapping(path: "dict_data/edit")] #[Auth(auth: "dict_data:edit")] public function dictDataEdit() { $request = $this->container->get(ddRequest::class); $request->scene('edit')->validateResolved(); $data = Param::only(["data_id", "dict_id", "dict_label", "dict_value", "is_default", "rank", "status", "remark"]); return $this->toAjax(ddModel::edit($data)); } #[DeleteMapping(path: "dict_data/del")] #[Auth(auth: "dict_data:del")] public function dictDataDel() { $ids = $this->request->input("ids"); return $this->toAjax(ddModel::del($ids)); } #[GetMapping(path: "translation/list")] #[Auth(auth: "translation:list")] public function translationList() { $param = Param::only(["group", "translation_key", "limit"=>10]); return $this->success("列表接口", tsModel::list($param)); } #[PostMapping(path: "translation/add")] #[Auth(auth: "translation:add")] public function translationAdd() { $request = $this->container->get(tsRequest::class); $request->scene('add')->validateResolved(); $data = Param::only(["group", "translation_key", "remark", "values"]); return $this->toAjax(tsModel::add($data)); } #[PutMapping(path: "translation/edit")] #[Auth(auth: "translation:edit")] public function translationEdit() { $request = $this->container->get(tsRequest::class); $request->scene('edit')->validateResolved(); $data = Param::only(["translation_id", "group", "translation_key", "remark", "values"]); return $this->toAjax(tsModel::edit($data)); } #[DeleteMapping(path: "translation/del")] #[Auth(auth: "translation:del")] public function translationDel() { $ids = $this->request->input("ids"); return $this->toAjax(tsModel::del($ids)); } }