删除优化

This commit is contained in:
zhang zhuo 2025-11-17 16:19:03 +08:00
parent 50f90090e3
commit 557d70b0aa
8 changed files with 256 additions and 37 deletions

View File

@ -72,6 +72,21 @@ abstract class Base extends AbstractController
return $this->response(1, $msg, $data, $count, $summary);
}
/**
* @param int|bool $res
* @return ResponseInterface
*/
public function toAjax(int|bool $res)
{
if (is_int($res)) {
return $res > 0 ? $this->success("操作成功") : $this->error("操作失败");
}
if (is_bool($res)) {
return $res ? $this->success("操作成功") : $this->error("操作失败");
}
return $this->error("操作失败");
}
/**
* 响应
* @param int $code
@ -89,6 +104,4 @@ abstract class Base extends AbstractController
if ($summary === null) unset($body['summary']);
return $this->response->json($body);
}
}

View File

@ -11,10 +11,10 @@ use App\Model\Crontab as cModel;
use App\Model\CrontabLog as clModel;
use App\Model\Dept as dModel;
use App\Model\Menu as mModel;
use App\Model\Online;
use App\Model\Online as oModel;
use App\Model\Post as pModel;
use App\Model\Role as rModel;
use App\Model\GenTable as gtModel;
use App\Request\Account as aRequest;
use App\Request\Dept as dRequest;
use App\Request\Menu as mRequest;
@ -29,11 +29,13 @@ use App\Utils\Param;
use App\Utils\RedisInfoHelper;
use App\Utils\SystemHelper;
use Hyperf\Context\ApplicationContext;
use Hyperf\DbConnection\Db;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\DeleteMapping;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\PostMapping;
use Hyperf\HttpServer\Annotation\PutMapping;
use function Hyperf\Config\config;
#[Controller(prefix: "admin")]
class System extends Base
@ -83,10 +85,8 @@ class System extends Base
#[Auth(needAuth: false, auth: "menu:del")]
public function menuDel()
{
$ids = $this->request->input("ids", "");
if (!$ids) return $this->error("请选择要删除的菜单");
$res = mModel::del($ids);
return $res ? $this->success("操作成功") : $this->error("操作失败");
$ids = $this->request->input("ids");
return $this->toAjax(mModel::del($ids));
}
#[PostMapping(path: "menu/quick")]
@ -143,10 +143,8 @@ class System extends Base
#[Auth(auth: "dept:del")]
public function deptDel()
{
$ids = $this->request->input("ids", "");
if (!$ids) return $this->error("请选择要删除的菜单");
$res = dModel::del($ids);
return $res ? $this->success("操作成功") : $this->error("操作失败");
$ids = $this->request->input("ids");
return $this->toAjax(dModel::del($ids));
}
// 角色列表
@ -197,10 +195,8 @@ class System extends Base
#[Auth(auth: "role:del")]
public function roleDel()
{
$ids = $this->request->input("ids", "");
if (!$ids) return $this->error("请选择要删除的角色");
$res = rModel::del($ids);
return $res ? $this->success("操作成功") : $this->error("操作失败");
$ids = $this->request->input("ids");
return $this->toAjax(rModel::del($ids));
}
#[GetMapping(path: "post/list")]
@ -246,10 +242,8 @@ class System extends Base
#[Auth(auth: "post:del")]
public function postDel()
{
$ids = $this->request->input("ids", "");
if (!$ids) return $this->error("请选择要删除的岗位");
$res = pModel::del($ids);
return $res ? $this->success("操作成功") : $this->error("操作失败");
$ids = $this->request->input("ids");
return $this->toAjax(pModel::del($ids));
}
// 账号列表
@ -376,7 +370,7 @@ class System extends Base
{
$param = Param::only(['session_id' => '']);
// 退出登录状态
$online = Online::getByUuid($param['session_id']);
$online = oModel::getByUuid($param['session_id']);
if (empty($online) || $online['status'] == 0) {
return $this->success("用户已不在线");
}
@ -385,7 +379,7 @@ class System extends Base
$redis->del("USER:" . $param['session_id']);
$redis->del("AUTH:" . $online['account_id']);
// 设置离线
Online::leave($param['session_id']);
oModel::leave($param['session_id']);
return $this->success("强退成功");
}
@ -432,13 +426,8 @@ class System extends Base
#[Auth(auth: "crontab:del")]
public function crontabDel()
{
$ids = $this->request->input("ids", "");
if (!$ids) return $this->error("操作失败");
$res = cModel::del($ids);
if ($res) {
return $this->success("操作成功");
}
return $this->error("操作失败");
$ids = $this->request->input("ids");
return $this->toAjax(cModel::del($ids));
}
#[GetMapping(path: "crontab_log/list")]
@ -454,8 +443,7 @@ class System extends Base
public function crontabLogDel()
{
$param = Param::only(['ids']);
$res = clModel::del($param['ids']);
return $res ? $this->success("操作成功") : $this->error("操作失败");
return $this->toAjax(clModel::del($param['ids']));
}
#[DeleteMapping(path: "crontab_log/remove_all")]
@ -466,4 +454,105 @@ class System extends Base
$res = clModel::removeAll($param);
return $res ? $this->success("操作成功") : $this->error("操作失败");
}
#[GetMapping(path: "gen_table/list")]
#[Auth(auth: "gen_table:list")]
public function genTableList()
{
$param = Param::only(['table_name', 'table_comment', 'limit' => 10]);
return $this->success("表列表", gtModel::list($param));
}
#[DeleteMapping(path: "gen_table/del")]
#[Auth(auth: "gen_table:del")]
public function genTableDel()
{
$param = Param::only(['ids' => []]);
return $this->toAjax(gtModel::del($param['ids']));
}
#[GetMapping(path: "gen_table/select")]
#[Auth(auth: "gen_table:select")]
public function genTableSelect()
{
$param = Param::only(['table_name', 'table_comment', 'limit' => 10, 'page' => 1]);
$offset = ($param['page'] - 1) * $param['limit'];
// 获取所有表
$rows = Db::select("SHOW TABLE STATUS");
// 获取已经导入的表
$tables = gtModel::pluck("table_name")->toArray();
// 表前缀
$prefix = config("databases.default.prefix");
// 构建表不导入 gen_table gen_table_column
$tables = array_merge([$prefix . "gen_table", $prefix . "gen_table_column"], $tables);
$rows = array_filter($rows, function ($item) use ($tables, $param) {
// 过滤表
if (in_array($item->Name, $tables)) {
return false;
}
// 表名筛选
if (isset($param['table_name']) && $param['table_name'] != "" && !str_contains($item->Name, $param['table_name'])) {
return false;
}
// 表描述筛选
if (isset($param['table_comment']) && $param['table_comment'] != "" && !str_contains($item->Comment, $param['table_comment'])) {
return false;
}
return true;
});
$count = count($rows);
$rows = array_slice($rows, $offset, $param['limit']);
// 过滤返回数据
$data = [];
foreach ($rows as $row) {
$data[] = [
'table_name' => $row->Name,
'table_comment' => $row->Comment,
'create_time' => $row->Create_time,
'update_time' => $row->Update_time
];
}
return $this->success("表列表", $data, $count);
}
#[PostMapping(path: "gen_table/build")]
#[Auth(auth: "gen_table:build")]
public function genTableBuild()
{
// 生成表数据
$param = Param::only(['names' => []]);
if (empty($param['names'])) {
return $this->error("数据表不为空");
}
// 获取已经导入的表
$tables = gtModel::pluck("table_name")->toArray();
// 表前缀
$prefix = config("databases.default.prefix");
// 构建表不导入 gen_table gen_table_column
$tables = array_merge([$prefix . "gen_table", $prefix . "gen_table_column"], $tables);
// 获取所有表
$rows = Db::select("SHOW TABLE STATUS");
// 去重
$rows = array_filter($rows, function ($item) use ($tables, $param) {
// 过滤表
if (in_array($item->Name, $tables)) {
return false;
}
// 表名筛选
if (isset($param['table_name']) && $param['table_name'] != "" && !str_contains($item->Name, $param['table_name'])) {
return false;
}
// 表描述筛选
if (isset($param['table_comment']) && $param['table_comment'] != "" && !str_contains($item->Comment, $param['table_comment'])) {
return false;
}
return true;
});
if (empty($rows)) {
return $this->error("数据表不为空");
}
// 生成数据
$res = gtModel::genTable($rows);
return $res ? $this->success("操作成功") : $this->error("操作失败");
}
}

67
app/Model/GenTable.php Normal file
View File

@ -0,0 +1,67 @@
<?php
declare(strict_types=1);
namespace App\Model;
use Hyperf\DbConnection\Db;
/**
* @property int $table_id
* @property string $table_name
* @property string $table_comment
* @property string $controller_name
* @property string $module_name
* @property string $remark
* @property string $deleted_at
* @property string $create_time
* @property string $update_time
*/
class GenTable extends Model
{
/**
* The table associated with the model.
*/
protected ?string $table = 'gen_table';
protected string $primaryKey = 'table_id';
/**
* The attributes that are mass assignable.
*/
protected array $fillable = [];
/**
* The attributes that should be cast to native types.
*/
protected array $casts = ['table_id' => 'integer'];
/**
* @param array $param
*/
public static function list(array $param)
{
return (new self())->when(isset($param['table_name']) && $param['table_name'] != "", function ($query) use ($param) {
$query->where('table_name', "like", "%{$param['table_name']}%");
})->when(isset($param['table_comment']) && $param['table_comment'] != "", function ($query) use ($param) {
$query->where('table_comment', "like", "%{$param['table_comment']}%");
})->select(['table_id', 'table_name', 'table_comment', 'controller_name', 'module_name', 'remark', 'create_time', 'update_time'])
->orderByDesc('table_id')->paginate((int)$param['limit']);
}
public static function genTable(array $rows)
{
Db::beginTransaction();
foreach ($rows as $row) {
var_dump($row);
// 添加主表
// $res = self::insert([
// 'table_name' => $row,
// ]);
// $columns = Db::select("SHOW FULL COLUMNS FROM `{$row}`");
// var_dump($columns);
}
Db::commit();
return true;
}
}

View File

@ -0,0 +1,44 @@
<?php
declare(strict_types=1);
namespace App\Model;
/**
* @property int $column_id
* @property int $table_id
* @property string $column_name
* @property string $column_comment
* @property string $column_type
* @property string $php_type
* @property string $php_field
* @property int $is_insert
* @property int $is_edit
* @property int $is_list
* @property int $is_query
* @property string $query_type
* @property string $html_type
* @property int $rank
* @property string $create_time
* @property string $update_time
* @property string $deleted_at
*/
class GenTableColumn extends Model
{
/**
* The table associated with the model.
*/
protected ?string $table = 'gen_table_column';
protected string $primaryKey = 'column_id ';
/**
* The attributes that are mass assignable.
*/
protected array $fillable = [];
/**
* The attributes that should be cast to native types.
*/
protected array $casts = ['column_id' => 'integer', 'table_id' => 'integer', 'is_insert' => 'integer', 'is_edit' => 'integer', 'is_list' => 'integer', 'is_query' => 'integer', 'rank' => 'integer'];
}

View File

@ -49,7 +49,7 @@ class Online extends Model
$q->where('username', 'like', "%{$param['username']}%");
})
->orderByDesc("online_id")
->select(['session_id', 'username', 'ip', 'ua', 'online_time', 'account_id'])
->select(['session_id', 'username', 'ip', 'ua', 'online_time', 'account_id', 'update_time'])
->paginate((int)$param['limit']);
}

View File

@ -74,9 +74,9 @@ class AppInfoHelper
private static function formatDuration(int $seconds): string
{
$h = floor($seconds / 3600);
$d = floor($seconds / 86400);
$h = floor(($seconds % 86400) / 3600);
$m = floor(($seconds % 3600) / 60);
$s = $seconds % 60;
return sprintf('%2d天%2d小时%2d分钟', $h, $m, $s);
return sprintf('%d天%d小时%d分钟', $d, $h, $m);
}
}

View File

@ -21,9 +21,16 @@ trait Crud
return $model->where($model->primaryKey, $data[$model->primaryKey])->update($data);
}
public static function del(string $ids): int
public static function del(array $ids = null): int
{
return self::destroy(explode(",", $ids));
if (empty($ids)) {
$param = Param::only(['ids' => []]);
$ids = $param['ids'];
}
if (count($ids) <= 0) {
return false;
}
return self::destroy($ids);
}
public static function getById(int $id, array $field = ['*']): array

View File

@ -31,7 +31,6 @@
"hyperf/process": "~3.1.0",
"hyperf/rate-limit": "^3.1",
"hyperf/redis": "~3.1.0",
"hyperf/swagger": "^3.1",
"hyperf/validation": "^3.1",
"hyperf/websocket-server": "^3.1",
"phpoffice/phpspreadsheet": "^4.5",