User.php 11 KB

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