LangCodeServices.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. namespace app\services\system\lang;
  3. use app\dao\system\lang\LangCodeDao;
  4. use app\services\BaseServices;
  5. use crmeb\exceptions\AdminException;
  6. use crmeb\services\CacheService;
  7. use crmeb\utils\Translate;
  8. class LangCodeServices extends BaseServices
  9. {
  10. /**
  11. * @param LangCodeDao $dao
  12. */
  13. public function __construct(LangCodeDao $dao)
  14. {
  15. $this->dao = $dao;
  16. }
  17. /**
  18. * 语言列表
  19. * @param array $where
  20. * @return array
  21. * @throws \think\db\exception\DataNotFoundException
  22. * @throws \think\db\exception\DbException
  23. * @throws \think\db\exception\ModelNotFoundException
  24. */
  25. public function langCodeList(array $where = [])
  26. {
  27. [$page, $limit] = $this->getPageValue();
  28. $list = $this->dao->selectList($where, '*', $page, $limit, 'id desc', [], true)->toArray();
  29. /** @var LangTypeServices $langTypeServices */
  30. $langTypeServices = app()->make(LangTypeServices::class);
  31. $typeList = $langTypeServices->getColumn([['status', '=', 1], ['is_del', '=', 0]], 'language_name,file_name,id', 'id');
  32. $langType = [
  33. 'isAdmin' => [
  34. ['title' => '页面语言', 'value' => 0],
  35. ['title' => '接口语言', 'value' => 1]
  36. ]
  37. ];
  38. foreach ($typeList as $value) {
  39. $langType['langType'][] = ['title' => $value['language_name'] . '(' . $value['file_name'] . ')', 'value' => $value['id']];
  40. }
  41. foreach ($list as &$item) {
  42. $item['language_name'] = $typeList[$item['type_id']]['language_name'] . '(' . $typeList[$item['type_id']]['file_name'] . ')';
  43. }
  44. $count = $this->dao->count($where);
  45. return compact('list', 'count', 'langType');
  46. }
  47. /**
  48. * 语言详情
  49. * @param $code
  50. * @return array
  51. * @throws \think\db\exception\DataNotFoundException
  52. * @throws \think\db\exception\DbException
  53. * @throws \think\db\exception\ModelNotFoundException
  54. */
  55. public function langCodeInfo($code)
  56. {
  57. if (!$code) throw new AdminException(100026);
  58. /** @var LangTypeServices $langTypeServices */
  59. $langTypeServices = app()->make(LangTypeServices::class);
  60. $typeList = $langTypeServices->getColumn([['status', '=', 1], ['is_del', '=', 0]], 'language_name,file_name,id', 'id');
  61. $list = $this->dao->selectList([['code', '=', $code], ['type_id', 'in', array_column($typeList, 'id')]])->toArray();
  62. foreach ($list as &$item) {
  63. $item['language_name'] = $typeList[$item['type_id']]['language_name'] . '(' . $typeList[$item['type_id']]['file_name'] . ')';
  64. }
  65. $remarks = $list[0]['remarks'];
  66. return compact('list', 'code', 'remarks');
  67. }
  68. /**
  69. * 保存修改语言
  70. * @param $data
  71. * @return bool
  72. * @throws \Exception
  73. */
  74. public function langCodeSave($data)
  75. {
  76. if ($data['edit'] == 0) {
  77. if ($data['is_admin'] == 1) {
  78. $code = $this->dao->getMax(['is_admin' => 1], 'code');
  79. if ($code < 500000) {
  80. $code = 500000;
  81. } else {
  82. $code = $code + 1;
  83. }
  84. } else {
  85. $code = $data['remarks'];
  86. }
  87. } else {
  88. $code = $data['code'];
  89. }
  90. $saveData = [];
  91. foreach ($data['list'] as $key => $item) {
  92. $saveData[$key] = [
  93. 'code' => $code,
  94. 'remarks' => $data['remarks'],
  95. 'lang_explain' => $item['lang_explain'],
  96. 'type_id' => $item['type_id'],
  97. 'is_admin' => $data['is_admin'],
  98. ];
  99. if (isset($item['id']) && $item['id'] != 0) {
  100. $saveData[$key]['id'] = $item['id'];
  101. }
  102. }
  103. $this->dao->saveAll($saveData);
  104. $this->clearLangCache();
  105. return true;
  106. }
  107. /**
  108. * 删除语言
  109. * @param $id
  110. * @return bool
  111. */
  112. public function langCodeDel($id)
  113. {
  114. $code = $this->dao->value(['id' => $id], 'code');
  115. $res = $this->dao->delete(['code' => $code]);
  116. $this->clearLangCache();
  117. if ($res) return true;
  118. throw new AdminException(100008);
  119. }
  120. /**
  121. * 清除语言缓存
  122. * @return bool
  123. */
  124. public function clearLangCache()
  125. {
  126. /** @var LangTypeServices $langTypeServices */
  127. $langTypeServices = app()->make(LangTypeServices::class);
  128. $typeList = $langTypeServices->getColumn(['status' => 1, 'is_del' => 0], 'file_name');
  129. foreach ($typeList as $value) {
  130. $langStr = 'api_lang_' . str_replace('-', '_', $value);
  131. CacheService::delete($langStr);
  132. }
  133. CacheService::clear();
  134. return true;
  135. }
  136. /**
  137. * 机器翻译
  138. * @param string $text
  139. * @return array
  140. * @throws \Throwable
  141. */
  142. public function langCodeTranslate(string $text = ''): array
  143. {
  144. if (sys_config('hs_accesskey') == '' || sys_config('hs_secretkey') == '') {
  145. throw new AdminException('请先配置火山翻译key');
  146. }
  147. $translator = Translate::getInstance();
  148. $translator->setAccessKey(sys_config('hs_accesskey'));
  149. $translator->setSecretKey(sys_config('hs_secretkey'));
  150. /** @var LangTypeServices $langTypeServices */
  151. $langTypeServices = app()->make(LangTypeServices::class);
  152. $typeList = $langTypeServices->getColumn([['status', '=', 1], ['is_del', '=', 0]], 'language_name,file_name,id', 'id');
  153. $data = [];
  154. foreach ($typeList as $item) {
  155. if ($item['file_name'] == 'zh-Hant') {
  156. $lang = 'zh-Hant';
  157. } else {
  158. $lang = substr($item['file_name'], 0, 2);
  159. }
  160. $data[$item['id']] = $translator->translateText("", $lang, array($text))[0]['Translation'];
  161. }
  162. return $data;
  163. }
  164. /**
  165. * 获取多语言缓存
  166. * @return mixed
  167. * @author 吴汐
  168. * @email 442384644@qq.com
  169. * @date 2023/03/06
  170. */
  171. public function getLangVersion()
  172. {
  173. return CacheService::remember('lang_version', function () {
  174. return [
  175. 'version' => uniqid()
  176. ];
  177. });
  178. }
  179. }