LangCodeServices.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. class LangCodeServices extends BaseServices
  7. {
  8. /**
  9. * @param LangCodeDao $dao
  10. */
  11. public function __construct(LangCodeDao $dao)
  12. {
  13. $this->dao = $dao;
  14. }
  15. /**
  16. * 语言列表
  17. * @param array $where
  18. * @return array
  19. * @throws \think\db\exception\DataNotFoundException
  20. * @throws \think\db\exception\DbException
  21. * @throws \think\db\exception\ModelNotFoundException
  22. */
  23. public function langCodeList(array $where = [])
  24. {
  25. [$page, $limit] = $this->getPageValue();
  26. $list = $this->dao->selectList($where, '*', $page, $limit, 'id desc', true)->toArray();
  27. /** @var LangTypeServices $langTypeServices */
  28. $langTypeServices = app()->make(LangTypeServices::class);
  29. $typeList = $langTypeServices->getColumn([['status', '=', 1], ['is_del', '=', 0]], 'language_name,file_name,id', 'id');
  30. $langType = [
  31. 'isAdmin' => [
  32. ['title' => '用户端', 'value' => 0],
  33. ['title' => '管理端', 'value' => 1]
  34. ]
  35. ];
  36. foreach ($typeList as $value) {
  37. $langType['langType'][] = ['title' => $value['language_name'] . '(' . $value['file_name'] . ')', 'value' => $value['id']];
  38. }
  39. foreach ($list as &$item) {
  40. $item['language_name'] = $typeList[$item['type_id']]['language_name'] . '(' . $typeList[$item['type_id']]['file_name'] . ')';
  41. }
  42. $count = $this->dao->count($where);
  43. return compact('list', 'count', 'langType');
  44. }
  45. /**
  46. * 语言详情
  47. * @param $code
  48. * @return array
  49. * @throws \think\db\exception\DataNotFoundException
  50. * @throws \think\db\exception\DbException
  51. * @throws \think\db\exception\ModelNotFoundException
  52. */
  53. public function langCodeInfo($code)
  54. {
  55. $list = $this->dao->selectList(['code' => $code], '*', 0, 0, '', true)->toArray();
  56. if (!$list) 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. foreach ($list as &$item) {
  61. $item['language_name'] = $typeList[$item['type_id']]['language_name'] . '(' . $typeList[$item['type_id']]['file_name'] . ')';
  62. }
  63. $remarks = $list[0]['remarks'];
  64. return compact('list', 'code', 'remarks');
  65. }
  66. /**
  67. * 保存修改语言
  68. * @param $data
  69. * @return bool
  70. * @throws \Exception
  71. */
  72. public function langCodeSave($data)
  73. {
  74. $saveData = [];
  75. foreach ($data['list'] as $key => $item) {
  76. $saveData[$key] = [
  77. 'code' => $data['code'],
  78. 'remarks' => $data['remarks'],
  79. 'lang_explain' => $item['lang_explain'],
  80. 'type_id' => $item['type_id'],
  81. 'is_admin' => $data['is_admin'],
  82. ];
  83. if (isset($item['id']) && $item['id'] != 0) {
  84. $saveData[$key]['id'] = $item['id'];
  85. }
  86. }
  87. $this->dao->saveAll($saveData);
  88. return true;
  89. }
  90. /**
  91. * 删除语言
  92. * @param $id
  93. * @return bool
  94. */
  95. public function langCodeDel($id)
  96. {
  97. $code = $this->dao->value(['id' => $id], 'code');
  98. $res = $this->dao->delete(['code' => $code]);
  99. if($res) return true;
  100. throw new AdminException(100008);
  101. }
  102. }