8 changed files with 293 additions and 32 deletions
@ -0,0 +1,150 @@ |
|||
<?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( |
|||
) |
|||
); |
|||
} |
|||
} |
|||
|
|||
|
|||
} |
|||
@ -0,0 +1,25 @@ |
|||
<?php |
|||
|
|||
namespace App\Models; |
|||
|
|||
use Illuminate\Database\Eloquent\Factories\HasFactory; |
|||
use Illuminate\Database\Eloquent\Model; |
|||
use Illuminate\Database\Eloquent\SoftDeletes; |
|||
|
|||
class AdminConfigs extends Model |
|||
{ |
|||
use HasFactory, SoftDeletes; |
|||
|
|||
protected $fillable = [ |
|||
'content' |
|||
]; |
|||
|
|||
protected $hidden = [ |
|||
'deleted_at', |
|||
'updated_at' |
|||
]; |
|||
|
|||
protected $casts = [ |
|||
'content' => 'json' |
|||
]; |
|||
} |
|||
@ -0,0 +1,82 @@ |
|||
<?php |
|||
|
|||
namespace Database\Seeders; |
|||
|
|||
use Illuminate\Database\Console\Seeds\WithoutModelEvents; |
|||
use Illuminate\Database\Seeder; |
|||
use Illuminate\Support\Facades\DB; |
|||
|
|||
class AdminConfigSeeder extends Seeder |
|||
{ |
|||
/** |
|||
* Run the database seeds. |
|||
*/ |
|||
public function run(): void |
|||
{ |
|||
// |
|||
DB::table('admin_configs')->insert($this->data()); |
|||
} |
|||
|
|||
protected function data(): array |
|||
{ |
|||
$created_at = date("Y-m-d H:i:s", time()); |
|||
return [ |
|||
[ |
|||
'title' => '车场配置', |
|||
'name' => 'parking_lot', |
|||
'content' => json_encode([ |
|||
[ |
|||
'name' => 'parking_lot_id', |
|||
'value' => 666 |
|||
], |
|||
[ |
|||
'name' => 'parking_lot_sum', |
|||
'value' => 1000 |
|||
], |
|||
[ |
|||
'name' => 'parking_lot_image_path', |
|||
'value' => '/home/wwwroot/test/saas-8' |
|||
], |
|||
[ |
|||
'name' => 'zombie_car_parking_duration', |
|||
'value' => 10 |
|||
], |
|||
[ |
|||
'name' => 'numberplate_fuzzy_search', |
|||
'value' => 1 |
|||
], |
|||
[ |
|||
'name' => 'automatically_clear_photo_cycle', |
|||
'value' => 10 |
|||
] |
|||
]), |
|||
'status' => 1, |
|||
'created_at' => $created_at |
|||
], |
|||
[ |
|||
'title' => '第三方平台配置', |
|||
'name' => 'third_party', |
|||
'content' => json_encode([ |
|||
[ |
|||
'name' => 'report_data_switch', |
|||
'value' => 1 |
|||
], |
|||
[ |
|||
'name' => 'third_parking_lot_id', |
|||
'value' => 666 |
|||
], |
|||
[ |
|||
'name' => 'report_api_url', |
|||
'value' => 'https://xg-saas-8.icecloud-car.cc' |
|||
], |
|||
[ |
|||
'name' => 'report_cycle', |
|||
'value' => 60 |
|||
], |
|||
]), |
|||
'status' => 1, |
|||
'created_at' => $created_at |
|||
] |
|||
]; |
|||
} |
|||
} |
|||
Loading…
Reference in new issue