更新功能:数据字典和定时任务

This commit is contained in:
2026-02-18 10:43:25 +08:00
parent 6623c656f4
commit 790b3140a7
15 changed files with 2847 additions and 25 deletions

View File

@@ -91,6 +91,7 @@ class User extends Controller
'real_name' => 'nullable|string|max:50',
'email' => 'nullable|email|unique:auth_users,email,' . $id,
'phone' => 'nullable|string|max:20',
'avatar' => 'nullable|string|max:500',
'department_id' => 'nullable|integer|exists:auth_departments,id',
'role_ids' => 'nullable|array',
'role_ids.*' => 'integer|exists:auth_roles,id',

View File

@@ -20,7 +20,8 @@ class DictionaryService
});
}
if (isset($params['status']) && $params['status'] !== '') {
// 处理状态筛选只接受布尔值或数字1/0
if (array_key_exists('status', $params) && is_bool($params['status'])) {
$query->where('status', $params['status']);
}
@@ -37,20 +38,48 @@ class DictionaryService
public function getAll(): array
{
return Dictionary::where('status', true)
->orderBy('sort')
->get()
->toArray();
$cacheKey = 'system:dictionaries:all';
$dictionaries = Cache::get($cacheKey);
if ($dictionaries === null) {
$dictionaries = Dictionary::where('status', true)
->orderBy('sort')
->get()
->toArray();
Cache::put($cacheKey, $dictionaries, 3600);
}
return $dictionaries;
}
public function getById(int $id): ?Dictionary
{
return Dictionary::with('items')->find($id);
$cacheKey = 'system:dictionaries:' . $id;
$dictionary = Cache::get($cacheKey);
if ($dictionary === null) {
$dictionary = Dictionary::with('items')->find($id);
if ($dictionary) {
Cache::put($cacheKey, $dictionary->toArray(), 3600);
}
}
return $dictionary ? $dictionary : null;
}
public function getByCode(string $code): ?Dictionary
{
return Dictionary::where('code', $code)->first();
$cacheKey = 'system:dictionaries:code:' . $code;
$dictionary = Cache::get($cacheKey);
if ($dictionary === null) {
$dictionary = Dictionary::where('code', $code)->first();
if ($dictionary) {
Cache::put($cacheKey, $dictionary->toArray(), 3600);
}
}
return $dictionary;
}
public function getItemsByCode(string $code): array
@@ -125,11 +154,26 @@ class DictionaryService
return true;
}
private function clearCache(): void
private function clearCache($dictionaryId = null): void
{
$codes = Dictionary::pluck('code')->toArray();
foreach ($codes as $code) {
Cache::forget('system:dictionary:' . $code);
// 清理所有字典列表缓存
Cache::forget('system:dictionaries:all');
if ($dictionaryId) {
// 清理特定字典的缓存
$dictionary = Dictionary::find($dictionaryId);
if ($dictionary) {
Cache::forget('system:dictionaries:' . $dictionaryId);
Cache::forget('system:dictionaries:code:' . $dictionary->code);
Cache::forget('system:dictionary:' . $dictionary->code);
}
} else {
// 清理所有字典缓存
$codes = Dictionary::pluck('code')->toArray();
foreach ($codes as $code) {
Cache::forget('system:dictionary:' . $code);
Cache::forget('system:dictionaries:code:' . $code);
}
}
}
@@ -141,7 +185,8 @@ class DictionaryService
$query->where('dictionary_id', $params['dictionary_id']);
}
if (isset($params['status']) && $params['status'] !== '') {
// 处理状态筛选只接受布尔值或数字1/0
if (array_key_exists('status', $params) && is_bool($params['status'])) {
$query->where('status', $params['status']);
}
@@ -167,7 +212,7 @@ class DictionaryService
])->validate();
$item = DictionaryItem::create($data);
$this->clearCache();
$this->clearCache($data['dictionary_id']);
return $item;
}
@@ -182,29 +227,46 @@ class DictionaryService
])->validate();
$item->update($data);
$this->clearCache();
$this->clearCache($item->dictionary_id);
return $item;
}
public function deleteItem(int $id): bool
{
$item = DictionaryItem::findOrFail($id);
$dictionaryId = $item->dictionary_id;
$item->delete();
$this->clearCache();
$this->clearCache($dictionaryId);
return true;
}
public function batchDeleteItems(array $ids): bool
{
$items = DictionaryItem::whereIn('id', $ids)->get();
$dictionaryIds = $items->pluck('dictionary_id')->unique()->toArray();
DictionaryItem::whereIn('id', $ids)->delete();
$this->clearCache();
// 清理相关字典的缓存
foreach ($dictionaryIds as $dictionaryId) {
$this->clearCache($dictionaryId);
}
return true;
}
public function batchUpdateItemsStatus(array $ids, bool $status): bool
{
$items = DictionaryItem::whereIn('id', $ids)->get();
$dictionaryIds = $items->pluck('dictionary_id')->unique()->toArray();
DictionaryItem::whereIn('id', $ids)->update(['status' => $status]);
$this->clearCache();
// 清理相关字典的缓存
foreach ($dictionaryIds as $dictionaryId) {
$this->clearCache($dictionaryId);
}
return true;
}
}

View File

@@ -66,6 +66,10 @@ class LogService
$query->where('action', $params['action']);
}
if (!empty($params['method'])) {
$query->where('method', $params['method']);
}
if (!empty($params['status'])) {
$query->where('status', $params['status']);
}

View File

@@ -5,7 +5,8 @@ namespace App\Services\System;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use Intervention\Image\Facades\Image;
use Intervention\Image\ImageManager;
use Intervention\Image\Drivers\Gd\Driver;
class UploadService
{
@@ -17,6 +18,7 @@ class UploadService
public function __construct()
{
$this->disk = Storage::disk('public');
$this->imageManager = new ImageManager(new Driver());
}
public function upload(UploadedFile $file, string $directory = 'uploads', array $options = []): array
@@ -135,17 +137,14 @@ class UploadService
$width = $options['width'] ?? null;
$height = $options['height'] ?? null;
$image = Image::make($file);
$image = $this->imageManager->read($file);
if ($width || $height) {
$image->resize($width, $height, function ($constraint) {
$constraint->aspectRatio();
$constraint->upsize();
});
$image->scale($width, $height);
}
$image->encode(null, $quality);
$this->disk->put($filePath, (string) $image);
$encoded = $image->toJpeg(quality: $quality);
$this->disk->put($filePath, (string) $encoded);
}
public function getFileUrl(string $path): string