You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
150 lines
4.9 KiB
150 lines
4.9 KiB
<?php
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
use App\Exceptions\CustomException;
|
|
use App\Models\AdminConfigs;
|
|
use App\Services\ApiResponseService;
|
|
use App\Services\OperationLogService;
|
|
use Exception;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Validator;
|
|
use Illuminate\Validation\ValidationException;
|
|
|
|
class ConfigController extends BaseController
|
|
{
|
|
/**
|
|
* @var ApiResponseService
|
|
*/
|
|
protected ApiResponseService $responseService;
|
|
|
|
/**
|
|
* @var OperationLogService
|
|
*/
|
|
private OperationLogService $logService;
|
|
|
|
/**
|
|
* 构造函数
|
|
* @param ApiResponseService $responseService
|
|
* @param OperationLogService $logService
|
|
*/
|
|
public function __construct(
|
|
ApiResponseService $responseService,
|
|
OperationLogService $logService
|
|
) {
|
|
$this->responseService = $responseService;
|
|
$this->logService = $logService;
|
|
}
|
|
|
|
//
|
|
public function index(): JsonResponse
|
|
{
|
|
try {
|
|
$columns = ['id', 'title', 'name', 'content'];
|
|
$data = AdminConfigs::query()->select($columns)->get()->toArray();
|
|
$this->translationContent($data);
|
|
return $this->responseService->success($data);
|
|
} catch (Exception $e) {
|
|
$m_prefix = __('exception.get_data_failed');
|
|
return $this->responseService->systemError(
|
|
$m_prefix . ':' . $e->getMessage()
|
|
);
|
|
}
|
|
}
|
|
|
|
protected function translationContent(array &$data)
|
|
{
|
|
foreach ($data as &$value) {
|
|
$value['title'] = __('controller.config.' . $value['name']);
|
|
if (empty($value['content'])) {
|
|
continue;
|
|
}
|
|
foreach ($value['content'] as &$val) {
|
|
$val['title'] = __('controller.config.' . $val['name']);
|
|
}
|
|
}
|
|
}
|
|
|
|
public function update(Request $request, string $id): JsonResponse
|
|
{
|
|
try {
|
|
$this->validateId($id, AdminConfigs::class);
|
|
|
|
$model = AdminConfigs::findOrFail($id);
|
|
$oldValue = $model->toArray();
|
|
$data = $request->all();
|
|
$content = $data['content'];
|
|
if (empty($content)) {
|
|
throw new CustomException(
|
|
__('validation.admin_config.content_empty')
|
|
);
|
|
}
|
|
|
|
$default_arr = ['name', 'value'];
|
|
$validator_data = [];
|
|
foreach ($content as $key => $value) {
|
|
$validator_data[$value['name']] = $value['value'];
|
|
// 处理多余字段
|
|
$array_keys = array_keys($value);
|
|
$diff_keys = array_diff($array_keys, $default_arr);
|
|
if ($diff_keys) {
|
|
foreach ($diff_keys as $v) {
|
|
unset($value[$v]);
|
|
}
|
|
}
|
|
$content[$key] = $value;
|
|
}
|
|
|
|
if ($oldValue['name'] == 'parking_lot') {
|
|
$rules = [
|
|
'parking_lot_id' => 'required',
|
|
'parking_lot_sum' => 'required|numeric',
|
|
'zombie_car_parking_duration' => 'required|numeric',
|
|
];
|
|
$messages = [
|
|
'parking_lot_id.required' => __(
|
|
'validation.admin_config.parking_lot_id_empty'
|
|
),
|
|
'parking_lot_sum.required' => __(
|
|
'validation.admin_config.parking_lot_sum_empty'
|
|
),
|
|
'parking_lot_sum.numeric' => __(
|
|
'validation.admin_config.parking_lot_sum_num'
|
|
),
|
|
'zombie_car_parking_duration.required' => __(
|
|
'validation.admin_config.zombie_car_empty'
|
|
),
|
|
'zombie_car_parking_duration.numeric' => __(
|
|
'validation.admin_config.zombie_car_num'
|
|
),
|
|
];
|
|
|
|
$validator = Validator::make($validator_data, $rules, $messages);
|
|
if ($validator->fails()) {
|
|
throw new ValidationException($validator);
|
|
}
|
|
}
|
|
|
|
$model->update([
|
|
'content' => $content,
|
|
'updated_at' => get_datetime()
|
|
]);
|
|
|
|
$this->logService->logUpdated($model, $oldValue, '系统配置更新');
|
|
return $this->responseService->success(
|
|
null,
|
|
__('admin.update_succeeded')
|
|
);
|
|
} catch (ValidationException|CustomException $e) {
|
|
throw $e;
|
|
} catch (Exception $e) {
|
|
return $this->responseService->systemError(
|
|
__('exception.update_admin_config_failed') . ':' . $e->getMessage(
|
|
)
|
|
);
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
|