123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213 |
- <?php
- namespace app\admin\controller;
- use think\Db;
- class Config extends Auth
- {
- /**
- * 系统网站设置
- */
- public function group($id=1){
- $type = model('Config')->getConfig('config_group_list');
- $list = Db::name("config")
- ->where(['status'=>1,'group'=>$id,'cate'=>1])
- ->field('id,name,title,extra,value,remark,type')
- ->order('sort asc')
- ->select();
- $list = $list?$list:[];
- $this->assign('list',$list);
- $this->assign('id',$id);
- $this->assign('type',$type);
- $this->assign('meta_title',$type[$id].'设置');
- return $this->fetch();
- }
- /**
- * 组织配置
- */
- public function org($id=1){
- $type = model('Config')->getConfig('config_org_group_list');
- $list = Db::name("config")
- ->where(['status'=>1,'group'=>$id,'cate'=>2])
- ->field('id,name,title,extra,value,remark,type')
- ->order('sort asc')
- ->select();
- $list = $list?$list:[];
- $user = model('WorkTypeMode')->getWorkerUserApp($this->orgId);
- $nuser = [];
- foreach ($user as $kk=>$vv){
- $nuser[] = [
- 'id' => $vv['id'],
- 'title' => $vv['real_name']
- ];
- }
- foreach ($list as $k=>$v){
- $value = Db::name('config_org')->where('config_id',$v['id'])->where('org_id',$this->orgId)->find();
- $list[$k]['value'] = $value?$value['value']:'';
- if($v['type'] == 5){ // 工人
- $list[$k]['user'] = $nuser;
- $list[$k]['value'] = $list[$k]['value']?explode(',',$list[$k]['value']):[];
- }
- }
- $this->assign('list',$list);
- $this->assign('type',$type);
- $this->assign('id',$id);
- return $this->fetch();
- }
- /**
- * 批量保存配置
- */
- public function save(){
- $config = request()->post();
- if($config && is_array($config)){
- foreach ($config as $name => $value) {
- Db::name('config')->where('name',$name)->update(['value' => $value]);
- }
- }
- $this->success('保存成功');
- }
- /**
- * 批量保存项目配置
- */
- public function orgSave(){
- $config = request()->post();
- if($config && is_array($config)){
- foreach ($config as $name => $value) {
- $info = Db::name('config')->where('name',$name)->find();
- if(!$info){
- continue;
- }
- $oinfo = Db::name('config_org')->where('config_id',$info['id'])->where('org_id',$this->orgId)->find();
- if($oinfo){
- Db::name('config_org')->where('id',$oinfo['id'])->update(['value'=>$value]);
- }else{
- Db::name('config_org')->insert([
- 'config_id' => $info['id'],
- 'org_id' => $this->orgId,
- 'value' => $value
- ]);
- }
- }
- }
- $this->success('保存成功');
- }
- /**
- * 配置列表
- */
- public function index(){
- if(request()->isAjax()){
- //分页参数
- $length = input('rows',10,'intval'); //每页条数
- $page = input('page',1,'intval'); //第几页
- $start = ($page - 1) * $length; //分页开始位置
- //排序
- $sortRow = input('sidx','sort','trim'); //排序列
- $sort = input('sord','asc','trim'); //排序方式
- $order = $sortRow.' '.$sort;
- $name = input('name','','trim');
- $type = input('type','','trim');
- $cate = input('cate','','trim');
- $group = input('group','','trim');
- if($name != ''){
- $map['name'] = ['like','%'.$name.'%'];
- }
- if($type != ''){
- $map['type'] = $type;
- }
- if($group != ''){
- $map['group'] = $group;
- }
- if($cate != ''){
- $map['cate'] = $cate;
- }
- $map= empty($map) ? true: $map;
- //数据查询
- $lists = Db::name('config')
- ->where($map)
- ->limit($start,$length)
- ->order($order)
- ->select();
- foreach ($lists as $k=>$v){
- $lists[$k]['type'] = model('Config')->getConfigType($v['type']);
- if($v['cate'] == 1){
- $lists[$k]['group'] = model('Config')->getConfigGroup($v['group']);
- }else{
- $lists[$k]['group'] = model('Config')->getConfigOrgGroup($v['group']);
- }
- }
- //数据返回
- $totalCount = Db::name('config')->where($map)->count();
- $totalPage = ceil($totalCount/$length);
- $result['page'] = $page;
- $result['total'] = $totalPage;
- $result['records'] = $totalCount;
- $result['rows'] = $lists;
- return json($result);
- }else{
- $type_list = model('Config')->getConfig('config_type_list');
- $group_list = model('Config')->getConfig('config_group_list');
- $this->assign('type_list',$type_list);
- $this->assign('group_list',$group_list);
- $this->assign('meta_title','配置列表');
- return $this->fetch();
- }
- }
- /**
- * 新增/编辑
- */
- public function add($id=0){
- if(request()->isPost()){
- $res = model('Config')->updates();
- if($res){
- $this->success('操作成功',url('index'));
- }else{
- $this->error(model('Config')->getError());
- }
- }else{
- $meta_title = '新增配置';
- if($id){
- $info = Db::name('config')->where('id',$id)->find();
- $this->assign('info',$info);
- $meta_title = '编辑配置';
- }
- $type_list = model('Config')->getConfig('config_type_list');
- $group_list = model('Config')->getConfig('config_group_list');
- $org_group_list = model('Config')->getConfig('config_org_group_list');
- $this->assign('type_list',$type_list);
- $this->assign('group_list',$group_list);
- $this->assign('org_group_list',$org_group_list);
- $this->assign('meta_title',$meta_title);
- return $this->fetch();
- }
- }
- /**
- * 删除记录
- * @param int $id
- */
- public function del($id=0){
- $name = Db::name('config')->where('id',$id)->value('name');
- if(!$name){
- $this->error('该记录不存在');
- }
- if(in_array($name,['config_group_list','config_type_list'])){
- $this->error('该配置不能删除');
- }
- $res = Db::name('config')->delete($id);
- if($res){
- $this->success('删除成功');
- }else{
- $this->error('删除失败');
- }
- }
- }
|