This commit is contained in:
u2nyakim 2025-08-22 10:39:04 +08:00
parent 5f28e13fb5
commit 38d922fa70
11 changed files with 248 additions and 11 deletions

87
app/command/Crontab.php Normal file
View File

@ -0,0 +1,87 @@
<?php
namespace app\command;
use app\enum\CrontabEnum;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use Cron\CronExpression;
use think\facade\Console;
use app\entity\SysDevCrontab as CrontabModel;
/**
* 定时任务
*/
class Crontab extends Command
{
protected function configure()
{
$this->setName('crontab')
->setDescription('定时任务');
}
protected function execute(Input $input, Output $output)
{
$lists = CrontabModel::where('status', CrontabEnum::START)->select()->toArray();
if (empty($lists)) {
return false;
}
$time = time();
foreach ($lists as $item) {
if (empty($item['last_time'])) {
$lastTime = (new CronExpression($item['expression']))
->getNextRunDate()
->getTimestamp();
CrontabModel::where('id', $item['id'])->update([
'last_time' => $lastTime,
]);
continue;
}
$nextTime = (new CronExpression($item['expression']))
->getNextRunDate($item['last_time'])
->getTimestamp();
if ($nextTime > $time) {
// 未到时间,不执行
continue;
}
// 开始执行
self::start($item);
}
}
public static function start($item)
{
// 开始执行
$startTime = microtime(true);
try {
$params = explode(' ', $item['params']);
if (is_array($params) && !empty($item['params'])) {
Console::call($item['command'], $params);
} else {
Console::call($item['command']);
}
// 清除错误信息
CrontabModel::where('id', $item['id'])->update(['error' => '']);
} catch (\Exception $e) {
// 记录错误信息
CrontabModel::where('id', $item['id'])->update([
'error' => $e->getMessage(),
'status' => CrontabEnum::ERROR
]);
} finally {
$endTime = microtime(true);
// 本次执行时间
$useTime = round(($endTime - $startTime), 2);
// 最大执行时间
$maxTime = max($useTime, $item['max_time']);
// 更新最后执行时间
CrontabModel::where('id', $item['id'])->update([
'last_time' => time(),
'time' => $useTime,
'max_time' => $maxTime
]);
}
}
}

View File

@ -0,0 +1,23 @@
<?php
namespace app\controller\admin\system;
use app\BaseController;
use app\entity\SysConfig;
use app\service\CurdService;
use think\response\Json;
class CacheController extends BaseController
{
public function list(): Json
{
$model = SysConfig::with([])
->withSearch([], [
]);
$paginate = CurdService::getPaginate($this->request, $model);
return $this->writeSuccess('success', $paginate);
}
}

View File

@ -0,0 +1,23 @@
<?php
namespace app\controller\admin\system;
use app\BaseController;
use app\entity\SysConfig;
use app\service\CurdService;
use think\response\Json;
class CacheDataController extends BaseController
{
public function list(): Json
{
$model = SysConfig::with([])
->withSearch([], [
]);
$paginate = CurdService::getPaginate($this->request, $model);
return $this->writeSuccess('success', $paginate);
}
}

View File

@ -0,0 +1,23 @@
<?php
namespace app\controller\admin\system;
use app\BaseController;
use app\entity\SysConfig;
use app\service\CurdService;
use think\response\Json;
class ConfigController extends BaseController
{
public function list(): Json
{
$model = SysConfig::with([])
->withSearch([], [
]);
$paginate = CurdService::getPaginate($this->request, $model);
return $this->writeSuccess('success', $paginate);
}
}

View File

@ -4,9 +4,7 @@ namespace app\controller\admin\system;
use app\BaseController;
use app\entity\SysDictionary;
use app\entity\SysDictionaryData;
use app\service\CurdService;
use think\db\exception\DbException;
use think\response\Json;
class DictionaryController extends BaseController

14
app/entity/SysConfig.php Normal file
View File

@ -0,0 +1,14 @@
<?php
namespace app\entity;
use think\Entity;
/**
* 系统配置实体类
* @see \app\model\SysConfig
*/
class SysConfig extends Entity
{
}

View File

@ -0,0 +1,14 @@
<?php
namespace app\entity;
use think\Entity;
/**
* 系统定时计划实体类
* @see \app\model\SysDevCrontab
*/
class SysDevCrontab extends Entity
{
}

10
app/enum/CrontabEnum.php Normal file
View File

@ -0,0 +1,10 @@
<?php
namespace app\enum;
enum CrontabEnum: int
{
case WAIT = 0;// 等待
case START = 1;// 开始
case ERROR = 2;// 错误
}

View File

@ -5,10 +5,12 @@ namespace app\model;
use app\BaseModel;
use think\model\concern\SoftDelete;
/**
* 系统配置模型类
*/
class SysConfig extends BaseModel
{
use SoftDelete;
protected $table = "sys_config";
protected $pk = 'config_id';
protected $name = "sys_config";
}

View File

@ -0,0 +1,16 @@
<?php
namespace app\model;
use app\BaseModel;
use think\model\concern\SoftDelete;
/**
* 系统定时计划模型类
*/
class SysDevCrontab extends BaseModel
{
use SoftDelete;
protected $name = "sys_dev_crontab";
}

View File

@ -6,6 +6,9 @@ use app\entity\SysOrganization;
use app\http\middleware\ClientMiddleware;
use app\model\SysDictionary;
use app\controller\admin\{auth,
system\CacheController,
system\CacheDataController,
system\ConfigController,
system\DictionaryController,
system\DictionaryDataController,
system\FileController,
@ -14,7 +17,8 @@ use app\controller\admin\{auth,
system\OperateRecordController,
system\RequestRecordController,
system\RoleController,
system\UserController};
system\UserController
};
use app\http\middleware\AuthMiddleware;
use think\facade\Route;
use think\middleware\AllowCrossDomain;
@ -26,6 +30,8 @@ Route::group("adminapi", function () {
Route::get("captcha", [auth\LoginController::class, "captcha"])->name("admin.LoginCaptcha");
Route::post("login", [auth\LoginController::class, "index"])->name("admin.SysUserLogin")->middleware([ClientMiddleware::class]);
Route::group(function () {
@ -34,18 +40,39 @@ Route::group("adminapi", function () {
Route::get("user", [auth\AuthController::class, "user"])->name("admin.SysUserInfo");
});
/*
* 系统文件管理
*/
Route::get('file/page', [FileController::class, "page"])->name("system.pageFiles");
Route::delete('file/remove/batch', [FileController::class, "batchRemove"])->name("system.batchRemoveFile");
Route::group("file", function () {
/*
* 文件管理
*/
Route::get('page', [FileController::class, "page"])->name("system.pageFiles");
Route::delete('remove/batch', [FileController::class, "batchRemove"])->name("system.batchRemoveFile");
})->name('文件接口');
Route::group("system", function () {
Route::get('login-record/page', [LoginRecordController::class, "page"])->name("system.pageLoginRecords");
Route::get('operate-record/page', [OperateRecordController::class, "page"])->name("system.pageOperateRecords");
Route::get('request-record/page', [RequestRecordController::class, "page"])->name("system.pageRequestRecords");
/*
* 缓存管理
*/
Route::get('cache', [CacheController::class, "list"])->name("system.listCache");
Route::put('cache', [CacheController::class, "update"])->name("system.updateCache");
/*
* 缓存数据管理
*/
Route::get('cache-data', [CacheDataController::class, "list"])->name("system.listCacheData");
Route::delete('cache-data/batch', [CacheDataController::class, "batchDelete"])->name("system.batchRemoveCacheData");
Route::put('cache-data', [CacheDataController::class, "update"])->name("system.updateCacheData");
/*
* 配置管理
*/
Route::get('config', [ConfigController::class, "list"])->name("system.listConfig");
Route::post("config", [UserController::class, "add"])->name("system.addConfig");
Route::put("config$", [UserController::class, "update"])->name("system.updateConfig");
Route::delete("config", [UserController::class, "delete"])->name("system.deleteConfig");
Route::put("config/cache", [UserController::class, "cache"])->name("system.cacheConfig");
/*
* 用户管理
*/