Files
ProxyPanel/app/Http/Controllers/Admin/Config/CategoryController.php
BrettonYe ad3662cda0 🚀 Refactor Blade
- Optimize Blade JavaScript code.
- Refactored multiple admin controllers for improved validation, error handling, and query efficiency.
- Added ProxyConfig trait to centralize proxy configuration options.
- Updated NodeStatusDetection to use model relationships for heartbeat checks.
- Improved category, label, and country management logic and error logging.
- Added new Blade components for admin UI and updated language files and assets for better localization and frontend support.
- Bug fixed & introduced more bug :)
2026-01-30 20:04:17 +08:00

77 lines
2.9 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace App\Http\Controllers\Admin\Config;
use App\Http\Controllers\Controller;
use App\Models\GoodsCategory;
use Exception;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Log;
use Validator;
class CategoryController extends Controller
{
public function store(Request $request): JsonResponse
{ // 添加分类
$validator = Validator::make($request->all(), [
'name' => 'required',
'sort' => 'nullable|numeric',
]);
if ($validator->fails()) {
return response()->json(['status' => 'fail', 'message' => $validator->errors()->all()]);
}
$data = $validator->validated();
// 如果没有提供sort值则设为0
if (! isset($data['sort'])) {
$data['sort'] = 0;
}
if (GoodsCategory::create($data)) {
return response()->json(['status' => 'success', 'message' => trans('common.success_item', ['attribute' => trans('common.add')])]);
}
return response()->json(['status' => 'fail', 'message' => trans('common.failed_item', ['attribute' => trans('common.add')])]);
}
public function update(Request $request, GoodsCategory $category): JsonResponse
{ // 编辑分类
$validator = Validator::make($request->all(), [
'name' => 'required',
'sort' => 'required|numeric',
]);
if ($validator->fails()) {
return response()->json(['status' => 'fail', 'message' => $validator->errors()->all()]);
}
if ($category->update($validator->validated())) {
return response()->json(['status' => 'success', 'message' => trans('common.success_item', ['attribute' => trans('common.edit')])]);
}
return response()->json(['status' => 'fail', 'message' => trans('common.failed_item', ['attribute' => trans('common.edit')])]);
}
public function destroy(GoodsCategory $category): JsonResponse
{ // 删除分类
// 校验该分类下是否存在关联商品
if ($category->goods()->exists()) {
return response()->json(['status' => 'fail', 'message' => trans('common.exists_error', ['attribute' => trans('model.goods.category')])]);
}
try {
if ($category->delete()) {
return response()->json(['status' => 'success', 'message' => trans('common.success_item', ['attribute' => trans('common.delete')])]);
}
} catch (Exception $e) {
Log::error(trans('common.error_action_item', ['action' => trans('common.delete'), 'attribute' => trans('model.goods.category')]).': '.$e->getMessage());
return response()->json(['status' => 'fail', 'message' => trans('common.failed_item', ['attribute' => trans('common.delete')]).', '.$e->getMessage()]);
}
return response()->json(['status' => 'fail', 'message' => trans('common.failed_item', ['attribute' => trans('common.delete')])]);
}
}