From 1213f86045fcf99f79ced691bddb86d7acf99740 Mon Sep 17 00:00:00 2001 From: zhang zhuo Date: Thu, 4 Dec 2025 14:46:31 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AF=AD=E8=A8=80=E8=AF=8D=E5=85=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Controller/Admin/System.php | 41 +++++++++++++++- app/Model/DictData.php | 12 ++++- app/Model/Translation.php | 83 +++++++++++++++++++++++++++++++++ app/Model/TranslationValue.php | 51 ++++++++++++++++++++ app/Request/Translation.php | 47 +++++++++++++++++++ 5 files changed, 231 insertions(+), 3 deletions(-) create mode 100644 app/Model/Translation.php create mode 100644 app/Model/TranslationValue.php create mode 100644 app/Request/Translation.php diff --git a/app/Controller/Admin/System.php b/app/Controller/Admin/System.php index 2d6df1a..4e978a9 100644 --- a/app/Controller/Admin/System.php +++ b/app/Controller/Admin/System.php @@ -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)); + } } \ No newline at end of file diff --git a/app/Model/DictData.php b/app/Model/DictData.php index 5249abb..05e15d6 100644 --- a/app/Model/DictData.php +++ b/app/Model/DictData.php @@ -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(); } } diff --git a/app/Model/Translation.php b/app/Model/Translation.php new file mode 100644 index 0000000..aaa3b98 --- /dev/null +++ b/app/Model/Translation.php @@ -0,0 +1,83 @@ + '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; + } +} diff --git a/app/Model/TranslationValue.php b/app/Model/TranslationValue.php new file mode 100644 index 0000000..7cc319c --- /dev/null +++ b/app/Model/TranslationValue.php @@ -0,0 +1,51 @@ + '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; + } +} diff --git a/app/Request/Translation.php b/app/Request/Translation.php new file mode 100644 index 0000000..2dc545c --- /dev/null +++ b/app/Request/Translation.php @@ -0,0 +1,47 @@ + ["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' => '描述必传!', + ]; + } +}