LangCodeServices.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. $saveData = [];
  76. foreach ($data['list'] as $key => $item) {
  77. $saveData[$key] = [
  78. 'code' => $data['code'],
  79. 'remarks' => $data['remarks'],
  80. 'lang_explain' => $item['lang_explain'],
  81. 'type_id' => $item['type_id'],
  82. 'is_admin' => $data['is_admin'],
  83. ];
  84. if (isset($item['id']) && $item['id'] != 0) {
  85. $saveData[$key]['id'] = $item['id'];
  86. }
  87. }
  88. $this->dao->saveAll($saveData);
  89. $this->clearLangCache();
  90. return true;
  91. }
  92. /**
  93. * 删除语言
  94. * @param $id
  95. * @return bool
  96. */
  97. public function langCodeDel($id)
  98. {
  99. $code = $this->dao->value(['id' => $id], 'code');
  100. $res = $this->dao->delete(['code' => $code]);
  101. $this->clearLangCache();
  102. if ($res) return true;
  103. throw new AdminException(100008);
  104. }
  105. /**
  106. * 清除语言缓存
  107. * @return bool
  108. */
  109. public function clearLangCache()
  110. {
  111. /** @var LangTypeServices $langTypeServices */
  112. $langTypeServices = app()->make(LangTypeServices::class);
  113. $typeList = $langTypeServices->getColumn(['status' => 1, 'is_del' => 0], 'file_name');
  114. foreach ($typeList as $value) {
  115. $langStr = 'api_lang_' . str_replace('-', '_', $value);
  116. CacheService::redisHandler()->delete($langStr);
  117. }
  118. return true;
  119. }
  120. }