LangCountryServices.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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('请填写所属地区')->appendRule('suffix', [
  56. 'type' => 'div',
  57. 'class' => 'tips-info',
  58. 'domProps' => ['innerHTML' => '例如:中国、香港、德国']
  59. ]);
  60. $field[] = Form::input('code', '语言识别码', $info['code'] ?? '')->required('请填写浏览器语言识别码')->appendRule('suffix', [
  61. 'type' => 'div',
  62. 'class' => 'tips-info',
  63. 'domProps' => ['innerHTML' => '浏览器语言识别码']
  64. ]);
  65. /** @var LangTypeServices $langTypeServices */
  66. $langTypeServices = app()->make(LangTypeServices::class);
  67. $list = $langTypeServices->getColumn(['is_del' => 0, 'status' => 1], 'language_name,file_name,id', 'id');
  68. $setOption = function () use ($list) {
  69. $menus = [];
  70. foreach ($list as $item) {
  71. $menus[] = ['value' => $item['id'], 'label' => $item['language_name'] . '(' . $item['file_name'] . ')'];
  72. }
  73. return $menus;
  74. };
  75. $field[] = Form::select('type_id', '关联语言', $info['type_id'] ?? 0)->setOptions(Form::setOptions($setOption))->filterable(true)->appendRule('suffix', [
  76. 'type' => 'div',
  77. 'class' => 'tips-info',
  78. 'domProps' => ['innerHTML' => '请选择关联语言,语言类型是由您自行添加的']
  79. ]);
  80. return create_form($id ? '修改语言地区' : '新增语言地区', $field, Url::buildUrl('/setting/lang_country/save/' . $id), 'POST');
  81. }
  82. /**
  83. * 保存语言地区
  84. * @param $id
  85. * @param $typeId
  86. * @return bool
  87. */
  88. public function LangCountrySave($id, $data)
  89. {
  90. if ($id) {
  91. $res = $this->dao->update(['id' => $id], $data);
  92. } else {
  93. $res = $this->dao->save($data);
  94. }
  95. if (!$res) throw new AdminException(100007);
  96. $this->cacheDriver()->clear();
  97. return true;
  98. }
  99. /**
  100. * 删除语言地区
  101. * @param $id
  102. * @return bool
  103. */
  104. public function langCountryDel($id)
  105. {
  106. $res = $this->dao->delete($id);
  107. if (!$res) throw new AdminException(100008);
  108. $this->cacheDriver()->clear();
  109. return true;
  110. }
  111. }