LangCountryServices.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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\CacheService;
  7. use crmeb\services\FormBuilder as Form;
  8. use think\facade\Route as Url;
  9. class LangCountryServices extends BaseServices
  10. {
  11. /**
  12. * @param LangCountryDao $dao
  13. */
  14. public function __construct(LangCountryDao $dao)
  15. {
  16. $this->dao = $dao;
  17. }
  18. /**
  19. * 地区语言列表
  20. * @param array $where
  21. * @return array
  22. * @throws \think\db\exception\DataNotFoundException
  23. * @throws \think\db\exception\DbException
  24. * @throws \think\db\exception\ModelNotFoundException
  25. */
  26. public function LangCountryList(array $where = []): array
  27. {
  28. [$page, $limit] = $this->getPageValue();
  29. $list = $this->dao->selectList($where, '*', $page, $limit, 'id desc', [], true)->toArray();
  30. /** @var LangTypeServices $langTypeServices */
  31. $langTypeServices = app()->make(LangTypeServices::class);
  32. $langTypeList = $langTypeServices->getColumn([], 'language_name,file_name,id', 'id');
  33. foreach ($list as &$item) {
  34. if (isset($langTypeList[$item['type_id']])) {
  35. $item['link_lang'] = $langTypeList[$item['type_id']]['language_name'] . '(' . $langTypeList[$item['type_id']]['file_name'] . ')';
  36. } else {
  37. $item['link_lang'] = '暂无';
  38. }
  39. }
  40. $count = $this->dao->count($where);
  41. return compact('list', 'count');
  42. }
  43. /**
  44. * 添加语言地区表单
  45. * @param $id
  46. * @return array
  47. * @throws \FormBuilder\Exception\FormBuilderException
  48. * @throws \think\db\exception\DataNotFoundException
  49. * @throws \think\db\exception\DbException
  50. * @throws \think\db\exception\ModelNotFoundException
  51. */
  52. public function langCountryForm($id)
  53. {
  54. if ($id) $info = $this->dao->get($id);
  55. $field = [];
  56. $field[] = Form::input('name', '所属地区', $info['name'] ?? '')->required('请填写所属地区')->appendRule('suffix', [
  57. 'type' => 'div',
  58. 'class' => 'tips-info',
  59. 'domProps' => ['innerHTML' => '例如:中国、香港、德国']
  60. ]);
  61. $field[] = Form::input('code', '语言识别码', $info['code'] ?? '')->required('请填写浏览器语言识别码')->appendRule('suffix', [
  62. 'type' => 'div',
  63. 'class' => 'tips-info',
  64. 'domProps' => ['innerHTML' => '浏览器语言识别码']
  65. ]);
  66. /** @var LangTypeServices $langTypeServices */
  67. $langTypeServices = app()->make(LangTypeServices::class);
  68. $list = $langTypeServices->getColumn(['is_del' => 0, 'status' => 1], 'language_name,file_name,id', 'id');
  69. $setOption = function () use ($list) {
  70. $menus = [];
  71. foreach ($list as $item) {
  72. $menus[] = ['value' => $item['id'], 'label' => $item['language_name'] . '(' . $item['file_name'] . ')'];
  73. }
  74. return $menus;
  75. };
  76. $field[] = Form::select('type_id', '关联语言', $info['type_id'] ?? 0)->setOptions(Form::setOptions($setOption))->filterable(true)->appendRule('suffix', [
  77. 'type' => 'div',
  78. 'class' => 'tips-info',
  79. 'domProps' => ['innerHTML' => '请选择关联语言,语言类型是由您自行添加的']
  80. ]);
  81. return create_form($id ? '修改语言地区' : '新增语言地区', $field, Url::buildUrl('/setting/lang_country/save/' . $id), 'POST');
  82. }
  83. /**
  84. * 保存语言地区
  85. * @param $id
  86. * @param $typeId
  87. * @return bool
  88. */
  89. public function LangCountrySave($id, $data)
  90. {
  91. if ($id) {
  92. $res = $this->dao->update(['id' => $id], $data);
  93. } else {
  94. $res = $this->dao->save($data);
  95. }
  96. if (!$res) throw new AdminException(100007);
  97. CacheService::clear();
  98. return true;
  99. }
  100. /**
  101. * 删除语言地区
  102. * @param $id
  103. * @return bool
  104. */
  105. public function langCountryDel($id)
  106. {
  107. $res = $this->dao->delete($id);
  108. if (!$res) throw new AdminException(100008);
  109. CacheService::clear();
  110. return true;
  111. }
  112. }