From fe3240cc000ada89e73b9eda91543496ed965456 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=B4=E6=98=8A=E5=A4=A9?= <442384644@qq.com> Date: Thu, 15 Sep 2022 11:15:00 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E7=A8=8B=E5=BA=8F=E7=9B=AE=E5=BD=95?= =?UTF-8?q?=E3=80=91=E6=9B=B4=E6=96=B0=E5=A4=9A=E8=AF=AD=E8=A8=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/v1/setting/LangCode.php | 82 +++++++++++++ .../controller/v1/setting/LangCountry.php | 63 ++++++++++ .../controller/v1/setting/LangType.php | 73 ++++++++++++ .../api/controller/v1/PublicController.php | 55 +++++++++ crmeb/app/dao/system/lang/LangCodeDao.php | 18 +++ crmeb/app/dao/system/lang/LangCountryDao.php | 18 +++ crmeb/app/dao/system/lang/LangTypeDao.php | 18 +++ crmeb/app/model/system/lang/LangCode.php | 63 ++++++++++ crmeb/app/model/system/lang/LangCountry.php | 43 +++++++ crmeb/app/model/system/lang/LangType.php | 43 +++++++ .../services/system/lang/LangCodeServices.php | 109 ++++++++++++++++++ .../system/lang/LangCountryServices.php | 74 ++++++++++++ .../services/system/lang/LangTypeServices.php | 102 ++++++++++++++++ 13 files changed, 761 insertions(+) create mode 100644 crmeb/app/adminapi/controller/v1/setting/LangCode.php create mode 100644 crmeb/app/adminapi/controller/v1/setting/LangCountry.php create mode 100644 crmeb/app/adminapi/controller/v1/setting/LangType.php create mode 100644 crmeb/app/dao/system/lang/LangCodeDao.php create mode 100644 crmeb/app/dao/system/lang/LangCountryDao.php create mode 100644 crmeb/app/dao/system/lang/LangTypeDao.php create mode 100644 crmeb/app/model/system/lang/LangCode.php create mode 100644 crmeb/app/model/system/lang/LangCountry.php create mode 100644 crmeb/app/model/system/lang/LangType.php create mode 100644 crmeb/app/services/system/lang/LangCodeServices.php create mode 100644 crmeb/app/services/system/lang/LangCountryServices.php create mode 100644 crmeb/app/services/system/lang/LangTypeServices.php diff --git a/crmeb/app/adminapi/controller/v1/setting/LangCode.php b/crmeb/app/adminapi/controller/v1/setting/LangCode.php new file mode 100644 index 000000000..5cb7b12bd --- /dev/null +++ b/crmeb/app/adminapi/controller/v1/setting/LangCode.php @@ -0,0 +1,82 @@ +services = $services; + } + + /** + * 获取语言列表 + * @return mixed + * @throws \ReflectionException + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + */ + public function langCodeList() + { + $where = $this->request->getMore([ + ['is_admin', 0], + ['type_id', 0], + ['code', ''], + ['remarks', ''] + ]); + return app('json')->success($this->services->langCodeList($where)); + } + + /** + * 获取语言详情 + * @return mixed + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + */ + public function langCodeInfo() + { + [$code] = $this->request->getMore([ + ['code', ''], + ], true); + return app('json')->success($this->services->langCodeInfo($code)); + } + + /** + * 新增编辑语言 + * @return mixed + * @throws \Exception + */ + public function langCodeSave() + { + $data = $this->request->postMore([ + ['is_admin', 0], + ['code', ''], + ['remarks', ''], + ['list', []] + ]); + $this->services->langCodeSave($data); + return app('json')->success(100000); + } + + /** + * 删除语言 + * @param $id + * @return mixed + */ + public function langCodeDel($id) + { + $this->services->langCodeDel($id); + return app('json')->success(100002); + } +} \ No newline at end of file diff --git a/crmeb/app/adminapi/controller/v1/setting/LangCountry.php b/crmeb/app/adminapi/controller/v1/setting/LangCountry.php new file mode 100644 index 000000000..1235ddbdf --- /dev/null +++ b/crmeb/app/adminapi/controller/v1/setting/LangCountry.php @@ -0,0 +1,63 @@ +services = $services; + } + + /** + * 国家语言列表 + * @return mixed + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + */ + public function langCountryList() + { + $where = $this->request->getMore([ + ['keyword', ''], + ]); + return app('json')->success($this->services->langCountryList($where)); + } + + /** + * 设置国家语言类型表单 + * @param $id + * @return mixed + * @throws \FormBuilder\Exception\FormBuilderException + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + */ + public function langCountryTypeForm($id) + { + return app('json')->success($this->services->LangCountryTypeForm($id)); + } + + /** + * 国家语言修改 + * @param $id + * @return mixed + */ + public function langCountrySave($id) + { + [$typeId] = $this->request->postMore([ + ['type', 0], + ], true); + $this->services->langCountrySave($id, $typeId); + return app('json')->success(100000); + } +} \ No newline at end of file diff --git a/crmeb/app/adminapi/controller/v1/setting/LangType.php b/crmeb/app/adminapi/controller/v1/setting/LangType.php new file mode 100644 index 000000000..91067f671 --- /dev/null +++ b/crmeb/app/adminapi/controller/v1/setting/LangType.php @@ -0,0 +1,73 @@ +services = $services; + } + + /** + * 获取语言类型列表 + * @return mixed + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + */ + public function langTypeList() + { + $where['is_del'] = 0; + return app('json')->success($this->services->langTypeList($where)); + } + + /** + * 添加语言类型表单 + * @param int $id + * @return mixed + * @throws \FormBuilder\Exception\FormBuilderException + */ + public function langTypeForm(int $id = 0) + { + return app('json')->success($this->services->langTypeForm($id)); + } + + /** + * 保存语言类型 + * @return mixed + */ + public function langTypeSave() + { + $data = $this->request->postMore([ + ['id', 0], + ['language_name', ''], + ['file_name', ''], + ['is_default', 0], + ['status', 0] + ]); + $this->services->langTypeSave($data); + return app('json')->success(100000); + } + + /** + * 删除语言类型 + * @param int $id + * @return mixed + */ + public function langTypeDel(int $id = 0) + { + $this->services->langTypeDel($id); + return app('json')->success(100002); + } +} \ No newline at end of file diff --git a/crmeb/app/api/controller/v1/PublicController.php b/crmeb/app/api/controller/v1/PublicController.php index fd9cfe6d8..4642333e0 100644 --- a/crmeb/app/api/controller/v1/PublicController.php +++ b/crmeb/app/api/controller/v1/PublicController.php @@ -583,4 +583,59 @@ class PublicController } return app('json')->success(compact('copyrightContext', 'copyrightImage')); } + + /** + * 获取多语言类型列表 + * @return mixed + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + */ + public function getLangTypeList() + { + /** @var LangTypeServices $langTypeServices */ + $langTypeServices = app()->make(LangTypeServices::class); + $list = $langTypeServices->langTypeList(['status' => 1, 'is_del' => 0])['list']; + $data = []; + foreach ($list as $item) { + $data[] = ['name' => $item['language_name'], 'value' => $item['file_name']]; + } + return app('json')->success($data); + } + + /** + * 获取当前语言json + * @return mixed + * @throws \Throwable + */ + public function getLangJson() + { + $request = app()->request; + //获取接口传入的语言类型 + if (!$range = $request->header('cb-lang')) { + if ($request->header('accept-language') !== null) { + $range = explode(',', $request->header('accept-language'))[0]; + } else { + $range = 'zh-CN'; + } + } + // 获取type_id + /** @var LangCountryServices $langCountryServices */ + $langCountryServices = app()->make(LangCountryServices::class); + $typeId = $langCountryServices->value(['code' => $range], 'type_id') ?: 1; + + // 获取缓存key + /** @var LangTypeServices $langTypeServices */ + $langTypeServices = app()->make(LangTypeServices::class); + $langData = $langTypeServices->getColumn(['status' => 1, 'is_del' => 0], 'file_name', 'id'); + $langStr = 'api_lang_' . str_replace('-', '_', $langData[$typeId]); + + //读取当前语言的语言包 + $lang = CacheService::redisHandler()->remember($langStr, function () use ($typeId, $range) { + /** @var LangCodeServices $langCodeServices */ + $langCodeServices = app()->make(LangCodeServices::class); + return $langCodeServices->getColumn(['type_id' => $typeId, 'is_admin' => 0], 'lang_explain', 'code'); + }, 3600); + return app('json')->success([$range => $lang]); + } } diff --git a/crmeb/app/dao/system/lang/LangCodeDao.php b/crmeb/app/dao/system/lang/LangCodeDao.php new file mode 100644 index 000000000..f5253381f --- /dev/null +++ b/crmeb/app/dao/system/lang/LangCodeDao.php @@ -0,0 +1,18 @@ +where('type_id', $value); + } + + /** + * code搜索器 + * @param $query + * @param $value + */ + public function searchCodeAttr($query, $value) + { + if ($value !== '') $query->where('code', 'like', '%' . $value . '%'); + } + + /** + * remarks搜索器 + * @param $query + * @param $value + */ + public function searchRemarksAttr($query, $value) + { + if ($value !== '') $query->where('remarks', 'like', '%' . $value . '%'); + } + + /** + * is_admin搜索器 + * @param $query + * @param $value + */ + public function searchIsAdminAttr($query, $value) + { + if ($value !== '') $query->where('is_admin', $value); + } +} \ No newline at end of file diff --git a/crmeb/app/model/system/lang/LangCountry.php b/crmeb/app/model/system/lang/LangCountry.php new file mode 100644 index 000000000..5e8efe7a4 --- /dev/null +++ b/crmeb/app/model/system/lang/LangCountry.php @@ -0,0 +1,43 @@ +where('type_id', $value); + } + + /** + * code/name搜索器 + * @param $query + * @param $value + */ + public function searchKeywordAttr($query, $value) + { + if ($value !== '') $query->where('name|code', 'like', '%' . $value . '%'); + } +} \ No newline at end of file diff --git a/crmeb/app/model/system/lang/LangType.php b/crmeb/app/model/system/lang/LangType.php new file mode 100644 index 000000000..9ced3a49e --- /dev/null +++ b/crmeb/app/model/system/lang/LangType.php @@ -0,0 +1,43 @@ +where('is_del', $value); + } + + /** + * status搜索器 + * @param $query + * @param $value + */ + public function searchStatusAttr($query, $value) + { + if ($value !== '') $query->where('status', $value); + } +} \ No newline at end of file diff --git a/crmeb/app/services/system/lang/LangCodeServices.php b/crmeb/app/services/system/lang/LangCodeServices.php new file mode 100644 index 000000000..53f319b6e --- /dev/null +++ b/crmeb/app/services/system/lang/LangCodeServices.php @@ -0,0 +1,109 @@ +dao = $dao; + } + + /** + * 语言列表 + * @param array $where + * @return array + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + */ + public function langCodeList(array $where = []) + { + [$page, $limit] = $this->getPageValue(); + $list = $this->dao->selectList($where, '*', $page, $limit, 'id desc', true)->toArray(); + /** @var LangTypeServices $langTypeServices */ + $langTypeServices = app()->make(LangTypeServices::class); + $typeList = $langTypeServices->getColumn([['status', '=', 1], ['is_del', '=', 0]], 'language_name,file_name,id', 'id'); + $langType = [ + 'isAdmin' => [ + ['title' => '用户端', 'value' => 0], + ['title' => '管理端', 'value' => 1] + ] + ]; + foreach ($typeList as $value) { + $langType['langType'][] = ['title' => $value['language_name'] . '(' . $value['file_name'] . ')', 'value' => $value['id']]; + } + foreach ($list as &$item) { + $item['language_name'] = $typeList[$item['type_id']]['language_name'] . '(' . $typeList[$item['type_id']]['file_name'] . ')'; + } + $count = $this->dao->count($where); + return compact('list', 'count', 'langType'); + } + + /** + * 语言详情 + * @param $code + * @return array + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + */ + public function langCodeInfo($code) + { + $list = $this->dao->selectList(['code' => $code], '*', 0, 0, '', true)->toArray(); + if (!$list) throw new AdminException(100026); + /** @var LangTypeServices $langTypeServices */ + $langTypeServices = app()->make(LangTypeServices::class); + $typeList = $langTypeServices->getColumn([['status', '=', 1], ['is_del', '=', 0]], 'language_name,file_name,id', 'id'); + foreach ($list as &$item) { + $item['language_name'] = $typeList[$item['type_id']]['language_name'] . '(' . $typeList[$item['type_id']]['file_name'] . ')'; + } + $remarks = $list[0]['remarks']; + return compact('list', 'code', 'remarks'); + } + + /** + * 保存修改语言 + * @param $data + * @return bool + * @throws \Exception + */ + public function langCodeSave($data) + { + $saveData = []; + foreach ($data['list'] as $key => $item) { + $saveData[$key] = [ + 'code' => $data['code'], + 'remarks' => $data['remarks'], + 'lang_explain' => $item['lang_explain'], + 'type_id' => $item['type_id'], + 'is_admin' => $data['is_admin'], + ]; + if (isset($item['id']) && $item['id'] != 0) { + $saveData[$key]['id'] = $item['id']; + } + } + $this->dao->saveAll($saveData); + return true; + } + + /** + * 删除语言 + * @param $id + * @return bool + */ + public function langCodeDel($id) + { + $code = $this->dao->value(['id' => $id], 'code'); + $res = $this->dao->delete(['code' => $code]); + if($res) return true; + throw new AdminException(100008); + } +} \ No newline at end of file diff --git a/crmeb/app/services/system/lang/LangCountryServices.php b/crmeb/app/services/system/lang/LangCountryServices.php new file mode 100644 index 000000000..1cfece08c --- /dev/null +++ b/crmeb/app/services/system/lang/LangCountryServices.php @@ -0,0 +1,74 @@ +dao = $dao; + } + + /** + * 国家语言列表 + * @param array $where + * @return array + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + */ + public function LangCountryList(array $where = []): array + { + [$page, $limit] = $this->getPageValue(); + $list = $this->dao->selectList($where, '*', $page, $limit, 'id desc', true)->toArray(); + $count = $this->dao->count($where); + return compact('list', 'count'); + } + + /** + * 设置国家语言类型表单 + * @param $id + * @return array + * @throws \FormBuilder\Exception\FormBuilderException + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + */ + public function LangCountryTypeForm($id) + { + if (!$id) throw new AdminException(100100); + $info = $this->dao->get($id); + if (!$info) throw new AdminException(100026); + /** @var LangTypeServices $typeServices */ + $typeServices = app()->make(LangTypeServices::class); + $typeList = $typeServices->getColumn(['status' => 1, 'is_del' => 0], 'language_name,file_name', 'id'); + $options[] = ['value' => 0, 'label' => '未定义']; + foreach ($typeList as $item) { + $options[] = ['value' => $item['id'], 'label' => $item['language_name'] . '(' . $item['file_name'] . ')']; + } + $field[] = Form::select('type', '选择语言类型', $info->type_id)->setOptions($options)->filterable(1); + return create_form('设置语言类型', $field, Url::buildUrl('/setting/lang_country/save/' . $id), 'POST'); + } + + /** + * 国家语言修改 + * @param $id + * @param $typeId + * @return bool + */ + public function LangCountrySave($id, $typeId) + { + $res = $this->dao->update(['id' => $id], ['type_id' => $typeId]); + if (!$res) throw new AdminException(100007); + return true; + } +} \ No newline at end of file diff --git a/crmeb/app/services/system/lang/LangTypeServices.php b/crmeb/app/services/system/lang/LangTypeServices.php new file mode 100644 index 000000000..be28c9421 --- /dev/null +++ b/crmeb/app/services/system/lang/LangTypeServices.php @@ -0,0 +1,102 @@ +dao = $dao; + } + + /** + * 获取语言类型列表 + * @param array $where + * @return array + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + */ + public function langTypeList(array $where = []): array + { + [$page, $limit] = $this->getPageValue(); + $list = $this->dao->selectList($where, '*', $page, $limit); + $count = $this->dao->count($where); + return compact('list', 'count'); + } + + /** + * 添加语言类型表单 + * @param int $id + * @return array + * @throws FormBuilderException + */ + public function langTypeForm(int $id = 0): array + { + if ($id) $info = $this->dao->get($id); + $field = []; + $field[] = Form::input('language_name', '语言名称', $info['language_name'] ?? '')->required('请填写语言名称'); + $field[] = Form::input('file_name', '语言标识', $info['file_name'] ?? '')->required('请填写语言标识'); + $field[] = Form::radio('is_default', '是否默认', $info['is_default'] ?? 0)->options([['label' => '开启', 'value' => 1], ['label' => '关闭', 'value' => 0]]); + $field[] = Form::radio('status', '状态', $info['status'] ?? 1)->options([['label' => '开启', 'value' => 1], ['label' => '关闭', 'value' => 0]]); + return create_form($id ? '修改语言类型' : '新增语言类型', $field, Url::buildUrl('/setting/lang_type/save/' . $id), 'POST'); + } + + /** + * 保存语言类型 + * @param array $data + * @return bool + */ + public function langTypeSave(array $data) + { + if ($data['id']) { + $this->dao->update($data['id'], $data); + } else { + unset($data['id']); + $res = $this->dao->save($data); + if ($res) { + //同步语言 + /** @var LangCodeServices $codeServices */ + $codeServices = app()->make(LangCodeServices::class); + $list = $codeServices->selectList(['type_id' => 1])->toArray(); + foreach ($list as $key => $item) { + unset($list[$key]['id']); + $list[$key]['type_id'] = $res->id; + } + $codeServices->saveAll($list); + } else { + throw new AdminException(100006); + } + } + //设置默认 + if ($data['is_default'] == 1) $this->dao->update([['id', '<>', $res->id]], ['is_default' => 0]); + return true; + } + + /** + * 删除语言类型 + * @param int $id + * @return bool + */ + public function langTypeDel(int $id = 0) + { + $this->dao->update(['id' => $id], ['is_del' => 1]); + /** @var LangCountryServices $countryServices */ + $countryServices = app()->make(LangCountryServices::class); + $countryServices->update(['type_id' => $id], ['type_id' => 0]); + /** @var LangCodeServices $codeServices */ + $codeServices = app()->make(LangCodeServices::class); + $codeServices->delete(['type_id' => $id]); + return true; + } +} \ No newline at end of file