diff --git a/app/Http/Controllers/Admin/RolesController.php b/app/Http/Controllers/Admin/RolesController.php index e8731d3..4a2c2d3 100644 --- a/app/Http/Controllers/Admin/RolesController.php +++ b/app/Http/Controllers/Admin/RolesController.php @@ -216,6 +216,7 @@ class RolesController extends BaseController * @param string $id * @return JsonResponse * @throws CustomException + * @throws ValidationException */ public function destroy(string $id): JsonResponse { @@ -226,11 +227,12 @@ class RolesController extends BaseController null, __('admin.delete_succeeded') ); - } catch (CustomException $e) { + } catch (ValidationException|CustomException $e) { throw $e; } catch (Exception $e) { return $this->responseService->systemError( - '删除数据模型失败:' . $e->getMessage() + __('exception.admin_role.destroy_failed') . ':' + . $e->getMessage() ); } } diff --git a/app/Http/Controllers/Admin/TranslationController.php b/app/Http/Controllers/Admin/TranslationController.php new file mode 100644 index 0000000..d14238b --- /dev/null +++ b/app/Http/Controllers/Admin/TranslationController.php @@ -0,0 +1,175 @@ +responseService = $responseService; + $this->AdminTranslationService = $AdminTranslationService; + } + + /** + * @param Request $request + * @return JsonResponse + */ + public function index(Request $request): JsonResponse + { + try { + $query = AdminTranslation::query(); + + // 分页 + $page = $request->input('page', 1); + $perPage = $request->input('per_page', 10); + + $total = $query->count(); + $items = $query->latest()->forPage($page, $perPage)->get(); + + return $this->responseService->success([ + 'items' => $items, + 'total' => $total, + 'page' => $page, + 'per_page' => $perPage, + 'last_page' => ceil($total / $perPage), + ]); + } catch (Exception $e) { + $m_prefix = __('exception.exception_handler.resource'); + return $this->responseService->systemError( + $m_prefix . ':' . $e->getMessage() + ); + } + } + + /** + * @param Request $request + * @return JsonResponse + * @throws CustomException + * @throws ValidationException + */ + public function store(Request $request): JsonResponse + { + try { + $this->saveValidator($request->all()); + $this->AdminTranslationService->createModel($request->all()); + return $this->responseService->success( + null, + __('admin.save_succeeded') + ); + } catch (ValidationException|CustomException $e) { + throw $e; + } catch (Exception $e) { + return $this->responseService->systemError( + __('exception.admin_translation.create_failed') . ':' . $e->getMessage( + ) + ); + } + } + + + /** + * @param array $data + * @param int $id + * @return void + * @throws ValidationException + */ + private function saveValidator(array $data, int $id = 0): void + { + $rules = [ + 'en' => 'required', + 'zh_cn' => 'required', + 'zh_tw' => 'required' + ]; + $messages = [ + 'en.required' => __('validation.admin_translation.en_empty'), + 'zh_cn.required' => __('validation.admin_translation.zh_cn_empty'), + 'zh_tw.required' => __('validation.admin_translation.zh_tw_empty'), + ]; + if ($id) { + $this->validateId($id, AdminTranslation::class); + } + $validator = Validator::make($data, $rules, $messages); + + if ($validator->fails()) { + throw new ValidationException($validator); + } + } + + /** + * @param Request $request + * @param string $id + * @return JsonResponse + * @throws CustomException + * @throws ValidationException + */ + public function update(Request $request, string $id): JsonResponse + { + try { + $this->saveValidator($request->all(), $id); + $this->AdminTranslationService->updateModel($request->all(), $id); + return $this->responseService->success( + null, + __('admin.save_succeeded') + ); + } catch (ValidationException|CustomException $e) { + throw $e; + } catch (Exception $e) { + return $this->responseService->systemError( + __('exception.admin_translation.create_failed') . ':' . $e->getMessage( + ) + ); + } + } + + /** + * @param string $id + * @return JsonResponse + * @throws CustomException + * @throws ValidationException + */ + public function destroy(string $id): JsonResponse + { + try { + $this->validateId($id, AdminTranslation::class); + $this->AdminTranslationService->deleteModel($id); + return $this->responseService->success( + null, + __('admin.delete_succeeded') + ); + } catch (ValidationException|CustomException $e) { + throw $e; + } catch (Exception $e) { + return $this->responseService->systemError( + __('exception.admin_translation.destroy_failed') . ':' + . $e->getMessage() + ); + } + } +} diff --git a/app/Http/Controllers/Admin/UserController.php b/app/Http/Controllers/Admin/UserController.php index 9b8a521..f66d8b3 100644 --- a/app/Http/Controllers/Admin/UserController.php +++ b/app/Http/Controllers/Admin/UserController.php @@ -245,6 +245,7 @@ class UserController extends BaseController * @param string $id * @return JsonResponse * @throws CustomException + * @throws ValidationException */ public function destroy(string $id): JsonResponse { @@ -254,11 +255,12 @@ class UserController extends BaseController null, __('admin.delete_succeeded') ); - } catch (CustomException $e) { + } catch (ValidationException|CustomException $e) { throw $e; } catch (Exception $e) { return $this->responseService->systemError( - '删除数据模型失败:' . $e->getMessage() + __('exception.admin_user.destroy_failed') . ':' + . $e->getMessage() ); } } diff --git a/app/Models/AdminTranslation.php b/app/Models/AdminTranslation.php new file mode 100644 index 0000000..aa646e9 --- /dev/null +++ b/app/Models/AdminTranslation.php @@ -0,0 +1,24 @@ +logService = $logService; + } + + /** + * @param array $data + * @return Model|Builder + * @throws Exception + */ + public function createModel(array $data): Model|Builder + { + try { + DB::beginTransaction(); + DB::commit(); + + $save_data = [ + 'en' => $data['en'], + 'zh_cn' => $data['zh_cn'], + 'zh_tw' => $data['zh_tw'] + ]; + if (AdminTranslation::query()->where($save_data)->exists()) { + throw new Exception( + __('service.admin_translation.data_exists') + ); + } + + $save_data['created_at'] = get_datetime(); + $model = AdminTranslation::query()->create($save_data); + + $this->logService->logCreated($model, '创建翻译'); + + return $model; + } catch (Exception $e) { + DB::rollBack(); + throw $e; + } + } + + /** + * @param array $data + * @param int $id + * @return Model|Builder + * @throws Exception + */ + public function updateModel(array $data, int $id): Model|Builder + { + try { + DB::beginTransaction(); + DB::commit(); + + $update_data = [ + 'en' => $data['en'], + 'zh_cn' => $data['zh_cn'], + 'zh_tw' => $data['zh_tw'] + ]; + + if (AdminTranslation::query()->where($update_data)->where( + 'id', + '<>', + $id + )->exists() + ) { + throw new Exception( + __('service.admin_translation.data_exists') + ); + } + + $model = AdminTranslation::query()->findOrFail($id); + $oldValues = $model->toArray(); + $update_data['updated_at'] = get_datetime(); + $model->update($update_data); + + $this->logService->logUpdated($model, $oldValues, '更新翻译'); + + return $model; + } catch (Exception $e) { + DB::rollBack(); + throw $e; + } + } + + /** + * @param int $id + * @return bool + * @throws Exception + */ + public function deleteModel(int $id): bool + { + try { + DB::beginTransaction(); + + $model = AdminTranslation::query()->findOrFail($id); + + $this->logService->logDeleted($model, '删除翻译'); + + $model->delete(); + + DB::commit(); + return true; + } catch (Exception $e) { + DB::rollBack(); + throw $e; + } + } +} diff --git a/database/migrations/2026_02_10_175744_create_admin_translation_table.php b/database/migrations/2026_02_10_175744_create_admin_translation_table.php new file mode 100644 index 0000000..cb9d9e4 --- /dev/null +++ b/database/migrations/2026_02_10_175744_create_admin_translation_table.php @@ -0,0 +1,31 @@ +id(); + $table->string('en')->comment('英文'); + $table->string('zh_cn')->comment('中文简体'); + $table->string('zh_tw')->comment('中文繁体'); + $table->softDeletes(); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('admin_translation'); + } +};