Files
swiftadmin/app/admin/controller/system/Admin.php

412 lines
12 KiB
PHP
Raw Normal View History

2022-08-19 19:48:37 +08:00
<?php
declare(strict_types=1);
// +----------------------------------------------------------------------
// | swiftAdmin 极速开发框架 [基于WebMan开发]
// +----------------------------------------------------------------------
// | Copyright (c) 2020-2030 http://www.swiftadmin.net
// +----------------------------------------------------------------------
// | swiftAdmin.net High Speed Development Framework
// +----------------------------------------------------------------------
// | Author: meystack <coolsec@foxmail.com> Apache 2.0 License
// +----------------------------------------------------------------------
namespace app\admin\controller\system;
2023-07-03 10:08:34 +08:00
use app\admin\enums\AdminEnum;
use app\admin\service\AdminService;
2022-08-19 19:48:37 +08:00
use app\AdminController;
2023-07-03 10:08:34 +08:00
use app\common\exception\OperateException;
2022-11-28 19:11:12 +08:00
use app\common\model\system\AdminNotice;
2022-08-19 19:48:37 +08:00
use app\common\model\system\Jobs;
use app\common\model\system\Department;
use app\common\model\system\Admin as AdminModel;
use app\common\model\system\AdminGroup as AdminGroupModel;
use app\common\model\system\AdminAccess as AdminAccessModel;
2022-11-28 19:11:12 +08:00
use support\Log;
use support\Response;
2022-08-22 19:07:32 +08:00
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
2022-11-28 19:11:12 +08:00
use think\Exception;
2023-06-19 14:32:30 +08:00
use support\Cache;
2022-08-22 19:07:32 +08:00
use Webman\Http\Request;
2022-08-19 19:48:37 +08:00
/**
* 管理员管理
* Class Admin
* @package app\admin\controller\system
*/
class Admin extends AdminController
{
/**
* 用户管理组
2022-11-28 19:11:12 +08:00
* @var mixed
2022-08-19 19:48:37 +08:00
*/
2022-11-28 19:11:12 +08:00
protected mixed $group;
2022-08-19 19:48:37 +08:00
/**
* 用户岗位
2022-11-28 19:11:12 +08:00
* @var mixed
2022-08-19 19:48:37 +08:00
*/
2022-11-28 19:11:12 +08:00
public mixed $jobs;
2022-08-19 19:48:37 +08:00
/**
* 用户部门
2022-11-28 19:11:12 +08:00
* @var mixed
2022-08-19 19:48:37 +08:00
*/
2022-11-28 19:11:12 +08:00
public mixed $department;
2022-08-19 19:48:37 +08:00
// 初始化函数
public function __construct()
{
parent::__construct();
$this->model = new AdminModel();
}
/**
* 获取资源列表
2023-07-03 10:08:34 +08:00
* @return Response
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
2022-08-19 19:48:37 +08:00
*/
2023-07-03 10:08:34 +08:00
public function index(): Response
2022-08-19 19:48:37 +08:00
{
2022-08-29 10:55:15 +08:00
$this->jobs = Jobs::select()->toArray();
$this->group = AdminGroupModel::select()->toArray();
$this->department = Department::getListTree();
2022-08-19 19:48:37 +08:00
if (request()->isAjax()) {
2023-07-03 10:08:34 +08:00
$params = request()->all();
list('count' => $count, 'list' => $list) = AdminService::dataList($params);
2022-08-19 19:48:37 +08:00
return $this->success('查询成功', null, $list, $count);
}
return view('/system/admin/index', [
2022-11-28 19:11:12 +08:00
'jobs' => $this->jobs,
'group' => $this->group,
2022-08-19 19:48:37 +08:00
'department' => json_encode($this->department),
]);
}
/**
* 添加管理员
2022-11-28 19:11:12 +08:00
* @return Response
2023-07-03 10:08:34 +08:00
* @throws OperateException
2022-08-19 19:48:37 +08:00
*/
2023-07-03 10:08:34 +08:00
public function add(): Response
2022-08-19 19:48:37 +08:00
{
if (request()->isPost()) {
$post = request()->post();
2023-07-03 10:08:34 +08:00
validate(\app\common\validate\system\Admin::class)->scene('add')->check($post);
AdminService::add($post);
return $this->success('添加管理员成功');
2022-08-19 19:48:37 +08:00
}
// 获取用户组
return view('', ['group' => $this->group]);
}
/**
* 更新管理员
2023-07-03 10:08:34 +08:00
* @return Response
* @throws OperateException
2022-08-19 19:48:37 +08:00
*/
2023-07-03 10:08:34 +08:00
public function edit(): Response
2022-08-19 19:48:37 +08:00
{
if (request()->isPost()) {
2023-07-03 10:08:34 +08:00
$post = request()->all();
validate(\app\common\validate\system\Admin::class)->scene('edit')->check($post);
AdminService::edit($post);
return $this->success('更新管理员成功');
2022-08-19 19:48:37 +08:00
}
2023-06-19 14:32:30 +08:00
return $this->error('更新管理员失败');
2022-08-19 19:48:37 +08:00
}
/**
2023-07-03 10:08:34 +08:00
* 获取用户权限树
* @access public
* getAdminRules
2022-08-19 19:48:37 +08:00
*/
2023-07-03 10:08:34 +08:00
public function getPermissions()
2022-08-19 19:48:37 +08:00
{
2023-07-03 10:08:34 +08:00
return $this->authService->getPermissionsMenu();
2022-08-19 19:48:37 +08:00
}
/**
2023-07-03 10:08:34 +08:00
* 获取节点数据
* @access public
2022-08-19 19:48:37 +08:00
*/
2023-07-03 10:08:34 +08:00
public function getRuleCateTree()
2022-08-19 19:48:37 +08:00
{
2023-07-03 10:08:34 +08:00
$type = input('type', AdminEnum::ADMIN_AUTH_RULES);
return $this->authService->getRuleCatesTree($type, $this->authService->authPrivate);
2022-08-19 19:48:37 +08:00
}
/**
2023-07-03 10:08:34 +08:00
* 编辑权限
* @access public
* @return Response
* @throws OperateException
2022-08-19 19:48:37 +08:00
*/
2023-07-03 10:08:34 +08:00
public function editRules(): Response
2022-08-19 19:48:37 +08:00
{
2023-07-03 10:08:34 +08:00
$adminId = input('admin_id', 0);
AdminService::updateRulesNodes($adminId, AdminEnum::ADMIN_AUTH_RULES);
return $this->success('更新权限成功!');
2022-08-19 19:48:37 +08:00
}
/**
2023-07-03 10:08:34 +08:00
* 编辑栏目权限
* @access public
* @return Response
* @throws OperateException
2022-08-19 19:48:37 +08:00
*/
2023-07-03 10:08:34 +08:00
public function editCates(): Response
2022-08-19 19:48:37 +08:00
{
2023-07-03 10:08:34 +08:00
$adminId = input('admin_id', 0);
AdminService::updateRulesNodes($adminId, AdminEnum::ADMIN_AUTH_CATES);
return $this->success('更新权限成功!');
2022-08-19 19:48:37 +08:00
}
/**
* 模版页面
2022-11-28 19:11:12 +08:00
* @return Response
2022-08-19 19:48:37 +08:00
*/
2022-11-28 19:11:12 +08:00
public function theme(): Response
2022-08-19 19:48:37 +08:00
{
return view('/system/admin/theme');
}
/**
* 个人中心
2022-08-22 19:07:32 +08:00
* @param Request $request
2022-08-19 19:48:37 +08:00
* @return mixed
2022-08-22 19:07:32 +08:00
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
2022-08-19 19:48:37 +08:00
*/
2023-07-03 10:08:34 +08:00
public function center(Request $request): Response
2022-08-19 19:48:37 +08:00
{
if (request()->isPost()) {
$post = request()->post();
$post['id'] = get_admin_id();
2022-08-19 19:48:37 +08:00
if ($this->model->update($post)) {
return $this->success();
}
return $this->error();
}
$title = [];
$data = $this->model->find(get_admin_id());
2022-08-19 19:48:37 +08:00
if (!empty($data['group_id'])) {
$group = AdminGroupModel::field('title')
2022-08-29 10:55:15 +08:00
->whereIn('id', $data['group_id'])
->select()
->toArray();
2022-08-19 19:48:37 +08:00
foreach ($group as $key => $value) {
$title[$key] = $value['title'];
}
}
2022-12-03 19:25:57 +08:00
$data['jobs'] = Jobs::where('id', $data['jobs_id'])->value('title');
2022-08-19 19:48:37 +08:00
$data['group'] = implode('', $title);
$data['tags'] = empty($data['tags']) ? $data['tags'] : unserialize($data['tags']);
return view('/system/admin/center', [
'data' => $data
]);
}
/**
* 修改个人资料
*/
2022-08-22 19:07:32 +08:00
public function modify(Request $request)
2022-08-19 19:48:37 +08:00
{
if (request()->isAjax()) {
$post = request()->post();
$id = get_admin_id();
2022-08-19 19:48:37 +08:00
try {
//code...
switch ($post['field']) {
case 'face':
$id = $this->model->update(['id' => $id, 'face' => $post['face']]);
break;
case 'mood':
$id = $this->model->update(['id' => $id, 'mood' => $post['mood']]);
break;
case 'tags':
if (\is_empty($post['tags'])) {
break;
}
$data = $this->model->field('tags')->find($id);
if (!empty($data['tags'])) {
$tags = unserialize($data['tags']);
if (!empty($post['del'])) {
foreach ($tags as $key => $value) {
if ($value == $post['tags']) {
unset($tags[$key]);
}
}
} else {
$merge = array($post['tags']);
$tags = array_unique(array_merge($merge, $tags));
if (count($tags) > 10) {
throw new \Exception('最多拥有10个标签');
}
}
$tags = serialize($tags);
} else {
$tags = serialize(array($post['tags']));
}
$id = $this->model->update(['id' => $id, 'tags' => $tags]);
break;
default:
# code...
break;
}
} catch (\Exception $e) {
return $this->error($e->getMessage());
}
return $id ? $this->success() : $this->error();
}
}
/**
* 修改密码
* @return mixed
2022-08-22 19:07:32 +08:00
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
2022-08-19 19:48:37 +08:00
*/
2023-07-03 10:08:34 +08:00
public function pwd(): Response
2022-08-19 19:48:37 +08:00
{
if (request()->isPost()) {
$pwd = input('pwd');
$post = request()->except(['pwd']);
if ($post['pass'] !== $post['repass']) {
return $this->error('两次输入的密码不一样!');
}
// 查找数据
$where[] = ['id', '=', get_admin_id()];
2022-08-19 19:48:37 +08:00
$where[] = ['pwd', '=', encryptPwd($pwd)];
$result = $this->model->where($where)->find();
if (!empty($result)) {
$this->model->where($where)->update(['pwd' => encryptPwd($post['pass'])]);
return $this->success('更改密码成功!');
} else {
2022-08-29 10:55:15 +08:00
return $this->error('原始密码输入错误');
2022-08-19 19:48:37 +08:00
}
}
return view('/system/admin/pwd');
}
/**
* 语言配置
2023-07-03 10:08:34 +08:00
* @return Response
2022-08-19 19:48:37 +08:00
*/
2023-07-03 10:08:34 +08:00
public function language(): Response
2022-08-19 19:48:37 +08:00
{
$language = input('l');
$env = base_path() . '/.env';
$array = parse_ini_file($env, true);
$array['LANG_DEFAULT_LANG'] = $language;
$content = parse_array_ini($array);
2022-08-24 12:04:09 +08:00
request()->session()->set('lang', $language);
2022-08-19 19:48:37 +08:00
if (write_file($env, $content)) {
return json(['success']);
}
2023-07-03 10:08:34 +08:00
return json(['error']);
2022-08-19 19:48:37 +08:00
}
/**
* 更改状态
2023-07-03 10:08:34 +08:00
* @return Response
2022-08-19 19:48:37 +08:00
*/
2023-07-03 10:08:34 +08:00
public function status(): Response
2022-08-19 19:48:37 +08:00
{
$id = input('id');
if ($id == 1) {
return $this->error('超级管理员不能更改状态!');
}
$array['id'] = $id;
$array['status'] = input('status');
if ($this->model->update($array)) {
return $this->success('修改成功!');
}
return $this->error('修改失败,请检查您的数据!');
}
/**
* 删除管理员
2023-07-03 10:08:34 +08:00
* @return Response
* @throws DbException
2022-08-19 19:48:37 +08:00
*/
2023-07-03 10:08:34 +08:00
public function del(): Response
2022-08-19 19:48:37 +08:00
{
$id = input('id');
!is_array($id) && ($id = array($id));
2023-07-03 10:08:34 +08:00
if (!empty($id)) {
2022-08-19 19:48:37 +08:00
// 过滤权限
if (in_array("1", $id)) {
return $this->error('禁止删除超管帐号!');
}
// 删除用户
if ($this->model->destroy($id)) {
$arr = implode(',', $id);
$where[] = ['admin_id', 'in', $arr];
AdminAccessModel::where($where)->delete();
return $this->success('删除管理员成功!');
}
}
return $this->error('删除管理员失败,请检查您的参数!');
}
/**
* 清理系统缓存
2023-07-03 10:08:34 +08:00
* @return Response
2022-08-19 19:48:37 +08:00
*/
2023-07-03 10:08:34 +08:00
public function clear(): Response
2022-08-19 19:48:37 +08:00
{
if (request()->isAjax()) {
$type = input('type');
try {
// 清理内容
if ($type == 'all' || $type == 'content') {
2023-07-03 10:08:34 +08:00
$session = session(AdminEnum::ADMIN_SESSION);
Cache::clear();
request()->session()->set(AdminEnum::ADMIN_SESSION, $session);
2022-08-19 19:48:37 +08:00
}
// 清理模板
if ($type == 'all' || $type == 'template') {
recursive_delete(root_path('runtime/views'));
}
// 清理插件缓存
if ($type == 'all' || $type == 'plugin') {
plugin_refresh_hooks();
}
} catch (\Throwable $th) {
return $this->error($th->getMessage());
}
}
return $this->success('清理缓存成功,请刷新页面!');
}
}