Files
swiftadmin/app/AdminController.php

546 lines
16 KiB
PHP
Raw Normal View History

2022-08-19 19:48:37 +08:00
<?php
// +----------------------------------------------------------------------
// | 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;
2023-07-03 10:08:34 +08:00
use app\admin\enums\AdminEnum;
use app\admin\service\AuthService;
use support\Log;
2022-08-19 19:48:37 +08:00
use support\Response;
use think\helper\Str;
2022-08-19 19:48:37 +08:00
class AdminController extends BaseController
{
/**
* 数据库实例
* @var object
*/
2022-11-28 19:11:12 +08:00
public object $model;
2022-08-19 19:48:37 +08:00
/**
* 数据表名称
* @var string
*/
2022-11-28 19:11:12 +08:00
public string $tableName;
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 $status;
2022-08-19 19:48:37 +08:00
/**
* 获取模板
* @var string
*/
2022-11-28 19:11:12 +08:00
public string $template = '';
2022-08-19 19:48:37 +08:00
/**
* 权限验证类
* @var object
*/
2023-07-03 10:08:34 +08:00
public object $authService;
2022-08-19 19:48:37 +08:00
/**
* 当前表字段
* @var array
*/
2022-11-28 19:11:12 +08:00
protected array $tableFields = [];
2022-08-19 19:48:37 +08:00
/**
* 默认开关
* @var string
*/
2022-11-28 19:11:12 +08:00
protected string $keepField = 'status';
2022-08-19 19:48:37 +08:00
/**
* 开启数据限制
* @var boolean
*/
2022-11-28 19:11:12 +08:00
protected bool $dataLimit = false;
2022-08-19 19:48:37 +08:00
2023-06-19 14:32:30 +08:00
/**
* 是否开启部门限制
* @var bool
*/
protected bool $departmentLimit = false;
2022-08-19 19:48:37 +08:00
/**
* 数据限制字段
* @var string
*/
2022-11-28 19:11:12 +08:00
protected string $dataLimitField = 'admin_id';
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
protected mixed $ruleOutFields = '';
2022-08-19 19:48:37 +08:00
/**
* 查询过滤字段
* @var array
*/
2022-11-28 19:11:12 +08:00
protected array $filterWhere = ['page', 'limit'];
2022-08-19 19:48:37 +08:00
/**
* 查询转换字段
* @var array
*/
2022-11-28 19:11:12 +08:00
protected array $converTime = ['create_time', 'update_time', 'delete_time'];
2022-08-19 19:48:37 +08:00
/**
* 定义关联模型
* @var array
*/
protected array $relationModel = [];
2022-08-19 19:48:37 +08:00
/**
* 跳转URL地址
* @var string
*/
2022-11-28 19:11:12 +08:00
protected string $JumpUrl = '/';
2022-08-19 19:48:37 +08:00
/**
* 构造函数
*/
public function __construct()
{
parent::__construct();
2023-07-03 10:08:34 +08:00
$this->authService = AuthService::instance();
2022-08-19 19:48:37 +08:00
}
/**
* 获取资源列表
2023-06-19 14:32:30 +08:00
* @return Response
2022-08-19 19:48:37 +08:00
*/
public function index()
{
if (request()->isAjax()) {
$page = (int)input('page', 1);
$limit = (int)input('limit', 18);
2022-08-19 19:48:37 +08:00
$where = $this->buildSelectParams();
$count = $this->model->where($where)->count();
$page = $count <= $limit ? 1 : $page;
2022-11-28 19:11:12 +08:00
$fieldList = $this->model->getFields();
$order = !array_key_exists('sort', $fieldList) ? 'id' : 'sort';
$subQuery = $this->model->field('id')->where($where)->order($order, 'desc')->limit($limit)->page($page)->buildSql();
2022-08-19 19:48:37 +08:00
$subQuery = '( SELECT object.id FROM ' . $subQuery . ' AS object )';
$list = $this->model->with($this->relationModel)->where('id in' . $subQuery)->order($order, 'desc')->select()->toArray();
2022-11-28 19:11:12 +08:00
foreach ($list as $key => $value) {
if (isset($value['user_id'])) {
$list[$key]['user_id'] = $value['user']['nickname'] ?? $value['user_id'];
}
if (isset($value['admin_id'])) {
$list[$key]['admin_id'] = $value['admin']['nickname'] ?? $value['admin_id'];
2022-11-28 19:11:12 +08:00
}
}
return $this->success('查询成功', '/', $list, $count);
2022-08-19 19:48:37 +08:00
}
return $this->view();
}
/**
* 添加资源
* @return Response|void
*/
public function add()
{
if (request()->isPost()) {
$post = $this->preRuleOutFields(\request()->post());
if ($this->dataLimit) {
$post[$this->dataLimitField] = get_admin_id();
2022-08-19 19:48:37 +08:00
}
$validate = $this->isValidate ? get_class($this->model) : $this->isValidate;
$post = request_validate_rules($post, $validate, $this->scene);
if (empty($post) || !is_array($post)) {
return $this->error($post);
}
$this->status = $this->model->create($post);
return $this->status ? $this->success() : $this->error();
}
return $this->view('', ['data' => $this->getTableFields()]);
}
/**
* 编辑资源
* @return Response|void
*/
public function edit()
{
$id = input('id');
2023-07-03 19:21:19 +08:00
$data = $this->model->where('id', $id)->findOrEmpty()->toArray();
2022-08-19 19:48:37 +08:00
// 限制数据调用
2023-07-03 10:08:34 +08:00
if (!$this->authService->SuperAdmin() && $this->dataLimit
2022-08-19 19:48:37 +08:00
&& in_array($this->dataLimitField, $this->model->getFields())) {
if ($data[$this->dataLimitField] != get_admin_id()) {
2022-08-19 19:48:37 +08:00
return $this->error('没有权限');
}
}
if (request()->isPost()) {
$post = $this->preRuleOutFields(\request()->post());
$validate = $this->isValidate ? get_class($this->model) : $this->isValidate;
$post = request_validate_rules($post, $validate, $this->scene);
if (empty($post) || !is_array($post)) {
return $this->error($post);
}
$this->status = $this->model->update($post);
return $this->status ? $this->success() : $this->error();
}
/**
* 默认共享模板
*/
2023-07-03 20:41:13 +08:00
$template = str_replace('/_', '/', Str::snake(request()->getController()));
return $this->view($template . '/add', [
2022-08-19 19:48:37 +08:00
'data' => $data
]);
}
/**
* 删除资源
2023-06-19 14:32:30 +08:00
* @return Response
2022-08-19 19:48:37 +08:00
*/
public function del()
{
$id = input('id');
if (!is_array($id)) {
$id = [$id];
}
try {
$list = $this->model->whereIn('id', $id)->select();
foreach ($list as $item) {
2023-07-03 10:08:34 +08:00
if (!$this->authService->SuperAdmin() && $this->dataLimit
2022-08-19 19:48:37 +08:00
&& in_array($this->dataLimitField, $this->model->getFields())) {
if ($item[$this->dataLimitField] != get_admin_id()) {
2022-08-19 19:48:37 +08:00
continue;
}
}
if (isset($item->isSystem) && $item->isSystem) {
throw new \Exception('禁止删除系统级数据');
}
$item->delete();
$this->status = true;
}
} catch (\Throwable $th) {
$this->status = false;
return $this->error($th->getMessage());
}
return $this->status ? $this->success() : $this->error();
}
/**
* 修改资源状态
* @return Response|void
*/
public function status()
{
if (request()->isAjax()) {
$where[] = ['id', '=', input('id')];
2023-07-03 10:08:34 +08:00
if (!$this->authService->SuperAdmin() && $this->dataLimit
2022-08-19 19:48:37 +08:00
&& in_array($this->dataLimitField, $this->model->getFields())) {
$where[] = [$this->dataLimitField, '=', get_admin_id()];
2022-08-19 19:48:37 +08:00
}
try {
$this->status = $this->model->where($where)->update(['status' => input('status')]);
} catch (\Throwable $th) {
return $this->error($th->getMessage());
}
if ($this->status) {
return $this->success();
}
}
return $this->error();
}
/**
* 数据表排序
* @return Response
2022-08-19 19:48:37 +08:00
*/
public function sort()
{
if (request()->isPost()) {
if (array_search('sort', $this->model->getTableFields())) {
try {
$ids = request()->post('ids');
$list = $this->model->where('id', 'in', $ids)->orderRaw('field(id,' . implode(',', $ids) . ')')->select()->toArray();
$newSort = array_column($list, 'sort');
rsort($newSort);
$array = [];
// 循环处理排序字段
foreach ($list as $key => $value) {
$array[] = [
'id' => $value['id'],
'sort' => $newSort[$key],
];
}
$this->model->saveAll($array);
} catch (\Throwable $th) {
return $this->error($th->getMessage());
}
} else {
return $this->error('数据表未包含排序字段');
}
}
return $this->success();
}
/**
* 自动获取view模板
* @param string $template
* @param array $vars
* @param null $app
* @return Response
*/
public function view(string $template = '', array $vars = [], $app = null): Response
{
$request = explode('/', \request()->getController());
if (empty($template)) {
$parseArr = array_map(function ($item) {
return Str::snake($item);
}, $request);
$template = implode('/', $parseArr) . '/' . Str::snake(\request()->getAction());
}
return view($template, $vars, $app);
}
/**
* 排除特定字段
*
* @param [type] $params
* @return array
*/
protected function preRuleOutFields($params): array
{
if (is_array($this->ruleOutFields)) {
foreach ($this->ruleOutFields as $field) {
if (key_exists($field, $params)) {
unset($params[$field]);
}
}
} else {
if (key_exists($this->ruleOutFields, $params)) {
unset($params[$this->ruleOutFields]);
}
}
return $params;
}
/**
* 获取查询参数
2023-06-19 14:32:30 +08:00
* @return array
2022-08-19 19:48:37 +08:00
*/
2023-06-19 14:32:30 +08:00
protected function buildSelectParams(): array
2022-08-19 19:48:37 +08:00
{
2023-06-19 14:32:30 +08:00
$where = [];
2022-08-19 19:48:37 +08:00
$params = request()->all();
if (!empty($params) && is_array($params)) {
$this->tableFields = $this->model->getFields();
foreach ($params as $field => $value) {
// 过滤字段
if (in_array($field, $this->filterWhere)) {
continue;
}
// 非表内字段
if (!array_key_exists($field, $this->tableFields)) {
continue;
}
// 默认状态字段
if ($field == $this->keepField && $value) {
$where[] = [$field, '=', intval($value - 1)];
continue;
}
// 获取类型
$type = $this->tableFields[$field]['type'];
$type = explode('(', $type)[0];
$value = str_replace('/\s+/', '', $value);
switch ($type) {
case 'char':
case 'text':
case 'varchar':
case 'tinytext':
case 'longtext':
$where[] = [$field, 'like', '%' . $value . '%'];
break;
case 'int':
case 'bigint':
case 'integer':
case 'tinyint':
case 'smallint':
case 'mediumint':
case 'float':
case 'double':
case 'timestamp':
case 'year':
$value = str_replace(',', '-', $value);
if (strpos($value, '-')) {
2023-08-04 11:13:14 +08:00
2022-08-19 19:48:37 +08:00
$arr = explode(' - ', $value);
if (empty($arr)) {
continue 2;
}
if (in_array($field, $this->converTime)) {
if (isset($arr[0])) {
$arr[0] = strtotime($arr[0]);
}
if (isset($arr[1])) {
$arr[1] = strtotime($arr[1]);
}
}
$exp = 'between';
if ($arr[0] === '') {
$exp = '<=';
$arr = $arr[1];
} elseif ($arr[1] === '') {
$exp = '>=';
$arr = $arr[0];
}
$where[] = [$field, $exp, $arr];
} else {
$where[] = [$field, '=', $value];
}
break;
case 'set';
$where[] = [$field, 'find in set', $value];
break;
case 'enum';
$where[] = [$field, '=', $value];
break;
case 'date';
case 'time';
case 'datetime';
$value = str_replace(',', '-', $value);
if (strpos($value, '-')) {
$arr = explode(' - ', $value);
if (!array_filter($arr)) {
continue 2;
}
2023-08-04 11:13:14 +08:00
$exp = 'between';
2022-08-19 19:48:37 +08:00
if ($arr[0] === '') {
2023-08-04 11:13:14 +08:00
$exp = '<=';
2022-08-19 19:48:37 +08:00
$arr = $arr[1];
} elseif ($arr[1] === '') {
2023-08-04 11:13:14 +08:00
$exp = '>=';
2022-08-19 19:48:37 +08:00
$arr = $arr[0];
}
$where[] = [$field, $exp, $arr];
} else {
$where[] = [$field, '=', $value];
}
break;
case 'blob';
break;
default:
// 默认值
break;
}
}
2023-06-19 14:32:30 +08:00
// 限制个人数据权限
2023-07-03 10:08:34 +08:00
$superAdmin = $this->authService->SuperAdmin();
2023-06-19 14:32:30 +08:00
if (!$superAdmin && $this->dataLimit) {
2022-08-19 19:48:37 +08:00
if (in_array($this->dataLimitField, $this->tableFields)) {
$where[] = [$this->dataLimitField, '=', get_admin_id()];
2022-08-19 19:48:37 +08:00
}
2023-06-19 14:32:30 +08:00
} // 限制部门数据权限
else if (!$superAdmin && $this->departmentLimit
&& in_array('department_id', $this->tableFields)) {
$where[] = ['department_id', 'in', get_admin_info('AdminLogin.department_id')];
2022-08-19 19:48:37 +08:00
}
}
2023-06-19 14:32:30 +08:00
return $where;
2022-08-19 19:48:37 +08:00
}
/**
* 递归查询父节点
* @access public
* @param int $pid 查询条件
* @param array $array 返回数组
* @return array
*/
public function parentNode(int $pid, array &$array = []): array
{
$result = $this->model->where('id', $pid)->find()->toArray();
if (!empty($result)) {
/**
* 多语言字段
*/
if (isset($result['title'])) {
$result['title'] = __($result['title']);
}
$array[] = $result;
if ($result['pid'] !== 0) {
$this->parentNode($result['pid'], $array);
}
}
return $array;
}
/**
* 管理员退出
* @return Response
*/
public function logout(): Response
{
2023-07-03 10:08:34 +08:00
request()->session()->set(AdminEnum::ADMIN_SESSION, null);
2022-08-24 10:04:37 +08:00
return $this->success('退出成功!', '/');
2022-08-19 19:48:37 +08:00
}
/**
* 错误页面
* @param int $code
* @param string $msg
* @return Response
*/
public function abortPage(string $msg = '', int $code = 404): Response
{
$exception = config('app.exception_template');
if (isset($exception[$code])) {
$template = @file_get_contents($exception[$code]);
} else {
$template = $msg;
}
return \response($template, $code);
}
}