ConfigClass.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. namespace app\admin\controller\base\config;
  3. /**
  4. * @title: 系统设置分类
  5. */
  6. use app\admin\controller\Base;
  7. use app\admin\validate\base\config\ConfigClass as ConfigGroupValidate;
  8. use app\common\model\base\config\ConfigClass as configClassModel;
  9. use app\common\model\base\config\Systemconfig as systemConfigModel;
  10. use think\facade\Db;
  11. class ConfigClass extends Base
  12. {
  13. protected $classModel = null;
  14. protected $configModel = null;
  15. public function initialize()
  16. {
  17. parent::initialize();
  18. $this->classModel = new configClassModel;
  19. $this->configModel = new systemConfigModel;
  20. }
  21. private function createWhere()
  22. {
  23. $data = $this->request->param();
  24. $group_id = !empty($data['group_id'])?$data['group_id']:0;
  25. $name = !empty($data['name'])?$data['name']:"";
  26. $where = [];
  27. if(!empty($group_id)){
  28. $where[] = ['group_id','=',$group_id];
  29. }
  30. if(!$this->userinfo['is_developer']){
  31. $where[] = ['is_developer','=',2];
  32. }
  33. if(!empty($name)){
  34. $where[] = ['name','LIKE',"%$name%"];
  35. }
  36. return $where;
  37. }
  38. /**
  39. * @title: 获取配置分类列表
  40. * @param {int} {group_id} {非必填} {分组id}
  41. * @param {int} {name} {非必填} {名称搜索}
  42. * @return array
  43. * @Author: wangkewei
  44. * @Date: 2021/5/18 9:08
  45. */
  46. public function getList()
  47. {
  48. $where = $this->createWhere();
  49. $list = $this->classModel->where($where)->order("weigh ASC")->select();
  50. $list = FieldConverList($list);
  51. return res(1,"获取成功",$list);
  52. }
  53. /**
  54. * @title: 获取配置分类树形数据
  55. * @desc: 描述
  56. * @return {*}
  57. * @author: Rock
  58. * @method: POST
  59. * @Date: 2023-03-27 09:19:52
  60. */
  61. public function getTree()
  62. {
  63. $where = $this->createWhere();
  64. $list = $this->classModel->where($where)->order('weigh ASC')->select()->toArray();
  65. $tree = array2tree($list,'pid','group_id');
  66. return res(1,"获取成功",$tree);
  67. }
  68. /**
  69. * @title: 配置分类编辑
  70. * @param array
  71. * @return array
  72. * @Author: wangkewei
  73. * @Date: 2021/5/18 9:10
  74. */
  75. public function doEdit()
  76. {
  77. $data = $this->request->param();
  78. $check = $this->validate($data,ConfigGroupValidate::class);
  79. //验证字段
  80. if(true!==$check){
  81. return res(2,$check);
  82. }
  83. if(!$this->userinfo['is_developer']){
  84. return res(2,"没有权限");
  85. }
  86. $res = $this->classModel->replace()->save($data);
  87. return res(1,"保存成功");
  88. }
  89. /**
  90. * @title: 配置分类删除
  91. * @param {int} {id} {必填} {分类id}
  92. * @return array
  93. * @Author: wangkewei
  94. * @Date: 2021/5/18 9:11
  95. */
  96. public function doDelete(int $group_id = 0)
  97. {
  98. if(empty($group_id)){
  99. return res(2,'参数错误');
  100. }
  101. //检查分组下是否有配置项
  102. $info = $this->classModel->where('group_id',$group_id)->find();
  103. $configCount = $this->configModel->where([
  104. ['status','=',1],['group_code','=',$info->code]
  105. ])->count();
  106. if($configCount > 0){
  107. return res(2,'该分组下有使用中的配置,无法删除');
  108. }else{
  109. $this->classModel->where("group_id",$group_id)->delete();
  110. $this->configModel->destroy(function($query)use($info){
  111. $query->where("group_code",$info->code);
  112. });
  113. return res(1,"删除成功");
  114. }
  115. }
  116. /**
  117. * @title: 启用/禁用配置分类
  118. * @desc: 描述
  119. * @param {int} {group_id} {} {分类ID}
  120. * @return {*}
  121. * @author: Rock
  122. * @method: POST
  123. * @Date: 2023-03-27 09:19:01
  124. */
  125. public function changeStatus(int $group_id=0){
  126. $this->classModel->where('group_id',$group_id)->update(['status' => Db::raw('ABS(3 * `status` - 5)')]);
  127. return res(1,"操作成功");
  128. }
  129. }