User.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. <?php
  2. defined('BASEPATH') OR exit('No direct script access allowed');
  3. /**
  4. * Class 用户管理类
  5. */
  6. class User extends MY_controller
  7. {
  8. function __construct()
  9. {
  10. parent::__construct();
  11. $this->load->helper(array('form', 'url', 'date'));
  12. $this->load->model("user_model");
  13. $this->load->driver('cache', array('adapter' => 'apc', 'backup' => 'file'));
  14. if(array_key_exists("from",$this->session->userdata)){
  15. $this->user_model->set_collection_name("sso_users");
  16. }
  17. $this->load->library('MY_pagination');
  18. $this->assign("user_status", $this->user_status);
  19. $this->assign("user_types", $this->user_types);
  20. }
  21. /**
  22. * 用户列表页
  23. */
  24. public function index()
  25. {
  26. $url = site_url("user/index?");
  27. $wheres = array();
  28. $order_info = array();
  29. $keyword = $this->input->get("keyword", TRUE);
  30. $user_type = $this->input->get("user_type", TRUE);
  31. $status = $this->input->get("status", TRUE);
  32. $order = $this->input->get("order", TRUE);
  33. $page_num = $this->input->get("per_page", TRUE);
  34. $page_size = $this->input->get("page_size", TRUE);
  35. if ($keyword) {
  36. $url .= "&keyword=" . $keyword;
  37. }
  38. if ($user_type) {
  39. $wheres['user_type'] = $user_type;
  40. $url .= "&user_type=" . $user_type;
  41. }
  42. if ($status) {
  43. $wheres['status'] = $status;
  44. $url .= "&status=" . $status;
  45. }
  46. if ($order) {
  47. $orders = explode(" ", $order);
  48. if (count($orders) == 2) {
  49. $order_info[$orders[0]] = $orders[1];
  50. $url .= "&order=" . $order;
  51. }
  52. }
  53. if ($page_size) {
  54. $this->page_size = $page_size;
  55. $url .= "&page_size=" . $page_size;
  56. }
  57. $count = $this->user_model->count_user($keyword, $wheres);
  58. $this->assign("count", $count);
  59. $config = $this->page_config($count, $this->page_size, $url);
  60. $this->my_pagination->initialize($config);
  61. if($page_num && $page_num>1) {
  62. $offset = (intval($page_num)-1)*$this->page_size;
  63. }else {
  64. $offset = 0;
  65. }
  66. $user_list = $this->user_model->list_user($this->page_size, $offset, $keyword, $wheres, $order_info);
  67. $this->assign("keyword", $keyword);
  68. $this->assign("user_type", $user_type);
  69. $this->assign("status", $status);
  70. $this->assign("order", $order);
  71. $this->assign("page_size", $this->page_size);
  72. $this->assign("page", $this->my_pagination->create_pages());
  73. $this->assign("user_list", $user_list);
  74. $this->display("user/index.html");
  75. }
  76. /**
  77. * 查看用户详情
  78. * @param $user_id 用户id
  79. */
  80. public function view($user_id)
  81. {
  82. $message = "";
  83. $user = $this->user_model->get_user_with_user_id($user_id);
  84. if($user){
  85. $this->assign("user",$user);
  86. }else{
  87. $message = "用户信息不存在或者已经被删除";
  88. }
  89. $this->assign("message",$message);
  90. $this->display("user/view.html");
  91. }
  92. /**
  93. * 更新用户信息
  94. */
  95. public function update()
  96. {
  97. $cahce_branch = $this->cache->get('branchs');
  98. $msg=array();
  99. $data = array();
  100. $msg['code'] = 1;
  101. $msg['icon'] = 2;
  102. $data['user_id']=$user_id = $this->input->post("user_id",true);
  103. $old_user = $this->user_model->get_user_with_user_id($user_id);
  104. $data['username']=$username = $this->input->post("username",true);
  105. $password = $this->input->post("password",true);
  106. $repassword = $this->input->post("repassword",true);
  107. $data['name']=$name = $this->input->post("name",true);
  108. $data['mobile']=$mobile = $this->input->post("mobile",true);
  109. $data['email']=$email = $this->input->post("email",true);
  110. $branch_id = $this->input->post("branch",true);
  111. if($branch_id != "") {
  112. $branchs = array();
  113. for ($i = 0;$i<count($branch_id);$i++){
  114. $branchs[] = array('branch_id' => $branch_id[$i], 'branch_type' => $cahce_branch[$branch_id[$i]]['branch_type']);
  115. }
  116. $data['branch'] = $branchs;
  117. }else{
  118. $data['branch'] = array(array('branch_id' => "", 'branch_type' => ""));
  119. }
  120. $data['user_type']=$user_type = $this->input->post("user_type",true);
  121. if(!$username){
  122. $msg['code'] = 0;
  123. $msg['msg'] = "用户名不能为空!";
  124. }
  125. if(!preg_match_all("/^[a-zA-Z\d_]{5,18}$/",$username) && $msg['code']){
  126. $msg['code'] = 0;
  127. $msg['msg'] = "用户名格式不正确,只支持英文和数字,最小长度为5,最大长度为18!";
  128. }
  129. if($this->user_model->is_exists("username",$username,$old_user['username']) && $msg['code']){
  130. $msg['code'] = 0;
  131. $msg['msg'] = "该用户名已存在请换一个。";
  132. }
  133. if($password && strlen($password)<8 && $msg['code']){
  134. $msg['code'] = 0;
  135. $msg['msg'] = "密码格式不正确,最小长度为8!";
  136. }
  137. if($password && $password != $repassword && $msg['code']){
  138. $msg['code'] = 0;
  139. $msg['msg'] = "两次密码输入不一致,请检查。";
  140. }
  141. if(!$name && $msg['code']){
  142. $msg['code'] = 0;
  143. $msg['msg'] = "姓名不能为空!";
  144. }
  145. if(!preg_match('/[a-zA-Z\x{4e00}-\x{9fa5}]/u', $name) && $msg['code']){
  146. $msg['code'] = 0;
  147. $msg['msg'] = "姓名格式不正确,正能是中文或者英文!";
  148. }
  149. if(!$mobile && $msg['code']) {
  150. $msg['code'] = 0;
  151. $msg['msg'] = "手机号码不能为空!";
  152. }
  153. if(!preg_match('/^0?(13|14|15|17|18)[0-9]{9}$/', $mobile) && $msg['code']){
  154. $msg['code'] = 0;
  155. $msg['msg'] = "手机号码格式不正确!";
  156. }
  157. if($this->user_model->is_exists("mobile",$mobile,$old_user['mobile']) && $msg['code']){
  158. $msg['code'] = 0;
  159. $msg['msg'] = "该手机号已存被使用请换一个。";
  160. }
  161. if(!$email && $msg['code']) {
  162. $msg['code'] = 0;
  163. $msg['msg'] = "邮箱不能为空!";
  164. }
  165. if(!preg_match('/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,})$/', $email) && $msg['code']){
  166. $msg['code'] = 0;
  167. $msg['msg'] = "邮箱格式不正确!";
  168. }
  169. if($this->user_model->is_exists("email",$email,$old_user['email']) && $msg['code']){
  170. $msg['code'] = 0;
  171. $msg['msg'] = "该邮箱已存被使用请换一个。";
  172. }
  173. if($user_type == 2 && $branch_id =="" && $msg['code']){
  174. $msg['code'] = 0;
  175. $msg['msg'] = "用户类型为工程师时,必须填写分支机构。";
  176. }
  177. if($msg['code']){
  178. $this->user_model->update_user($data);
  179. $msg['icon'] = 1;
  180. $msg['msg'] = "用户信息更新成功!";
  181. }
  182. $this->response($msg);
  183. }
  184. /**
  185. * 验证是否存在
  186. * @param null $old_val 原数据
  187. */
  188. public function validate($old_val=NULL){
  189. $field = $this->input->post("name",true);
  190. $value = $this->input->post("param",true);
  191. if($field == "username"){
  192. $msg = "用户名已存在!";
  193. }
  194. if($field == "mobile"){
  195. $msg = "手机号码已存在!";
  196. }
  197. if($field == "email"){
  198. $old_val = urldecode($old_val);
  199. $msg = "邮箱已存在!";
  200. }
  201. if($this->user_model->is_exists($field,$value,$old_val)){
  202. $this->response(array("info"=>$msg,"status"=>"n"));
  203. }else{
  204. $this->response(array("info"=>"已验证通过!","status"=>"y"));
  205. }
  206. }
  207. /**
  208. * 增加用户
  209. */
  210. public function add()
  211. {
  212. $id = $this->create_id();
  213. $this->assign("user_id", $id);
  214. $this->display("user/add.html");
  215. }
  216. /**
  217. * 保存用户数据
  218. */
  219. public function save(){
  220. $cahce_branch = $this->cache->get('branchs');
  221. $msg=array();
  222. $data = array();
  223. $msg['code'] = 1;
  224. $msg['icon'] = 2;
  225. $data['user_id']=$user_id = $this->input->post("user_id",true);
  226. $data['username']=$username = $this->input->post("username",true);
  227. $password = $this->input->post("password",true);
  228. $repassword = $this->input->post("repassword",true);
  229. $data['name']=$name = $this->input->post("name",true);
  230. $data['mobile']=$mobile = $this->input->post("mobile",true);
  231. $data['email']=$email = $this->input->post("email",true);
  232. $branch_id = $this->input->post("branch",true);
  233. if($branch_id != "") {
  234. $branchs = array();
  235. for ($i = 0;$i<count($branch_id);$i++){
  236. $branchs[] = array('branch_id' => $branch_id[$i], 'branch_type' => $cahce_branch[$branch_id[$i]]['branch_type']);
  237. }
  238. $data['branch'] = $branchs;
  239. }else{
  240. $data['branch'] = array(array('branch_id' => "", 'branch_type' => ""));
  241. }
  242. $data['user_type']=$user_type = $this->input->post("user_type",true);
  243. if(!$username){
  244. $msg['code'] = 0;
  245. $msg['msg'] = "用户名不能为空!";
  246. }elseif(!preg_match_all("/^[a-zA-Z\d_]{5,18}$/",$username) && $msg['code']){
  247. $msg['code'] = 0;
  248. $msg['msg'] = "用户名格式不正确,只支持英文和数字,最小长度为5,最大长度为18!";
  249. }elseif($this->user_model->is_exists("username",$username) && $msg['code']){
  250. $msg['code'] = 0;
  251. $msg['msg'] = "该用户名已存在请换一个。";
  252. }
  253. if(!$password && $msg['code']){
  254. $msg['code'] = 0;
  255. $msg['msg'] = "密码不能为空!";
  256. }elseif(strlen($password)<8 && $msg['code']){
  257. $msg['code'] = 0;
  258. $msg['msg'] = "密码格式不正确,最小长度为8!";
  259. }
  260. if($password != $repassword && $msg['code']){
  261. $msg['code'] = 0;
  262. $msg['msg'] = "两次密码输入不一致,请检查。";
  263. }
  264. if(!$name && $msg['code']){
  265. $msg['code'] = 0;
  266. $msg['msg'] = "姓名不能为空!";
  267. }elseif(!preg_match('/[a-zA-Z\x{4e00}-\x{9fa5}]/u', $name) && $msg['code']){
  268. $msg['code'] = 0;
  269. $msg['msg'] = "姓名格式不正确,正能是中文或者英文!";
  270. }
  271. if(!$mobile && $msg['code']) {
  272. $msg['code'] = 0;
  273. $msg['msg'] = "手机号码不能为空!";
  274. }elseif(!preg_match('/^0?(13|14|15|17|18)[0-9]{9}$/', $mobile) && $msg['code']){
  275. $msg['code'] = 0;
  276. $msg['msg'] = "手机号码格式不正确!";
  277. }elseif($this->user_model->is_exists("mobile",$mobile) && $msg['code']){
  278. $msg['code'] = 0;
  279. $msg['msg'] = "该手机号已存被使用请换一个。";
  280. }
  281. if(!$email && $msg['code']) {
  282. $msg['code'] = 0;
  283. $msg['msg'] = "邮箱不能为空!";
  284. }elseif(!preg_match('/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,})$/', $email) && $msg['code']){
  285. $msg['code'] = 0;
  286. $msg['msg'] = "邮箱格式不正确!";
  287. }elseif($this->user_model->is_exists("email",$email) && $msg['code']){
  288. $msg['code'] = 0;
  289. $msg['msg'] = "该邮箱已存被使用请换一个。";
  290. }
  291. if($user_type == 2 && $branch_id =="" && $msg['code']){
  292. $msg['code'] = 0;
  293. $msg['msg'] = "用户类型为工程师时,必须填写分支机构。";
  294. }
  295. if($msg['code']){
  296. $data['password']= hash_hmac('sha256',$password,$this->config->item('secret_key'));
  297. $data['create_time'] = new MongoDB\BSON\UTCDateTime(time()*1000);
  298. $data['status'] = "10";
  299. $data['last_login_time'] = "";
  300. $this->user_model->insert_user($data);
  301. $msg['icon'] = 1;
  302. $msg['msg'] = "用户信息保存成功!";
  303. }
  304. $this->response($msg);
  305. }
  306. /**
  307. * 删除用户数据
  308. * @param $user_id 用户ID
  309. */
  310. public function delete($user_id)
  311. {
  312. $data['icon'] = 1;
  313. if ($user_id) {
  314. if($user_id != 1) {
  315. $user = $this->user_model->get_user_with_user_id($user_id);
  316. if ($user) {
  317. $this->user_model->delete_user($user_id);
  318. $data['msg'] = "用户删除成功!";
  319. } else {
  320. $data['icon'] = 2;
  321. $data['msg'] = "用户信息不存在或者已经被删除!";
  322. }
  323. }else{
  324. $data['icon'] = 2;
  325. $data['msg'] = "系统预定义的账户不允许删除!";
  326. }
  327. } else {
  328. $data['icon'] = 2;
  329. $data['msg'] = "参数错误请刷新后重试!";
  330. }
  331. $this->response($data);
  332. }
  333. /**
  334. * 禁用用户
  335. * @param $user_id 用户Id
  336. */
  337. public function stopuse($user_id){
  338. $data['icon'] = 1;
  339. if ($user_id) {
  340. $user = $this->user_model->get_user_with_user_id($user_id);
  341. if($user) {
  342. $user['status'] = "40";
  343. $this->user_model->update_user($user);
  344. $data['msg'] = "用户停用成功!";
  345. }else{
  346. $data['icon'] = 2;
  347. $data['msg'] = "用户信息不存在或者已经被删除!";
  348. }
  349. } else {
  350. $data['icon'] = 2;
  351. $data['msg'] = "参数错误请刷新后重试!";
  352. }
  353. $this->response($data);
  354. }
  355. /**
  356. * 启用用户
  357. * @param $user_id 用户Id
  358. */
  359. public function restore($user_id){
  360. $data['icon'] = 1;
  361. if ($user_id) {
  362. $user = $this->user_model->get_user_with_user_id($user_id);
  363. if($user) {
  364. $user['status'] = "10";
  365. $this->user_model->update_user($user);
  366. $data['msg'] = "用户恢复成功!";
  367. }else{
  368. $data['icon'] = 2;
  369. $data['msg'] = "用户信息不存在或者已经被删除!";
  370. }
  371. } else {
  372. $data['icon'] = 2;
  373. $data['msg'] = "参数错误请刷新后重试!";
  374. }
  375. $this->response($data);
  376. }
  377. /**
  378. * 修改用户密码
  379. * @param $user_id 用户ID
  380. */
  381. public function change_password($user_id){
  382. $data['code'] = 0;
  383. $message = "";
  384. $user = $this->user_model->get_user_with_user_id($user_id);
  385. if($user){
  386. $this->assign("user",$user);
  387. }else{
  388. $message = "用户信息不存在或者已经被删除!";
  389. }
  390. if($this->input->post()){
  391. $password = $this->input->post("password",true);
  392. $repassword = $this->input->post("repassword",true);
  393. if($password) {
  394. if ($this->session->user_type != "1") {
  395. $old_password = hash_hmac('sha256',$this->input->post("old_password", true),$this->config->item('secret_key'));
  396. if ($old_password != $user['password']) {
  397. $data['icon'] = 2;
  398. $data['msg'] = "旧密码不正确,请重新输入!";
  399. } elseif ($password != $repassword) {
  400. $data['icon'] = 2;
  401. $data['msg'] = "两次密码输入不一致,请重新输入!";
  402. } else {
  403. $user['password'] = hash_hmac('sha256',$password,$this->config->item('secret_key'));
  404. $this->user_model->update_user($user);
  405. $data['code'] = 1;
  406. $data['icon'] = 1;
  407. $data['msg'] = "密码修改成功!";
  408. }
  409. } else {
  410. if ($password != $repassword) {
  411. $data['icon'] = 2;
  412. $data['msg'] = "两次密码输入不一致,请重新输入!";
  413. } else {
  414. $user['password'] = hash_hmac('sha256',$password,$this->config->item('secret_key'));
  415. $this->user_model->update_user($user);
  416. $data['code'] = 1;
  417. $data['icon'] = 1;
  418. $data['msg'] = "密码修改成功!";
  419. }
  420. }
  421. }else{
  422. $data['code'] = 1;
  423. $data['icon'] = 1;
  424. $data['msg'] = "放弃修改密码!";
  425. }
  426. $this->response($data);
  427. }else{
  428. $this->assign("message",$message);
  429. $this->display("user/change_password.html");
  430. }
  431. }
  432. /**
  433. * 批量删除用户
  434. */
  435. public function delete_all(){
  436. $ids = rtrim($this->input->post("ids",true),",");
  437. $ids = explode(",",$ids);
  438. if(count($ids)<1){
  439. $data['icon'] = 2;
  440. $data['msg'] = "参数错误请刷新后重试!";
  441. }else{
  442. $this->user_model->delete_all_user($ids);
  443. $data['icon'] = 1;
  444. $data['msg'] = "批量删除用户成功!";
  445. }
  446. $this->response($data);
  447. }
  448. /**
  449. * 批量禁用用户
  450. */
  451. public function disable_all(){
  452. $ids = rtrim($this->input->post("ids",true),",");
  453. $ids = explode(",",$ids);
  454. if(count($ids)<1){
  455. $data['icon'] = 2;
  456. $data['msg'] = "参数错误请刷新后重试!";
  457. }else{
  458. $wheres = array('user_id'=>array('$in'=>$ids));
  459. $this->user_model->set_val("status",$wheres,"40");
  460. $data['icon'] = 1;
  461. $data['msg'] = "批量禁用用户成功!";
  462. }
  463. $this->response($data);
  464. }
  465. /**
  466. * 批量启用用户
  467. */
  468. public function enable_all(){
  469. $ids = rtrim($this->input->post("ids",true),",");
  470. $ids = explode(",",$ids);
  471. if(count($ids)<1){
  472. $data['icon'] = 2;
  473. $data['msg'] = "参数错误请刷新后重试!";
  474. }else{
  475. $wheres = array('user_id'=>array('$in'=>$ids));
  476. $this->user_model->set_val("status",$wheres,"10");
  477. $data['icon'] = 1;
  478. $data['msg'] = "批量启用用户成功!";
  479. }
  480. $this->response($data);
  481. }
  482. }