User.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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. 'permCount' => 0,
  45. 'view' => 0,
  46. 'operating' => 0,
  47. 'parameter' => 0
  48. );
  49. $role = $this->get_user_info('role');
  50. $privilege = $this->get_user_info('privilege');
  51. if ($role == SYSTEM_ADMIN) {
  52. $data['permCount'] = 50;
  53. $data['view'] = 1;
  54. $data['operating'] = 1;
  55. $data['parameter'] = 1;
  56. }else{
  57. $arr = explode(',', $privilege);
  58. $data['permCount'] = count($arr);
  59. $list = $this->Privilnode_model->get_list(['id'=>$arr],'parentid as pid');
  60. foreach ($list as $key => $value) {
  61. if ($value['pid'] == 1) $data['view'] = 1;
  62. if ($value['pid'] == 18) $data['operating'] = 1;
  63. if ($value['pid'] == 39) $data['parameter'] = 1;
  64. }
  65. }
  66. $id = $this->get_user_info('id');
  67. $where = array('id !=' => $id);
  68. if ($role != SYSTEM_ADMIN) $where['pid'] = $id;
  69. $data['total'] = $this->User_model->get_count($where);
  70. $where['role'] = 2;
  71. $data['manuCount'] = $this->User_model->get_count($where);
  72. $where['role'] = 3;
  73. $data['suppCount'] = $this->User_model->get_count($where);
  74. $where['role'] = 4;
  75. $data['poCount'] = $this->User_model->get_count($where);
  76. $where['role'] = 5;
  77. $data['upaCount'] = $this->User_model->get_count($where);
  78. $where['role'] = 6;
  79. $data['monCount'] = $this->User_model->get_count($where);
  80. $where['role'] = 7;
  81. $data['conCount'] = $this->User_model->get_count($where);
  82. exit(json_result('0000',$this->response['0000'],$data));
  83. }
  84. // 修改用户密码
  85. public function password(){
  86. $username = $this->get_user_info('username');
  87. $old_pass = $this->input->post('old',true);
  88. $new_pass = $this->input->post('new',true);
  89. $new_second_pass = $this->input->post('new_second',true);
  90. if($new_pass == $old_pass){
  91. exit(json_result('0709',$this->response['0709'],array()));
  92. }
  93. // 验证确认密码
  94. if($new_pass != $new_second_pass){
  95. exit(json_result('0701',$this->response['0701'],array()));
  96. }
  97. // 验证密码长度
  98. if (mb_strlen($new_pass) < 6 || mb_strlen($new_pass) > 12) {
  99. exit(json_result('0703',$this->response['0703'],array()));
  100. }
  101. // 验证旧密码
  102. if (!$this->User_model->validate_password($username,md5($old_pass))) {
  103. exit(json_result('0702',$this->response['0702'],array()));
  104. }
  105. $id = $this->get_user_info('id');
  106. $new_pass = md5($new_pass);
  107. $res = $this->User_model->change_password($id,$new_pass);
  108. if($res){
  109. exit(json_result('0000',$this->response['0000'],array()));
  110. }else{
  111. exit(json_result('0704',$this->response['0704'],array()));
  112. }
  113. }
  114. // 修改账号状态
  115. public function block_user(){
  116. $userid = $this->input->post('id',true);
  117. $status = intval($this->input->post('status',true));
  118. if (empty($userid) || $status < 0) {
  119. exit(json_result('0007',$this->response['0007'],array()));
  120. }
  121. $this->User_model->update(array('status'=>$status),array('id'=>$userid));
  122. exit(json_result('0000',$this->response['0000'],array()));
  123. }
  124. // 用户列表
  125. public function sub_list(){
  126. $userRole = $this->get_user_info('role');
  127. $userid = $this->get_user_info('id');
  128. $where = array('id !='=>$userid);
  129. if ($userRole != SYSTEM_ADMIN) {
  130. $where['pid'] = $userid;
  131. }
  132. $role = intval($this->input->post('roleType',true));
  133. if (!empty($role)) $where['role'] = $role;
  134. $keywords = $this->input->post('keywords',true);
  135. if ($keywords !== NULL && $keywords !== '') $where['name|'] = $keywords;
  136. $field = "id,name,role,phone,email,status,company,username as account";
  137. $list = $this->User_model->get_list($where,$field);
  138. exit(json_result('0000',$this->response['0000'],array('list'=>$list)));
  139. }
  140. // 添加编辑用户
  141. public function user_update(){
  142. $where['id'] = intval($this->input->post('id',true));
  143. // $role = $this->get_user_info('role');
  144. // if ($role == COMPANY_CUSTOMER) {
  145. // exit(json_result('0011', $this->response['0011'], array()));
  146. // }
  147. $data['name'] = $this->input->post('name',true);
  148. $phone = $this->input->post('phone',true);
  149. $email = $this->input->post('email',true);
  150. $data['company'] = $this->input->post('company',true);
  151. // $data['role'] = intval($this->input->post('roleType'));
  152. $avatar = $this->input->post('avatar',true);
  153. if (isset($avatar) && !empty($avatar)) {
  154. $path = parse_url($avatar);
  155. $data['avatar'] = substr($path['path'], 1);
  156. }
  157. if (!empty($phone)) $data['phone'] = $phone;
  158. if (!empty($telephone)) $data['telephone'] = $telephone;
  159. if (!empty($email)) $data['email'] = $email;
  160. if(!isset($data['name']) || isset($data['name']) == '') exit(json_result('0707',$this->response['0707'],array()));
  161. if(empty($data['company'])) exit(json_result('0712',$this->response['0712'],array()));
  162. $res = $this->Company_model->get_one(['id'=>$data['company']],'type');
  163. if (empty($res) || empty($res['type'])) exit(json_result('0007',$this->response['0007']));
  164. $data['role'] = $res['type'];
  165. // $data['zone'] = empty($zone) ? '' : $zone;
  166. // 验证请求数据
  167. $config = array();
  168. if(!empty($data['phone'])){
  169. $config[] = array(
  170. 'field' => 'phone',
  171. 'label' => 'Phone',
  172. 'rules' => 'numeric|exact_length[11]',
  173. 'errors' => array(
  174. 'numeric' => '0718',
  175. 'exact_length' => '0718'
  176. )
  177. );
  178. }
  179. if (!empty($data['email'])) {
  180. $config[] = array(
  181. 'field' => 'email',
  182. 'label' => 'Email',
  183. 'rules' => 'valid_email',
  184. 'errors' => array(
  185. 'valid_email' => '0719',
  186. )
  187. );
  188. }
  189. if (empty($where['id'])) { // 添加用户
  190. $data['username'] = trim($this->input->post('account',true));
  191. $data['password'] = $this->input->post('password',true);
  192. $config[] = array(
  193. 'field' => 'password',
  194. 'label' => 'Password',
  195. 'rules' => 'min_length[6]',
  196. 'errors' => array(
  197. 'min_length' => '0720',
  198. )
  199. );
  200. if (!empty($config)) {
  201. $this->load->library('form_validation');
  202. $this->form_validation->set_rules($config);
  203. if ($this->form_validation->run() == FALSE){
  204. $errors = $this->form_validation->error_array();
  205. exit(json_result(current($errors),$this->response[current($errors)],array()));
  206. }
  207. }
  208. if(!isset($data['username']) || $data['username'] == '') exit(json_result('0708',$this->response['0708'],array()));
  209. if(mb_strlen($data['username']) > 20) exit(json_result('0723',$this->response['0723'],array()));
  210. if (empty($data['password'])) {
  211. exit(json_result('0713',$this->response['0713'],array()));
  212. }
  213. $data['password'] = md5($data['password']);
  214. $privilegeIds = $this->input->post('privilegeIds',true);
  215. $data['privilege'] = $privilegeIds;
  216. // 验证登录账号是否存在
  217. if ($this->User_model->getDataCount(array('username'=>$data['username'],'role'=>$data['role']))) {
  218. exit(json_result('0706',$this->response['0706'],array()));
  219. }
  220. if (!empty($data['phone']) && $this->User_model->getDataCount(array('phone'=>$data['phone']))) {
  221. exit(json_result('0721',$this->response['0721'],array()));
  222. }
  223. if (!empty($data['email']) && $this->User_model->getDataCount(array('email'=>$data['email']))) {
  224. exit(json_result('0722',$this->response['0722'],array()));
  225. }
  226. $data['pid'] = $this->get_user_info('id');
  227. if (empty($data['role'])) exit(json_result('0724',$this->response['0724']));
  228. $userid = $this->User_model->add($data);
  229. $this->add_operation_log('insert',"添加用户,用户名\"{$data['name']}\"",0);
  230. $this->add_operation_log('insert',"Add user.User name:\"{$data['name']}\"",0,1);
  231. }else{ // 编辑用户
  232. unset($data['password']);
  233. if (!empty($config)) {
  234. $this->load->library('form_validation');
  235. $this->form_validation->set_rules($config);
  236. if ($this->form_validation->run() == FALSE){
  237. $errors = $this->form_validation->error_array();
  238. exit(json_result(current($errors),$this->response[current($errors)],array()));
  239. }
  240. }
  241. // if ($this->User_model->getDataCount(array('username'=>$data['username']),$where['id'])) {
  242. // exit(json_result('0706',$this->response['0706'],array()));
  243. // }
  244. if (!empty($data['phone']) && $this->User_model->getDataCount(array('phone'=>$data['phone']),$where['id'])) {
  245. exit(json_result('0721',$this->response['0721'],array()));
  246. }
  247. if (!empty($data['email']) && $this->User_model->getDataCount(array('email'=>$data['email']),$where['id'])) {
  248. exit(json_result('0722',$this->response['0722'],array()));
  249. }
  250. $oldData = $this->User_model->get_one($where,'company');
  251. if ($oldData['company'] != $data['company']) {
  252. $this->Project_model->update(array('company'=>$data['company']),array('userId'=>$where['id'],'company'=>$data['company']));
  253. }
  254. $privilegeIds = $this->input->post('privilegeIds',true);
  255. if ($privilegeIds !== NULL && $privilegeIds !== '') $data['privilege'] = $privilegeIds;
  256. if (empty($data['role'])) exit(json_result('0724',$this->response['0724']));
  257. $this->User_model->update($data,$where);
  258. $userid = $where['id'];
  259. $this->add_operation_log('update',"修改用户,用户名\"{$data['name']}\"",0);
  260. $this->add_operation_log('update',"Update user.User name:\"{$data['name']}\"",0,1);
  261. }
  262. exit(json_result('0000',$this->response['0000'],array('id'=>$userid)));
  263. }
  264. // 删除账户
  265. public function del(){
  266. $id = intval($this->input->post('id',true));
  267. if (empty($id)) exit(json_result('0007',$this->response['0007']));
  268. $this->User_model->delete(['id'=>$id]);
  269. exit(json_result('0000',$this->response['0000']));
  270. }
  271. // 获取用户权限列表
  272. public function privilege_list(){
  273. $userid = $this->input->post('id',true);
  274. if (empty($userid)) $userid = $this->get_user_info('id');
  275. $version = $this->session->userdata('version');
  276. $userData = $this->User_model->get_one(['id'=>$userid],'role');
  277. $privilege_list = $this->Privilnode_model->get_all_privilnode(SYSTEM_ADMIN);
  278. // 选中用户拥有的权限
  279. if (!empty($userid)) {
  280. $data = $this->User_model->get_one(['id'=>$userid],'privilege');
  281. $privilegeArr = explode(',', $data['privilege']);
  282. foreach ($privilege_list as &$v) {
  283. if (!empty($version)) {
  284. $v['name'] = $v['en_name'];
  285. }
  286. if ($userData['role'] == SYSTEM_ADMIN || in_array($v['id'], $privilegeArr)) {
  287. $v['select'] = 1;
  288. }else{
  289. $v['select'] = 0;
  290. }
  291. }
  292. }
  293. // 权限分级
  294. $res = list_to_tree($privilege_list, $pk='id', $pid = 'parentid', $child = 'sub_list', $root = 0);
  295. exit(json_result('0000',$this->response['0000'],array('list'=>$res)));
  296. }
  297. // 修改用户权限
  298. public function save_user_privilege(){
  299. // $role = $this->get_user_info('role');
  300. $privilegeIds = $this->input->post('privilegeIds',true);
  301. $userid = $this->input->post('userid',true);
  302. // $res = $this->User_model->get_one($userid);
  303. // 判断用户权限
  304. // if ($role >= $res['role']) {
  305. // exit(json_result('0011',$this->response['0011'],array()));
  306. // }
  307. // 参数判断
  308. // if (empty($privilegeIds) || empty($userid)) {
  309. // json_result('0000',$this->response['0000'],array());
  310. // }
  311. $this->User_model->update(['privilege'=>$privilegeIds],['id'=>$userid]);
  312. $this->add_operation_log('update','修改用户权限 id:'.$userid,0);
  313. $this->add_operation_log('update','Update user rights.User ID:'.$userid,0,1);
  314. exit(json_result('0000',$this->response['0000'],array()));
  315. }
  316. // 修改用户头像
  317. public function update_avatar(){
  318. $path = '../upload/image';
  319. $config['file_name'] = md5(uniqid()); // 设置图片名字
  320. if (!file_exists('../upload')) mkdir('../upload');
  321. if (!file_exists('../upload/image')) mkdir('../upload/image');
  322. $config['upload_path'] = $path.'/'; // 设置图片上传路径
  323. $config['allowed_types'] = '*'; // 设置图片上传格式
  324. $config['max_size'] = 10240; // 设置文件上传大小
  325. $this->load->library('upload', $config);
  326. if ( ! $this->upload->do_upload('file'))
  327. {
  328. $error = array('error' => $this->upload->display_errors('',''));
  329. // if (empty($this->version)) {
  330. // $data = array('error'=>transfer_error_tips($error['error']));
  331. // }else{
  332. $data = array('error'=>$error['error']);
  333. // }
  334. exit(json_result('0012',$this->response['0012'],$data));
  335. }
  336. else
  337. {
  338. $data = $this->upload->data();
  339. $imagePath = '/upload/image/'.$data['file_name'];
  340. $id = $this->get_user_info('id');
  341. $this->User_model->update(['avatar'=>$imagePath],['id'=>$id]);
  342. exit(json_result('0000',$this->response['0000']));
  343. }
  344. }
  345. }
  346. ?>