User.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <?php
  2. if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  3. include_once(FCPATH . 'application/controllers/Base_Controller.php');
  4. class User extends Base_Controller{
  5. public function __construct()
  6. {
  7. parent::__construct();
  8. $this->load->model('Company_model');
  9. $this->load->model('User_model');
  10. $this->load->model('Feedback_model');
  11. $this->load->model('Privilnode_model');
  12. $this->load->model('Zone_model');
  13. $this->load->model('Project_model');
  14. $this->load->model('Global_location_model');
  15. }
  16. // 个人信息
  17. public function info(){
  18. $id = intval($this->input->post('id',true));
  19. $data = array();
  20. if (empty($id)) {
  21. $data['role'] = $this->get_user_info('role');
  22. $data['id'] = $this->get_user_info('id');
  23. $data['phone'] = $this->get_user_info('phone');
  24. $data['email'] = $this->get_user_info('email');
  25. $data['name'] = $this->get_user_info('name');
  26. $data['company'] = $this->get_user_info('company');
  27. }else{
  28. $data = $this->User_model->get_one(['id'=>$id],'role,id,name,company,phone,email');
  29. }
  30. exit(json_result('0000',$this->response['0000'],$data));
  31. }
  32. // 账号统计信息
  33. public function data(){
  34. $data = array(
  35. 'total' => 0,
  36. 'manuCount' => 0,
  37. 'suppCount' => 0,
  38. 'poCount' => 0,
  39. 'upaCount' => 0,
  40. 'monCount' => 0,
  41. 'conCount' => 0,
  42. );
  43. $role = $this->get_user_info('role');
  44. $id = $this->get_user_info('id');
  45. $where = array(['id !=' => $id]);
  46. if ($role != SYSTEM_ADMIN) $where['pid'] = $id;
  47. $data['total'] = $this->User_model->get_count($where);
  48. $where['role'] = 2;
  49. $data['manuCount'] = $this->User_model->get_count($where);
  50. $where['role'] = 3;
  51. $data['suppCount'] = $this->User_model->get_count($where);
  52. $where['role'] = 4;
  53. $data['poCount'] = $this->User_model->get_count($where);
  54. $where['role'] = 5;
  55. $data['upaCount'] = $this->User_model->get_count($where);
  56. $where['role'] = 6;
  57. $data['monCount'] = $this->User_model->get_count($where);
  58. $where['role'] = 7;
  59. $data['conCount'] = $this->User_model->get_count($where);
  60. exit(json_result('0000',$this->response['0000'],$data));
  61. }
  62. // 修改用户密码
  63. public function password(){
  64. $username = $this->get_user_info('username');
  65. $old_pass = $this->input->post('old',true);
  66. $new_pass = $this->input->post('new',true);
  67. $new_second_pass = $this->input->post('new_second',true);
  68. if($new_pass == $old_pass){
  69. exit(json_result('0709',$this->response['0709'],array()));
  70. }
  71. // 验证确认密码
  72. if($new_pass != $new_second_pass){
  73. exit(json_result('0701',$this->response['0701'],array()));
  74. }
  75. // 验证密码长度
  76. if (mb_strlen($new_pass) < 6 || mb_strlen($new_pass) > 12) {
  77. exit(json_result('0703',$this->response['0703'],array()));
  78. }
  79. // 验证旧密码
  80. if (!$this->User_model->validate_password($username,md5($old_pass))) {
  81. exit(json_result('0702',$this->response['0702'],array()));
  82. }
  83. $id = $this->get_user_info('id');
  84. $new_pass = md5($new_pass);
  85. $res = $this->User_model->change_password($id,$new_pass);
  86. if($res){
  87. exit(json_result('0000',$this->response['0000'],array()));
  88. }else{
  89. exit(json_result('0704',$this->response['0704'],array()));
  90. }
  91. }
  92. // 修改账号状态
  93. public function block_user(){
  94. $userid = $this->input->post('id',true);
  95. $status = intval($this->input->post('status',true));
  96. if (empty($userid) || $status < 0) {
  97. exit(json_result('0007',$this->response['0007'],array()));
  98. }
  99. $this->User_model->update(array('status'=>$status),array('id'=>$userid));
  100. exit(json_result('0000',$this->response['0000'],array()));
  101. }
  102. // 用户列表
  103. public function sub_list(){
  104. $userRole = $this->get_user_info('role');
  105. $userid = $this->get_user_info('id');
  106. $where = array('id !='=>$userid);
  107. if ($userRole != SYSTEM_ADMIN) {
  108. $where['pid'] = $userid;
  109. }
  110. $role = intval($this->input->post('role',true));
  111. if (!empty($role)) $where['role'] = $role;
  112. $keywords = $this->input->post('keywords',true);
  113. if ($keywords !== NULL && $keywords !== '') $where['name|'] = $keywords;
  114. $field = "id,name,role,phone,email,status";
  115. $list = $this->User_model->get_list($where,$field);
  116. exit(json_result('0000',$this->response['0000'],array('list'=>$list)));
  117. }
  118. // 添加编辑用户
  119. public function user_update(){
  120. $where['id'] = intval($this->input->post('id',true));
  121. $role = $this->get_user_info('role');
  122. if ($role == COMPANY_CUSTOMER) {
  123. exit(json_result('0011', $this->response['0011'], array()));
  124. }
  125. $data['name'] = $this->input->post('name',true);
  126. $data['username'] = trim($this->input->post('account',true));
  127. $phone = $this->input->post('phone',true);
  128. $data['password'] = $this->input->post('password',true);
  129. $email = $this->input->post('email',true);
  130. $data['company'] = $this->input->post('company',true);
  131. if (!empty($phone)) $data['phone'] = $phone;
  132. if (!empty($telephone)) $data['telephone'] = $telephone;
  133. if (!empty($email)) $data['email'] = $email;
  134. if(!isset($data['name']) || isset($data['name']) == '') exit(json_result('0707',$this->response['0707'],array()));
  135. if(!isset($data['username']) || $data['username'] == '') exit(json_result('0708',$this->response['0708'],array()));
  136. if(mb_strlen($data['username']) > 20) exit(json_result('0723',$this->response['0723'],array()));
  137. if(empty($data['company'])) exit(json_result('0712',$this->response['0712'],array()));
  138. // $data['zone'] = empty($zone) ? '' : $zone;
  139. // 验证请求数据
  140. $config = array();
  141. $config[] = array(
  142. 'field' => 'password',
  143. 'label' => 'Password',
  144. 'rules' => 'min_length[6]',
  145. 'errors' => array(
  146. 'min_length' => '0720',
  147. )
  148. );
  149. if(!empty($data['phone'])){
  150. $config[] = array(
  151. 'field' => 'phone',
  152. 'label' => 'Phone',
  153. 'rules' => 'numeric|exact_length[11]',
  154. 'errors' => array(
  155. 'numeric' => '0718',
  156. 'exact_length' => '0718'
  157. )
  158. );
  159. }
  160. if (!empty($data['email'])) {
  161. $config[] = array(
  162. 'field' => 'email',
  163. 'label' => 'Email',
  164. 'rules' => 'valid_email',
  165. 'errors' => array(
  166. 'valid_email' => '0719',
  167. )
  168. );
  169. }
  170. if (!empty($config)) {
  171. $this->load->library('form_validation');
  172. $this->form_validation->set_rules($config);
  173. if ($this->form_validation->run() == FALSE){
  174. $errors = $this->form_validation->error_array();
  175. exit(json_result(current($errors),$this->response[current($errors)],array()));
  176. }
  177. }
  178. if (empty($where['id'])) { // 添加用户
  179. if (empty($data['password'])) {
  180. exit(json_result('0713',$this->response['0713'],array()));
  181. }
  182. $data['password'] = md5($data['password']);
  183. // 验证登录账号是否存在
  184. if ($this->User_model->getDataCount(array('username'=>$data['username']))) {
  185. exit(json_result('0706',$this->response['0706'],array()));
  186. }
  187. if (!empty($data['phone']) && $this->User_model->getDataCount(array('phone'=>$data['phone']))) {
  188. exit(json_result('0721',$this->response['0721'],array()));
  189. }
  190. if (!empty($data['email']) && $this->User_model->getDataCount(array('email'=>$data['email']))) {
  191. exit(json_result('0722',$this->response['0722'],array()));
  192. }
  193. $data['pid'] = $this->get_user_info('id');
  194. $data['role'] = intval($this->input->post('role'));
  195. if (empty($data['role'])) exit(json_result('0724',$this->response['0724']));
  196. $userid = $this->User_model->add_user($data);
  197. $this->add_operation_log('insert',"添加用户,用户名\"{$data['name']}\"",0);
  198. $this->add_operation_log('insert',"Add user.User name:\"{$data['name']}\"",0,1);
  199. }else{ // 编辑用户
  200. unset($data['password']);
  201. if ($this->User_model->getDataCount(array('username'=>$data['username']),$where['id'])) {
  202. exit(json_result('0706',$this->response['0706'],array()));
  203. }
  204. if (!empty($data['phone']) && $this->User_model->getDataCount(array('phone'=>$data['phone']),$where['id'])) {
  205. exit(json_result('0721',$this->response['0721'],array()));
  206. }
  207. if (!empty($data['email']) && $this->User_model->getDataCount(array('email'=>$data['email']),$where['id'])) {
  208. exit(json_result('0722',$this->response['0722'],array()));
  209. }
  210. $this->User_model->update_user($data,$where['id']);
  211. $userid = $where['id'];
  212. $this->add_operation_log('update',"修改用户,用户名\"{$data['name']}\"",0);
  213. $this->add_operation_log('update',"Update user.User name:\"{$data['name']}\"",0,1);
  214. }
  215. exit(json_result('0000',$this->response['0000'],array('id'=>$userid)));
  216. }
  217. // 删除账户
  218. public function del(){
  219. $id = intval($this->input->post('id',true));
  220. if (empty($id)) exit(json_result('0007',$this->response['0007']));
  221. $this->User_model->delete(['id'=>$id]);
  222. exit(json_result('0000',$this->response['0000']));
  223. }
  224. }
  225. ?>