Systemconfig.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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\Systemconfig as SystemconfigValidate;
  8. use app\common\model\base\config\ConfigClass as configClassModel;
  9. use app\common\model\base\config\Systemconfig as systemConfigModel;
  10. class Systemconfig extends Base
  11. {
  12. protected $configModel = null;
  13. protected $groupModel = null;
  14. protected $noNeedLogin = ['gettypelist','getconfig'];
  15. protected $noNeedAuth = ['getList'];
  16. public function initialize()
  17. {
  18. parent::initialize();
  19. $this->classModel = new configClassModel;
  20. $this->configModel = new systemConfigModel;
  21. }
  22. private function createWhere()
  23. {
  24. $data = $this->request->param();
  25. $where = [];
  26. if(!empty($data['group_id'])){
  27. $group_code = $this->classModel->value('code');
  28. $where[] = ['group_code','=',$group_code];
  29. }
  30. if(!empty($data['group_code'])){
  31. $where[] = ['group_code','=',$data['group_code']];
  32. }
  33. if(!$this->userinfo['is_developer']){
  34. $where[] = ['is_developer','=',2];
  35. }
  36. return $where;
  37. }
  38. /**
  39. * @title: 获取配置项列表
  40. * @desc: 描述
  41. * @param {int} {pageNo} {0} {页码,传0不分页}
  42. * @param {int} {pageSize} {10} {每页数量}
  43. * @param {int} {return_json} {0} {是否返回json对象,0不返回,其他都返回}
  44. * @return {*}
  45. * @author: Rock
  46. * @method: POST
  47. * @Date: 2023-04-06 10:34:24
  48. */
  49. public function getList(int $pageNo=0,int $pageSize=10,int $return_json=0)
  50. {
  51. $data = $this->request->param();
  52. $where = $this->createWhere();
  53. if(!empty($pageNo)){
  54. $list = $this->configModel->where($where)->order("weigh ASC")->paginate(['page'=>$pageNo,'list_rows'=>$pageSize]);
  55. $total = $list->total();
  56. $list = $list->items();
  57. }else{
  58. $list = $this->configModel->where($where)->order("weigh ASC")->select()->toArray();
  59. }
  60. if(!empty($return_json)){
  61. $list = array_column($list,null,'key');
  62. }
  63. if(!empty($pageNo)){
  64. return pageRes(1,'获取成功',$total,$list);
  65. }else{
  66. return res(1,"获取成功",$list);
  67. }
  68. }
  69. /**
  70. * @title: 编辑配置项
  71. * @param array
  72. * @return array
  73. * @Author: wangkewei
  74. * @Date: 2021/5/18 10:02
  75. */
  76. public function doEdit()
  77. {
  78. $data = $this->request->param();
  79. $check = $this->validate($data,SystemconfigValidate::class);
  80. //验证字段
  81. if(true!==$check){
  82. return res(2,$check);
  83. }
  84. $this->configModel->replace()->save($data);
  85. slog(1,"编辑了系统配置项".$data['name']);
  86. return res(1,"保存成功");
  87. }
  88. /**
  89. * @title: 删除配置项
  90. * @param {int} {config_id} {必填} {id}
  91. * @return array
  92. * @Author: wangkewei
  93. * @Date: 2021/5/18 10:02
  94. */
  95. public function doDelete(int $config_id)
  96. {
  97. $info = $this->configModel->where("config_id",$config_id)->find();
  98. $info->delete();
  99. slog(1,"删除了系统配置项".$info['name']);
  100. return res(1,"删除成功");
  101. }
  102. /**
  103. * @title: 保存配置
  104. * @desc:
  105. * @param {array} {} {} {}
  106. * @return {}
  107. * @Author: Rock
  108. * @Date: 2021-05-20 15:41:30
  109. * @LastEditTime: Do not edit
  110. */
  111. public function doSave()
  112. {
  113. $data = $this->request->param();
  114. if(isset($data['jwtData']))unset($data['jwtData']);
  115. foreach($data as $row){
  116. if(!empty($row['rule'])){
  117. $validateData = [];
  118. $validateData[$row['key']] = $row['value'];
  119. $rule = [];
  120. $rule[$row['key']] = $row['rule'];
  121. $check = $this->validate($validateData,$rule);
  122. if(true!==$check){
  123. return res(2,$check);
  124. }else{
  125. slog(1,"设置了系统配置项".$row['name']);
  126. }
  127. }
  128. }
  129. $this->configModel->replace()->saveAll($data);
  130. return res(1,"配置成功");
  131. }
  132. /**
  133. * @title: 获取已有的最大排序号,返回最大排序号加1的结果
  134. * @desc: 描述
  135. * @param {string} $group_code
  136. * @return {*}
  137. * @author: Rock
  138. * @method: POST
  139. * @Date: 2023-03-31 10:49:10
  140. */
  141. public function getMaxWeigh(string $group_code=''){
  142. $where = [];
  143. if(!empty($group_code)){
  144. $where[] = ['group_code','=',$group_code];
  145. }
  146. $maxWeigh = $this->configModel->where($where)->max('weigh');
  147. return res(1,"获取成功",$maxWeigh + 1);
  148. }
  149. /**
  150. * @title: 批量操作
  151. * @desc: 描述
  152. * @param {array} {select} {} {选中的配置}
  153. * @param {int} {type} {0} {操作类型,1=启用,2=禁用,3=删除}
  154. * @return {*}
  155. * @author: Rock
  156. * @method: POST
  157. * @Date: 2023-03-31 11:10:12
  158. */
  159. public function batchOperate(array $select=[],int $type=0){
  160. if(empty($select) || empty($type)){
  161. return res(2,"请选择操作项");
  162. }
  163. $ids = array_column($select,'config_id');
  164. if($type==1){
  165. $this->configModel->where('config_id','IN',$ids)->update(['status'=>1]);
  166. }elseif($type==2){
  167. $this->configModel->where('config_id','IN',$ids)->update(['status'=>2]);
  168. }elseif($type==3){
  169. $this->configModel->destroy(function($query)use($ids){
  170. $query->where('config_id','IN',$ids);
  171. });
  172. }
  173. return res(1,"操作成功");
  174. }
  175. /**
  176. * @title: 获取系统配置
  177. * @desc:
  178. * @param {string} {key} {} {配置名称,为空时获取全部生效配置}
  179. * @return {mixed} {} {} {有传key时,如果key包含".",表示获取分组下的配置,例如,group_code.key,不包含".",表示获取group_code等于key的所有配置;没传key则返回所有配置}
  180. * @Author: Rock
  181. * @Date: 2021-05-20 15:42:12
  182. * @LastEditTime: Do not edit
  183. */
  184. public function getConfig($key="base")
  185. {
  186. $result = sysconfig($key);
  187. return res(1,"获取成功",$result);
  188. }
  189. /**
  190. * @title: 获取可选项
  191. * @desc: 描述
  192. * @return {*}
  193. * @author: Rock
  194. * @method: POST
  195. * @Date: 2023-03-31 11:32:40
  196. */
  197. public function getOptions()
  198. {
  199. $options = [
  200. 'typeList' => systemConfigModel::getTypeList(),
  201. 'statusList' => systemConfigModel::getStatusList()
  202. ];
  203. return res(1,"获取成功",$options);
  204. }
  205. }