User.php 10 KB

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