LangCodeServices.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. class LangCodeServices extends BaseServices
  8. {
  9. /**
  10. * @param LangCodeDao $dao
  11. */
  12. public function __construct(LangCodeDao $dao)
  13. {
  14. $this->dao = $dao;
  15. }
  16. /**
  17. * 语言列表
  18. * @param array $where
  19. * @return array
  20. * @throws \think\db\exception\DataNotFoundException
  21. * @throws \think\db\exception\DbException
  22. * @throws \think\db\exception\ModelNotFoundException
  23. */
  24. public function langCodeList(array $where = [])
  25. {
  26. [$page, $limit] = $this->getPageValue();
  27. $list = $this->dao->selectList($where, '*', $page, $limit, 'id desc', true)->toArray();
  28. /** @var LangTypeServices $langTypeServices */
  29. $langTypeServices = app()->make(LangTypeServices::class);
  30. $typeList = $langTypeServices->getColumn([['status', '=', 1], ['is_del', '=', 0]], 'language_name,file_name,id', 'id');
  31. $langType = [
  32. 'isAdmin' => [
  33. ['title' => '用户端', 'value' => 0],
  34. ['title' => '管理端', 'value' => 1]
  35. ]
  36. ];
  37. foreach ($typeList as $value) {
  38. $langType['langType'][] = ['title' => $value['language_name'] . '(' . $value['file_name'] . ')', 'value' => $value['id']];
  39. }
  40. foreach ($list as &$item) {
  41. $item['language_name'] = $typeList[$item['type_id']]['language_name'] . '(' . $typeList[$item['type_id']]['file_name'] . ')';
  42. }
  43. $count = $this->dao->count($where);
  44. return compact('list', 'count', 'langType');
  45. }
  46. /**
  47. * 语言详情
  48. * @param $code
  49. * @return array
  50. * @throws \think\db\exception\DataNotFoundException
  51. * @throws \think\db\exception\DbException
  52. * @throws \think\db\exception\ModelNotFoundException
  53. */
  54. public function langCodeInfo($code)
  55. {
  56. if (!$code) throw new AdminException(100026);
  57. /** @var LangTypeServices $langTypeServices */
  58. $langTypeServices = app()->make(LangTypeServices::class);
  59. $typeList = $langTypeServices->getColumn([['status', '=', 1], ['is_del', '=', 0]], 'language_name,file_name,id', 'id');
  60. $list = $this->dao->selectList([['code', '=', $code], ['type_id', 'in', array_column($typeList, 'id')]])->toArray();
  61. foreach ($list as &$item) {
  62. $item['language_name'] = $typeList[$item['type_id']]['language_name'] . '(' . $typeList[$item['type_id']]['file_name'] . ')';
  63. }
  64. $remarks = $list[0]['remarks'];
  65. return compact('list', 'code', 'remarks');
  66. }
  67. /**
  68. * 保存修改语言
  69. * @param $data
  70. * @return bool
  71. * @throws \Exception
  72. */
  73. public function langCodeSave($data)
  74. {
  75. if ($data['edit'] == 0) {
  76. if ($data['is_admin'] == 1) {
  77. $code = $this->dao->getMax(['is_admin' => 1], 'code');
  78. if ($code < 500000) {
  79. $code = 500000;
  80. } else {
  81. $code = $code + 1;
  82. }
  83. } else {
  84. $code = $data['remarks'];
  85. }
  86. } else {
  87. $code = $data['code'];
  88. }
  89. $saveData = [];
  90. foreach ($data['list'] as $key => $item) {
  91. $saveData[$key] = [
  92. 'code' => $code,
  93. 'remarks' => $data['remarks'],
  94. 'lang_explain' => $item['lang_explain'],
  95. 'type_id' => $item['type_id'],
  96. 'is_admin' => $data['is_admin'],
  97. ];
  98. if (isset($item['id']) && $item['id'] != 0) {
  99. $saveData[$key]['id'] = $item['id'];
  100. }
  101. }
  102. $this->dao->saveAll($saveData);
  103. $this->clearLangCache();
  104. return true;
  105. }
  106. /**
  107. * 删除语言
  108. * @param $id
  109. * @return bool
  110. */
  111. public function langCodeDel($id)
  112. {
  113. $code = $this->dao->value(['id' => $id], 'code');
  114. $res = $this->dao->delete(['code' => $code]);
  115. $this->clearLangCache();
  116. if ($res) return true;
  117. throw new AdminException(100008);
  118. }
  119. /**
  120. * 清除语言缓存
  121. * @return bool
  122. */
  123. public function clearLangCache()
  124. {
  125. /** @var LangTypeServices $langTypeServices */
  126. $langTypeServices = app()->make(LangTypeServices::class);
  127. $typeList = $langTypeServices->getColumn(['status' => 1, 'is_del' => 0], 'file_name');
  128. foreach ($typeList as $value) {
  129. $langStr = 'api_lang_' . str_replace('-', '_', $value);
  130. CacheService::redisHandler()->delete($langStr);
  131. }
  132. return true;
  133. }
  134. }