LangCountryServices.php 4.8 KB

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