User.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  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('Zone_model');
  13. $this->load->model('Project_model');
  14. $this->load->model('Global_location_model');
  15. }
  16. // 个人信息
  17. public function info(){
  18. $id = intval($this->input->post('id',true));
  19. $data = array();
  20. if (empty($id)) {
  21. $data['role'] = $this->get_user_info('role');
  22. $data['id'] = $this->get_user_info('id');
  23. $data['phone'] = $this->get_user_info('phone');
  24. $data['email'] = $this->get_user_info('email');
  25. $data['name'] = $this->get_user_info('name');
  26. $data['company'] = $this->get_user_info('company');
  27. }else{
  28. $data = $this->User_model->get_one(['id'=>$id],'role,id,name,company,phone,email');
  29. }
  30. exit(json_result('0000',$this->response['0000'],$data));
  31. }
  32. // 账号统计信息
  33. public function data(){
  34. $data = array(
  35. 'total' => 0,
  36. 'manuCount' => 0,
  37. 'suppCount' => 0,
  38. 'poCount' => 0,
  39. 'upaCount' => 0,
  40. 'monCount' => 0,
  41. 'conCount' => 0,
  42. );
  43. $role = $this->get_user_info('role');
  44. $id = $this->get_user_info('id');
  45. $where = array(['id !=' => $id]);
  46. if ($role != SYSTEM_ADMIN) $where['pid'] = $id;
  47. $data['total'] = $this->User_model->get_count($where);
  48. $where['role'] = 2;
  49. $data['manuCount'] = $this->User_model->get_count($where);
  50. $where['role'] = 3;
  51. $data['suppCount'] = $this->User_model->get_count($where);
  52. $where['role'] = 4;
  53. $data['poCount'] = $this->User_model->get_count($where);
  54. $where['role'] = 5;
  55. $data['upaCount'] = $this->User_model->get_count($where);
  56. $where['role'] = 6;
  57. $data['monCount'] = $this->User_model->get_count($where);
  58. $where['role'] = 7;
  59. $data['conCount'] = $this->User_model->get_count($where);
  60. exit(json_result('0000',$this->response['0000'],$data));
  61. }
  62. // 编辑个人信息
  63. public function update_user_info(){
  64. $userid = $this->get_user_info('id');
  65. $data['realname'] = $this->input->post('realname',true);
  66. $mobile = $this->input->post('mobile',true);
  67. $data['username'] = $this->input->post('name',true);
  68. $telephone = $this->input->post('telephone',true);
  69. $email = $this->input->post('email',true);
  70. $address = $this->input->post('address',true);
  71. $memo = $this->input->post('memo',true);
  72. $avatar = $this->input->post('avatar',true);
  73. if (!empty($avatar)) $data['avatar'] = $avatar;
  74. if (!empty($mobile)) $data['mobile'] = $mobile;
  75. if (!empty($telephone)) $data['telephone'] = $telephone;
  76. if (!empty($email)) $data['email'] = $email;
  77. if (!empty($address)) $data['address'] = $address;
  78. if (!empty($memo)) $data['memo'] = $memo;
  79. // 检测表单数据
  80. if(empty($data['realname'])) exit(json_result('0707',$this->response['0707'],array()));
  81. if(empty($data['username'])) exit(json_result('0708',$this->response['0708'],array()));
  82. if(mb_strlen($data['username']) > 20) exit(json_result('0723',$this->response['0723'],array()));
  83. // 检测登录账号是否存在
  84. if ($this->User_model->getDataCount(array('username'=>$data['username']),$userid)) {
  85. exit(json_result('0706',$this->response['0706'],array()));
  86. }
  87. // 验证请求数据
  88. $config = array();
  89. if(!empty($data['mobile'])){
  90. $config[] = array(
  91. 'field' => 'mobile',
  92. 'label' => 'Mobile',
  93. 'rules' => 'numeric|exact_length[11]',
  94. 'errors' => array(
  95. 'numeric' => '0718',
  96. 'exact_length' => '0718'
  97. )
  98. );
  99. }
  100. if (!empty($data['email'])) {
  101. $config[] = array(
  102. 'field' => 'email',
  103. 'label' => 'Email',
  104. 'rules' => 'valid_email',
  105. 'errors' => array(
  106. 'valid_email' => '0719',
  107. )
  108. );
  109. }
  110. if (!empty($config)) {
  111. $this->load->library('form_validation');
  112. $this->form_validation->set_rules($config);
  113. if ($this->form_validation->run() == FALSE){
  114. $errors = $this->form_validation->error_array();
  115. exit(json_result(current($errors),$this->response[current($errors)],array()));
  116. }
  117. }
  118. $this->User_model->update_user($data,$userid);
  119. $this->userinfo = $this->User_model->get_user_by_name($data['username']);
  120. exit(json_result('0000',$this->response['0000'],array('id'=>$userid)));
  121. }
  122. // 修改用户密码
  123. public function password(){
  124. $username = $this->get_user_info('username');
  125. $old_pass = $this->input->post('old',true);
  126. $new_pass = $this->input->post('new',true);
  127. $new_second_pass = $this->input->post('new_second',true);
  128. if($new_pass == $old_pass){
  129. exit(json_result('0709',$this->response['0709'],array()));
  130. }
  131. // 验证确认密码
  132. if($new_pass != $new_second_pass){
  133. exit(json_result('0701',$this->response['0701'],array()));
  134. }
  135. // 验证密码长度
  136. if (mb_strlen($new_pass) < 6 || mb_strlen($new_pass) > 12) {
  137. exit(json_result('0703',$this->response['0703'],array()));
  138. }
  139. // 验证旧密码
  140. if (!$this->User_model->validate_password($username,md5($old_pass))) {
  141. exit(json_result('0702',$this->response['0702'],array()));
  142. }
  143. $id = $this->get_user_info('id');
  144. $new_pass = md5($new_pass);
  145. $res = $this->User_model->change_password($id,$new_pass);
  146. if($res){
  147. exit(json_result('0000',$this->response['0000'],array()));
  148. }else{
  149. exit(json_result('0704',$this->response['0704'],array()));
  150. }
  151. }
  152. // 修改账号状态
  153. public function block_user(){
  154. $userid = $this->input->post('userid',true);
  155. $status = intval($this->input->post('status',true));
  156. if (empty($userid) || $status < 0) {
  157. exit(json_result('0007',$this->response['0007'],array()));
  158. }
  159. $this->User_model->update(array('status'=>$status),array('id'=>$userid));
  160. exit(json_result('0000',$this->response['0000'],array()));
  161. }
  162. // 用户列表
  163. public function sub_list(){
  164. $userRole = $this->get_user_info('role');
  165. $userid = $this->get_user_info('id');
  166. $where = array('id !='=>$userid);
  167. if ($userRole != SYSTEM_ADMIN) {
  168. $where['pid'] = $userid;
  169. }
  170. $role = intval($this->input->post('role',true));
  171. if (!empty($role)) $where['role'] = $role;
  172. $field = "id,name,role,phone,email,status";
  173. $list = $this->User_model->get_list($where,$field);
  174. exit(json_result('0000',$this->response['0000'],array('list'=>$list)));
  175. }
  176. // 添加编辑用户
  177. public function user_update(){
  178. $where['id'] = $this->input->post('userid',true);
  179. $role = $this->get_user_info('role');
  180. if ($role == COMPANY_CUSTOMER) {
  181. exit(json_result('0011', $this->response['0011'], array()));
  182. }
  183. $data['realname'] = $this->input->post('realname',true);
  184. $data['username'] = trim($this->input->post('name',true));
  185. $telephone = $this->input->post('telephone',true);
  186. $mobile = $this->input->post('mobile',true);
  187. $data['password'] = $this->input->post('password',true);
  188. $email = $this->input->post('email',true);
  189. $data['companyid'] = $this->input->post('companyid',true);
  190. $address = $this->input->post('address',true);
  191. $memo = $this->input->post('memo',true);
  192. $avatar = $this->input->post('avatar',true);
  193. $data['zone'] = $this->input->post('zone',true);
  194. if (!empty($avatar)) $data['avatar'] = $avatar;
  195. if (!empty($mobile)) $data['mobile'] = $mobile;
  196. if (!empty($telephone)) $data['telephone'] = $telephone;
  197. if (!empty($email)) $data['email'] = $email;
  198. if (!empty($address)) $data['address'] = $address;
  199. if (!empty($memo)) $data['memo'] = $memo;
  200. if(!isset($data['realname']) || isset($data['realname']) == '') exit(json_result('0707',$this->response['0707'],array()));
  201. if(!isset($data['username']) || $data['username'] == '') exit(json_result('0708',$this->response['0708'],array()));
  202. if(mb_strlen($data['username']) > 20) exit(json_result('0723',$this->response['0723'],array()));
  203. if(empty($data['companyid'])) exit(json_result('0712',$this->response['0712'],array()));
  204. // $data['zone'] = empty($zone) ? '' : $zone;
  205. // 验证请求数据
  206. $config = array();
  207. $config[] = array(
  208. 'field' => 'password',
  209. 'label' => 'Password',
  210. 'rules' => 'min_length[6]',
  211. 'errors' => array(
  212. 'min_length' => '0720',
  213. )
  214. );
  215. if(!empty($data['mobile'])){
  216. $config[] = array(
  217. 'field' => 'mobile',
  218. 'label' => 'Mobile',
  219. 'rules' => 'numeric|exact_length[11]',
  220. 'errors' => array(
  221. 'numeric' => '0718',
  222. 'exact_length' => '0718'
  223. )
  224. );
  225. }
  226. if (!empty($data['email'])) {
  227. $config[] = array(
  228. 'field' => 'email',
  229. 'label' => 'Email',
  230. 'rules' => 'valid_email',
  231. 'errors' => array(
  232. 'valid_email' => '0719',
  233. )
  234. );
  235. }
  236. if (!empty($config)) {
  237. $this->load->library('form_validation');
  238. $this->form_validation->set_rules($config);
  239. if ($this->form_validation->run() == FALSE){
  240. $errors = $this->form_validation->error_array();
  241. exit(json_result(current($errors),$this->response[current($errors)],array()));
  242. }
  243. }
  244. if (empty($where['id'])) { // 添加用户
  245. if (empty($data['password'])) {
  246. exit(json_result('0713',$this->response['0713'],array()));
  247. }
  248. $data['password'] = md5($data['password']);
  249. // 验证登录账号是否存在
  250. if ($this->User_model->getDataCount(array('username'=>$data['username']))) {
  251. exit(json_result('0706',$this->response['0706'],array()));
  252. }
  253. if (!empty($data['mobile']) && $this->User_model->getDataCount(array('mobile'=>$data['mobile']))) {
  254. exit(json_result('0721',$this->response['0721'],array()));
  255. }
  256. if (!empty($data['email']) && $this->User_model->getDataCount(array('email'=>$data['email']))) {
  257. exit(json_result('0722',$this->response['0722'],array()));
  258. }
  259. $data['avatar'] = empty($data['avatar']) ? 'assets/avatars/user.png' : $data['avatar'];
  260. $data['parentid'] = $this->get_user_info('id');
  261. $data['role'] = $role == 1 ? 2 : 3;
  262. $data['privilege'] = $this->Privilnode_model->get_default_privilege($data['role']);
  263. if (empty($data['zone']) && $data['role'] == 2) {
  264. $projectids = $this->Project_model->get_project_ids(array('company'=>$data['companyid']));
  265. $data['zone'] = !empty($projectids) ? implode(',', $projectids) : '';
  266. }
  267. $userid = $this->User_model->add_user($data);
  268. $this->add_operation_log('insert',"添加用户,用户名\"{$data['realname']}\"",0);
  269. $this->add_operation_log('insert',"Add user.User name:\"{$data['realname']}\"",0,1);
  270. }else{ // 编辑用户
  271. if (empty($data['password'])) {
  272. unset($data['password']);
  273. }else {
  274. $data['password'] = md5($data['password']);
  275. }
  276. $res = $this->User_model->getOne($where['id'],'role,companyid');
  277. if (intval($role) >= intval($res['role'])) {
  278. exit(json_result('0011',$this->response['0011'],array()));
  279. }
  280. if ($data['companyid'] != $res['companyid'] && empty($data['zone']) && $res['role'] == 2) {
  281. $projectids = $this->Project_model->get_project_ids(array('company'=>$data['companyid']));
  282. $data['zone'] = !empty($projectids) ? implode(',', $projectids) : 0;
  283. }
  284. if ($this->User_model->getDataCount(array('username'=>$data['username']),$where['id'])) {
  285. exit(json_result('0706',$this->response['0706'],array()));
  286. }
  287. if (!empty($data['mobile']) && $this->User_model->getDataCount(array('mobile'=>$data['mobile']),$where['id'])) {
  288. exit(json_result('0721',$this->response['0721'],array()));
  289. }
  290. if (!empty($data['email']) && $this->User_model->getDataCount(array('email'=>$data['email']),$where['id'])) {
  291. exit(json_result('0722',$this->response['0722'],array()));
  292. }
  293. $this->User_model->update_user($data,$where['id']);
  294. $userid = $where['id'];
  295. $this->add_operation_log('update',"修改用户,用户名\"{$data['realname']}\"",0);
  296. $this->add_operation_log('update',"Update user.User name:\"{$data['realname']}\"",0,1);
  297. }
  298. exit(json_result('0000',$this->response['0000'],array('id'=>$userid)));
  299. }
  300. // 获取用户权限列表
  301. public function privilege_list(){
  302. $userid = $this->input->post('userid',true);
  303. if (empty($userid)) {
  304. exit(json_result('0007',$this->response['0007'],array()));
  305. }
  306. $version = $this->session->userdata('version');
  307. $userData = $this->User_model->getOne($userid,'role');
  308. $privilege_list = $this->Privilnode_model->get_all_privilnode(SYSTEM_ADMIN);
  309. // 选中用户拥有的权限
  310. if (!empty($userid)) {
  311. $data = $this->User_model->getOne($userid,'privilege');
  312. $privilegeArr = explode(',', $data['privilege']);
  313. foreach ($privilege_list as &$v) {
  314. if (!empty($version)) {
  315. $v['name'] = $v['en_name'];
  316. }
  317. if ($userData['role'] == SYSTEM_ADMIN || in_array($v['id'], $privilegeArr)) {
  318. $v['select'] = 1;
  319. }else{
  320. $v['select'] = 0;
  321. }
  322. }
  323. }
  324. // 权限分级
  325. $res = list_to_tree($privilege_list, $pk='id', $pid = 'parentid', $child = 'sub_list', $root = 0);
  326. exit(json_result('0000',$this->response['0000'],array('list'=>$res)));
  327. }
  328. // 修改用户权限
  329. public function save_user_privilege(){
  330. $role = $this->get_user_info('role');
  331. // if ($role == COMPANY_CUSTOMER) {
  332. // exit(json_result('0011', $this->response['0011'], array()));
  333. // }
  334. $privilegeIds = $this->input->post('privilegeIds',true);
  335. $userid = $this->input->post('userid',true);
  336. $res = $this->User_model->getOne($userid);
  337. // 判断用户权限
  338. if ($role >= $res['role']) {
  339. exit(json_result('0011',$this->response['0011'],array()));
  340. }
  341. // 参数判断
  342. if (empty($privilegeIds) || empty($userid)) {
  343. json_result('0000',$this->response['0000'],array());
  344. }
  345. $this->User_model->change_privilege($privilegeIds,$userid);
  346. $this->add_operation_log('update','修改用户权限 id:'.$userid,0);
  347. $this->add_operation_log('update','Update user rights.User ID:'.$userid,0,1);
  348. exit(json_result('0000',$this->response['0000'],array()));
  349. }
  350. // 用户反馈
  351. public function feedback(){
  352. $data['username'] = $this->input->post('name',true);
  353. $data['mobile'] = $this->input->post('mobile',true);
  354. $data['detail'] = $this->input->post('detail',true);
  355. if(empty($data['username'])) exit(json_result('0714',$this->response['0714'],array()));
  356. if(empty($data['mobile'])) exit(json_result('0715',$this->response['0715'],array()));
  357. if(empty($data['detail'])) exit(json_result('0716',$this->response['0716'],array()));
  358. $data['feedtime'] = date('Y-m-d H:i:s');
  359. $res = $this->Feedback_model->insert($data);
  360. exit(json_result('0000',$this->response['0000'],array('id'=>$res)));
  361. }
  362. // 获取项目权限列表
  363. public function get_porject_list(){
  364. $userid = intval($this->input->post('userid',true));
  365. $companyid = intval($this->input->post('companyid',true));
  366. $zoneId = intval($this->input->post('zoneid',true));
  367. $countryId = intval($this->input->post('countryId',true));
  368. $provinceId = intval($this->input->post('provinceId',true));
  369. $cityId = intval($this->input->post('cityId',true));
  370. $areaId = intval($this->input->post('areaId',true));
  371. $idArr = array();
  372. if (!empty($areaId)) {
  373. $idArr = array($areaId);
  374. }elseif (!empty($cityId)) {
  375. $res1 = $this->db->query('select id from global_location where pid = '.$cityId)->result_array();
  376. if (empty($res1)) {
  377. $idArr = array($cityId);
  378. }else{
  379. $idArr = array_column($res1, 'id');
  380. }
  381. }elseif (!empty($provinceId)) {
  382. $res1 = $this->db->query('select id from global_location where pid = '.$provinceId)->result_array();
  383. if (empty($res1)) {
  384. $idArr = array($provinceId);
  385. }else{
  386. $idStr1 = implode(',', array_column($res1, 'id'));
  387. $res2 = $this->db->query('select id from global_location where pid in ('.$idStr1.')')->result_array();
  388. if (empty($res2)) {
  389. $idArr = array_column($res1, 'id');
  390. }else{
  391. $idArr = array_column($res2, 'id');
  392. }
  393. }
  394. }elseif (!empty($countryId)) {
  395. $res1 = $this->db->query('select id from global_location where pid = '.$countryId)->result_array();
  396. if (empty($res1)) {
  397. $idArr = array($countryId);
  398. }else{
  399. $idStr1 = implode(',', array_column($res1, 'id'));
  400. $res2 = $this->db->query('select id from global_location where pid in ('.$idStr1.')')->result_array();
  401. if (empty($res2)) {
  402. $idArr = array_column($res1, 'id');
  403. }else{
  404. $idStr2 = implode(',', array_column($res1, 'id'));
  405. $res3 = $this->db->query('select id from global_location where pid in ('.$idStr2.')')->result_array();
  406. if (empty($res3)) {
  407. $idArr = array_column($res2, 'id');
  408. }else{
  409. $idArr = array_column($res3, 'id');
  410. }
  411. }
  412. }
  413. }
  414. $where = array();
  415. if (!empty($companyid)) {
  416. $where['company'] = $companyid;
  417. }
  418. if (!empty($idArr)) {
  419. $where['cityid'] = $idArr;
  420. }
  421. $projectList = $this->Project_model->get_list($where, 'id,projectname as name',NULL, NULL, 'id desc');
  422. $userData = $this->User_model->getOne($userid,'zone,role');
  423. $hasArr = empty($userData['zone']) ? [] : array_unique(explode(',', $userData['zone']));
  424. foreach ($projectList as &$p) {
  425. if ($userData['role'] == SYSTEM_ADMIN || in_array($p['id'], $hasArr)) {
  426. $p['select'] = 1;
  427. }else{
  428. $p['select'] = 0;
  429. }
  430. }
  431. exit(json_result('0000',$this->response['0000'],array('projects'=>$projectList)));
  432. }
  433. // 修改用户项目权限
  434. public function save_user_project(){
  435. $projects = $this->input->post('projects',true);
  436. $userid = intval($this->input->post('userid'));
  437. $role = $this->get_user_info('role');
  438. if ($userid <= 0) {
  439. exit(json_result('0007',$this->response['0007'],array()));
  440. }
  441. if(empty($projects)) exit(json_result('0717',$this->response['0717'],array()));
  442. $res = $this->User_model->getOne($userid);
  443. // 判断用户权限
  444. if ($role >= $res['role']) {
  445. exit(json_result('0011',$this->response['0011'],array()));
  446. }
  447. $this->User_model->update_user(array('zone'=>$projects),$userid);
  448. exit(json_result('0000',$this->response['0000'],array()));
  449. }
  450. }
  451. ?>