WebuserAction.class.php 6.8 KB
<?php
Class WebuserAction extends CommonAction{
	private $tab = 'Webuser';
	public function __construct() {
		parent::__construct();
		$this->checkLog();	//判断用户是否已经登录
		$this->assign('webuserList',$this->getPower());	//用户权限
		$this->assign('ispower',C('open_power'));	//是否开启交互管理
	}
	
	public function index()
	{
		$model = M($this->tab);
		//登录账号的信息
		$info = $this->_getRow($model,array('id'=>array('eq',cookie('uid'))),'id,depth');
		if($info){
			if(C('open_power')==='Y'){//开启同等级交互管理权限
				$dep_arr=explode(',',$info['depth']);
				if(count($dep_arr)>1){
					array_pop($dep_arr);
					$sbarr=implode(',',$dep_arr);
					$where['depth'] = array('like',$sbarr.',%,%');
				}else{
					$where['depth'] = array('like',$info['depth'].',%');
				}
			}else{
				$where['depth'] = array('like',$info['depth'].',%');
			}
		}else{	//登录账号不存在 
			$this->clearLog();
			$this->error('账号有误!',U('Public/index'));
		}
		$field = array('id','username','tel','email','name','isshow','ltime','lip','addtime','updatatime');
		// $where['id'] = array('neq',1);
		$data = $this->_getLists($model,$where,$order,$field,12,'Pages3',array('header'=>'<li style="border:none;">共%totalRows%条记录','pages'=>'%nowPage%/%totalPages%页 </li>','prev'=>'<上一页','next'=>'下一页>','first'=>'首页','last'=>'尾页','theme'=>'%header% %pages% %prePage% %linkPage% %nextPage% %end%'));
		//echo $model->getLastSql();
		$auth=M('authgroupf')->getField('id,title');//用户类别名称
		$this->assign('auth',$auth);
		$this->assign('list',$data['list']?$data['list']:array());	//列表
		$this->assign('page',$data['page']);	//分页
		$this->assign('p',$data['p']);	//当前页数
		$this->assign('page_title','用户列表');
		$this->assign('empty','<tr><td colspan="9" style="padding-left:15px;font-size:14px;">暂无相关信息!</td></tr>');
		$this->display();
	}

	public function edit()
	{
		$id=I('id',0,'intval');
		if(!$id){
			$this->error('参数有误!');
		}
		$Admin=D($this->tab);
		$data=$Admin->where('id='.$id)->find();
		if($this->isPost()){
			//添加用户
			if(!$Admin->create()){
				$this->error($Admin->getError());
			}else{
				if(!$Admin->password){	//修改账号,如果没有输入密码则删除password
					unset($Admin->password);
				}else{
					$Admin->password = md5($Admin->password);
				}
				$lastid=$Admin->where('id='.$id)->save();
				if($lastid>0){
					$this->success('系统用户修改成功',U('index'));
				}else{
					$this->error('系统用户修改失败');
				}
			}
		}else{
			$this->assign('data',$data);
			$this->display(); 
		}
	}
	
	public function add()
	{
		$Admin=D($this->tab);
		if($this->isPost()){
			if(cookie('uid')==1){//超级用户
				if(!$_POST['s_dengji']){
					$this->error('请选择用户等级');
				}else{
					if($_POST['s_dengji']!='2' && !$_POST['c_dengji']){
							$this->error('请选择父级用户');
					}
				}
			}
			//添加用户
			if(!$Admin->create()){
				$this->error($Admin->getError());
			}else{
				$depth = $this->getDepth(I('c_dengji'));	//获取depth=array('depth','pid')
				$Admin->depth = $depth['depth'];
				$Admin->pid = $depth['pid'];

				//获取与用户等级匹配的角色
				$grade = count(explode(',',$depth['depth']));	//用户等级
				$group_id = $this->getGroup($grade);	//根据用户等级数 获取对应的角色id
				if(!$group_id){
					$this->error('用户角色无法匹配,请联系系统管理员');
				}

				$lastid=$Admin->add();
				if($lastid>0){
					$this->fenpei($lastid,$group_id);	//添加用户时自动为用户分配角色
					$this->success('系统用户添加成功',U('index'));
				}else{
					$this->error('系统用户添加失败');
				}
			}
		}else{
			//获取用户等级
			$grade_arr=M('Authgroupf')->where('grade!=1')->field('grade')->select();
			$this->assign('grade_arr',$grade_arr);//用户等级
			$this->display(); 
		}
	}
	/**
	 * 异步获取选中级别上一级用户信息
	 */
	public function z_user(){
		$s_dengji=I('s_dengji')-1;
		$c_dengji=I('c_dengji');
		$user=M($this->tab)->field('id,depth,name')->select();
		if($user){
			foreach($user as $k=>$v){
				$djnum=count(explode(',',$v['depth']));
				if($djnum == $s_dengji)
				$str.='<option value="'.$v['id'].'"';
				if($c_dengji == $v['id']) $str.=' selected="selected"';
				$str.='>'.$v['name'].'</option>';
			}
		}
		echo json_encode($str);
	}
/**
 * 添加用户时自动为用户分配角色
 * @param  int $uid   [用户id]
 * @param  int $group_id [角色id]
 */
	private function fenpei($uid,$group_id){
		$m=M('Authgroupaccessf');
		$data['uid'] = $uid;
		$data['group_id'] = $group_id;	
		$m->data($data)->add();
	}
/**
 * 根据用户等级数 获取对应的角色
 * @param  int $grade [用户等级]
 * @return int 角色id
 */
	private function getGroup($grade){
		$m = M('Authgroupf');
		$map['grade'] = array('eq',$grade);
		$id = $this->_getOne($m,$map,'id');
		return $id;
	}
/**
 * 获取当前登录账号的depth+id
 * @return array('id','depth')
 */
	private function getDepth($uid=0){
		$m = M($this->tab);
		if($uid){
			$map['id'] = array('eq',$uid);
		}else{
			$map['id'] = array('eq',cookie('uid'));
		}
		$r = $m->field('id,depth')->where($map)->find();
		$back['depth'] = $r['depth'].','.$r['id'];
		$back['pid'] = $r['id'];
		return $back;
	}

	public function delete()
	{
		$where['id']=I('id',0,'intval');
		$Admin=D($this->tab);
		$data=$Admin->where($where)->find();
		if(!$data){
			$this->error('信息有误!');
		}
		if($data['id'] == cookie('uid')){
			$this->error('不允许删除当前使用的用户帐号');
		}elseif($data['id'] == 1){
			$this->error('该账号不允许删除!');
		}else{
			$count=$Admin->where($where)->delete();
			if($count){
				$this->delAuthAccess($data['id']);
				$this->success('用户删除成功',U('index',array('p'=>I('p',1,'intval'))));
			}else{
				$this->error('用户删除失败');
			}
		}
	}
/**
 * 删除会员时删除相应的角色分配
 * @param  int $uid 用户id
 */
	private function delAuthAccess($uid){
		if($uid){
			$m = M('Authgroupaccessf');
			$map['uid'] = array('eq',$uid);
			$r = $m->where($map)->delete();
		}
	}
/**
 * 修改分组开启属性
**/
	public function editattr()
	{
		$table=$this->_post('table');
		$m=M($table);
		$where['id']=array('eq',$this->_post('id'));
		$data[$this->_post('f')]=$this->_post('status');
		$count=$m->where($where)->data($data)->save();
		if($count)
		{
			$msg['success']=true;
			$msg['msg']='状态已修改';
		}
		else
		{
			$msg['success']=false;
			$msg['msg']='状态修改失败';
		}
		echo json_encode($msg);
	}
}