SystemConfigTabServices.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. $count = 0;
  65. return compact('list', 'count');
  66. }
  67. /**
  68. * 获取配置分类选择下拉树
  69. * @return array
  70. * @throws \think\db\exception\DataNotFoundException
  71. * @throws \think\db\exception\DbException
  72. * @throws \think\db\exception\ModelNotFoundException
  73. */
  74. public function getSelectForm()
  75. {
  76. $menuList = $this->dao->getConfigTabAll([], ['id', 'pid', 'title']);
  77. $list = sort_list_tier($menuList, 0, 'pid', 'id');
  78. $menus = [['value' => 0, 'label' => '顶级按钮']];
  79. foreach ($list as $menu) {
  80. $menus[] = ['value' => $menu['id'], 'label' => $menu['html'] . $menu['title']];
  81. }
  82. return $menus;
  83. }
  84. /**
  85. * 配置分类树形数据
  86. * @param $value
  87. * @return array
  88. * @throws \think\db\exception\DataNotFoundException
  89. * @throws \think\db\exception\DbException
  90. * @throws \think\db\exception\ModelNotFoundException
  91. * @author: 吴汐
  92. * @email: 442384644@qq.com
  93. * @date: 2023/9/12
  94. */
  95. public function getConfigTabListForm($value)
  96. {
  97. $configTabList = $this->dao->getConfigTabAll([], ['id as value', 'pid', 'title as label']);
  98. if ($value) {
  99. $data = get_tree_value($configTabList, $value);
  100. } else {
  101. $data = [0];
  102. }
  103. $configTabList = get_tree_children($configTabList, 'children', 'value');
  104. array_unshift($configTabList, ['value' => 0, 'pid' => 0, 'label' => '顶级分类']);
  105. return [$configTabList, array_reverse($data)];
  106. }
  107. /**
  108. * 创建form表单
  109. * @param array $formData
  110. * @return array
  111. * @throws \FormBuilder\Exception\FormBuilderException
  112. * @throws \think\db\exception\DataNotFoundException
  113. * @throws \think\db\exception\DbException
  114. * @throws \think\db\exception\ModelNotFoundException
  115. */
  116. public function createConfigTabForm(array $formData = [])
  117. {
  118. [$configTabList, $data] = $this->getConfigTabListForm((int)($formData['pid'] ?? 0), 3);
  119. $form[] = Form::cascader('pid', '父级分类', $data)->options($configTabList)->filterable(true)->props(['props' => ['multiple' => false, 'checkStrictly' => true, 'emitPath' => false]])->style(['width'=>'100%']);
  120. $form[] = Form::input('title', '分类名称', $formData['title'] ?? '');
  121. $form[] = Form::input('eng_title', '分类字段英文', $formData['eng_title'] ?? '');
  122. $form[] = Form::frameInput('icon', '图标', $this->url(config('app.admin_prefix', 'admin') . '/widget.widgets/icon', ['fodder' => 'icon'], true), $formData['icon'] ?? '')->icon('el-icon-picture-outline')->height('560px')->props(['footer' => false]);
  123. $form[] = Form::radio('type', '类型', $formData['type'] ?? 0)->options([
  124. ['value' => 0, 'label' => '系统'],
  125. ['value' => 3, 'label' => '其它']
  126. ]);
  127. $form[] = Form::radio('status', '状态', $formData['status'] ?? 1)->options([['value' => 1, 'label' => '显示'], ['value' => 2, 'label' => '隐藏']]);
  128. $form[] = Form::number('sort', '排序', (int)($formData['sort'] ?? 0))->precision(0)->controls(false);
  129. return $form;
  130. }
  131. /**
  132. * 添加配置分类表单
  133. * @return array
  134. * @throws \FormBuilder\Exception\FormBuilderException
  135. */
  136. public function createForm()
  137. {
  138. return create_form('添加配置分类', $this->createConfigTabForm(), $this->url('/setting/config_class'));
  139. }
  140. /**
  141. * 修改配置分类表单
  142. * @param int $id
  143. * @return array
  144. * @throws \FormBuilder\Exception\FormBuilderException
  145. */
  146. public function updateForm(int $id)
  147. {
  148. $configTabInfo = $this->dao->get($id);
  149. if (!$configTabInfo) {
  150. throw new AdminException(100026);
  151. }
  152. return create_form('编辑配置分类', $this->createConfigTabForm($configTabInfo->toArray()), $this->url('/setting/config_class/' . $id), 'PUT');
  153. }
  154. }