| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');include_once(FCPATH . 'application/controllers/Base_Controller.php');class User extends Base_Controller{	public function __construct()	{		parent::__construct();		$this->load->model('Company_model');		$this->load->model('User_model');		$this->load->model('Feedback_model');		$this->load->model('Privilnode_model');		$this->load->model('Project_model');		$this->load->model('Global_location_model');	}	// 个人信息	public function info(){		$id = intval($this->input->post('id',true));		$data = array();		if (empty($id)) {			$data['role'] = $this->get_user_info('role');			$data['id'] = $this->get_user_info('id');			$data['phone'] = $this->get_user_info('phone');			$data['email'] = $this->get_user_info('email');			$data['name'] = $this->get_user_info('name');			$data['company'] = $this->get_user_info('company');			$avatar = $this->get_user_info('avatar');			$data['avatar'] = !empty($avatar) ? base_url($avatar) : '';		}else{			$data = $this->User_model->get_one(['id'=>$id],'role,id,name,company,phone,email,avatar');			$data['avatar'] = !empty($data['avatar']) ? base_url($data['avatar']) : '';		}        exit(json_result('0000',$this->response['0000'],$data));	}	// 账号统计信息	public function data(){		$data = array(			'total' => 0,			'manuCount' => 0,			'suppCount' => 0,			'poCount' => 0,			'upaCount' => 0,			'monCount' => 0,			'conCount' => 0,			'permCount' => 0,			'view' => 0,			'operating' => 0,			'parameter' => 0		);		$role = $this->get_user_info('role');		$privilege = $this->get_user_info('privilege');		if ($role == SYSTEM_ADMIN) {			$data['permCount'] = 50;			$data['view'] = 1;			$data['operating'] = 1;			$data['parameter'] = 1;		}else{			$arr = explode(',', $privilege);			$data['permCount'] = count($arr);			$list = $this->Privilnode_model->get_list(['id'=>$arr],'parentid as pid');			foreach ($list as $key => $value) {				if ($value['pid'] == 1) $data['view'] = 1;				if ($value['pid'] == 18) $data['operating'] = 1;				if ($value['pid'] == 39) $data['parameter'] = 1;			}		}				$id = $this->get_user_info('id');		$where = array('id !=' => $id);		if ($role != SYSTEM_ADMIN) $where['pid'] = $id;		$data['total'] = $this->User_model->get_count($where);		$where['role'] = 2;		$data['manuCount'] = $this->User_model->get_count($where);		$where['role'] = 3;		$data['suppCount'] = $this->User_model->get_count($where);		$where['role'] = 4;		$data['poCount'] = $this->User_model->get_count($where);		$where['role'] = 5;		$data['upaCount'] = $this->User_model->get_count($where);		$where['role'] = 6;		$data['monCount'] = $this->User_model->get_count($where);		$where['role'] = 7;		$data['conCount'] = $this->User_model->get_count($where);		exit(json_result('0000',$this->response['0000'],$data));	}	// 修改用户密码	public function password(){		$username = $this->get_user_info('username');		$old_pass = $this->input->post('old',true);		$new_pass = $this->input->post('new',true);		$new_second_pass = $this->input->post('new_second',true);		if($new_pass == $old_pass){			exit(json_result('0709',$this->response['0709'],array()));		}		// 验证确认密码		if($new_pass != $new_second_pass){			exit(json_result('0701',$this->response['0701'],array()));		}		// 验证密码长度		if (mb_strlen($new_pass) < 6 || mb_strlen($new_pass) > 12) {			exit(json_result('0703',$this->response['0703'],array()));		}		// 验证旧密码		if (!$this->User_model->validate_password($username,md5($old_pass))) {			exit(json_result('0702',$this->response['0702'],array()));		}		$id = $this->get_user_info('id');		$new_pass = md5($new_pass);		$res = $this->User_model->change_password($id,$new_pass);		if($res){			exit(json_result('0000',$this->response['0000'],array()));		}else{			exit(json_result('0704',$this->response['0704'],array()));		}	}	// 修改账号状态	public function block_user(){		$userid = $this->input->post('id',true);		$status = intval($this->input->post('status',true));		if (empty($userid) || $status < 0) {			exit(json_result('0007',$this->response['0007'],array()));		}		$this->User_model->update(array('status'=>$status),array('id'=>$userid));		exit(json_result('0000',$this->response['0000'],array()));	}	// 用户列表	public function sub_list(){		$userRole = $this->get_user_info('role');		$userid = $this->get_user_info('id');		$where = array('id !='=>$userid);		if ($userRole != SYSTEM_ADMIN) {			$where['pid'] = $userid;		}		$role = intval($this->input->post('roleType',true));		if (!empty($role)) $where['role'] = $role;		$keywords = $this->input->post('keywords',true);		if ($keywords !== NULL && $keywords !== '') $where['name|'] = $keywords;		$field = "id,name,role,phone,email,status,company";		$list = $this->User_model->get_list($where,$field);		exit(json_result('0000',$this->response['0000'],array('list'=>$list)));	}	// 添加编辑用户	public function user_update(){		$where['id'] = intval($this->input->post('id',true));		// $role = $this->get_user_info('role');        // if ($role == COMPANY_CUSTOMER) {        //     exit(json_result('0011', $this->response['0011'], array()));        // }		$data['name'] = $this->input->post('name',true);				$phone = $this->input->post('phone',true);				$email = $this->input->post('email',true);		$data['company'] = $this->input->post('company',true);		// $data['role'] = intval($this->input->post('roleType'));		$avatar = $this->input->post('avatar',true);		if (isset($avatar) && !empty($avatar)) {            $path = parse_url($avatar);            $data['avatar'] = substr($path['path'], 1);        }		if (!empty($phone)) $data['phone'] = $phone;		if (!empty($telephone)) $data['telephone'] = $telephone;		if (!empty($email)) $data['email'] = $email;				if(!isset($data['name']) || isset($data['name']) == '') exit(json_result('0707',$this->response['0707'],array()));		if(empty($data['company'])) exit(json_result('0712',$this->response['0712'],array()));		$res = $this->Company_model->get_one(['id'=>$$data['company']],'type');		if (empty($res) || empty($res['type'])) exit(json_result('0007',$this->response['0007']));		$data['role'] = $res['type'];		// $data['zone'] = empty($zone) ? '' : $zone;		// 验证请求数据		$config = array();				if(!empty($data['phone'])){			$config[] = array(				        'field' => 'phone',				        'label' => 'Phone',				        'rules' => 'numeric|exact_length[11]',				        'errors' => array(				            'numeric' => '0718',				            'exact_length' => '0718'				        )				    );		}		if (!empty($data['email'])) {			$config[] = array(				        'field' => 'email',				        'label' => 'Email',				        'rules' => 'valid_email',				        'errors' => array(				            'valid_email' => '0719',				        )				    );		}				if (empty($where['id'])) { // 添加用户			$data['username'] = trim($this->input->post('account',true));			$data['password'] = $this->input->post('password',true);			$config[] = array(		        'field' => 'password',		        'label' => 'Password',		        'rules' => 'min_length[6]',		        'errors' => array(		            'min_length' => '0720',		        )		    );			if (!empty($config)) {				$this->load->library('form_validation');				$this->form_validation->set_rules($config);				if ($this->form_validation->run() == FALSE){					$errors = $this->form_validation->error_array();			        exit(json_result(current($errors),$this->response[current($errors)],array()));			    }			}			if(!isset($data['username']) || $data['username'] == '') exit(json_result('0708',$this->response['0708'],array()));			if(mb_strlen($data['username']) > 20) exit(json_result('0723',$this->response['0723'],array()));			if (empty($data['password'])) {				exit(json_result('0713',$this->response['0713'],array()));			}			$data['password'] = md5($data['password']);			$privilegeIds = $this->input->post('privilegeIds',true);			$data['privilege'] = $privilegeIds;			// 验证登录账号是否存在			if ($this->User_model->getDataCount(array('username'=>$data['username'],'role'=>$data['role']))) {				exit(json_result('0706',$this->response['0706'],array()));			}			if (!empty($data['phone']) && $this->User_model->getDataCount(array('phone'=>$data['phone']))) {                exit(json_result('0721',$this->response['0721'],array()));            }            if (!empty($data['email']) && $this->User_model->getDataCount(array('email'=>$data['email']))) {                exit(json_result('0722',$this->response['0722'],array()));            }						$data['pid'] = $this->get_user_info('id');			if (empty($data['role'])) exit(json_result('0724',$this->response['0724']));			$userid = $this->User_model->add($data);			$this->add_operation_log('insert',"添加用户,用户名\"{$data['name']}\"",0);			$this->add_operation_log('insert',"Add user.User name:\"{$data['name']}\"",0,1);		}else{  // 编辑用户			unset($data['password']);			if (!empty($config)) {				$this->load->library('form_validation');				$this->form_validation->set_rules($config);				if ($this->form_validation->run() == FALSE){					$errors = $this->form_validation->error_array();			        exit(json_result(current($errors),$this->response[current($errors)],array()));			    }			}			// if ($this->User_model->getDataCount(array('username'=>$data['username']),$where['id'])) {			// 	exit(json_result('0706',$this->response['0706'],array()));			// }			if (!empty($data['phone']) && $this->User_model->getDataCount(array('phone'=>$data['phone']),$where['id'])) {                exit(json_result('0721',$this->response['0721'],array()));            }            if (!empty($data['email']) && $this->User_model->getDataCount(array('email'=>$data['email']),$where['id'])) {                exit(json_result('0722',$this->response['0722'],array()));            }            $oldData = $this->User_model->get_one($where,'company');            if ($oldData['company'] != $data['company']) {            	$this->Project_model->update(array('company'=>$data['company']),array('userId'=>$where['id'],'company'=>$data['company']));            }            $privilegeIds = $this->input->post('privilegeIds',true);			if ($privilegeIds !== NULL && $privilegeIds !== '') $data['privilege'] = $privilegeIds;						if (empty($data['role'])) exit(json_result('0724',$this->response['0724']));			$this->User_model->update($data,$where);			$userid = $where['id'];			$this->add_operation_log('update',"修改用户,用户名\"{$data['name']}\"",0);			$this->add_operation_log('update',"Update user.User name:\"{$data['name']}\"",0,1);		}		exit(json_result('0000',$this->response['0000'],array('id'=>$userid)));	}	// 删除账户	public function del(){		$id = intval($this->input->post('id',true));		if (empty($id)) exit(json_result('0007',$this->response['0007']));		$this->User_model->delete(['id'=>$id]);		exit(json_result('0000',$this->response['0000']));	}	// 获取用户权限列表	public function privilege_list(){		$userid = $this->input->post('id',true);		if (empty($userid)) $userid = $this->get_user_info('id');		$version = $this->session->userdata('version');		$userData = $this->User_model->get_one(['id'=>$userid],'role');		$privilege_list = $this->Privilnode_model->get_all_privilnode(SYSTEM_ADMIN);		// 选中用户拥有的权限		if (!empty($userid)) {			$data = $this->User_model->get_one(['id'=>$userid],'privilege');			$privilegeArr = explode(',', $data['privilege']);			foreach ($privilege_list as &$v) {				if (!empty($version)) {					$v['name'] = $v['en_name'];				}				if ($userData['role'] == SYSTEM_ADMIN || in_array($v['id'], $privilegeArr)) {					$v['select'] = 1;				}else{					$v['select'] = 0;				}			}		}		// 权限分级		$res = list_to_tree($privilege_list, $pk='id', $pid = 'parentid', $child = 'sub_list', $root = 0);		exit(json_result('0000',$this->response['0000'],array('list'=>$res)));	}	// 修改用户权限	public function save_user_privilege(){		// $role = $this->get_user_info('role');        		$privilegeIds = $this->input->post('privilegeIds',true);		$userid = $this->input->post('userid',true);		// $res = $this->User_model->get_one($userid);		// 判断用户权限		// if ($role >= $res['role']) {		// 	exit(json_result('0011',$this->response['0011'],array()));		// }		// 参数判断		// if (empty($privilegeIds) || empty($userid)) {		// 	json_result('0000',$this->response['0000'],array());		// }		$this->User_model->update(['privilege'=>$privilegeIds],['id'=>$userid]);		$this->add_operation_log('update','修改用户权限 id:'.$userid,0);		$this->add_operation_log('update','Update user rights.User ID:'.$userid,0,1);		exit(json_result('0000',$this->response['0000'],array()));	}	// 修改用户头像    public function update_avatar(){        $path = '../upload/image';        $config['file_name'] = md5(uniqid());   // 设置图片名字               if (!file_exists('../upload')) mkdir('../upload');        if (!file_exists('../upload/image')) mkdir('../upload/image');        $config['upload_path']      = $path.'/';   // 设置图片上传路径        $config['allowed_types']    = '*';  // 设置图片上传格式        $config['max_size']     = 10240;   // 设置文件上传大小                $this->load->library('upload', $config);        if ( ! $this->upload->do_upload('file'))        {            $error = array('error' => $this->upload->display_errors('',''));            // if (empty($this->version)) {                // $data = array('error'=>transfer_error_tips($error['error']));            // }else{                $data = array('error'=>$error['error']);            // }            exit(json_result('0012',$this->response['0012'],$data));        }        else        {            $data = $this->upload->data();            $imagePath = '/upload/image/'.$data['file_name'];                        $id = $this->get_user_info('id');            $this->User_model->update(['avatar'=>$imagePath],['id'=>$id]);            exit(json_result('0000',$this->response['0000']));        }    }}?>
 |