123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314 |
- <?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 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('role',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";
- $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);
- $data['username'] = trim($this->input->post('account',true));
- $phone = $this->input->post('phone',true);
- $data['password'] = $this->input->post('password',true);
- $email = $this->input->post('email',true);
- $data['company'] = $this->input->post('company',true);
- 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(!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['company'])) 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['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($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']);
- $privilegeIds = $this->input->post('privilegeIds',true);
- $data['privilege'] = $privilegeIds;
- // 验证登录账号是否存在
- if ($this->User_model->getDataCount(array('username'=>$data['username']))) {
- 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');
- $data['role'] = intval($this->input->post('role'));
- if (empty($data['role'])) exit(json_result('0724',$this->response['0724']));
- $userid = $this->User_model->add_user($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 ($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($wher,'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;
- $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->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)));
- }
- }
- ?>
|