User.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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. $phone = $this->input->post('phone',true);
  129. $email = $this->input->post('email',true);
  130. $data['company'] = $this->input->post('company',true);
  131. $avatar = $this->input->post('avatar',true);
  132. if (isset($avatar) && !empty($avatar)) {
  133. $path = parse_url($avatar);
  134. $data['avatar'] = substr($path['path'], 1);
  135. }
  136. if (!empty($phone)) $data['phone'] = $phone;
  137. if (!empty($telephone)) $data['telephone'] = $telephone;
  138. if (!empty($email)) $data['email'] = $email;
  139. if(!isset($data['name']) || isset($data['name']) == '') exit(json_result('0707',$this->response['0707'],array()));
  140. if(empty($data['company'])) exit(json_result('0712',$this->response['0712'],array()));
  141. // $data['zone'] = empty($zone) ? '' : $zone;
  142. // 验证请求数据
  143. $config = array();
  144. if(!empty($data['phone'])){
  145. $config[] = array(
  146. 'field' => 'phone',
  147. 'label' => 'Phone',
  148. 'rules' => 'numeric|exact_length[11]',
  149. 'errors' => array(
  150. 'numeric' => '0718',
  151. 'exact_length' => '0718'
  152. )
  153. );
  154. }
  155. if (!empty($data['email'])) {
  156. $config[] = array(
  157. 'field' => 'email',
  158. 'label' => 'Email',
  159. 'rules' => 'valid_email',
  160. 'errors' => array(
  161. 'valid_email' => '0719',
  162. )
  163. );
  164. }
  165. if (empty($where['id'])) { // 添加用户
  166. $data['username'] = trim($this->input->post('account',true));
  167. $data['password'] = $this->input->post('password',true);
  168. $config[] = array(
  169. 'field' => 'password',
  170. 'label' => 'Password',
  171. 'rules' => 'min_length[6]',
  172. 'errors' => array(
  173. 'min_length' => '0720',
  174. )
  175. );
  176. if (!empty($config)) {
  177. $this->load->library('form_validation');
  178. $this->form_validation->set_rules($config);
  179. if ($this->form_validation->run() == FALSE){
  180. $errors = $this->form_validation->error_array();
  181. exit(json_result(current($errors),$this->response[current($errors)],array()));
  182. }
  183. }
  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 (!empty($config)) {
  211. $this->load->library('form_validation');
  212. $this->form_validation->set_rules($config);
  213. if ($this->form_validation->run() == FALSE){
  214. $errors = $this->form_validation->error_array();
  215. exit(json_result(current($errors),$this->response[current($errors)],array()));
  216. }
  217. }
  218. // if ($this->User_model->getDataCount(array('username'=>$data['username']),$where['id'])) {
  219. // exit(json_result('0706',$this->response['0706'],array()));
  220. // }
  221. if (!empty($data['phone']) && $this->User_model->getDataCount(array('phone'=>$data['phone']),$where['id'])) {
  222. exit(json_result('0721',$this->response['0721'],array()));
  223. }
  224. if (!empty($data['email']) && $this->User_model->getDataCount(array('email'=>$data['email']),$where['id'])) {
  225. exit(json_result('0722',$this->response['0722'],array()));
  226. }
  227. $oldData = $this->User_model->get_one($where,'company');
  228. if ($oldData['company'] != $data['company']) {
  229. $this->Project_model->update(array('company'=>$data['company']),array('userId'=>$where['id'],'company'=>$data['company']));
  230. }
  231. $privilegeIds = $this->input->post('privilegeIds',true);
  232. if ($privilegeIds !== NULL && $privilegeIds !== '') $data['privilege'] = $privilegeIds;
  233. $this->User_model->update($data,$where);
  234. $userid = $where['id'];
  235. $this->add_operation_log('update',"修改用户,用户名\"{$data['name']}\"",0);
  236. $this->add_operation_log('update',"Update user.User name:\"{$data['name']}\"",0,1);
  237. }
  238. exit(json_result('0000',$this->response['0000'],array('id'=>$userid)));
  239. }
  240. // 删除账户
  241. public function del(){
  242. $id = intval($this->input->post('id',true));
  243. if (empty($id)) exit(json_result('0007',$this->response['0007']));
  244. $this->User_model->delete(['id'=>$id]);
  245. exit(json_result('0000',$this->response['0000']));
  246. }
  247. // 获取用户权限列表
  248. public function privilege_list(){
  249. $userid = $this->input->post('id',true);
  250. if (empty($userid)) $userid = $this->get_user_info('id');
  251. $version = $this->session->userdata('version');
  252. $userData = $this->User_model->get_one(['id'=>$userid],'role');
  253. $privilege_list = $this->Privilnode_model->get_all_privilnode(SYSTEM_ADMIN);
  254. // 选中用户拥有的权限
  255. if (!empty($userid)) {
  256. $data = $this->User_model->get_one(['id'=>$userid],'privilege');
  257. $privilegeArr = explode(',', $data['privilege']);
  258. foreach ($privilege_list as &$v) {
  259. if (!empty($version)) {
  260. $v['name'] = $v['en_name'];
  261. }
  262. if ($userData['role'] == SYSTEM_ADMIN || in_array($v['id'], $privilegeArr)) {
  263. $v['select'] = 1;
  264. }else{
  265. $v['select'] = 0;
  266. }
  267. }
  268. }
  269. // 权限分级
  270. $res = list_to_tree($privilege_list, $pk='id', $pid = 'parentid', $child = 'sub_list', $root = 0);
  271. exit(json_result('0000',$this->response['0000'],array('list'=>$res)));
  272. }
  273. // 修改用户权限
  274. public function save_user_privilege(){
  275. // $role = $this->get_user_info('role');
  276. $privilegeIds = $this->input->post('privilegeIds',true);
  277. $userid = $this->input->post('userid',true);
  278. // $res = $this->User_model->get_one($userid);
  279. // 判断用户权限
  280. // if ($role >= $res['role']) {
  281. // exit(json_result('0011',$this->response['0011'],array()));
  282. // }
  283. // 参数判断
  284. // if (empty($privilegeIds) || empty($userid)) {
  285. // json_result('0000',$this->response['0000'],array());
  286. // }
  287. $this->User_model->update(['privilege'=>$privilegeIds],['id'=>$userid]);
  288. $this->add_operation_log('update','修改用户权限 id:'.$userid,0);
  289. $this->add_operation_log('update','Update user rights.User ID:'.$userid,0,1);
  290. exit(json_result('0000',$this->response['0000'],array()));
  291. }
  292. }
  293. ?>