LangCountryServices.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. namespace app\services\system\lang;
  3. use app\dao\system\lang\LangCountryDao;
  4. use app\services\BaseServices;
  5. use crmeb\exceptions\AdminException;
  6. use crmeb\services\FormBuilder as Form;
  7. use think\facade\Route as Url;
  8. class LangCountryServices extends BaseServices
  9. {
  10. /**
  11. * @param LangCountryDao $dao
  12. */
  13. public function __construct(LangCountryDao $dao)
  14. {
  15. $this->dao = $dao;
  16. }
  17. /**
  18. * 地区语言列表
  19. * @param array $where
  20. * @return array
  21. * @throws \think\db\exception\DataNotFoundException
  22. * @throws \think\db\exception\DbException
  23. * @throws \think\db\exception\ModelNotFoundException
  24. */
  25. public function LangCountryList(array $where = []): array
  26. {
  27. [$page, $limit] = $this->getPageValue();
  28. $list = $this->dao->selectList($where, '*', $page, $limit, 'id desc', true)->toArray();
  29. /** @var LangTypeServices $langTypeServices */
  30. $langTypeServices = app()->make(LangTypeServices::class);
  31. $langTypeList = $langTypeServices->getColumn([], 'language_name,file_name,id', 'id');
  32. foreach ($list as &$item) {
  33. if (isset($langTypeList[$item['type_id']])) {
  34. $item['link_lang'] = $langTypeList[$item['type_id']]['language_name'] . '(' . $langTypeList[$item['type_id']]['file_name'] . ')';
  35. } else {
  36. $item['link_lang'] = '';
  37. }
  38. }
  39. $count = $this->dao->count($where);
  40. return compact('list', 'count');
  41. }
  42. /**
  43. * 添加语言地区表单
  44. * @param $id
  45. * @return array
  46. * @throws \FormBuilder\Exception\FormBuilderException
  47. * @throws \think\db\exception\DataNotFoundException
  48. * @throws \think\db\exception\DbException
  49. * @throws \think\db\exception\ModelNotFoundException
  50. */
  51. public function langCountryForm($id)
  52. {
  53. if ($id) $info = $this->dao->get($id);
  54. $field = [];
  55. $field[] = Form::input('name', '所属地区', $info['name'] ?? '')->required('请填写所属地区');
  56. $field[] = Form::input('code', '语言码', $info['code'] ?? '')->required('请填写语言码');
  57. /** @var LangTypeServices $langTypeServices */
  58. $langTypeServices = app()->make(LangTypeServices::class);
  59. $list = $langTypeServices->getColumn(['is_del' => 0, 'status' => 1], 'language_name,file_name,id', 'id');
  60. $setOption = function () use ($list) {
  61. $menus = [];
  62. foreach ($list as $item) {
  63. $menus[] = ['value' => $item['id'], 'label' => $item['language_name'] . '(' . $item['file_name'] . ')'];
  64. }
  65. return $menus;
  66. };
  67. $field[] = Form::select('type_id', '语言类型', $info['type_id'] ?? 0)->setOptions(Form::setOptions($setOption))->filterable(true);
  68. return create_form($id ? '修改语言地区' : '新增语言地区', $field, Url::buildUrl('/setting/lang_country/save/' . $id), 'POST');
  69. }
  70. /**
  71. * 保存语言地区
  72. * @param $id
  73. * @param $typeId
  74. * @return bool
  75. */
  76. public function LangCountrySave($id, $data)
  77. {
  78. if ($id) {
  79. $res = $this->dao->update(['id' => $id], $data);
  80. } else {
  81. $res = $this->dao->save($data);
  82. }
  83. if (!$res) throw new AdminException(100007);
  84. return true;
  85. }
  86. /**
  87. * 删除语言地区
  88. * @param $id
  89. * @return bool
  90. */
  91. public function langCountryDel($id)
  92. {
  93. $res = $this->dao->delete($id);
  94. if (!$res) throw new AdminException(100008);
  95. return true;
  96. }
  97. }