Auth.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. namespace app\index\controller;
  3. use app\BaseController;
  4. use app\common\api\UserApi;
  5. use think\facade\Db;
  6. /**
  7. * Description of Login
  8. *
  9. * @author sgq
  10. */
  11. class Auth extends BaseController {
  12. /**
  13. * 登录
  14. * @return type
  15. */
  16. public function login() {
  17. if ($user = session("user")) {
  18. if($user['usertype'] == 2){
  19. return redirect("/enterprise");
  20. }
  21. }
  22. $msg = "";
  23. if ($this->request->isPost()) {
  24. $username = $this->request["username"];
  25. $pwd = $this->request["password"];
  26. $usertype = $this->request["usertype"];
  27. $user = new UserApi($username, $pwd, $usertype);
  28. if (!$userinfo = $user->getUserInfo()) {
  29. $msg = "用户不存在";
  30. } else if (!$user->checkPwd()) {
  31. $login_fail = session('login_fail');
  32. if($login_fail){
  33. $login_fail++;
  34. if($login_fail >= 5){
  35. session('captcha',1);
  36. }
  37. }else{
  38. $login_fail = 1;
  39. }
  40. session('login_fail',$login_fail);
  41. $msg = "用户名或者密码错误";
  42. } else if($res_msg = $user->checkState()){
  43. $msg = $res_msg;
  44. }
  45. $url = "/admin";
  46. switch ($usertype) {
  47. case 1:
  48. //验证错误
  49. break;
  50. case 2:
  51. //验证错误
  52. $url = "/enterprise";
  53. break;
  54. case 3:
  55. //验证错误
  56. $url = "/person";
  57. break;
  58. }
  59. if (!$msg) {
  60. $user->setSession();
  61. return redirect($url);
  62. }
  63. }
  64. return view("", ["msg" => $msg]);
  65. }
  66. /**
  67. * 退出
  68. * @return type
  69. */
  70. public function logout() {
  71. session("user", null);
  72. return redirect("/index/auth/login");
  73. }
  74. /**
  75. * 验证密码
  76. */
  77. public function valid_password() {
  78. if ($user = session("user")) {
  79. $username = $user["account"];
  80. $usertype = $user["usertype"];
  81. $pwd = $this->request["password"];
  82. $user = new UserApi($username, $pwd, $usertype);
  83. if (!$user->checkPwd()) {
  84. return json()->data(["status" => 1, "msg" => "密码错误"]);
  85. } else {
  86. return json(["code" => 200]);
  87. }
  88. } else {
  89. return json()->data(["status" => 2]);
  90. }
  91. }
  92. public function policy(){
  93. return view("", []);
  94. }
  95. public function policy_list(){
  96. $level = $this->request->post('level');
  97. if($level){
  98. $where[] = ['level','=',$level];
  99. }else{
  100. $where[] = ['level','>',0];
  101. }
  102. $list = Db::table('new_policy')->where($where)->select()->toArray();
  103. $result = [];
  104. foreach ($list as $k => $v){
  105. $check = [];
  106. $condition = [];
  107. if(!empty($v['checks'])){
  108. $check = explode(',',$v['checks']);
  109. }
  110. if(!empty($v['condition'])){
  111. $condition = explode(',',$v['condition']);
  112. }
  113. $item = [
  114. 'id' => $v['id'],
  115. 'tag' => $v['tag'],
  116. 'policy' => $v['policy_name'],
  117. 'checks' => $check,
  118. 'condition' => $condition
  119. ];
  120. array_push($result,$item);
  121. }
  122. return json($result);
  123. }
  124. public function policy_update(){
  125. $res = $this->request->post();
  126. foreach ($res as $k => $v){
  127. $update = [];
  128. if(count($v['checks']) > 0){
  129. $update['checks'] = implode(',',$v['checks']);
  130. }
  131. if(count($v['condition']) > 0){
  132. $update['condition'] = implode(',',$v['condition']);
  133. }
  134. if(count($update) > 0){
  135. Db::table('new_policy')->where('id',$v['id'])->save($update);
  136. }
  137. }
  138. }
  139. }