Browse Source

系统翻译管理

master
wanghongjun 1 month ago
parent
commit
924ffe9e14
  1. 6
      app/Http/Controllers/Admin/RolesController.php
  2. 175
      app/Http/Controllers/Admin/TranslationController.php
  3. 6
      app/Http/Controllers/Admin/UserController.php
  4. 24
      app/Models/AdminTranslation.php
  5. 126
      app/Services/AdminTranslationService.php
  6. 31
      database/migrations/2026_02_10_175744_create_admin_translation_table.php

6
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()
);
}
}

175
app/Http/Controllers/Admin/TranslationController.php

@ -0,0 +1,175 @@
<?php
namespace App\Http\Controllers\Admin;
use App\Exceptions\CustomException;
use App\Models\AdminTranslation;
use App\Services\AdminTranslationService;
use App\Services\ApiResponseService;
use Exception;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\ValidationException;
class TranslationController extends BaseController
{
/**
* @var ApiResponseService
*/
protected ApiResponseService $responseService;
/**
* @var AdminTranslationService
*/
protected AdminTranslationService $AdminTranslationService;
/**
* 构造函数
* @param ApiResponseService $responseService
* @param AdminTranslationService $AdminTranslationService
*/
public function __construct(
ApiResponseService $responseService,
AdminTranslationService $AdminTranslationService,
) {
$this->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()
);
}
}
}

6
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()
);
}
}

24
app/Models/AdminTranslation.php

@ -0,0 +1,24 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class AdminTranslation extends Model
{
use HasFactory;
protected $table = 'admin_translation';
protected $fillable = [
'en',
'zh_cn',
'zh_tw'
];
protected $hidden = [
'updated_at',
'deleted_at'
];
}

126
app/Services/AdminTranslationService.php

@ -0,0 +1,126 @@
<?php
namespace App\Services;
use App\Models\AdminTranslation;
use Exception;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
class AdminTranslationService
{
/**
* @var OperationLogService
*/
private OperationLogService $logService;
/**
* @param OperationLogService $logService
*/
public function __construct(OperationLogService $logService)
{
$this->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;
}
}
}

31
database/migrations/2026_02_10_175744_create_admin_translation_table.php

@ -0,0 +1,31 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('admin_translation', function (Blueprint $table) {
$table->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');
}
};
Loading…
Cancel
Save