User.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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('role',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";
  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('role'));
  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. // $data['zone'] = empty($zone) ? '' : $zone;
  163. // 验证请求数据
  164. $config = array();
  165. if(!empty($data['phone'])){
  166. $config[] = array(
  167. 'field' => 'phone',
  168. 'label' => 'Phone',
  169. 'rules' => 'numeric|exact_length[11]',
  170. 'errors' => array(
  171. 'numeric' => '0718',
  172. 'exact_length' => '0718'
  173. )
  174. );
  175. }
  176. if (!empty($data['email'])) {
  177. $config[] = array(
  178. 'field' => 'email',
  179. 'label' => 'Email',
  180. 'rules' => 'valid_email',
  181. 'errors' => array(
  182. 'valid_email' => '0719',
  183. )
  184. );
  185. }
  186. if (empty($where['id'])) { // 添加用户
  187. $data['username'] = trim($this->input->post('account',true));
  188. $data['password'] = $this->input->post('password',true);
  189. $config[] = array(
  190. 'field' => 'password',
  191. 'label' => 'Password',
  192. 'rules' => 'min_length[6]',
  193. 'errors' => array(
  194. 'min_length' => '0720',
  195. )
  196. );
  197. if (!empty($config)) {
  198. $this->load->library('form_validation');
  199. $this->form_validation->set_rules($config);
  200. if ($this->form_validation->run() == FALSE){
  201. $errors = $this->form_validation->error_array();
  202. exit(json_result(current($errors),$this->response[current($errors)],array()));
  203. }
  204. }
  205. if(!isset($data['username']) || $data['username'] == '') exit(json_result('0708',$this->response['0708'],array()));
  206. if(mb_strlen($data['username']) > 20) exit(json_result('0723',$this->response['0723'],array()));
  207. if (empty($data['password'])) {
  208. exit(json_result('0713',$this->response['0713'],array()));
  209. }
  210. $data['password'] = md5($data['password']);
  211. $privilegeIds = $this->input->post('privilegeIds',true);
  212. $data['privilege'] = $privilegeIds;
  213. // 验证登录账号是否存在
  214. if ($this->User_model->getDataCount(array('username'=>$data['username'],'role'=>$data['role']))) {
  215. exit(json_result('0706',$this->response['0706'],array()));
  216. }
  217. if (!empty($data['phone']) && $this->User_model->getDataCount(array('phone'=>$data['phone']))) {
  218. exit(json_result('0721',$this->response['0721'],array()));
  219. }
  220. if (!empty($data['email']) && $this->User_model->getDataCount(array('email'=>$data['email']))) {
  221. exit(json_result('0722',$this->response['0722'],array()));
  222. }
  223. $data['pid'] = $this->get_user_info('id');
  224. if (empty($data['role'])) exit(json_result('0724',$this->response['0724']));
  225. $userid = $this->User_model->add($data);
  226. $this->add_operation_log('insert',"添加用户,用户名\"{$data['name']}\"",0);
  227. $this->add_operation_log('insert',"Add user.User name:\"{$data['name']}\"",0,1);
  228. }else{ // 编辑用户
  229. unset($data['password']);
  230. if (!empty($config)) {
  231. $this->load->library('form_validation');
  232. $this->form_validation->set_rules($config);
  233. if ($this->form_validation->run() == FALSE){
  234. $errors = $this->form_validation->error_array();
  235. exit(json_result(current($errors),$this->response[current($errors)],array()));
  236. }
  237. }
  238. // if ($this->User_model->getDataCount(array('username'=>$data['username']),$where['id'])) {
  239. // exit(json_result('0706',$this->response['0706'],array()));
  240. // }
  241. if (!empty($data['phone']) && $this->User_model->getDataCount(array('phone'=>$data['phone']),$where['id'])) {
  242. exit(json_result('0721',$this->response['0721'],array()));
  243. }
  244. if (!empty($data['email']) && $this->User_model->getDataCount(array('email'=>$data['email']),$where['id'])) {
  245. exit(json_result('0722',$this->response['0722'],array()));
  246. }
  247. $oldData = $this->User_model->get_one($where,'company');
  248. if ($oldData['company'] != $data['company']) {
  249. $this->Project_model->update(array('company'=>$data['company']),array('userId'=>$where['id'],'company'=>$data['company']));
  250. }
  251. $privilegeIds = $this->input->post('privilegeIds',true);
  252. if ($privilegeIds !== NULL && $privilegeIds !== '') $data['privilege'] = $privilegeIds;
  253. if (empty($data['role'])) exit(json_result('0724',$this->response['0724']));
  254. $this->User_model->update($data,$where);
  255. $userid = $where['id'];
  256. $this->add_operation_log('update',"修改用户,用户名\"{$data['name']}\"",0);
  257. $this->add_operation_log('update',"Update user.User name:\"{$data['name']}\"",0,1);
  258. }
  259. exit(json_result('0000',$this->response['0000'],array('id'=>$userid)));
  260. }
  261. // 删除账户
  262. public function del(){
  263. $id = intval($this->input->post('id',true));
  264. if (empty($id)) exit(json_result('0007',$this->response['0007']));
  265. $this->User_model->delete(['id'=>$id]);
  266. exit(json_result('0000',$this->response['0000']));
  267. }
  268. // 获取用户权限列表
  269. public function privilege_list(){
  270. $userid = $this->input->post('id',true);
  271. if (empty($userid)) $userid = $this->get_user_info('id');
  272. $version = $this->session->userdata('version');
  273. $userData = $this->User_model->get_one(['id'=>$userid],'role');
  274. $privilege_list = $this->Privilnode_model->get_all_privilnode(SYSTEM_ADMIN);
  275. // 选中用户拥有的权限
  276. if (!empty($userid)) {
  277. $data = $this->User_model->get_one(['id'=>$userid],'privilege');
  278. $privilegeArr = explode(',', $data['privilege']);
  279. foreach ($privilege_list as &$v) {
  280. if (!empty($version)) {
  281. $v['name'] = $v['en_name'];
  282. }
  283. if ($userData['role'] == SYSTEM_ADMIN || in_array($v['id'], $privilegeArr)) {
  284. $v['select'] = 1;
  285. }else{
  286. $v['select'] = 0;
  287. }
  288. }
  289. }
  290. // 权限分级
  291. $res = list_to_tree($privilege_list, $pk='id', $pid = 'parentid', $child = 'sub_list', $root = 0);
  292. exit(json_result('0000',$this->response['0000'],array('list'=>$res)));
  293. }
  294. // 修改用户权限
  295. public function save_user_privilege(){
  296. // $role = $this->get_user_info('role');
  297. $privilegeIds = $this->input->post('privilegeIds',true);
  298. $userid = $this->input->post('userid',true);
  299. // $res = $this->User_model->get_one($userid);
  300. // 判断用户权限
  301. // if ($role >= $res['role']) {
  302. // exit(json_result('0011',$this->response['0011'],array()));
  303. // }
  304. // 参数判断
  305. // if (empty($privilegeIds) || empty($userid)) {
  306. // json_result('0000',$this->response['0000'],array());
  307. // }
  308. $this->User_model->update(['privilege'=>$privilegeIds],['id'=>$userid]);
  309. $this->add_operation_log('update','修改用户权限 id:'.$userid,0);
  310. $this->add_operation_log('update','Update user rights.User ID:'.$userid,0,1);
  311. exit(json_result('0000',$this->response['0000'],array()));
  312. }
  313. // 修改用户头像
  314. public function update_avatar(){
  315. $path = '../upload/image';
  316. $config['file_name'] = md5(uniqid()); // 设置图片名字
  317. if (!file_exists('../upload')) mkdir('../upload');
  318. if (!file_exists('../upload/image')) mkdir('../upload/image');
  319. $config['upload_path'] = $path.'/'; // 设置图片上传路径
  320. $config['allowed_types'] = '*'; // 设置图片上传格式
  321. $config['max_size'] = 10240; // 设置文件上传大小
  322. $this->load->library('upload', $config);
  323. if ( ! $this->upload->do_upload('file'))
  324. {
  325. $error = array('error' => $this->upload->display_errors('',''));
  326. // if (empty($this->version)) {
  327. // $data = array('error'=>transfer_error_tips($error['error']));
  328. // }else{
  329. $data = array('error'=>$error['error']);
  330. // }
  331. exit(json_result('0012',$this->response['0012'],$data));
  332. }
  333. else
  334. {
  335. $data = $this->upload->data();
  336. $imagePath = '/upload/image/'.$data['file_name'];
  337. $id = $this->get_user_info('id');
  338. $this->User_model->update(['avatar'=>$imagePath],['id'=>$id]);
  339. exit(json_result('0000',$this->response['0000']));
  340. }
  341. }
  342. }
  343. ?>