语言词典

This commit is contained in:
zhang zhuo 2025-12-04 14:46:31 +08:00
parent 026ca74f1e
commit 1213f86045
5 changed files with 231 additions and 3 deletions

View File

@ -30,6 +30,8 @@ use App\Model\Dict as dtModel;
use App\Request\Dict as dtRequest;
use App\Model\DictData as ddModel;
use App\Request\DictData as ddRequest;
use App\Model\Translation as tsModel;
use App\Request\Translation as tsRequest;
use App\Utils\AppInfoHelper;
use App\Utils\CpuHelper;
use App\Utils\DiskInfoHelper;
@ -882,7 +884,8 @@ class System extends Base
#[Auth(needAuth: false)]
public function dictDataOption()
{
return $this->success(ddModel::options());
$param = Param::only(["key"=>""]);
return $this->success(ddModel::options($param));
}
#[GetMapping(path: "dict_data/info")]
@ -920,4 +923,40 @@ class System extends Base
$ids = $this->request->input("ids");
return $this->toAjax(ddModel::del($ids));
}
#[GetMapping(path: "translation/list")]
#[Auth(auth: "translation:list")]
public function translationList()
{
$param = Param::only(["group", "translation_key", "limit"=>10]);
return $this->success("列表接口", tsModel::list($param));
}
#[PostMapping(path: "translation/add")]
#[Auth(auth: "translation:add")]
public function translationAdd()
{
$request = $this->container->get(tsRequest::class);
$request->scene('add')->validateResolved();
$data = Param::only(["group", "translation_key", "remark", "values"]);
return $this->toAjax(tsModel::add($data));
}
#[PutMapping(path: "translation/edit")]
#[Auth(auth: "translation:edit")]
public function translationEdit()
{
$request = $this->container->get(tsRequest::class);
$request->scene('edit')->validateResolved();
$data = Param::only(["translation_id", "group", "translation_key", "remark", "values"]);
return $this->toAjax(tsModel::edit($data));
}
#[DeleteMapping(path: "translation/del")]
#[Auth(auth: "translation:del")]
public function translationDel()
{
$ids = $this->request->input("ids");
return $this->toAjax(tsModel::del($ids));
}
}

View File

@ -53,8 +53,16 @@ class DictData extends Model
->paginate((int)$param['limit']);
}
public static function options()
public static function options($param)
{
return self::select(["data_id", "data_name"])->get()->toArray();
return (new self())->setTable("da")
->from("dict_data as da")
->join('dict as d', 'd.dict_id', '=', 'da.dict_id')
->where('d.dict_type', $param['key'])
->where('da.status', 1)
->select(["da.dict_value", "da.dict_label", "da.is_default"])
->orderByDesc("da.rank")
->get()
->toArray();
}
}

83
app/Model/Translation.php Normal file
View File

@ -0,0 +1,83 @@
<?php
declare(strict_types=1);
namespace App\Model;
use Hyperf\DbConnection\Db;
/**
* @property int $translation_id
* @property string $group
* @property string $translation_key
* @property string $remark
* @property string $create_time
* @property string $update_time
* @property string $deleted_at
*/
class Translation extends Model
{
/**
* The table associated with the model.
*/
protected ?string $table = 'translation';
protected string $primaryKey = "translation_id";
/**
* The attributes that are mass assignable.
*/
protected array $fillable = [];
/**
* The attributes that should be cast to native types.
*/
protected array $casts = ['translation_id' => 'integer'];
public static function list(array $param)
{
$model = self::query()->with("values");
if (isset($param['group']) && $param['group'] != '') {
$model = $model->where('group', $param['group']);
}
if (isset($param['translation_key']) && $param['translation_key'] != '') {
$model = $model->where('translation_key', $param['translation_key']);
}
return $model->orderByDesc("translation_id")
->select(["translation_id", "group", "translation_key", "remark", "create_time", "update_time"])
->paginate((int)$param['limit']);
}
public function values()
{
return $this->hasMany(TranslationValue::class, 'translation_id', 'translation_id');
}
public static function add($data): bool
{
$values = $data['values'];
unset($data['values']);
// 更新主表
$id = parent::add($data);
if (!$id) {
return false;
}
// 更新子表
TranslationValue::setValue($id, $values);
return true;
}
public static function edit($data): bool
{
$values = $data['values'];
unset($data['values']);
// 更新主表
$res = parent::edit($data);
if (!$res) {
return false;
}
// 更新子表
TranslationValue::setValue($data['translation_id'], $values);
return true;
}
}

View File

@ -0,0 +1,51 @@
<?php
declare(strict_types=1);
namespace App\Model;
/**
* @property int $value_id
* @property int $translation_id
* @property string $lang_code
* @property string $lang_value
* @property string $create_time
* @property string $update_time
* @property string $deleted_at
*/
class TranslationValue extends Model
{
/**
* The table associated with the model.
*/
protected ?string $table = 'translation_value';
protected string $primaryKey = "value_id";
/**
* The attributes that are mass assignable.
*/
protected array $fillable = [];
/**
* The attributes that should be cast to native types.
*/
protected array $casts = ['value_id' => 'integer', 'translation_id' => 'integer'];
public static function setValue(int $id, array $values): bool
{
foreach ($values as $value) {
$info = self::where("translation_id", $id)
->where("lang_code", $value['lang_code'])
->first();
if (empty($info)) {
$value['translation_id'] = $id;
self::add($value);
} else {
$info->lang_value = $value['lang_value'];
$info->save();
}
}
return true;
}
}

View File

@ -0,0 +1,47 @@
<?php
namespace App\Request;
use Hyperf\Validation\Request\FormRequest;
class Translation extends FormRequest
{
protected array $scenes = [
'add' => ["group", "translation_key"],
'edit' => ["translation_id", "group", "translation_key"],
];
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return true;
}
/**
* Get the validation rules that apply to the request.
*/
public function rules(): array
{
return [
'translation_id' => 'required',
'group' => 'required',
'translation_key' => 'required',
'remark' => 'required',
'create_time' => 'required',
'update_time' => 'required',
'deleted_at' => 'required',
];
}
public function messages(): array
{
return [
'translation_id.required' => 'ID必传',
'group.required' => '分组必传!',
'translation_key.required' => 'key必传',
'remark.required' => '描述必传!',
];
}
}