LangCodeServices.php 6.6 KB

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