wzh пре 5 година
родитељ
комит
97593c1de4
1 измењених фајлова са 517 додато и 0 уклоњено
  1. 517 0
      api/application/controllers/User.php

+ 517 - 0
api/application/controllers/User.php

@@ -0,0 +1,517 @@
+<?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('Zone_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');
+		}else{
+			$data = $this->User_model->get_one(['id'=>$id],'role,id,name,company,phone,email');
+		}
+
+        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,
+		);
+
+		$role = $this->get_user_info('role');
+		$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 update_user_info(){
+		$userid = $this->get_user_info('id');
+		$data['realname'] = $this->input->post('realname',true);
+		$mobile = $this->input->post('mobile',true);
+		$data['username'] = $this->input->post('name',true);
+		$telephone = $this->input->post('telephone',true);
+		$email = $this->input->post('email',true);
+		$address = $this->input->post('address',true);
+		$memo = $this->input->post('memo',true);
+		$avatar = $this->input->post('avatar',true);
+
+		if (!empty($avatar)) $data['avatar'] = $avatar;
+		if (!empty($mobile)) $data['mobile'] = $mobile;
+		if (!empty($telephone)) $data['telephone'] = $telephone;
+		if (!empty($email)) $data['email'] = $email;
+		if (!empty($address)) $data['address'] = $address;
+		if (!empty($memo)) $data['memo'] = $memo;
+
+		// 检测表单数据
+		if(empty($data['realname'])) exit(json_result('0707',$this->response['0707'],array()));
+		if(empty($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 ($this->User_model->getDataCount(array('username'=>$data['username']),$userid)) {
+			exit(json_result('0706',$this->response['0706'],array()));
+		}
+
+		// 验证请求数据
+		$config = array();
+		if(!empty($data['mobile'])){
+			$config[] = array(
+				        'field' => 'mobile',
+				        'label' => 'Mobile',
+				        '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($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()));
+		    }
+		}
+
+		$this->User_model->update_user($data,$userid);
+		$this->userinfo = $this->User_model->get_user_by_name($data['username']);
+		exit(json_result('0000',$this->response['0000'],array('id'=>$userid)));
+	}
+
+	// 修改用户密码
+	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('userid',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('role',true));
+		if (!empty($role)) $where['role'] = $role;
+
+		$field = "id,name,role,phone,email,status";
+		$list = $this->User_model->get_list($where,$field);
+		exit(json_result('0000',$this->response['0000'],array('list'=>$list)));
+	}
+
+	// 添加编辑用户
+	public function user_update(){
+		$where['id'] = $this->input->post('userid',true);
+		$role = $this->get_user_info('role');
+        if ($role == COMPANY_CUSTOMER) {
+            exit(json_result('0011', $this->response['0011'], array()));
+        }
+		$data['realname'] = $this->input->post('realname',true);
+		$data['username'] = trim($this->input->post('name',true));
+		$telephone = $this->input->post('telephone',true);
+		$mobile = $this->input->post('mobile',true);
+		$data['password'] = $this->input->post('password',true);
+		$email = $this->input->post('email',true);
+		$data['companyid'] = $this->input->post('companyid',true);
+		$address = $this->input->post('address',true);
+		$memo = $this->input->post('memo',true);
+		$avatar = $this->input->post('avatar',true);
+		$data['zone'] = $this->input->post('zone',true);
+		
+		if (!empty($avatar)) $data['avatar'] = $avatar;
+		if (!empty($mobile)) $data['mobile'] = $mobile;
+		if (!empty($telephone)) $data['telephone'] = $telephone;
+		if (!empty($email)) $data['email'] = $email;
+		if (!empty($address)) $data['address'] = $address;
+		if (!empty($memo)) $data['memo'] = $memo;
+		
+		if(!isset($data['realname']) || isset($data['realname']) == '') exit(json_result('0707',$this->response['0707'],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['companyid'])) exit(json_result('0712',$this->response['0712'],array()));
+
+		// $data['zone'] = empty($zone) ? '' : $zone;
+		// 验证请求数据
+		$config = array();
+		$config[] = array(
+		        'field' => 'password',
+		        'label' => 'Password',
+		        'rules' => 'min_length[6]',
+		        'errors' => array(
+		            'min_length' => '0720',
+		        )
+		    );
+		if(!empty($data['mobile'])){
+			$config[] = array(
+				        'field' => 'mobile',
+				        'label' => 'Mobile',
+				        '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($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 (empty($where['id'])) { // 添加用户
+			if (empty($data['password'])) {
+				exit(json_result('0713',$this->response['0713'],array()));
+			}
+			$data['password'] = md5($data['password']);
+
+			// 验证登录账号是否存在
+			if ($this->User_model->getDataCount(array('username'=>$data['username']))) {
+				exit(json_result('0706',$this->response['0706'],array()));
+			}
+			if (!empty($data['mobile']) && $this->User_model->getDataCount(array('mobile'=>$data['mobile']))) {
+                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['avatar'] = empty($data['avatar']) ? 'assets/avatars/user.png' : $data['avatar'];
+			$data['parentid'] = $this->get_user_info('id');
+			$data['role'] = $role == 1 ? 2 : 3;
+			$data['privilege'] = $this->Privilnode_model->get_default_privilege($data['role']);
+			if (empty($data['zone']) && $data['role'] == 2) {
+				$projectids = $this->Project_model->get_project_ids(array('company'=>$data['companyid']));
+				$data['zone'] = !empty($projectids) ? implode(',', $projectids) : '';
+			}
+
+			$userid = $this->User_model->add_user($data);
+
+			$this->add_operation_log('insert',"添加用户,用户名\"{$data['realname']}\"",0);
+			$this->add_operation_log('insert',"Add user.User name:\"{$data['realname']}\"",0,1);
+		}else{  // 编辑用户
+			if (empty($data['password'])) {
+				unset($data['password']);
+			}else  {
+				$data['password'] = md5($data['password']);
+			}
+			$res = $this->User_model->getOne($where['id'],'role,companyid');
+			if (intval($role) >= intval($res['role'])) {
+				exit(json_result('0011',$this->response['0011'],array()));
+			}
+			if ($data['companyid'] != $res['companyid'] && empty($data['zone']) && $res['role'] == 2) {
+				$projectids = $this->Project_model->get_project_ids(array('company'=>$data['companyid']));
+				$data['zone'] = !empty($projectids) ? implode(',', $projectids) : 0;
+			}
+			if ($this->User_model->getDataCount(array('username'=>$data['username']),$where['id'])) {
+				exit(json_result('0706',$this->response['0706'],array()));
+			}
+			if (!empty($data['mobile']) && $this->User_model->getDataCount(array('mobile'=>$data['mobile']),$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()));
+            }
+
+			$this->User_model->update_user($data,$where['id']);
+
+			$userid = $where['id'];
+			$this->add_operation_log('update',"修改用户,用户名\"{$data['realname']}\"",0);
+			$this->add_operation_log('update',"Update user.User name:\"{$data['realname']}\"",0,1);
+		}
+
+		exit(json_result('0000',$this->response['0000'],array('id'=>$userid)));
+	}
+
+	// 获取用户权限列表
+	public function privilege_list(){
+		$userid = $this->input->post('userid',true);
+
+		if (empty($userid)) {
+			exit(json_result('0007',$this->response['0007'],array()));
+		}
+
+		$version = $this->session->userdata('version');
+		$userData = $this->User_model->getOne($userid,'role');
+
+		$privilege_list = $this->Privilnode_model->get_all_privilnode(SYSTEM_ADMIN);
+		// 选中用户拥有的权限
+		if (!empty($userid)) {
+			$data = $this->User_model->getOne($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');
+        // if ($role == COMPANY_CUSTOMER) {
+        //     exit(json_result('0011', $this->response['0011'], array()));
+        // }
+        
+		$privilegeIds = $this->input->post('privilegeIds',true);
+		$userid = $this->input->post('userid',true);
+
+		$res = $this->User_model->getOne($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->change_privilege($privilegeIds,$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 feedback(){
+		$data['username'] = $this->input->post('name',true);
+		$data['mobile'] = $this->input->post('mobile',true);
+		$data['detail'] = $this->input->post('detail',true);
+
+		if(empty($data['username'])) exit(json_result('0714',$this->response['0714'],array()));
+		if(empty($data['mobile'])) exit(json_result('0715',$this->response['0715'],array()));
+		if(empty($data['detail'])) exit(json_result('0716',$this->response['0716'],array()));
+		$data['feedtime'] = date('Y-m-d H:i:s');
+
+		$res = $this->Feedback_model->insert($data);
+
+		exit(json_result('0000',$this->response['0000'],array('id'=>$res)));
+	}
+
+	// 获取项目权限列表
+	public function get_porject_list(){
+		$userid = intval($this->input->post('userid',true));
+		$companyid = intval($this->input->post('companyid',true));
+		$zoneId = intval($this->input->post('zoneid',true));
+		$countryId = intval($this->input->post('countryId',true));
+		$provinceId = intval($this->input->post('provinceId',true));
+		$cityId = intval($this->input->post('cityId',true));
+		$areaId = intval($this->input->post('areaId',true));
+
+		$idArr = array();
+		if (!empty($areaId)) {
+			$idArr = array($areaId);
+		}elseif (!empty($cityId)) {
+			$res1 = $this->db->query('select id from global_location where pid = '.$cityId)->result_array();
+			if (empty($res1)) {
+				$idArr = array($cityId);
+			}else{
+				$idArr = array_column($res1, 'id');
+			}	
+		}elseif (!empty($provinceId)) {
+			$res1 = $this->db->query('select id from global_location where pid = '.$provinceId)->result_array();
+			if (empty($res1)) {
+				$idArr = array($provinceId);
+			}else{
+				$idStr1 = implode(',', array_column($res1, 'id'));
+				$res2 = $this->db->query('select id from global_location where pid in ('.$idStr1.')')->result_array();
+				if (empty($res2)) {
+					$idArr = array_column($res1, 'id');
+				}else{
+					$idArr = array_column($res2, 'id');
+				}
+			}
+		}elseif (!empty($countryId)) {
+			$res1 = $this->db->query('select id from global_location where pid = '.$countryId)->result_array();
+			if (empty($res1)) {
+				$idArr = array($countryId);
+			}else{
+				$idStr1 = implode(',', array_column($res1, 'id'));
+				$res2 = $this->db->query('select id from global_location where pid in ('.$idStr1.')')->result_array();
+				if (empty($res2)) {
+					$idArr = array_column($res1, 'id');
+				}else{
+					$idStr2 = implode(',', array_column($res1, 'id'));
+					$res3 = $this->db->query('select id from global_location where pid in ('.$idStr2.')')->result_array();
+					if (empty($res3)) {
+						$idArr = array_column($res2, 'id');
+					}else{
+						$idArr = array_column($res3, 'id');
+					}
+				}
+			}
+		}
+		$where = array();
+		if (!empty($companyid)) {
+			$where['company'] = $companyid;
+		}
+		if (!empty($idArr)) {
+			$where['cityid'] = $idArr;
+		}
+		$projectList = $this->Project_model->get_list($where, 'id,projectname as name',NULL, NULL, 'id desc');
+		$userData = $this->User_model->getOne($userid,'zone,role');
+		$hasArr = empty($userData['zone']) ? [] : array_unique(explode(',', $userData['zone']));
+		foreach ($projectList as &$p) {
+			if ($userData['role'] == SYSTEM_ADMIN || in_array($p['id'], $hasArr)) {
+				$p['select'] = 1;
+			}else{
+				$p['select'] = 0;
+			}
+		}
+		
+		exit(json_result('0000',$this->response['0000'],array('projects'=>$projectList)));
+	}
+
+	// 修改用户项目权限
+	public function save_user_project(){
+		$projects = $this->input->post('projects',true);
+		$userid = intval($this->input->post('userid'));
+		$role = $this->get_user_info('role');
+		if ($userid <= 0) {
+			exit(json_result('0007',$this->response['0007'],array()));
+		}
+
+		if(empty($projects)) exit(json_result('0717',$this->response['0717'],array()));
+
+		$res = $this->User_model->getOne($userid);
+		// 判断用户权限
+		if ($role >= $res['role']) {
+			exit(json_result('0011',$this->response['0011'],array()));
+		}
+
+		$this->User_model->update_user(array('zone'=>$projects),$userid);
+
+		exit(json_result('0000',$this->response['0000'],array()));
+	}
+}
+?>