Config.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. namespace app\admin\controller;
  3. use think\Db;
  4. class Config extends Auth
  5. {
  6. /**
  7. * 系统网站设置
  8. */
  9. public function group($id=1){
  10. $type = model('Config')->getConfig('config_group_list');
  11. $list = Db::name("config")
  12. ->where(['status'=>1,'group'=>$id,'cate'=>1])
  13. ->field('id,name,title,extra,value,remark,type')
  14. ->order('sort asc')
  15. ->select();
  16. $list = $list?$list:[];
  17. $this->assign('list',$list);
  18. $this->assign('id',$id);
  19. $this->assign('type',$type);
  20. $this->assign('meta_title',$type[$id].'设置');
  21. return $this->fetch();
  22. }
  23. /**
  24. * 组织配置
  25. */
  26. public function org($id=1){
  27. $type = model('Config')->getConfig('config_org_group_list');
  28. $list = Db::name("config")
  29. ->where(['status'=>1,'group'=>$id,'cate'=>2])
  30. ->field('id,name,title,extra,value,remark,type')
  31. ->order('sort asc')
  32. ->select();
  33. $list = $list?$list:[];
  34. $user = model('WorkTypeMode')->getWorkerUserApp($this->orgId);
  35. $nuser = [];
  36. foreach ($user as $kk=>$vv){
  37. $nuser[] = [
  38. 'id' => $vv['id'],
  39. 'title' => $vv['real_name']
  40. ];
  41. }
  42. foreach ($list as $k=>$v){
  43. $value = Db::name('config_org')->where('config_id',$v['id'])->where('org_id',$this->orgId)->find();
  44. $list[$k]['value'] = $value?$value['value']:'';
  45. if($v['type'] == 5){ // 工人
  46. $list[$k]['user'] = $nuser;
  47. $list[$k]['value'] = $list[$k]['value']?explode(',',$list[$k]['value']):[];
  48. }elseif ($v['type'] == 8){ //角色
  49. $list[$k]['roles'] = model('Roles')->getList();;
  50. $list[$k]['value'] = $list[$k]['value']?explode(',',$list[$k]['value']):[];
  51. }
  52. }
  53. $this->assign('list',$list);
  54. $this->assign('type',$type);
  55. $this->assign('id',$id);
  56. return $this->fetch();
  57. }
  58. /**
  59. * 批量保存配置
  60. */
  61. public function save(){
  62. $config = request()->post();
  63. if($config && is_array($config)){
  64. foreach ($config as $name => $value) {
  65. Db::name('config')->where('name',$name)->update(['value' => $value]);
  66. }
  67. }
  68. $this->success('保存成功');
  69. }
  70. /**
  71. * 批量保存项目配置
  72. */
  73. public function orgSave(){
  74. $config = request()->post();
  75. if($config && is_array($config)){
  76. foreach ($config as $name => $value) {
  77. $info = Db::name('config')->where('name',$name)->find();
  78. if(!$info){
  79. continue;
  80. }
  81. $oinfo = Db::name('config_org')->where('config_id',$info['id'])->where('org_id',$this->orgId)->find();
  82. if($oinfo){
  83. Db::name('config_org')->where('id',$oinfo['id'])->update(['value'=>$value]);
  84. }else{
  85. Db::name('config_org')->insert([
  86. 'config_id' => $info['id'],
  87. 'org_id' => $this->orgId,
  88. 'value' => $value
  89. ]);
  90. }
  91. }
  92. }
  93. $this->success('保存成功');
  94. }
  95. /**
  96. * 配置列表
  97. */
  98. public function index(){
  99. if(request()->isAjax()){
  100. //分页参数
  101. $length = input('rows',10,'intval'); //每页条数
  102. $page = input('page',1,'intval'); //第几页
  103. $start = ($page - 1) * $length; //分页开始位置
  104. //排序
  105. $sortRow = input('sidx','sort','trim'); //排序列
  106. $sort = input('sord','asc','trim'); //排序方式
  107. $order = $sortRow.' '.$sort;
  108. $name = input('name','','trim');
  109. $type = input('type','','trim');
  110. $cate = input('cate','','trim');
  111. $group = input('group','','trim');
  112. if($name != ''){
  113. $map['name'] = ['like','%'.$name.'%'];
  114. }
  115. if($type != ''){
  116. $map['type'] = $type;
  117. }
  118. if($group != ''){
  119. $map['group'] = $group;
  120. }
  121. if($cate != ''){
  122. $map['cate'] = $cate;
  123. }
  124. $map= empty($map) ? true: $map;
  125. //数据查询
  126. $lists = Db::name('config')
  127. ->where($map)
  128. ->limit($start,$length)
  129. ->order($order)
  130. ->select();
  131. foreach ($lists as $k=>$v){
  132. $lists[$k]['type'] = model('Config')->getConfigType($v['type']);
  133. if($v['cate'] == 1){
  134. $lists[$k]['group'] = model('Config')->getConfigGroup($v['group']);
  135. }else{
  136. $lists[$k]['group'] = model('Config')->getConfigOrgGroup($v['group']);
  137. }
  138. }
  139. //数据返回
  140. $totalCount = Db::name('config')->where($map)->count();
  141. $totalPage = ceil($totalCount/$length);
  142. $result['page'] = $page;
  143. $result['total'] = $totalPage;
  144. $result['records'] = $totalCount;
  145. $result['rows'] = $lists;
  146. return json($result);
  147. }else{
  148. $type_list = model('Config')->getConfig('config_type_list');
  149. $group_list = model('Config')->getConfig('config_group_list');
  150. $this->assign('type_list',$type_list);
  151. $this->assign('group_list',$group_list);
  152. $this->assign('meta_title','配置列表');
  153. return $this->fetch();
  154. }
  155. }
  156. /**
  157. * 新增/编辑
  158. */
  159. public function add($id=0){
  160. if(request()->isPost()){
  161. $res = model('Config')->updates();
  162. if($res){
  163. $this->success('操作成功',url('index'));
  164. }else{
  165. $this->error(model('Config')->getError());
  166. }
  167. }else{
  168. $meta_title = '新增配置';
  169. if($id){
  170. $info = Db::name('config')->where('id',$id)->find();
  171. $this->assign('info',$info);
  172. $meta_title = '编辑配置';
  173. }
  174. $type_list = model('Config')->getConfig('config_type_list');
  175. $group_list = model('Config')->getConfig('config_group_list');
  176. $org_group_list = model('Config')->getConfig('config_org_group_list');
  177. $this->assign('type_list',$type_list);
  178. $this->assign('group_list',$group_list);
  179. $this->assign('org_group_list',$org_group_list);
  180. $this->assign('meta_title',$meta_title);
  181. return $this->fetch();
  182. }
  183. }
  184. /**
  185. * 删除记录
  186. * @param int $id
  187. */
  188. public function del($id=0){
  189. $name = Db::name('config')->where('id',$id)->value('name');
  190. if(!$name){
  191. $this->error('该记录不存在');
  192. }
  193. if(in_array($name,['config_group_list','config_type_list'])){
  194. $this->error('该配置不能删除');
  195. }
  196. $res = Db::name('config')->delete($id);
  197. if($res){
  198. $this->success('删除成功');
  199. }else{
  200. $this->error('删除失败');
  201. }
  202. }
  203. }