Config.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. }
  49. }
  50. $this->assign('list',$list);
  51. $this->assign('type',$type);
  52. $this->assign('id',$id);
  53. return $this->fetch();
  54. }
  55. /**
  56. * 批量保存配置
  57. */
  58. public function save(){
  59. $config = request()->post();
  60. if($config && is_array($config)){
  61. foreach ($config as $name => $value) {
  62. Db::name('config')->where('name',$name)->update(['value' => $value]);
  63. }
  64. }
  65. $this->success('保存成功');
  66. }
  67. /**
  68. * 批量保存项目配置
  69. */
  70. public function orgSave(){
  71. $config = request()->post();
  72. if($config && is_array($config)){
  73. foreach ($config as $name => $value) {
  74. $info = Db::name('config')->where('name',$name)->find();
  75. if(!$info){
  76. continue;
  77. }
  78. $oinfo = Db::name('config_org')->where('config_id',$info['id'])->where('org_id',$this->orgId)->find();
  79. if($oinfo){
  80. Db::name('config_org')->where('id',$oinfo['id'])->update(['value'=>$value]);
  81. }else{
  82. Db::name('config_org')->insert([
  83. 'config_id' => $info['id'],
  84. 'org_id' => $this->orgId,
  85. 'value' => $value
  86. ]);
  87. }
  88. }
  89. }
  90. $this->success('保存成功');
  91. }
  92. /**
  93. * 配置列表
  94. */
  95. public function index(){
  96. if(request()->isAjax()){
  97. //分页参数
  98. $length = input('rows',10,'intval'); //每页条数
  99. $page = input('page',1,'intval'); //第几页
  100. $start = ($page - 1) * $length; //分页开始位置
  101. //排序
  102. $sortRow = input('sidx','sort','trim'); //排序列
  103. $sort = input('sord','asc','trim'); //排序方式
  104. $order = $sortRow.' '.$sort;
  105. $name = input('name','','trim');
  106. $type = input('type','','trim');
  107. $cate = input('cate','','trim');
  108. $group = input('group','','trim');
  109. if($name != ''){
  110. $map['name'] = ['like','%'.$name.'%'];
  111. }
  112. if($type != ''){
  113. $map['type'] = $type;
  114. }
  115. if($group != ''){
  116. $map['group'] = $group;
  117. }
  118. if($cate != ''){
  119. $map['cate'] = $cate;
  120. }
  121. $map= empty($map) ? true: $map;
  122. //数据查询
  123. $lists = Db::name('config')
  124. ->where($map)
  125. ->limit($start,$length)
  126. ->order($order)
  127. ->select();
  128. foreach ($lists as $k=>$v){
  129. $lists[$k]['type'] = model('Config')->getConfigType($v['type']);
  130. if($v['cate'] == 1){
  131. $lists[$k]['group'] = model('Config')->getConfigGroup($v['group']);
  132. }else{
  133. $lists[$k]['group'] = model('Config')->getConfigOrgGroup($v['group']);
  134. }
  135. }
  136. //数据返回
  137. $totalCount = Db::name('config')->where($map)->count();
  138. $totalPage = ceil($totalCount/$length);
  139. $result['page'] = $page;
  140. $result['total'] = $totalPage;
  141. $result['records'] = $totalCount;
  142. $result['rows'] = $lists;
  143. return json($result);
  144. }else{
  145. $type_list = model('Config')->getConfig('config_type_list');
  146. $group_list = model('Config')->getConfig('config_group_list');
  147. $this->assign('type_list',$type_list);
  148. $this->assign('group_list',$group_list);
  149. $this->assign('meta_title','配置列表');
  150. return $this->fetch();
  151. }
  152. }
  153. /**
  154. * 新增/编辑
  155. */
  156. public function add($id=0){
  157. if(request()->isPost()){
  158. $res = model('Config')->updates();
  159. if($res){
  160. $this->success('操作成功',url('index'));
  161. }else{
  162. $this->error(model('Config')->getError());
  163. }
  164. }else{
  165. $meta_title = '新增配置';
  166. if($id){
  167. $info = Db::name('config')->where('id',$id)->find();
  168. $this->assign('info',$info);
  169. $meta_title = '编辑配置';
  170. }
  171. $type_list = model('Config')->getConfig('config_type_list');
  172. $group_list = model('Config')->getConfig('config_group_list');
  173. $org_group_list = model('Config')->getConfig('config_org_group_list');
  174. $this->assign('type_list',$type_list);
  175. $this->assign('group_list',$group_list);
  176. $this->assign('org_group_list',$org_group_list);
  177. $this->assign('meta_title',$meta_title);
  178. return $this->fetch();
  179. }
  180. }
  181. /**
  182. * 删除记录
  183. * @param int $id
  184. */
  185. public function del($id=0){
  186. $name = Db::name('config')->where('id',$id)->value('name');
  187. if(!$name){
  188. $this->error('该记录不存在');
  189. }
  190. if(in_array($name,['config_group_list','config_type_list'])){
  191. $this->error('该配置不能删除');
  192. }
  193. $res = Db::name('config')->delete($id);
  194. if($res){
  195. $this->success('删除成功');
  196. }else{
  197. $this->error('删除失败');
  198. }
  199. }
  200. }