SystemConfigTabServices.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 app\services\system\SystemMenusServices;
  15. use crmeb\exceptions\AdminException;
  16. use crmeb\services\FormBuilder as Form;
  17. /**
  18. * 系统配置分类
  19. * Class SystemConfigTabServices
  20. * @package app\services\system\config
  21. * @method save(array $data) 写入数据
  22. * @method update($id, array $data, ?string $key = null) 修改数据
  23. * @method delete($id, ?string $key = null) 删除数据
  24. */
  25. class SystemConfigTabServices extends BaseServices
  26. {
  27. /**
  28. * SystemConfigTabServices constructor.
  29. * @param SystemConfigTabDao $dao
  30. */
  31. public function __construct(SystemConfigTabDao $dao)
  32. {
  33. $this->dao = $dao;
  34. }
  35. /**
  36. * 系统设置头部分类读取
  37. * @return array
  38. * @throws \think\db\exception\DataNotFoundException
  39. * @throws \think\db\exception\DbException
  40. * @throws \think\db\exception\ModelNotFoundException
  41. */
  42. public function getConfigTab(int $pid)
  43. {
  44. $list = $this->dao->getConfigTabAll(['status' => 1, 'pid' => $pid], ['id', 'id as value', 'title as label', 'pid', 'icon', 'type'], $pid ? [] : [['type', '=', '0']]);
  45. return get_tree_children($list);
  46. }
  47. /**
  48. * 获取配置分类列表
  49. * @param array $where
  50. * @return array
  51. * @throws \think\db\exception\DataNotFoundException
  52. * @throws \think\db\exception\DbException
  53. * @throws \think\db\exception\ModelNotFoundException
  54. */
  55. public function getConfgTabList(array $where)
  56. {
  57. [$page, $limit] = $this->getPageValue();
  58. $list = $this->dao->getConfgTabList($where, $page, $limit);
  59. $count = $this->dao->count($where);
  60. $menusValue = [];
  61. foreach ($list as $item) {
  62. $menusValue[] = $item->getData();
  63. }
  64. $list = get_tree_children($menusValue);
  65. $count = 0;
  66. return compact('list', 'count');
  67. }
  68. /**
  69. * 获取配置分类选择下拉树
  70. * @return array
  71. * @throws \think\db\exception\DataNotFoundException
  72. * @throws \think\db\exception\DbException
  73. * @throws \think\db\exception\ModelNotFoundException
  74. */
  75. public function getSelectForm()
  76. {
  77. $menuList = $this->dao->getConfigTabAll([], ['id', 'pid', 'title']);
  78. $list = sort_list_tier($menuList, 0, 'pid', 'id');
  79. $menus = [['value' => 0, 'label' => '顶级按钮']];
  80. foreach ($list as $menu) {
  81. $menus[] = ['value' => $menu['id'], 'label' => $menu['html'] . $menu['title']];
  82. }
  83. return $menus;
  84. }
  85. /**
  86. * 配置分类树形数据
  87. * @param $value
  88. * @return array
  89. * @throws \think\db\exception\DataNotFoundException
  90. * @throws \think\db\exception\DbException
  91. * @throws \think\db\exception\ModelNotFoundException
  92. * @author: 吴汐
  93. * @email: 442384644@qq.com
  94. * @date: 2023/9/12
  95. */
  96. public function getConfigTabListForm($value)
  97. {
  98. $configTabList = $this->dao->getConfigTabAll([], ['id as value', 'pid', 'title as label']);
  99. if ($value) {
  100. $data = get_tree_value($configTabList, $value);
  101. } else {
  102. $data = [0];
  103. }
  104. $configTabList = get_tree_children($configTabList, 'children', 'value');
  105. array_unshift($configTabList, ['value' => 0, 'pid' => 0, 'label' => '顶级分类']);
  106. return [$configTabList, array_reverse($data)];
  107. }
  108. /**
  109. * 创建form表单
  110. * @param array $formData
  111. * @return array
  112. * @throws \FormBuilder\Exception\FormBuilderException
  113. * @throws \think\db\exception\DataNotFoundException
  114. * @throws \think\db\exception\DbException
  115. * @throws \think\db\exception\ModelNotFoundException
  116. */
  117. public function createConfigTabForm(array $formData = [])
  118. {
  119. [$configTabList, $data1] = $this->getConfigTabListForm((int)($formData['pid'] ?? 0), 3);
  120. $form[] = Form::cascader('pid', '父级分类', $data1)->options($configTabList)->filterable(true)->props(['props' => ['multiple' => false, 'checkStrictly' => true, 'emitPath' => false]])->style(['width'=>'100%']);
  121. $form[] = Form::input('title', '分类名称', $formData['title'] ?? '');
  122. $form[] = Form::input('eng_title', '分类字段英文', $formData['eng_title'] ?? '');
  123. $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]);
  124. $form[] = Form::radio('type', '类型', $formData['type'] ?? 0)->options([
  125. ['value' => 0, 'label' => '系统'],
  126. ['value' => 3, 'label' => '其它']
  127. ]);
  128. [$menusList, $data2] = app()->make(SystemMenusServices::class)->getFormCascaderMenus((int)($formData['menus_id'] ?? 0));
  129. $form[] = Form::cascader('menus_id', '关联菜单', $data2)->options($menusList)->filterable(true)->props(['props' => ['multiple' => false, 'checkStrictly' => true, 'emitPath' => false]])->style(['width'=>'100%']);
  130. $form[] = Form::radio('status', '状态', $formData['status'] ?? 1)->options([['value' => 1, 'label' => '显示'], ['value' => 2, 'label' => '隐藏']]);
  131. $form[] = Form::number('sort', '排序', (int)($formData['sort'] ?? 0))->precision(0)->controls(false);
  132. return $form;
  133. }
  134. /**
  135. * 添加配置分类表单
  136. * @return array
  137. * @throws \FormBuilder\Exception\FormBuilderException
  138. */
  139. public function createForm()
  140. {
  141. return create_form('添加配置分类', $this->createConfigTabForm(), $this->url('/setting/config_class'));
  142. }
  143. /**
  144. * 修改配置分类表单
  145. * @param int $id
  146. * @return array
  147. * @throws \FormBuilder\Exception\FormBuilderException
  148. */
  149. public function updateForm(int $id)
  150. {
  151. $configTabInfo = $this->dao->get($id);
  152. if (!$configTabInfo) {
  153. throw new AdminException(100026);
  154. }
  155. return create_form('编辑配置分类', $this->createConfigTabForm($configTabInfo->toArray()), $this->url('/setting/config_class/' . $id), 'PUT');
  156. }
  157. }