SystemConfigTabServices.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 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\config;
  12. use app\dao\system\config\SystemConfigTabDao;
  13. use app\services\BaseServices;
  14. use crmeb\exceptions\AdminException;
  15. use crmeb\services\FormBuilder as Form;
  16. /**
  17. * 系统配置分类
  18. * Class SystemConfigTabServices
  19. * @package app\services\system\config
  20. * @method save(array $data) 写入数据
  21. * @method update($id, array $data, ?string $key = null) 修改数据
  22. * @method delete($id, ?string $key = null) 删除数据
  23. */
  24. class SystemConfigTabServices extends BaseServices
  25. {
  26. /**
  27. * SystemConfigTabServices constructor.
  28. * @param SystemConfigTabDao $dao
  29. */
  30. public function __construct(SystemConfigTabDao $dao)
  31. {
  32. $this->dao = $dao;
  33. }
  34. /**
  35. * 系统设置头部分类读取
  36. * @return array
  37. * @throws \think\db\exception\DataNotFoundException
  38. * @throws \think\db\exception\DbException
  39. * @throws \think\db\exception\ModelNotFoundException
  40. */
  41. public function getConfigTab(int $pid)
  42. {
  43. $list = $this->dao->getConfigTabAll(['status' => 1, 'pid' => $pid], ['id', 'id as value', 'title as label', 'pid', 'icon', 'type'], $pid ? [] : [['type', '=', '0']]);
  44. return get_tree_children($list);
  45. }
  46. /**
  47. * 获取配置分类列表
  48. * @param array $where
  49. * @return array
  50. * @throws \think\db\exception\DataNotFoundException
  51. * @throws \think\db\exception\DbException
  52. * @throws \think\db\exception\ModelNotFoundException
  53. */
  54. public function getConfgTabList(array $where)
  55. {
  56. [$page, $limit] = $this->getPageValue();
  57. $list = $this->dao->getConfgTabList($where, $page, $limit);
  58. $count = $this->dao->count($where);
  59. $menusValue = [];
  60. foreach ($list as $item) {
  61. $menusValue[] = $item->getData();
  62. }
  63. $list = get_tree_children($menusValue);
  64. return compact('list', 'count');
  65. }
  66. /**
  67. * 获取配置分类选择下拉树
  68. * @return array
  69. * @throws \think\db\exception\DataNotFoundException
  70. * @throws \think\db\exception\DbException
  71. * @throws \think\db\exception\ModelNotFoundException
  72. */
  73. public function getSelectForm()
  74. {
  75. $menuList = $this->dao->getConfigTabAll([], ['id', 'pid', 'title']);
  76. $list = sort_list_tier($menuList, 0, 'pid', 'id');
  77. $menus = [['value' => 0, 'label' => '顶级按钮']];
  78. foreach ($list as $menu) {
  79. $menus[] = ['value' => $menu['id'], 'label' => $menu['html'] . $menu['title']];
  80. }
  81. return $menus;
  82. }
  83. /**
  84. * 创建form表单
  85. * @param array $formData
  86. * @return array
  87. * @throws \FormBuilder\Exception\FormBuilderException
  88. * @throws \think\db\exception\DataNotFoundException
  89. * @throws \think\db\exception\DbException
  90. * @throws \think\db\exception\ModelNotFoundException
  91. */
  92. public function createConfigTabForm(array $formData = [])
  93. {
  94. $form[] = Form::select('pid', '父级分类', isset($formData['pid']) ? (string)$formData['pid'] : '')->setOptions($this->getSelectForm())->filterable(true);
  95. $form[] = Form::input('title', '分类名称', $formData['title'] ?? '');
  96. $form[] = Form::input('eng_title', '分类字段英文', $formData['eng_title'] ?? '');
  97. $form[] = Form::frameInput('icon', '图标', $this->url(config('app.admin_prefix', 'admin') . '/widget.widgets/icon', ['fodder' => 'icon'], true), $formData['icon'] ?? '')->icon('ios-ionic')->height('505px')->modal(['footer-hide' => true]);
  98. $form[] = Form::radio('type', '类型', $formData['type'] ?? 0)->options([
  99. ['value' => 0, 'label' => '系统'],
  100. ['value' => 3, 'label' => '其它']
  101. ]);
  102. $form[] = Form::radio('status', '状态', $formData['status'] ?? 1)->options([['value' => 1, 'label' => '显示'], ['value' => 2, 'label' => '隐藏']]);
  103. $form[] = Form::number('sort', '排序', (int)($formData['sort'] ?? 0))->precision(0);
  104. return $form;
  105. }
  106. /**
  107. * 添加配置分类表单
  108. * @return array
  109. * @throws \FormBuilder\Exception\FormBuilderException
  110. */
  111. public function createForm()
  112. {
  113. return create_form('添加配置分类', $this->createConfigTabForm(), $this->url('/setting/config_class'));
  114. }
  115. /**
  116. * 修改配置分类表单
  117. * @param int $id
  118. * @return array
  119. * @throws \FormBuilder\Exception\FormBuilderException
  120. */
  121. public function updateForm(int $id)
  122. {
  123. $configTabInfo = $this->dao->get($id);
  124. if (!$configTabInfo) {
  125. throw new AdminException(100026);
  126. }
  127. return create_form('编辑配置分类', $this->createConfigTabForm($configTabInfo->toArray()), $this->url('/setting/config_class/' . $id), 'PUT');
  128. }
  129. }