LangCodeServices.php 7.2 KB

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