Prechádzať zdrojové kódy

【程序目录】更新多语言

吴昊天 3 rokov pred
rodič
commit
fe3240cc00

+ 82 - 0
crmeb/app/adminapi/controller/v1/setting/LangCode.php

@@ -0,0 +1,82 @@
+<?php
+
+namespace app\adminapi\controller\v1\setting;
+
+use app\adminapi\controller\AuthController;
+use app\services\system\lang\LangCodeServices;
+use think\facade\App;
+
+class LangCode extends AuthController
+{
+    /**
+     * @param App $app
+     * @param LangCodeServices $services
+     */
+    public function __construct(App $app, LangCodeServices $services)
+    {
+        parent::__construct($app);
+        $this->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);
+    }
+}

+ 63 - 0
crmeb/app/adminapi/controller/v1/setting/LangCountry.php

@@ -0,0 +1,63 @@
+<?php
+
+namespace app\adminapi\controller\v1\setting;
+
+use app\adminapi\controller\AuthController;
+use app\services\system\lang\LangCountryServices;
+use think\facade\App;
+
+class LangCountry extends AuthController
+{
+    /**
+     * @param App $app
+     * @param LangCountryServices $services
+     */
+    public function __construct(App $app, LangCountryServices $services)
+    {
+        parent::__construct($app);
+        $this->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);
+    }
+}

+ 73 - 0
crmeb/app/adminapi/controller/v1/setting/LangType.php

@@ -0,0 +1,73 @@
+<?php
+
+namespace app\adminapi\controller\v1\setting;
+
+use app\adminapi\controller\AuthController;
+use app\services\system\lang\LangCountryServices;
+use app\services\system\lang\LangTypeServices;
+use think\facade\App;
+
+class LangType extends AuthController
+{
+    /**
+     * @param App $app
+     * @param LangTypeServices $services
+     */
+    public function __construct(App $app, LangTypeServices $services)
+    {
+        parent::__construct($app);
+        $this->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);
+    }
+}

+ 55 - 0
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]);
+    }
 }

+ 18 - 0
crmeb/app/dao/system/lang/LangCodeDao.php

@@ -0,0 +1,18 @@
+<?php
+
+namespace app\dao\system\lang;
+
+use app\dao\BaseDao;
+use app\model\system\lang\LangCode;
+
+class LangCodeDao extends BaseDao
+{
+    /**
+     * 设置模型
+     * @return string
+     */
+    protected function setModel(): string
+    {
+        return LangCode::class;
+    }
+}

+ 18 - 0
crmeb/app/dao/system/lang/LangCountryDao.php

@@ -0,0 +1,18 @@
+<?php
+
+namespace app\dao\system\lang;
+
+use app\dao\BaseDao;
+use app\model\system\lang\LangCountry;
+
+class LangCountryDao extends BaseDao
+{
+    /**
+     * 设置模型
+     * @return string
+     */
+    protected function setModel(): string
+    {
+        return LangCountry::class;
+    }
+}

+ 18 - 0
crmeb/app/dao/system/lang/LangTypeDao.php

@@ -0,0 +1,18 @@
+<?php
+
+namespace app\dao\system\lang;
+
+use app\dao\BaseDao;
+use app\model\system\lang\LangType;
+
+class LangTypeDao extends BaseDao
+{
+    /**
+     * 设置模型
+     * @return string
+     */
+    protected function setModel(): string
+    {
+        return LangType::class;
+    }
+}

+ 63 - 0
crmeb/app/model/system/lang/LangCode.php

@@ -0,0 +1,63 @@
+<?php
+
+namespace app\model\system\lang;
+
+use crmeb\basic\BaseModel;
+use crmeb\traits\ModelTrait;
+
+class LangCode extends BaseModel
+{
+    use ModelTrait;
+
+    /**
+     * 数据表主键
+     * @var string
+     */
+    protected $pk = 'id';
+
+    /**
+     * 模型名称
+     * @var string
+     */
+    protected $name = 'lang_code';
+
+    /**
+     * type_id搜索器
+     * @param $query
+     * @param $value
+     */
+    public function searchTypeIdAttr($query, $value)
+    {
+        if ($value !== '' && $value !== 0) $query->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);
+    }
+}

+ 43 - 0
crmeb/app/model/system/lang/LangCountry.php

@@ -0,0 +1,43 @@
+<?php
+
+namespace app\model\system\lang;
+
+use crmeb\basic\BaseModel;
+use crmeb\traits\ModelTrait;
+
+class LangCountry extends BaseModel
+{
+    use ModelTrait;
+
+    /**
+     * 数据表主键
+     * @var string
+     */
+    protected $pk = 'id';
+
+    /**
+     * 模型名称
+     * @var string
+     */
+    protected $name = 'lang_country';
+
+    /**
+     * type_id搜索器
+     * @param $query
+     * @param $value
+     */
+    public function searchTypeIdAttr($query, $value)
+    {
+        if ($value !== '') $query->where('type_id', $value);
+    }
+
+    /**
+     * code/name搜索器
+     * @param $query
+     * @param $value
+     */
+    public function searchKeywordAttr($query, $value)
+    {
+        if ($value !== '') $query->where('name|code', 'like', '%' . $value . '%');
+    }
+}

+ 43 - 0
crmeb/app/model/system/lang/LangType.php

@@ -0,0 +1,43 @@
+<?php
+
+namespace app\model\system\lang;
+
+use crmeb\basic\BaseModel;
+use crmeb\traits\ModelTrait;
+
+class LangType extends BaseModel
+{
+    use ModelTrait;
+
+    /**
+     * 数据表主键
+     * @var string
+     */
+    protected $pk = 'id';
+
+    /**
+     * 模型名称
+     * @var string
+     */
+    protected $name = 'lang_type';
+
+    /**
+     * is_del搜索器
+     * @param $query
+     * @param $value
+     */
+    public function searchIsDelAttr($query, $value)
+    {
+        if ($value !== '') $query->where('is_del', $value);
+    }
+
+    /**
+     * status搜索器
+     * @param $query
+     * @param $value
+     */
+    public function searchStatusAttr($query, $value)
+    {
+        if ($value !== '') $query->where('status', $value);
+    }
+}

+ 109 - 0
crmeb/app/services/system/lang/LangCodeServices.php

@@ -0,0 +1,109 @@
+<?php
+
+namespace app\services\system\lang;
+
+use app\dao\system\lang\LangCodeDao;
+use app\services\BaseServices;
+use crmeb\exceptions\AdminException;
+
+class LangCodeServices extends BaseServices
+{
+    /**
+     * @param LangCodeDao $dao
+     */
+    public function __construct(LangCodeDao $dao)
+    {
+        $this->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);
+    }
+}

+ 74 - 0
crmeb/app/services/system/lang/LangCountryServices.php

@@ -0,0 +1,74 @@
+<?php
+
+namespace app\services\system\lang;
+
+use app\dao\system\lang\LangCountryDao;
+use app\services\BaseServices;
+use crmeb\exceptions\AdminException;
+use crmeb\services\FormBuilder as Form;
+use think\facade\Route as Url;
+
+class LangCountryServices extends BaseServices
+{
+    /**
+     * @param LangCountryDao $dao
+     */
+    public function __construct(LangCountryDao $dao)
+    {
+        $this->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;
+    }
+}

+ 102 - 0
crmeb/app/services/system/lang/LangTypeServices.php

@@ -0,0 +1,102 @@
+<?php
+
+namespace app\services\system\lang;
+
+use app\dao\system\lang\LangTypeDao;
+use app\services\BaseServices;
+use crmeb\exceptions\AdminException;
+use crmeb\services\FormBuilder as Form;
+use FormBuilder\Exception\FormBuilderException;
+use think\facade\Route as Url;
+
+class LangTypeServices extends BaseServices
+{
+    /**
+     * @param LangTypeDao $dao
+     */
+    public function __construct(LangTypeDao $dao)
+    {
+        $this->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;
+    }
+}