diff --git a/app/admin/Service.php b/app/admin/Service.php index 19739030c..0604ac38e 100644 --- a/app/admin/Service.php +++ b/app/admin/Service.php @@ -1,47 +1,47 @@ _vali([ - 'status.in:0,1' => '状态值范围异常!', + 'status.in:0,1' => '状态值范围异常!', 'status.require' => '状态值不能为空!', ])); } /** - * 删除系统权限 + * 删除系统权限. * @auth true */ public function remove() @@ -70,9 +74,8 @@ class Auth extends Controller SystemAuth::mDelete(); } - /** - * 添加系统权限 + * 添加系统权限. * @auth true */ public function add() @@ -81,7 +84,7 @@ class Auth extends Controller } /** - * 编辑系统权限 + * 编辑系统权限. * @auth true */ public function edit() @@ -90,24 +93,29 @@ class Auth extends Controller } /** - * 表单后置数据处理 - * @param array $data + * 表单后置数据处理. */ protected function _form_filter(array $data) { if ($this->request->isGet()) { - $this->title = empty($data['title']) ? "添加访问授权" : "编辑【{$data['title']}】授权"; + $this->title = empty($data['title']) ? '添加访问授权' : "编辑【{$data['title']}】授权"; } elseif ($this->request->post('action') === 'json') { - if ($this->app->isDebug()) AdminService::clear(); + if ($this->app->isDebug()) { + AdminService::clear(); + } $ztree = AdminService::getTree(empty($data['id']) ? [] : SystemNode::mk()->where(['auth' => $data['id']])->column('node')); usort($ztree, static function ($a, $b) { if (explode('-', $a['node'])[0] !== explode('-', $b['node'])[0]) { - if (stripos($a['node'], 'plugin-') === 0) return 1; + if (stripos($a['node'], 'plugin-') === 0) { + return 1; + } } return $a['node'] === $b['node'] ? 0 : ($a['node'] > $b['node'] ? 1 : -1); }); [$ps, $cs] = [Plugin::get(), (array)$this->app->config->get('app.app_names', [])]; - foreach ($ztree as &$n) $n['title'] = lang($cs[$n['node']] ?? (($ps[$n['node']] ?? [])['name'] ?? $n['title'])); + foreach ($ztree as &$n) { + $n['title'] = lang($cs[$n['node']] ?? (($ps[$n['node']] ?? [])['name'] ?? $n['title'])); + } $this->success('获取权限节点成功!', $ztree); } elseif (empty($data['nodes'])) { $this->error('未配置功能节点!'); @@ -115,16 +123,15 @@ class Auth extends Controller } /** - * 节点更新处理 - * @param boolean $state - * @param array $post - * @return void + * 节点更新处理. */ protected function _form_result(bool $state, array $post) { if ($state && $this->request->post('action') === 'save') { [$map, $data] = [['auth' => $post['id']], []]; - foreach ($post['nodes'] ?? [] as $node) $data[] = $map + ['node' => $node]; + foreach ($post['nodes'] ?? [] as $node) { + $data[] = $map + ['node' => $node]; + } SystemNode::mk()->where($map)->delete(); count($data) > 0 && SystemNode::mk()->insertAll($data); sysoplog('系统权限管理', "配置系统权限[{$map['auth']}]授权成功"); diff --git a/app/admin/controller/Base.php b/app/admin/controller/Base.php index ebcb5f7cf..c9f7fdb89 100644 --- a/app/admin/controller/Base.php +++ b/app/admin/controller/Base.php @@ -1,41 +1,45 @@ _vali([ + 'status.in:0,1' => '状态值范围异常!', + 'status.require' => '状态值不能为空!', + ])); + } + + /** + * 删除数据记录. + * @auth true + */ + public function remove() + { + SystemBase::mDelete(); + } + + /** + * 表单数据处理. + * @throws DbException */ protected function _form_filter(array &$data) { @@ -85,29 +109,8 @@ class Base extends Controller $map[] = ['type', '=', $data['type']]; $map[] = ['id', '<>', $data['id'] ?? 0]; if (SystemBase::mk()->where($map)->count() > 0) { - $this->error("数据编码已经存在!"); + $this->error('数据编码已经存在!'); } } } - - /** - * 修改数据状态 - * @auth true - */ - public function state() - { - SystemBase::mSave($this->_vali([ - 'status.in:0,1' => '状态值范围异常!', - 'status.require' => '状态值不能为空!', - ])); - } - - /** - * 删除数据记录 - * @auth true - */ - public function remove() - { - SystemBase::mDelete(); - } -} \ No newline at end of file +} diff --git a/app/admin/controller/Config.php b/app/admin/controller/Config.php index 5a9c4324b..05821b768 100644 --- a/app/admin/controller/Config.php +++ b/app/admin/controller/Config.php @@ -1,20 +1,22 @@ '默认色0', - 'white' => '简约白0', - 'red-1' => '玫瑰红1', - 'blue-1' => '深空蓝1', + 'white' => '简约白0', + 'red-1' => '玫瑰红1', + 'blue-1' => '深空蓝1', 'green-1' => '小草绿1', 'black-1' => '经典黑1', - 'red-2' => '玫瑰红2', - 'blue-2' => '深空蓝2', + 'red-2' => '玫瑰红2', + 'blue-2' => '深空蓝2', 'green-2' => '小草绿2', 'black-2' => '经典黑2', ]; /** - * 系统参数配置 + * 系统参数配置. * @auth true * @menu true */ @@ -68,14 +69,16 @@ class Config extends Controller $this->showErrorMessage = lang("超级管理员账号的密码未修改,建议立即修改密码!", [$url]); } uasort($this->plugins, static function ($a, $b) { - if ($a['space'] === $b['space']) return 0; + if ($a['space'] === $b['space']) { + return 0; + } return $a['space'] > $b['space'] ? 1 : -1; }); $this->fetch(); } /** - * 修改系统参数 + * 修改系统参数. * @auth true * @throws \think\admin\Exception */ @@ -100,20 +103,24 @@ class Config extends Controller RuntimeService::set(null, [$post['xpath'] => 'admin']); } // 修改网站 ICON 图标,替换 public/favicon.ico - if (preg_match('#^https?://#', $post['site_icon'] ?? '')) try { - SystemService::setFavicon($post['site_icon'] ?? ''); - } catch (\Exception $exception) { - trace_file($exception); + if (preg_match('#^https?://#', $post['site_icon'] ?? '')) { + try { + SystemService::setFavicon($post['site_icon'] ?? ''); + } catch (\Exception $exception) { + trace_file($exception); + } } // 数据数据到系统配置表 - foreach ($post as $k => $v) sysconf($k, $v); - sysoplog('系统配置管理', "修改系统参数成功"); + foreach ($post as $k => $v) { + sysconf($k, $v); + } + sysoplog('系统配置管理', '修改系统参数成功'); $this->success('数据保存成功!', admuri('admin/config/index')); } } /** - * 修改文件存储 + * 修改文件存储. * @auth true * @throws \think\admin\Exception */ @@ -135,12 +142,16 @@ class Config extends Controller if (!empty($post['storage']['allow_exts'])) { $deny = ['sh', 'asp', 'bat', 'cmd', 'exe', 'php']; $exts = array_unique(str2arr(strtolower($post['storage']['allow_exts']))); - if (count(array_intersect($deny, $exts)) > 0) $this->error('禁止上传可执行的文件!'); + if (count(array_intersect($deny, $exts)) > 0) { + $this->error('禁止上传可执行的文件!'); + } $post['storage']['allow_exts'] = join(',', $exts); } - foreach ($post as $name => $value) sysconf($name, $value); - sysoplog('系统配置管理', "修改系统存储参数"); + foreach ($post as $name => $value) { + sysconf($name, $value); + } + sysoplog('系统配置管理', '修改系统存储参数'); $this->success('修改文件存储成功!'); } } -} \ No newline at end of file +} diff --git a/app/admin/controller/File.php b/app/admin/controller/File.php index 9c44143e5..1340396a5 100644 --- a/app/admin/controller/File.php +++ b/app/admin/controller/File.php @@ -1,20 +1,22 @@ types = Storage::types(); - } - - /** - * 系统文件管理 + * 系统文件管理. * @auth true * @menu true - * @throws \think\db\exception\DataNotFoundException - * @throws \think\db\exception\DbException - * @throws \think\db\exception\ModelNotFoundException + * @throws DataNotFoundException + * @throws DbException + * @throws ModelNotFoundException */ public function index() { @@ -66,21 +61,8 @@ class File extends Controller } /** - * 数据列表处理 - * @param array $data - * @return void - */ - protected function _page_filter(array &$data) - { - foreach ($data as &$vo) { - $vo['ctype'] = $this->types[$vo['type']] ?? $vo['type']; - } - } - - /** - * 编辑系统文件 + * 编辑系统文件. * @auth true - * @return void */ public function edit() { @@ -88,9 +70,8 @@ class File extends Controller } /** - * 删除系统文件 + * 删除系统文件. * @auth true - * @return void */ public function remove() { @@ -101,10 +82,9 @@ class File extends Controller } /** - * 清理重复文件 + * 清理重复文件. * @auth true - * @return void - * @throws \think\db\exception\DbException + * @throws DbException */ public function distinct() { @@ -117,4 +97,22 @@ class File extends Controller })->delete(); $this->success('清理重复文件成功!'); } -} \ No newline at end of file + + /** + * 控制器初始化. + */ + protected function initialize() + { + $this->types = Storage::types(); + } + + /** + * 数据列表处理. + */ + protected function _page_filter(array &$data) + { + foreach ($data as &$vo) { + $vo['ctype'] = $this->types[$vo['type']] ?? $vo['type']; + } + } +} diff --git a/app/admin/controller/Index.php b/app/admin/controller/Index.php index 4c40262f6..7e3716647 100644 --- a/app/admin/controller/Index.php +++ b/app/admin/controller/Index.php @@ -1,49 +1,56 @@ app->isDebug()); - /*! 读取当前用户权限菜单树 */ + /* ! 读取当前用户权限菜单树 */ $this->menus = MenuService::getTree(); - /*! 判断当前用户的登录状态 */ + /* ! 判断当前用户的登录状态 */ $this->login = AdminService::isLogin(); - /*! 菜单为空且未登录跳转到登录页 */ + /* ! 菜单为空且未登录跳转到登录页 */ if (empty($this->menus) && empty($this->login)) { $this->redirect(sysuri('admin/login/index')); } else { @@ -55,10 +62,9 @@ class Index extends Controller } /** - * 后台主题切换 + * 后台主题切换. * @login true - * @return void - * @throws \think\admin\Exception + * @throws Exception */ public function theme() { @@ -77,7 +83,7 @@ class Index extends Controller } /** - * 修改用户资料 + * 修改用户资料. * @login true */ public function info() @@ -90,34 +96,12 @@ class Index extends Controller } } - /** - * 资料修改表单处理 - * @param array $data - */ - protected function _info_form_filter(array &$data) - { - if ($this->request->isPost()) { - unset($data['username'], $data['authorize']); - } - } - - /** - * 资料修改结果处理 - * @param bool $status - */ - protected function _info_form_result(bool $status) - { - if ($status) { - $this->success('用户资料修改成功!', 'javascript:location.reload()'); - } - } - /** * 修改当前用户密码 * @login true - * @throws \think\db\exception\DataNotFoundException - * @throws \think\db\exception\DbException - * @throws \think\db\exception\ModelNotFoundException + * @throws DataNotFoundException + * @throws DbException + * @throws ModelNotFoundException */ public function pass() { @@ -130,13 +114,15 @@ class Index extends Controller SystemUser::mForm('user/pass', 'id', [], ['id' => $id]); } else { $data = $this->_vali([ - 'password.require' => '登录密码不能为空!', - 'repassword.require' => '重复密码不能为空!', - 'oldpassword.require' => '旧的密码不能为空!', + 'password.require' => '登录密码不能为空!', + 'repassword.require' => '重复密码不能为空!', + 'oldpassword.require' => '旧的密码不能为空!', 'password.confirm:repassword' => '两次输入的密码不一致!', ]); $user = SystemUser::mk()->find($id); - if (empty($user)) $this->error('用户不存在!'); + if (empty($user)) { + $this->error('用户不存在!'); + } if (md5($data['oldpassword']) !== $user['password']) { $this->error('旧密码验证失败,请重新输入!'); } @@ -144,7 +130,7 @@ class Index extends Controller sysoplog('系统用户管理', "修改用户[{$user['id']}]密码成功"); // 修改密码同步事件处理 $this->app->event->trigger('PluginAdminChangePassword', [ - 'uuid' => intval($user['id']), 'pass' => $data['password'] + 'uuid' => intval($user['id']), 'pass' => $data['password'], ]); $this->success('密码修改成功,下次请使用新密码登录!', ''); } else { @@ -152,4 +138,24 @@ class Index extends Controller } } } + + /** + * 资料修改表单处理. + */ + protected function _info_form_filter(array &$data) + { + if ($this->request->isPost()) { + unset($data['username'], $data['authorize']); + } + } + + /** + * 资料修改结果处理. + */ + protected function _info_form_result(bool $status) + { + if ($status) { + $this->success('用户资料修改成功!', 'javascript:location.reload()'); + } + } } diff --git a/app/admin/controller/Login.php b/app/admin/controller/Login.php index c5e544544..84012dd52 100644 --- a/app/admin/controller/Login.php +++ b/app/admin/controller/Login.php @@ -1,24 +1,27 @@ runtimeMode = RuntimeService::check(); // 后台背景处理 $images = str2arr(sysconf('login_image|raw') ?: '', '|'); - if (empty($images)) $images = [ - SystemService::uri('/static/theme/img/login/bg1.jpg'), - SystemService::uri('/static/theme/img/login/bg2.jpg'), - ]; + if (empty($images)) { + $images = [ + SystemService::uri('/static/theme/img/login/bg1.jpg'), + SystemService::uri('/static/theme/img/login/bg2.jpg'), + ]; + } $this->loginStyle = sprintf('style="background-image:url(%s)" data-bg-transition="%s"', $images[0], join(',', $images)); // 更新后台主域名,用于部分无法获取域名的场景调用 if ($this->request->domain() !== sysconf('base.site_host|raw')) { @@ -68,16 +70,16 @@ class Login extends Controller } else { $data = $this->_vali([ 'username.require' => '登录账号不能为空!', - 'username.min:4' => '账号不能少于4位字符!', + 'username.min:4' => '账号不能少于4位字符!', 'password.require' => '登录密码不能为空!', - 'password.min:4' => '密码不能少于4位字符!', - 'verify.require' => '图形验证码不能为空!', - 'uniqid.require' => '图形验证标识不能为空!', + 'password.min:4' => '密码不能少于4位字符!', + 'verify.require' => '图形验证码不能为空!', + 'uniqid.require' => '图形验证标识不能为空!', ]); if (!CaptchaService::instance()->check($data['verify'], $data['uniqid'])) { $this->error('图形验证码验证失败,请重新输入!'); } - /*! 用户信息验证 */ + /* ! 用户信息验证 */ $map = ['username' => $data['username'], 'is_deleted' => 0]; $user = SystemUser::mk()->where($map)->findOrEmpty(); if ($user->isEmpty()) { @@ -108,12 +110,11 @@ class Login extends Controller /** * 生成验证码 - * @return void */ public function captcha() { $input = $this->_vali([ - 'type.require' => '类型不能为空!', + 'type.require' => '类型不能为空!', 'token.require' => '标识不能为空!', ]); $image = CaptchaService::instance()->initialize(); @@ -126,8 +127,7 @@ class Login extends Controller } /** - * 退出登录 - * @return void + * 退出登录. */ public function out() { diff --git a/app/admin/controller/Menu.php b/app/admin/controller/Menu.php index 39594716b..63e1784c5 100644 --- a/app/admin/controller/Menu.php +++ b/app/admin/controller/Menu.php @@ -1,20 +1,22 @@ _applyFormToken(); + SystemMenu::mForm('form'); + } + + /** + * 编辑系统菜单. + * @auth true + */ + public function edit() + { + $this->_applyFormToken(); + SystemMenu::mForm('form'); + } + + /** + * 修改菜单状态 + * @auth true + */ + public function state() + { + SystemMenu::mSave($this->_vali([ + 'status.in:0,1' => '状态值范围异常!', + 'status.require' => '状态值不能为空!', + ])); + } + + /** + * 删除系统菜单. + * @auth true + */ + public function remove() + { + SystemMenu::mDelete(); + } + + /** + * 列表数据处理. */ protected function _index_page_filter(array &$data) { $data = DataExtend::arr2tree($data); // 回收站过滤有效菜单 - if ($this->type === 'recycle') foreach ($data as $k1 => &$p1) { - if (!empty($p1['sub'])) foreach ($p1['sub'] as $k2 => &$p2) { - if (!empty($p2['sub'])) foreach ($p2['sub'] as $k3 => $p3) { - if ($p3['status'] > 0) unset($p2['sub'][$k3]); + if ($this->type === 'recycle') { + foreach ($data as $k1 => &$p1) { + if (!empty($p1['sub'])) { + foreach ($p1['sub'] as $k2 => &$p2) { + if (!empty($p2['sub'])) { + foreach ($p2['sub'] as $k3 => $p3) { + if ($p3['status'] > 0) { + unset($p2['sub'][$k3]); + } + } + } + if (empty($p2['sub']) && ($p2['url'] === '#' or $p2['status'] > 0)) { + unset($p1['sub'][$k2]); + } + } + } + if (empty($p1['sub']) && ($p1['url'] === '#' or $p1['status'] > 0)) { + unset($data[$k1]); } - if (empty($p2['sub']) && ($p2['url'] === '#' or $p2['status'] > 0)) unset($p1['sub'][$k2]); } - if (empty($p1['sub']) && ($p1['url'] === '#' or $p1['status'] > 0)) unset($data[$k1]); } // 菜单数据树数据变平化 $data = DataExtend::arr2table($data); @@ -81,35 +137,14 @@ class Menu extends Controller } foreach ($data as &$vo) { - if ($vo['url'] !== '#' && !preg_match('/^(https?:)?(\/\/|\\\\)/i', $vo['url'])) { - $vo['url'] = trim(url($vo['url']) . ($vo['params'] ? "?{$vo['params']}" : ''), '\\/'); + if ($vo['url'] !== '#' && !preg_match('/^(https?:)?(\/\/|\\\)/i', $vo['url'])) { + $vo['url'] = trim(url($vo['url']) . ($vo['params'] ? "?{$vo['params']}" : ''), '\/'); } } } /** - * 添加系统菜单 - * @auth true - */ - public function add() - { - $this->_applyFormToken(); - SystemMenu::mForm('form'); - } - - /** - * 编辑系统菜单 - * @auth true - */ - public function edit() - { - $this->_applyFormToken(); - SystemMenu::mForm('form'); - } - - /** - * 表单数据处理 - * @param array $vo + * 表单数据处理. */ protected function _form_filter(array &$vo) { @@ -130,32 +165,25 @@ class Menu extends Controller /* 列出可选上级菜单 */ $menus = SystemMenu::mk()->order('sort desc,id asc')->column('id,pid,icon,url,node,title,params', 'id'); $this->menus = DataExtend::arr2table(array_merge($menus, [['id' => '0', 'pid' => '-1', 'url' => '#', 'title' => '顶部菜单']])); - if (isset($vo['id'])) foreach ($this->menus as $menu) if ($menu['id'] === $vo['id']) $vo = $menu; - foreach ($this->menus as $key => $menu) if ($menu['spt'] >= 3 || $menu['url'] !== '#') unset($this->menus[$key]); - if (isset($vo['spt']) && isset($vo['spc']) && in_array($vo['spt'], [1, 2]) && $vo['spc'] > 0) { - foreach ($this->menus as $key => $menu) if ($vo['spt'] <= $menu['spt']) unset($this->menus[$key]); + if (isset($vo['id'])) { + foreach ($this->menus as $menu) { + if ($menu['id'] === $vo['id']) { + $vo = $menu; + } + } + } + foreach ($this->menus as $key => $menu) { + if ($menu['spt'] >= 3 || $menu['url'] !== '#') { + unset($this->menus[$key]); + } + } + if (isset($vo['spt'], $vo['spc']) && in_array($vo['spt'], [1, 2]) && $vo['spc'] > 0) { + foreach ($this->menus as $key => $menu) { + if ($vo['spt'] <= $menu['spt']) { + unset($this->menus[$key]); + } + } } } } - - /** - * 修改菜单状态 - * @auth true - */ - public function state() - { - SystemMenu::mSave($this->_vali([ - 'status.in:0,1' => '状态值范围异常!', - 'status.require' => '状态值不能为空!', - ])); - } - - /** - * 删除系统菜单 - * @auth true - */ - public function remove() - { - SystemMenu::mDelete(); - } } diff --git a/app/admin/controller/Oplog.php b/app/admin/controller/Oplog.php index 08b342d0f..da100368c 100644 --- a/app/admin/controller/Oplog.php +++ b/app/admin/controller/Oplog.php @@ -1,43 +1,46 @@ simple($vo['geoip']); - } catch (\Exception $exception) { - $vo['geoip'] = $exception->getMessage(); - } - } - - /** - * 清理系统日志 + * 清理系统日志. * @auth true */ public function clear() @@ -80,16 +68,32 @@ class Oplog extends Controller throw $exception; } catch (\Exception $exception) { trace_file($exception); - $this->error(lang("日志清理失败,%s", [$exception->getMessage()])); + $this->error(lang('日志清理失败,%s', [$exception->getMessage()])); } } /** - * 删除系统日志 + * 删除系统日志. * @auth true */ public function remove() { SystemOplog::mDelete(); } + + /** + * 列表数据处理. + * @throws \Exception + */ + protected function _index_page_filter(array &$data) + { + $region = new \Ip2Region(); + foreach ($data as &$vo) { + try { + $vo['geoisp'] = $region->simple($vo['geoip']); + } catch (\Exception $exception) { + $vo['geoip'] = $exception->getMessage(); + } + } + } } diff --git a/app/admin/controller/Queue.php b/app/admin/controller/Queue.php index 95633997b..4d6176c38 100644 --- a/app/admin/controller/Queue.php +++ b/app/admin/controller/Queue.php @@ -1,20 +1,22 @@ 0, 'pre' => 0, 'oks' => 0, 'ers' => 0]; - SystemQueue::mk()->field('status,count(1) count')->group('status')->select()->map(static function ($item) use (&$result) { - if (intval($item['status']) === 1) $result['extra']['pre'] = $item['count']; - if (intval($item['status']) === 2) $result['extra']['dos'] = $item['count']; - if (intval($item['status']) === 3) $result['extra']['oks'] = $item['count']; - if (intval($item['status']) === 4) $result['extra']['ers'] = $item['count']; - }); - } - /** * 重启系统任务 * @auth true @@ -98,12 +82,12 @@ class Queue extends Controller } /** - * 清理运行数据 + * 清理运行数据. * @auth true */ public function clean() { - $this->_queue('定时清理系统运行数据', "xadmin:queue clean", 0, [], 0, 3600); + $this->_queue('定时清理系统运行数据', 'xadmin:queue clean', 0, [], 0, 3600); } /** @@ -114,4 +98,29 @@ class Queue extends Controller { SystemQueue::mDelete(); } + + /** + * 分页数据回调处理. + * @throws DataNotFoundException + * @throws DbException + * @throws ModelNotFoundException + */ + protected function _index_page_filter(array $data, array &$result) + { + $result['extra'] = ['dos' => 0, 'pre' => 0, 'oks' => 0, 'ers' => 0]; + SystemQueue::mk()->field('status,count(1) count')->group('status')->select()->map(static function ($item) use (&$result) { + if (intval($item['status']) === 1) { + $result['extra']['pre'] = $item['count']; + } + if (intval($item['status']) === 2) { + $result['extra']['dos'] = $item['count']; + } + if (intval($item['status']) === 3) { + $result['extra']['oks'] = $item['count']; + } + if (intval($item['status']) === 4) { + $result['extra']['ers'] = $item['count']; + } + }); + } } diff --git a/app/admin/controller/User.php b/app/admin/controller/User.php index f221b3616..d8d62e251 100644 --- a/app/admin/controller/User.php +++ b/app/admin/controller/User.php @@ -1,18 +1,22 @@ title = '系统用户管理'; $this->bases = SystemBase::items('身份权限'); }, function (QueryHelper $query) { - // 加载对应数据列表 $query->where(['is_deleted' => 0, 'status' => intval($this->type === 'index')]); // 关联用户身份资料 - /** @var \think\model\Relation|\think\db\Query $query */ + /* @var \think\model\Relation|\think\db\Query $query */ $query->with(['userinfo' => static function ($query) { $query->field('code,name,content'); }]); @@ -62,7 +67,7 @@ class User extends Controller } /** - * 添加系统用户 + * 添加系统用户. * @auth true */ public function add() @@ -71,7 +76,7 @@ class User extends Controller } /** - * 编辑系统用户 + * 编辑系统用户. * @auth true */ public function edit() @@ -91,16 +96,16 @@ class User extends Controller SystemUser::mForm('pass'); } else { $data = $this->_vali([ - 'id.require' => '用户ID不能为空!', - 'password.require' => '登录密码不能为空!', - 'repassword.require' => '重复密码不能为空!', + 'id.require' => '用户ID不能为空!', + 'password.require' => '登录密码不能为空!', + 'repassword.require' => '重复密码不能为空!', 'repassword.confirm:password' => '两次输入的密码不一致!', ]); $user = SystemUser::mk()->findOrEmpty($data['id']); if ($user->isExists() && $user->save(['password' => md5($data['password'])])) { // 修改密码同步事件处理 $this->app->event->trigger('PluginAdminChangePassword', [ - 'uuid' => $data['id'], 'pass' => $data['password'] + 'uuid' => $data['id'], 'pass' => $data['password'], ]); sysoplog('系统用户管理', "修改用户[{$data['id']}]密码成功"); $this->success('密码修改成功,请使用新密码登录!', ''); @@ -111,11 +116,33 @@ class User extends Controller } /** - * 表单数据处理 - * @param array $data - * @throws \think\db\exception\DataNotFoundException - * @throws \think\db\exception\DbException - * @throws \think\db\exception\ModelNotFoundException + * 修改用户状态 + * @auth true + */ + public function state() + { + $this->_checkInput(); + SystemUser::mSave($this->_vali([ + 'status.in:0,1' => '状态值范围异常!', + 'status.require' => '状态值不能为空!', + ])); + } + + /** + * 删除系统用户. + * @auth true + */ + public function remove() + { + $this->_checkInput(); + SystemUser::mDelete(); + } + + /** + * 表单数据处理. + * @throws DataNotFoundException + * @throws DbException + * @throws ModelNotFoundException */ protected function _form_filter(array &$data) { @@ -131,7 +158,7 @@ class User extends Controller // 检查账号是否重复 $map = ['username' => $data['username'], 'is_deleted' => 0]; if (SystemUser::mk()->where($map)->count() > 0) { - $this->error("账号已经存在,请使用其它账号!"); + $this->error('账号已经存在,请使用其它账号!'); } // 新添加的用户密码与账号相同 $data['password'] = md5($data['username']); @@ -148,30 +175,7 @@ class User extends Controller } /** - * 修改用户状态 - * @auth true - */ - public function state() - { - $this->_checkInput(); - SystemUser::mSave($this->_vali([ - 'status.in:0,1' => '状态值范围异常!', - 'status.require' => '状态值不能为空!', - ])); - } - - /** - * 删除系统用户 - * @auth true - */ - public function remove() - { - $this->_checkInput(); - SystemUser::mDelete(); - } - - /** - * 检查输入变量 + * 检查输入变量. */ private function _checkInput() { diff --git a/app/admin/controller/api/Plugs.php b/app/admin/controller/api/Plugs.php index d1e7702a1..61d752821 100644 --- a/app/admin/controller/api/Plugs.php +++ b/app/admin/controller/api/Plugs.php @@ -1,37 +1,38 @@ request->get('uptoken', ''); $domain = boolval(AdminService::withUploadUnid($token)); return response(join("\r\n", [ - sprintf("window.taDebug = %s;", $this->app->isDebug() ? 'true' : 'false'), + sprintf('window.taDebug = %s;', $this->app->isDebug() ? 'true' : 'false'), sprintf("window.taAdmin = '%s';", sysuri('admin/index/index', [], false, $domain)), sprintf("window.taEditor = '%s';", sysconf('base.editor|raw') ?: 'ckeditor4'), ]))->contentType('application/javascript'); } /** - * 优化数据库 + * 优化数据库. * @login true */ public function optimize() @@ -100,4 +100,4 @@ class Plugs extends Controller $this->error('请使用超管账号操作!'); } } -} \ No newline at end of file +} diff --git a/app/admin/controller/api/Queue.php b/app/admin/controller/api/Queue.php index b932c7a64..ebc34be76 100644 --- a/app/admin/controller/api/Queue.php +++ b/app/admin/controller/api/Queue.php @@ -1,20 +1,22 @@ app->console->call('xadmin:queue', ['stop'])->fetch(); - if (stripos($message, 'sent end signal to process')) { - sysoplog('系统运维管理', '尝试停止任务监听服务'); - $this->success('停止任务监听服务成功!'); - } elseif (stripos($message, 'processes to stop')) { - $this->success('没有找到需要停止的服务!'); - } else { - $this->error(nl2br($message)); + if (AdminService::isSuper()) { + try { + $message = $this->app->console->call('xadmin:queue', ['stop'])->fetch(); + if (stripos($message, 'sent end signal to process')) { + sysoplog('系统运维管理', '尝试停止任务监听服务'); + $this->success('停止任务监听服务成功!'); + } elseif (stripos($message, 'processes to stop')) { + $this->success('没有找到需要停止的服务!'); + } else { + $this->error(nl2br($message)); + } + } catch (HttpResponseException $exception) { + throw $exception; + } catch (\Exception $exception) { + trace_file($exception); + $this->error($exception->getMessage()); } - } catch (HttpResponseException $exception) { - throw $exception; - } catch (\Exception $exception) { - trace_file($exception); - $this->error($exception->getMessage()); } else { $this->error('请使用超管账号操作!'); } @@ -63,21 +66,23 @@ class Queue extends Controller */ public function start() { - if (AdminService::isSuper()) try { - $message = $this->app->console->call('xadmin:queue', ['start'])->fetch(); - if (stripos($message, 'daemons started successfully for pid')) { - sysoplog('系统运维管理', '尝试启动任务监听服务'); - $this->success('任务监听服务启动成功!'); - } elseif (stripos($message, 'daemons already exist for pid')) { - $this->success('任务监听服务已经启动!'); - } else { - $this->error(nl2br($message)); + if (AdminService::isSuper()) { + try { + $message = $this->app->console->call('xadmin:queue', ['start'])->fetch(); + if (stripos($message, 'daemons started successfully for pid')) { + sysoplog('系统运维管理', '尝试启动任务监听服务'); + $this->success('任务监听服务启动成功!'); + } elseif (stripos($message, 'daemons already exist for pid')) { + $this->success('任务监听服务已经启动!'); + } else { + $this->error(nl2br($message)); + } + } catch (HttpResponseException $exception) { + throw $exception; + } catch (\Exception $exception) { + trace_file($exception); + $this->error($exception->getMessage()); } - } catch (HttpResponseException $exception) { - throw $exception; - } catch (\Exception $exception) { - trace_file($exception); - $this->error($exception->getMessage()); } else { $this->error('请使用超管账号操作!'); } @@ -89,15 +94,17 @@ class Queue extends Controller */ public function status() { - if (AdminService::isSuper()) try { - $message = $this->app->console->call('xadmin:queue', ['status'])->fetch(); - if (preg_match('/process.*?\d+.*?running/', $message)) { - echo "{$this->app->lang->get('已启动')}"; - } else { - echo "{$this->app->lang->get('未启动')}"; + if (AdminService::isSuper()) { + try { + $message = $this->app->console->call('xadmin:queue', ['status'])->fetch(); + if (preg_match('/process.*?\d+.*?running/', $message)) { + echo "{$this->app->lang->get('已启动')}"; + } else { + echo "{$this->app->lang->get('未启动')}"; + } + } catch (\Error|\Exception $exception) { + echo "{$this->app->lang->get('异 常')}"; } - } catch (\Error|\Exception $exception) { - echo "{$this->app->lang->get('异 常')}"; } else { $message = lang('只有超级管理员才能操作!'); echo "{$this->app->lang->get('无权限')}"; @@ -105,7 +112,7 @@ class Queue extends Controller } /** - * 查询任务进度 + * 查询任务进度. * @login true */ public function progress() @@ -115,4 +122,4 @@ class Queue extends Controller $message = SystemQueue::mk()->where($input)->value('message', ''); $this->success('获取任务进度成功d!', json_decode($message, true)); } -} \ No newline at end of file +} diff --git a/app/admin/controller/api/System.php b/app/admin/controller/api/System.php index a9e1e9a1c..5e02b8faf 100644 --- a/app/admin/controller/api/System.php +++ b/app/admin/controller/api/System.php @@ -1,20 +1,22 @@ success('网站缓存加速成功!', 'javascript:location.reload()'); - } catch (HttpResponseException $exception) { - throw $exception; - } catch (\Exception $exception) { - trace_file($exception); - $this->error($exception->getMessage()); + if (AdminService::isSuper()) { + try { + RuntimeService::push() && sysoplog('系统运维管理', '刷新发布运行缓存'); + $this->success('网站缓存加速成功!', 'javascript:location.reload()'); + } catch (HttpResponseException $exception) { + throw $exception; + } catch (\Exception $exception) { + trace_file($exception); + $this->error($exception->getMessage()); + } } else { $this->error('请使用超管账号操作!'); } } /** - * 清理运行缓存 + * 清理运行缓存. * @login true */ public function clear() { - if (AdminService::isSuper()) try { - RuntimeService::clear() && sysoplog('系统运维管理', '清理网站日志缓存'); - $this->success('清空日志缓存成功!', 'javascript:location.reload()'); - } catch (HttpResponseException $exception) { - throw $exception; - } catch (\Exception $exception) { - trace_file($exception); - $this->error($exception->getMessage()); + if (AdminService::isSuper()) { + try { + RuntimeService::clear() && sysoplog('系统运维管理', '清理网站日志缓存'); + $this->success('清空日志缓存成功!', 'javascript:location.reload()'); + } catch (HttpResponseException $exception) { + throw $exception; + } catch (\Exception $exception) { + trace_file($exception); + $this->error($exception->getMessage()); + } } else { $this->error('请使用超管账号操作!'); } } /** - * 当前运行模式 + * 当前运行模式. * @login true */ public function debug() { - if (AdminService::isSuper()) if (input('state')) { - RuntimeService::set('product'); - sysoplog('系统运维管理', '开发模式切换为生产模式'); - $this->success('已切换为生产模式!', 'javascript:location.reload()'); - } else { - RuntimeService::set('debug'); - sysoplog('系统运维管理', '生产模式切换为开发模式'); - $this->success('已切换为开发模式!', 'javascript:location.reload()'); + if (AdminService::isSuper()) { + if (input('state')) { + RuntimeService::set('product'); + sysoplog('系统运维管理', '开发模式切换为生产模式'); + $this->success('已切换为生产模式!', 'javascript:location.reload()'); + } else { + RuntimeService::set('debug'); + sysoplog('系统运维管理', '生产模式切换为开发模式'); + $this->success('已切换为开发模式!', 'javascript:location.reload()'); + } } else { $this->error('请使用超管账号操作!'); } } /** - * 修改富文本编辑器 - * @return void + * 修改富文本编辑器. * @throws \think\admin\Exception */ public function editor() @@ -107,32 +112,36 @@ class System extends Controller } /** - * 清理系统配置 + * 清理系统配置. * @login true */ public function config() { - if (AdminService::isSuper()) try { - [$tmpdata, $newdata] = [[], []]; - foreach (SystemConfig::mk()->order('type,name asc')->cursor() as $item) { - $tmpdata[$item['type']][$item['name']] = $item['value']; + if (AdminService::isSuper()) { + try { + [$tmpdata, $newdata] = [[], []]; + foreach (SystemConfig::mk()->order('type,name asc')->cursor() as $item) { + $tmpdata[$item['type']][$item['name']] = $item['value']; + } + foreach ($tmpdata as $type => $items) { + foreach ($items as $name => $value) { + $newdata[] = ['type' => $type, 'name' => $name, 'value' => $value]; + } + } + $this->app->db->transaction(static function () use ($newdata) { + SystemConfig::mQuery()->empty()->insertAll($newdata); + }); + $this->app->cache->delete('SystemConfig'); + sysoplog('系统运维管理', '清理系统配置参数'); + $this->success('清理系统配置成功!', 'javascript:location.reload()'); + } catch (HttpResponseException $exception) { + throw $exception; + } catch (\Exception $exception) { + trace_file($exception); + $this->error($exception->getMessage()); } - foreach ($tmpdata as $type => $items) foreach ($items as $name => $value) { - $newdata[] = ['type' => $type, 'name' => $name, 'value' => $value]; - } - $this->app->db->transaction(static function () use ($newdata) { - SystemConfig::mQuery()->empty()->insertAll($newdata); - }); - $this->app->cache->delete('SystemConfig'); - sysoplog('系统运维管理', '清理系统配置参数'); - $this->success('清理系统配置成功!', 'javascript:location.reload()'); - } catch (HttpResponseException $exception) { - throw $exception; - } catch (\Exception $exception) { - trace_file($exception); - $this->error($exception->getMessage()); } else { $this->error('请使用超管账号操作!'); } } -} \ No newline at end of file +} diff --git a/app/admin/controller/api/Upload.php b/app/admin/controller/api/Upload.php index a9dbc4255..9a3d54914 100644 --- a/app/admin/controller/api/Upload.php +++ b/app/admin/controller/api/Upload.php @@ -1,20 +1,22 @@ []]; [$uuid, $unid, $exts] = $this->initUnid(false); $allows = str2arr(sysconf('storage.allow_exts|raw')); - if (empty($uuid) && $unid > 0) $allows = array_intersect($exts, $allows); - foreach ($allows as $ext) $data['exts'][$ext] = Storage::mime($ext); + if (empty($uuid) && $unid > 0) { + $allows = array_intersect($exts, $allows); + } + foreach ($allows as $ext) { + $data['exts'][$ext] = Storage::mime($ext); + } $data['exts'] = json_encode($data['exts'], JSON_UNESCAPED_UNICODE); $data['nameType'] = sysconf('storage.name_type|raw') ?: 'xmd5'; return view(dirname(__DIR__, 2) . '/view/api/upload.js', $data)->contentType('application/x-javascript'); } /** - * 文件选择器 - * @throws \think\db\exception\DataNotFoundException - * @throws \think\db\exception\DbException - * @throws \think\db\exception\ModelNotFoundException + * 文件选择器. + * @throws DataNotFoundException + * @throws DbException + * @throws ModelNotFoundException */ public function image() { @@ -69,10 +76,12 @@ class Upload extends Controller SystemFile::mQuery()->layTable(function () { $this->title = '文件选择器'; }, function (QueryHelper $query) use ($unid, $uuid) { - if ($unid && $uuid) $query->where(function ($query) use ($uuid, $unid) { - /** @var \think\db\Query $query */ - $query->whereOr([['uuid', '=', $uuid], ['unid', '=', $unid]]); - }); else { + if ($unid && $uuid) { + $query->where(function ($query) use ($uuid, $unid) { + /* @var \think\db\Query $query */ + $query->whereOr([['uuid', '=', $uuid], ['unid', '=', $unid]]); + }); + } else { $query->where($unid ? ['unid' => $unid] : ['uuid' => $uuid]); } $query->where(['status' => 2, 'issafe' => 0])->in('xext#type'); @@ -81,7 +90,7 @@ class Upload extends Controller } /** - * 文件上传检查 + * 文件上传检查. */ public function state() { @@ -90,10 +99,10 @@ class Upload extends Controller [$name, $safe] = [input('name'), $this->getSafe()]; $data = ['uptype' => $this->getType(), 'safe' => intval($safe), 'key' => input('key')]; $file = SystemFile::mk()->data($this->_vali([ - 'xkey.value' => $data['key'], - 'type.value' => $this->getType(), - 'uuid.value' => $uuid, - 'unid.value' => $unid, + 'xkey.value' => $data['key'], + 'type.value' => $this->getType(), + 'uuid.value' => $uuid, + 'unid.value' => $unid, 'name.require' => '名称不能为空!', 'hash.require' => '哈希不能为空!', 'xext.require' => '后缀不能为空!', @@ -102,22 +111,24 @@ class Upload extends Controller 'status.value' => 1, ])); $mime = $file->getAttr('mime'); - if (empty($mime)) $file->setAttr('mime', Storage::mime($file->getAttr('xext'))); + if (empty($mime)) { + $file->setAttr('mime', Storage::mime($file->getAttr('xext'))); + } $info = Storage::instance($data['uptype'])->info($data['key'], $safe, $name); - if (isset($info['url']) && isset($info['key'])) { + if (isset($info['url'], $info['key'])) { $file->save(['xurl' => $info['url'], 'isfast' => 1, 'issafe' => $data['safe']]); $extr = ['id' => $file->id ?? 0, 'url' => $info['url'], 'key' => $info['key']]; $this->success('文件已经上传', array_merge($data, $extr), 200); - } elseif ('local' === $data['uptype']) { + } elseif ($data['uptype'] === 'local') { $local = LocalStorage::instance(); $data['url'] = $local->url($data['key'], $safe, $name); $data['server'] = $local->upload(); - } elseif ('qiniu' === $data['uptype']) { + } elseif ($data['uptype'] === 'qiniu') { $qiniu = QiniuStorage::instance(); $data['url'] = $qiniu->url($data['key'], $safe, $name); $data['token'] = $qiniu->token($data['key'], 3600, $name); $data['server'] = $qiniu->upload(); - } elseif ('alioss' === $data['uptype']) { + } elseif ($data['uptype'] === 'alioss') { $alioss = AliossStorage::instance(); $token = $alioss->token($data['key'], 3600, $name); $data['url'] = $token['siteurl']; @@ -125,7 +136,7 @@ class Upload extends Controller $data['signature'] = $token['signature']; $data['OSSAccessKeyId'] = $token['keyid']; $data['server'] = $alioss->upload(); - } elseif ('txcos' === $data['uptype']) { + } elseif ($data['uptype'] === 'txcos') { $txcos = TxcosStorage::instance(); $token = $txcos->token($data['key'], 3600, $name); $data['url'] = $token['siteurl']; @@ -135,14 +146,14 @@ class Upload extends Controller $data['q-signature'] = $token['q-signature']; $data['q-sign-algorithm'] = $token['q-sign-algorithm']; $data['server'] = $txcos->upload(); - } elseif ('upyun' === $data['uptype']) { + } elseif ($data['uptype'] === 'upyun') { $upyun = UpyunStorage::instance(); $token = $upyun->token($data['key'], 3600, $name, input('hash', '')); $data['url'] = $token['siteurl']; $data['policy'] = $token['policy']; $data['server'] = $upyun->upload(); $data['authorization'] = $token['authorization']; - } elseif ('alist' === $data['uptype']) { + } elseif ($data['uptype'] === 'alist') { $alist = AlistStorage::instance(); $data['url'] = $alist->url($data['key']); $data['server'] = $alist->upload(); @@ -162,19 +173,20 @@ class Upload extends Controller /** * 更新文件状态 - * @return void */ public function done() { [$uuid, $unid] = $this->initUnid(); $data = $this->_vali([ - 'id.require' => '编号不能为空!', + 'id.require' => '编号不能为空!', 'hash.require' => '哈希不能为空!', - 'uuid.value' => $uuid, - 'unid.value' => $unid, + 'uuid.value' => $uuid, + 'unid.value' => $unid, ]); $file = SystemFile::mk()->where($data)->findOrEmpty(); - if ($file->isEmpty()) $this->error('文件不存在!'); + if ($file->isEmpty()) { + $this->error('文件不存在!'); + } if ($file->save(['status' => 2])) { $this->success('更新成功!'); } else { @@ -183,7 +195,7 @@ class Upload extends Controller } /** - * 文件上传入口 + * 文件上传入口. * @throws \think\admin\Exception */ public function file() @@ -251,8 +263,7 @@ class Upload extends Controller } /** - * 获取上传类型 - * @return boolean + * 获取上传类型. */ private function getSafe(): bool { @@ -260,8 +271,7 @@ class Upload extends Controller } /** - * 获取上传方式 - * @return string + * 获取上传方式. * @throws \think\admin\Exception */ private function getType(): string @@ -269,9 +279,8 @@ class Upload extends Controller $type = strtolower(input('uptype', '')); if (in_array($type, array_keys(Storage::types()))) { return $type; - } else { - return strtolower(sysconf('storage.type|raw')); } + return strtolower(sysconf('storage.type|raw')); } /** @@ -284,9 +293,8 @@ class Upload extends Controller $file = $this->request->file('file'); if ($file instanceof UploadedFile) { return $file; - } else { - $this->error('读取临时文件失败!'); } + $this->error('读取临时文件失败!'); } catch (HttpResponseException $exception) { throw $exception; } catch (\Exception $exception) { @@ -297,8 +305,6 @@ class Upload extends Controller /** * 初始化用户状态 - * @param boolean $check - * @return array */ private function initUnid(bool $check = true): array { @@ -312,9 +318,7 @@ class Upload extends Controller } /** - * 检查图片是否安全 - * @param string $filename - * @return boolean + * 检查图片是否安全. */ private function imgNotSafe(string $filename): bool { @@ -326,11 +330,17 @@ class Upload extends Controller } else { $hexs = bin2hex(fread($source, $size)); } - if (is_resource($source)) fclose($source); + if (is_resource($source)) { + fclose($source); + } $bins = hex2bin($hexs); /* 匹配十六进制中的 <% ( ) %> 或