User.php 14 KB

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