refactor: 重构权限服务类

This commit is contained in:
Ying
2023-07-03 10:08:34 +08:00
parent 8becf0ef36
commit 4e377def8d
42 changed files with 1679 additions and 1524 deletions

View File

@@ -308,7 +308,7 @@ class Index extends AdminController
$columns = ['用户注册' => 'create_time', '用户登录' => 'login_time', '邀请注册' => 'invite_id'];
foreach ($columns as $index => $field) {
$time = str_replace('invite_id', 'create_time', $field);
$resultList[$index] = \app\common\model\system\User::where($time, 'between time', [$dateBefore, $dateAfter])
$resultList[$index] = User::where($time, 'between time', [$dateBefore, $dateAfter])
->when($condition, function ($query) use ($condition, $time, $field) {
$query->field("FROM_UNIXTIME($time, '$condition') as day,count(*) as count");
if ($field == 'invite_id') {

View File

@@ -2,12 +2,11 @@
namespace app\admin\controller;
use app\admin\service\LoginService;
use app\common\exception\OperateException;
use support\Response;
use Webman\Event\Event;
use app\AdminController;
use app\common\model\system\Admin;
use app\common\model\system\AdminLog;
use Psr\SimpleCache\InvalidArgumentException;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
@@ -29,16 +28,16 @@ class Login extends AdminController
/**
* 登录函数
* @return Response
* @throws InvalidArgumentException
* @throws OperateException
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
*/
public function index(): \support\Response
public function index(): Response
{
// 禁止重复访问
$session = get_admin_info();
if (isset($session['id'])) {
$adminInfo = get_admin_info();
if (isset($adminInfo['id'])) {
return $this->redirect('/admin/index');
}
@@ -46,103 +45,17 @@ class Login extends AdminController
$user = request()->post('name');
$pwd = request()->post('pwd');
$captcha = request()->post('captcha');
if ((isset($session['count']) && $session['count'] >= 5)
&& (isset($session['time']) && $session['time'] >= strtotime('- 5 minutes'))) {
return $this->displayResponse('错误次数过多,请稍后再试!');
}
validate(\app\common\validate\system\Admin::class)->scene('login')->check([
'name' => $user,
'pwd' => $pwd,
]);
// 验证码
if (isset($session['isCaptcha'])) {
if (!$captcha || !$this->captchaCheck($captcha)) {
return $this->displayResponse('验证码错误!');
}
}
// 验证表单令牌
if (!request()->checkToken('__token__', request()->all())) {
return $this->displayResponse('表单令牌错误!', ['token' => token()]);
} else {
$result = Admin::checkLogin($user, $pwd);
if (empty($result)) {
$session['time'] = time();
$session['isCaptcha'] = true;
$session['count'] = isset($session['count']) ? $session['count'] + 1 : 1;
request()->session()->set(AdminSession, $session);
// 执行登录失败事件
Event::emit('adminLoginError', request()->all());
return $this->displayResponse('用户名或密码错误!', ['token' => token()]);
}
if ($result['status'] !== 1) {
return $this->displayResponse('账号已被禁用!');
}
$result->login_ip = request()->getRealIp();
$result->login_time = time();
$result->count = $result->count + 1;
try {
$result->save();
$session = array_merge($session, $result->toArray());
request()->session()->set(AdminSession, $session);
} catch (\Throwable $th) {
return $this->error($th->getMessage());
}
Event::emit('adminLoginSuccess', $result->toArray());
return $this->displayResponse('登录成功!', [] , $this->JumpUrl);
}
LoginService::accountLogin($user, $pwd, $captcha, $adminInfo);
return $this->success('登录成功!', $this->JumpUrl);
}
return view('login/index', [
'captcha' => $session['isCaptcha'] ?? false,
]);
}
/**
* 退出登录
* @param string $msg
* @param array $data
* @param string $url
* @return Response
*/
private function displayResponse(string $msg = 'error', array $data = [], string $url = ''): Response
{
$this->adminLoginLog($msg, $url ? 1 : 0);
return empty($url) ? $this->error($msg, $url, $data) : $this->success($msg, $url);
}
/**
* 写入登录日志
* @param string $error
* @param int $status
*/
private function adminLoginLog(string $error, int $status = 0)
{
$name = \request()->input('name');
$userAgent = \request()->header('user-agent');
$nickname = $this->model->where('name', $name)->value('nickname');
if (preg_match('/.*?\((.*?)\).*?/', $userAgent, $matches)) {
$user_os = substr($matches[1], 0, strpos($matches[1], ';'));
} else {
$user_os = '未知';
}
$user_browser = preg_replace('/[^(]+\((.*?)[^)]+\) .*?/', '$1', $userAgent);
$data = [
'user_ip' => request()->getRealIp(),
'user_agent' => $userAgent,
'user_os' => $user_os,
'user_browser' => $user_browser,
'name' => $name,
'nickname' => $nickname ?? '未知',
'error' => $error,
'status' => $status,
];
AdminLog::create($data);
}
}

View File

@@ -13,7 +13,10 @@ declare(strict_types=1);
namespace app\admin\controller\system;
use app\admin\enums\AdminEnum;
use app\admin\service\AdminService;
use app\AdminController;
use app\common\exception\OperateException;
use app\common\model\system\AdminNotice;
use app\common\model\system\Jobs;
use app\common\model\system\Department;
@@ -63,64 +66,20 @@ class Admin extends AdminController
/**
* 获取资源列表
* @return Response
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
*/
public function index()
public function index(): Response
{
$this->jobs = Jobs::select()->toArray();
$this->group = AdminGroupModel::select()->toArray();
$this->department = Department::getListTree();
// 判断isAjax
if (request()->isAjax()) {
// 获取数据
$post = \request()->all();
$page = (int)request()->input('page') ?? 1;
$limit = (int)request()->input('limit') ?? 10;
$status = !empty($post['status']) ? $post['status'] - 1 : 1;
// 生成查询条件
$where = array();
if (!empty($post['name'])) {
$where[] = ['name', 'like', '%' . $post['name'] . '%'];
}
if (!empty($post['dep'])) {
$where[] = ['department_id', 'find in set', $post['dep']];
}
if (!empty($post['group_id'])) {
$where[] = ['group_id', 'find in set', $post['group_id']];
}
// 生成查询数据
$where[] = ['status', '=', $status];
$count = $this->model->where($where)->count();
$page = ($count <= $limit) ? 1 : $page;
$list = $this->model->where($where)->order("id asc")->withoutField('pwd')->limit((int)$limit)->page((int)$page)->select()->toArray();
// 循环处理数据
foreach ($list as $key => $value) {
$groupIDs = explode(',', $value['group_id']);
foreach ($groupIDs as $field => $id) {
// 查找组
$result = list_search($this->group, ['id' => $id]);
if (!empty($result)) {
$list[$key]['group'][$field] = $result;
}
}
if (!empty($list[$key]['group'])) {
$list[$key]['group'] = list_sort_by($list[$key]['group'], 'id');
}
$authNodes = $this->auth->getRulesNode($value['id']);
$list[$key][AUTH_RULES] = $authNodes[$this->auth->authPrivate];
$authNodes = $this->auth->getRulesNode($value['id'], AUTH_CATE);
$list[$key][AUTH_CATE] = $authNodes[$this->auth->authPrivate];
}
$params = request()->all();
list('count' => $count, 'list' => $list) = AdminService::dataList($params);
return $this->success('查询成功', null, $list, $count);
}
@@ -134,39 +93,15 @@ class Admin extends AdminController
/**
* 添加管理员
* @return Response
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
* @throws OperateException
*/
public function add(): \support\Response
public function add(): Response
{
if (request()->isPost()) {
// 验证数据
$post = request()->post();
$post = request_validate_rules($post, get_class($this->model));
if (!is_array($post)) {
return $this->error($post);
}
$where[] = ['name', '=', $post['name']];
$where[] = ['email', '=', $post['email']];
if ($this->model->whereOr($where)->find()) {
return $this->error('该用户名或邮箱已被注册!');
}
// 管理员加密
$post['pwd'] = encryptPwd($post['pwd']);
$post['create_ip'] = request()->getRealIp();
$data = $this->model->create($post);
if (!is_empty($data['id'])) {
$access['admin_id'] = $data['id'];
$access['group_id'] = $data['group_id'];
AdminAccessModel::insert($access);
return $this->success('添加管理员成功!');
} else {
return $this->error('添加管理员失败!');
}
validate(\app\common\validate\system\Admin::class)->scene('add')->check($post);
AdminService::add($post);
return $this->success('添加管理员成功');
}
// 获取用户组
@@ -175,115 +110,65 @@ class Admin extends AdminController
/**
* 更新管理员
* @return Response
* @throws OperateException
*/
public function edit()
public function edit(): Response
{
if (request()->isPost()) {
$id = request()->input('id');
if (!empty($id) && is_numeric($id)) {
// 验证数据
$post = request()->all();
$retError = request_validate_rules($post, get_class($this->model), 'edit');
if (!is_array($retError)) {
return $this->error($retError);
}
if (isset($post['pwd']) && !empty($post['pwd'])) {
$post['pwd'] = encryptPwd($post['pwd']);
} else {
// 清空避免被覆盖
unset($post['pwd']);
}
if ($this->model->update($post)) {
$access['group_id'] = $post['group_id'];
AdminAccessModel::where('admin_id', $id)->update($access);
return $this->success('更新管理员成功!');
}
}
$post = request()->all();
validate(\app\common\validate\system\Admin::class)->scene('edit')->check($post);
AdminService::edit($post);
return $this->success('更新管理员成功');
}
return $this->error('更新管理员失败');
}
/**
* 获取用户权限树
* @access public
* getAdminRules
*/
public function getPermissions()
{
return $this->authService->getPermissionsMenu();
}
/**
* 获取节点数据
* @access public
*/
public function getRuleCateTree()
{
$type = input('type', AdminEnum::ADMIN_AUTH_RULES);
return $this->authService->getRuleCatesTree($type, $this->authService->authPrivate);
}
/**
* 编辑权限
* @return Response
* @access public
* @return Response
* @throws OperateException
*/
public function editRules(): Response
{
return $this->updateRuleCates();
$adminId = input('admin_id', 0);
AdminService::updateRulesNodes($adminId, AdminEnum::ADMIN_AUTH_RULES);
return $this->success('更新权限成功!');
}
/**
* 编辑栏目权限
* @return Response
* @access public
* @return Response
* @throws OperateException
*/
public function editCates(): Response
{
return $this->updateRuleCates(AUTH_CATE);
}
/**
* 更新权限函数
* @access protected
* @param string $type
* @return Response
*/
protected function updateRuleCates(string $type = AUTH_RULES): Response
{
$admin_id = input('admin_id');
$rules = request()->post($type) ?? [];
$access = $this->auth->getRulesNode($admin_id, $type);
$rules = array_diff($rules, $access[$this->auth->authGroup]);
// 权限验证
if (!$this->auth->checkRuleOrCateNodes($rules, $type, $this->auth->authPrivate)) {
return $this->error('没有权限!');
}
// 获取个人节点
$differ = array_diff($access[$this->auth->authPrivate], $access[$this->auth->authGroup]);
$current = [];
if (!$this->auth->superAdmin()) {
$current = $this->auth->getRulesNode();
$current = array_diff($differ, $current[$this->auth->authPrivate]);
}
$rules = array_unique(array_merge($rules, $current));
$AdminAccessModel = new AdminAccessModel();
$data = ["$type" => implode(',', $rules)];
if ($AdminAccessModel->update($data, ['admin_id' => $admin_id])) {
return $this->success('更新权限成功!');
}
return $this->error('更新权限失败!');
}
/**
* 获取用户权限树
* getAdminRules
* @return mixed
*/
public function getPermissions(): mixed
{
$list = [];
if (\request()->isAjax()) {
$type = input('type', 'menu');
$group = input('group', 0);
if ($type == 'menu') {
return $this->auth->getRulesMenu();
} else {
try {
$list = $this->auth->getRuleCatesTree($type, $group ? $this->auth->authGroup : $this->auth->authPrivate);
} catch (\Exception $e) {
return $this->error($e->getMessage());
}
return $list;
}
}
return $list;
$adminId = input('admin_id', 0);
AdminService::updateRulesNodes($adminId, AdminEnum::ADMIN_AUTH_CATES);
return $this->success('更新权限成功!');
}
/**
@@ -295,157 +180,6 @@ class Admin extends AdminController
return view('/system/admin/theme');
}
/**
* 消息模板
* @return Response
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
*/
public function bells(): Response
{
$list = [];
$count = [];
$array = ['notice', 'message', 'todo'];
$type = input('type', 'notice');
if (\request()->isAjax()) {
$page = input('page', 1);
$limit = input('limit', 3);
// 计算最大页码
$data = AdminNotice::with(['admin'])->where(['type' => $type, 'admin_id' => get_admin_id()])
->order('id', 'desc')->paginate(['list_rows' => $limit, 'page' => $page])->toArray();
return $this->success('获取成功', '', $data);
}
foreach ($array as $item) {
$where = [
['type', '=', $item],
['admin_id', '=', get_admin_id()]
];
$count[$item] = AdminNotice::where($where)->where('status', 0)->count();
$list[$item] = AdminNotice::with(['admin'])->withoutField('content')->where($where)->limit(3)->order('id desc')->select()->toArray();
}
return view('/system/admin/bells', [
'list' => $list,
'count' => $count
]);
}
/**
* 阅读消息
* @return response
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
*/
public function readNotice(): Response
{
$id = input('id', 0);
$type = input('type', 'notice');
if (!empty($id)) {
$detail = AdminNotice::with(['admin'])->where(['id' => $id, 'admin_id' => get_admin_id()])->find();
if (empty($detail)) {
return $this->error('404 Not Found');
}
// 默认已读
if ($type !== 'todo') {
$detail->status = 1;
$detail->save();
}
}
return $this->view('/system/admin/' . $type, [
'detail' => $detail ?? []
]);
}
/**
* 更新即时消息
* @return Response|void
*/
public function saveNotice()
{
if (\request()->post()) {
$post = request()->post();
$post['send_id'] = get_admin_id();
$post['type'] = 'message';
$post['send_ip'] = request()->getRealIp();
$post['create_time'] = time();
try {
AdminNotice::sendNotice($post, 'none');
} catch (\Exception $e) {
return $this->error('发送失败:' . $e->getMessage());
}
return $this->success('发送成功');
} else if (\request()->isAjax()) {
$id = input('id', 0);
$status = input('status', 1);
try {
if (empty($id)) {
throw new Exception('参数错误');
}
AdminNotice::where(['id' => $id, 'admin_id' => get_admin_id()])->update(['status' => $status]);
} catch (Exception $e) {
return $this->error('更新失败');
}
return $this->success('更新成功');
}
}
/**
* 清空消息
* @return Response|void
*/
public function clearNotice()
{
if (\request()->isAjax()) {
$type = input('type', 'notice');
$where = [
['type', '=', $type],
['status', '=', 1],
['admin_id', '=', get_admin_id()]
];
try {
AdminNotice::where($where)->delete();
} catch (Exception $e) {
return $this->error('清空失败');
}
return $this->success('清空成功');
}
}
/**
* 全部消息已读
* @return Response|void
*/
public function readAllNotice()
{
if (\request()->isAjax()) {
$type = input('type', 'notice');
$where = [
['type', '=', $type],
['admin_id', '=', get_admin_id()]
];
try {
AdminNotice::where($where)->update(['status' => 1]);
} catch (Exception $e) {
return $this->error('操作失败');
}
return $this->success('全部已读成功');
}
}
/**
* 个人中心
* @param Request $request
@@ -454,7 +188,7 @@ class Admin extends AdminController
* @throws DbException
* @throws ModelNotFoundException
*/
public function center(Request $request): \support\Response
public function center(Request $request): Response
{
if (request()->isPost()) {
$post = request()->post();
@@ -548,7 +282,7 @@ class Admin extends AdminController
* @throws DbException
* @throws ModelNotFoundException
*/
public function pwd(): \support\Response
public function pwd(): Response
{
if (request()->isPost()) {
@@ -576,10 +310,9 @@ class Admin extends AdminController
/**
* 语言配置
* @return mixed
* @throws \think\Exception
* @return Response
*/
public function language()
public function language(): Response
{
$language = input('l');
$env = base_path() . '/.env';
@@ -590,13 +323,14 @@ class Admin extends AdminController
if (write_file($env, $content)) {
return json(['success']);
}
return json(['error']);
}
/**
* 更改状态
* @return \support\Response
* @return Response
*/
public function status()
public function status(): Response
{
$id = input('id');
if ($id == 1) {
@@ -613,14 +347,14 @@ class Admin extends AdminController
/**
* 删除管理员
* @return mixed
* @throws \think\db\exception\DbException
* @return Response
* @throws DbException
*/
public function del()
public function del(): Response
{
$id = input('id');
!is_array($id) && ($id = array($id));
if (!empty($id) && is_array($id)) {
if (!empty($id)) {
// 过滤权限
if (in_array("1", $id)) {
@@ -641,21 +375,20 @@ class Admin extends AdminController
/**
* 清理系统缓存
* @return \support\Response
* @return Response
*/
public function clear(): \support\Response
public function clear(): Response
{
if (request()->isAjax()) {
$type = input('type');
try {
// 清理内容
if ($type == 'all' || $type == 'content') {
$session = session(AdminSession);
\support\Cache::clear();
request()->session()->set(AdminSession, $session);
$session = session(AdminEnum::ADMIN_SESSION);
Cache::clear();
request()->session()->set(AdminEnum::ADMIN_SESSION, $session);
}
// 清理模板

View File

@@ -11,8 +11,15 @@
// +----------------------------------------------------------------------
namespace app\admin\controller\system;
use app\admin\enums\AdminEnum;
use app\admin\service\AdminGroupService;
use app\AdminController;
use app\common\exception\OperateException;
use app\common\model\system\AdminGroup as AdminGroupModel;
use support\Response;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use Webman\Http\Request;
/**
@@ -22,165 +29,116 @@ use Webman\Http\Request;
*/
class AdminGroup extends AdminController
{
// 初始化函数
// 初始化函数
public function __construct()
{
parent::__construct();
$this->model = new AdminGroupModel();
}
$this->model = new AdminGroupModel();
}
/**
* 获取资源列表
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
*/
public function index()
public function index(): Response
{
if (request()->isAjax()) {
if (request()->isAjax()) {
$params = \request()->all();
list($count, $list) = AdminGroupService::dataList($params);
return $this->success('查询成功', '/', $list, $count);
}
$param = \request()->all();
$param['page'] = input('page');
$param['limit'] = input('limit');
return view('/system/admin/group', [
'group' => $this->model->getListGroup()
]);
}
// 查询条件
$where = array();
if (!empty($param['title'])) {
$where[] = ['title','like','%'.$param['title'].'%'];
}
if (!empty($param['alias'])) {
$where[] = ['alias','like','%'.$param['alias'].'%'];
}
if (!empty($param['content'])) {
$where[] = ['content','like','%'.$param['content'].'%'];
}
// 查询数据
$count = $this->model->where($where)->count();
$limit = is_empty($param['limit']) ? 10 : (int)$param['limit'];
$page = ($count <= $limit) ? 1 : $param['page'];
$list = $this->model->where($where)->order("id asc")->limit((int)$limit)->page((int)$page)->select()->toArray();
foreach ($list as $key => $value) {
$list[$key]['title'] = __($value['title']);
}
return $this->success('查询成功', null, $list, $count);
}
return view('/system/admin/group',['group'=>$this->model->getListGroup()]);
}
/**
* 添加角色
*/
/**
* 添加角色
*/
public function add()
{
if (request()->isPost()) {
// 接收数据
$post = request()->post();
$post = request_validate_rules($post, get_class($this->model));
if (empty($post) || !is_array($post)) {
return $this->error($post);
}
if ($this->model->create($post)) {
return $this->success('添加角色成功!');
}else {
return $this->error('添加角色失败!');
}
}
}
if (request()->isPost()) {
$post = request()->post();
validate(\app\common\validate\system\AdminGroup::class)->scene('add')->check($post);
AdminGroupService::add($post);
return $this->success('添加角色成功!');
}
/**
* 编辑角色
*/
return $this->error('添加角色失败!');
}
/**
* 编辑角色
*/
public function edit()
{
if (request()->isPost()) {
$post = request()->post();
$post = request_validate_rules($post, get_class($this->model));
if (empty($post) || !is_array($post)) {
return $this->error($post);
}
if ($this->model->update($post)) {
return $this->success('更新角色成功!');
}else {
return $this->error('更新角色失败');
}
}
}
if (request()->isPost()) {
$post = request()->post();
validate(\app\common\validate\system\AdminGroup::class)->scene('edit')->check($post);
AdminGroupService::edit($post);
return $this->success('更新角色成功!');
}
/**
* 更新权限
*/
public function editRules()
{
if (request()->isPost()) {
return $this->error('更新角色失败!');
}
$id = input('id');
/**
* 权限函数接口
* @access public
*/
public function getRuleCateTree()
{
$type = input('type', AdminEnum::ADMIN_AUTH_RULES);
return $this->authService->getRuleCatesTree($type, $this->authService->authGroup);
}
if (!is_empty($id) && is_numeric($id)) {
/**
* 更新权限
* @return Response
* @throws OperateException
*/
public function editRules(): Response
{
$id = input('id', 0);
$post = request()->post();
$rules = input(AdminEnum::ADMIN_AUTH_RULES, []);
validate(\app\common\validate\system\AdminGroup::class)->scene('edit')->check($post);
AdminGroupService::editRules((int)$id, $rules);
return $this->success('更新权限成功!');
}
$rules = request()->post('rules') ?? [];
$array = [
'id'=>$id,
'rules'=>implode(',',$rules)
];
/**
* 更新栏目
* @return Response
* @throws OperateException
*/
public function editCates(): Response
{
$id = input('id', 0);
$cates = input(AdminEnum::ADMIN_AUTH_CATES, []);
$post = request()->post();
validate(\app\common\validate\system\AdminGroup::class)->scene('edit')->check($post);
AdminGroupService::editCates($id, $cates);
return $this->success('更新权限成功!');
}
if (!$this->auth->checkRuleOrCateNodes($rules)) {
return $this->error('没有权限!');
}
/**
* 删除角色/用户组
*/
public function del(): Response
{
$id = input('id', 0);
validate(\app\common\validate\system\AdminGroup::class)->scene('edit')->check(request()->all());
if ($id == 1) {
return $this->error('系统内置禁止删除!');
} else if ($this->model::destroy($id)) {
return $this->success('删除角色成功!');
}
if ($this->model->update($array)) {
return $this->success('更新权限成功!');
}
}
return $this->error('更新权限失败!');
}
}
/**
* 更新栏目
*/
public function editCates()
{
if (request()->isPost()) {
$id = input('id');
if (!is_empty($id) && is_numeric($id)) {
$cates = request()->post('cates') ?? [];
$array = [
'id'=>$id,
'cates'=>implode(',',$cates)
];
if (!$this->auth->checkRuleOrCateNodes($cates,AUTH_CATE)) {
return $this->error('没有权限!');
}
if ($this->model->update($array)) {
return $this->success('更新栏目权限成功!');
}
}
return $this->error('更新栏目权限失败!');
}
}
/**
* 删除角色/用户组
*/
public function del()
{
$id = input('id');
if (!empty($id) && is_numeric($id)) {
if ($id == 1) {
return $this->error('系统内置禁止删除!');
}
if ($this->model::destroy($id)) {
return $this->success('删除角色成功!');
}
}
return $this->error('删除角色失败,请检查您的参数!');
}
return $this->error('删除角色失败,请检查您的参数!');
}
}

View File

@@ -11,8 +11,11 @@
// +----------------------------------------------------------------------
namespace app\admin\controller\system;
use app\admin\service\AdminRuleService;
use app\AdminController;
use app\common\model\system\AdminRules as AdminRuleModel;
use support\Response;
use think\db\exception\DbException;
use Webman\Http\Request;
/**
@@ -31,34 +34,14 @@ class AdminRules extends AdminController
/**
* 获取资源列表
*
* return Response
*/
public function index()
public function index(): Response
{
if (request()->isAjax()) {
// 查询参数
$where = array();
$post['title'] = input('title');
$post['router'] = input('router');
if (!empty($post['title'])) {
$where[] = ['title','like','%'.$post['title'].'%'];
}
if (!empty($post['router'])) {
$where[] = ['router','like','%'.$post['router'].'%'];
}
// 获取总数
$total = $this->model->where($where)->count();
$list = $this->model->where($where)->order('sort asc')->select()->toArray();
foreach ($list as $key => $value) {
$list[$key]['title'] = __($value['title']);
}
$rules = list_to_tree($list,'id','pid','children',0);
return $this->success('获取成功', '/',$rules, $total);
list($count, $list) = AdminRuleService::dataList(request()->all());
$rules = list_to_tree($list,'id','pid','children',0);
return $this->success('获取成功', '/',$rules, $count);
}
return view('/system/admin/rules');
@@ -66,47 +49,43 @@ class AdminRules extends AdminController
/**
* 添加节点数据
* @return Response
*/
public function add()
{
if (request()->isPost()) {
$post = \request()->post();
$post = request_validate_rules($post, get_class($this->model));
if (empty($post) || !is_array($post)) {
return $this->error($post);
}
if ($this->model->create($post)) {
return $this->success('添加菜单成功');
}else {
return $this->error('添加菜单失败!');
}
}
public function add(): Response
{
if (request()->isPost()) {
$post = \request()->post();
validate(\app\common\validate\system\AdminRules::class . '.add')->check($post);
if ($this->model->create($post)) {
return $this->success('添加菜单成功!');
}
}
return $this->error('添加菜单失败');
}
/**
* 编辑节点数据
* @return Response
*/
public function edit()
{
if (request()->isPost()) {
$post = \request()->post();
$post = request_validate_rules($post, get_class($this->model));
if (empty($post) || !is_array($post)) {
return $this->error($post);
}
if ($this->model->update($post)) {
return $this->success('更新菜单成功!');
}else {
return $this->error('更新菜单失败');
}
}
public function edit(): Response
{
if (request()->isPost()) {
$post = \request()->post();
validate(\app\common\validate\system\AdminRules::class . '.edit')->check($post);
if ($this->model->update($post)) {
return $this->success('更新菜单成功!');
}
}
return $this->error('更新菜单失败');
}
/**
* 删除节点数据
*/
public function del()
{
/**
* 删除节点数据
* @return Response
* @throws DbException
*/
public function del(): Response
{
$id = input('id');
if (!empty($id)) {
// 查询子节点

View File

@@ -25,9 +25,9 @@ use Webman\Http\Request;
* Class Company
* @package app\admin\controller\system
*/
class Company extends AdminController
class Company extends AdminController
{
// 初始化函数
public function __construct()
{
@@ -50,7 +50,7 @@ class Company extends AdminController
$post = input();
$where = array();
if (!empty($post['title'])) {
$where[] = ['title','like','%'.$post['title'].'%'];
$where[] = ['title', 'like', '%' . $post['title'] . '%'];
}
// 生成查询数据
@@ -58,55 +58,51 @@ class Company extends AdminController
return $this->success('查询成功', null, $list, count($list));
}
return view('/system/company/index');
return view('/system/company/index');
}
/**
* 添加公司信息
* @return Response
*/
public function add ()
public function add(): Response
{
if (request()->isPost()) {
$post = request()->post();
$post = request_validate_rules($post,get_class($this->model));
if (empty($post) || !is_array($post)) {
$this->error($post);
}
if ($this->model->create($post)){
if (request()->isPost()) {
$post = request()->post();
if ($this->model->create($post)) {
return $this->success();
}
return $this->error();
}
return view('/system/company/add',[
'data'=> $this->getTableFields()
return view('/system/company/add', [
'data' => $this->getTableFields()
]);
}
/**
* 编辑公司信息
* @return Response
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
*/
public function edit()
public function edit(): Response
{
$id = input('id');
if (request()->isPost()) {
$post = request()->post();
$post = request_validate_rules($post,get_class($this->model));
if (empty($post) || !is_array($post)) {
$this->error($post);
}
if ($this->model->update($post)){
if ($this->model->update($post)) {
return $this->success();
}
return $this->error();
}
$data = $this->model->find($id);
return view('/system/company/add',['data'=> $data]);
return view('/system/company/add', [
'data' => $data
]);
}
}

View File

@@ -81,22 +81,19 @@ class Department extends AdminController
/**
* 添加部门数据
* @return Response
*/
public function add()
{
public function add(): Response
{
if (request()->isPost()) {
$post = request()->post();
validate(\app\common\validate\system\Department::class.'.add')->check($post);
if ($this->model->create($post)) {
return $this->success('添加部门成功!');
}
}
if (request()->isPost()) {
$post = request()->post();
$post = request_validate_rules($post, get_class($this->model));
if (empty($post) || !is_array($post)) {
return $this->error($post);
}
if ($this->model->create($post)) {
return $this->success('添加部门成功!');
}else {
return $this->error('添加部门失败!');
}
}
return $this->error('添加部门失败!');
}
/**
@@ -104,27 +101,26 @@ class Department extends AdminController
*/
public function edit()
{
if (request()->isPost()) {
$post = request()->post();
$post = request_validate_rules($post, get_class($this->model));
if (empty($post) || !is_array($post)) {
return $this->error($post);
}
if ($this->model->update($post)) {
return $this->success('更新部门成功!');
}else {
return $this->error('更新部门失败');
}
}
if (request()->isPost()) {
$post = request()->post();
validate(\app\common\validate\system\Department::class.'.edit')->check($post);
if ($this->model->update($post)) {
return $this->success('更新部门成功!');
}
}
return $this->error('更新部门失败');
}
/**
* 删除部门数据
*/
public function del()
{
/**
* 删除部门数据
* @return Response
* @throws DbException
*/
public function del(): Response
{
$id = input('id');
if (!empty($id) && is_numeric($id)) {
if ($id > 0) {
// 查询子部门
if ($this->model->where('pid',$id)->count()) {
return $this->error('当前部门存在子部门!');

View File

@@ -14,6 +14,10 @@ namespace app\admin\controller\system;
use app\AdminController;
use app\common\model\system\Jobs as JobsModel;
use support\Response;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use Webman\Http\Request;
/**
@@ -29,11 +33,15 @@ class Jobs extends AdminController
parent::__construct();
$this->model = new JobsModel();
}
/**
* 获取资源列表
* @return Response
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
*/
public function index()
public function index(): Response
{
if (request()->isAjax()) {
@@ -76,16 +84,12 @@ class Jobs extends AdminController
{
if (request()->isPost()) {
$post = request()->post();
$post = request_validate_rules($post, get_class($this->model));
if (empty($post) || !is_array($post)) {
return $this->error($post);
}
if ($this->model->create($post)) {
return $this->success('添加岗位成功!');
}else {
return $this->error('添加岗位失败!');
}
}
return $this->error('添加岗位失败!');
}
/**
@@ -95,25 +99,21 @@ class Jobs extends AdminController
{
if (request()->isPost()) {
$post = request()->post();
$post = request_validate_rules($post, get_class($this->model));
if (empty($post) || !is_array($post)) {
return $this->error($post);
}
if ($this->model->update($post)) {
return $this->success('更新岗位成功!');
}else {
return $this->error('更新岗位失败');
}
}
}
return $this->error('更新岗位失败');
}
/**
* 删除岗位数据
* @return Response
*/
public function del()
{
public function del(): Response
{
$id = input('id');
if (!empty($id) && is_numeric($id)) {
if ($id > 0) {
if ($this->model::destroy($id)) {
return $this->success('删除岗位成功!');
}

View File

@@ -19,7 +19,6 @@ use system\File;
use system\Http;
use system\ZipArchives;
use app\AdminController;
use app\admin\library\Auth;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;

View File

@@ -12,7 +12,9 @@ declare (strict_types=1);
namespace app\admin\controller\system;
use app\admin\service\UserService;
use app\AdminController;
use app\common\exception\OperateException;
use app\common\library\Ip2Region;
use app\common\model\system\User as UserModel;
use app\common\model\system\UserGroup as UserGroupModel;
@@ -43,121 +45,52 @@ class User extends AdminController
* @throws DbException
* @throws ModelNotFoundException
*/
public function index(): \support\Response
public function index(): Response
{
$userGroup = UserGroupModel::select()->toArray();
if (request()->isAjax()) {
// 获取数据
$post = \request()->all();
$page = (int)input('page') ?? 1;
$limit = (int)input('limit') ?? 10;
$status = !empty($post['status']) ? (int)$post['status'] - 1 : 1;
// 生成查询条件
$where = array();
if (!empty($post['nickname'])) {
$where[] = ['nickname', 'like', '%' . $post['nickname'] . '%'];
}
if (!empty($post['group_id'])) {
$where[] = ['group_id', 'find in set', $post['group_id']];
}
// 生成查询数据
$where[] = ['status', '=', $status];
$count = $this->model->where($where)->count();
$page = ($count <= $limit) ? 1 : $page;
$list = $this->model->where($where)->order("id asc")->limit((int)$limit)->page((int)$page)->select();
// 循环处理数据
foreach ($list as $key => $value) {
$value->hidden(['pwd', 'salt']);
$region = Ip2Region::instance()->memorySearch($value['login_ip']);
$region = explode('|', $region['region']);
$list[$key]['region'] = $region;
$result = list_search($userGroup, ['id' => $value['group_id']]);
if (!empty($result)) {
$list[$key]['group'] = $result['title'];
}
}
// TODO..
return $this->success('查询成功', "", $list, $count);
$post = request()->all();
list($count, $list) = UserService::dataList($post);
return $this->success('查询成功', "/", $list, $count);
}
return view('/system/user/index', [
'UserGroup' => $userGroup,
'UserGroup' => UserGroupModel::select()->toArray()
]);
}
/**
* 添加会员
* @return Response
* @throws OperateException
*/
public function add()
public function add(): Response
{
if (request()->isPost()) {
$post = request()->post();
// 禁止重复注册
$whereName[] = ['nickname', '=', $post['nickname']];
$whereEmail[] = ['email', '=', $post['email']];
if ($this->model->whereOr([$whereName, $whereEmail])->findOrEmpty()->toArray()) {
return $this->error('该用户ID或邮箱已经存在');
}
// 生成密码
$salt = Random::alpha();
$post['salt'] = $salt;
$post['pwd'] = encryptPwd($post['pwd'], $post['salt']);
if ($this->model->create($post)) {
return $this->success('注册成功!');
}
return $this->error('注册失败!');
validate(\app\common\validate\system\User::class)->scene('add')->check($post);
UserService::add($post);
return $this->success('注册成功!');
}
return $this->error('注册失败!');
}
/**
* 编辑会员
* @return Response
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
* @throws OperateException
*/
public function edit()
public function edit(): Response
{
if (request()->isPost()) {
$post = \request()->post();
// 查询数据
$data = $this->model->find($post['id']);
if ($data['nickname'] != $post['nickname']) {
$whereName[] = ['nickname', '=', $post['nickname']];
if ($this->model->where($whereName)->find()) {
return $this->error('该用户ID已经存在');
}
}
if ($data['email'] != $post['email']) {
$whereEmail[] = ['email', '=', $post['email']];
if ($this->model->where($whereEmail)->find()) {
return $this->error('该用户邮箱已经存在!');
}
}
// 为空则去掉密码
if (empty($post['pwd'])) {
unset($post['pwd']);
} else {
$salt = Random::alpha();
$post['salt'] = $salt;
$post['pwd'] = encryptPwd($post['pwd'], $post['salt']);
}
if ($this->model->update($post)) {
return $this->success('更新成功!');
}
return $this->error('更新失败!');
$post = request()->post();
validate(\app\common\validate\system\User::class)->scene('edit')->check($post);
UserService::edit($post);
return $this->success('更新成功!');
}
return $this->error('更新失败!');
}
/**