Auth.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. if($user->info['checkState'] == 2){
  44. return redirect("/common/auth/enterprise_edit");
  45. }
  46. $msg = $res_msg;
  47. }
  48. $url = "/admin";
  49. switch ($usertype) {
  50. case 1:
  51. //验证错误
  52. break;
  53. case 2:
  54. //验证错误
  55. $url = "/enterprise";
  56. break;
  57. case 3:
  58. //验证错误
  59. $url = "/person";
  60. break;
  61. }
  62. if (!$msg) {
  63. $user->setSession();
  64. return redirect($url);
  65. }
  66. }
  67. return view("", ["msg" => $msg]);
  68. }
  69. /**
  70. * 退出
  71. * @return type
  72. */
  73. public function logout() {
  74. session("user", null);
  75. return redirect("/index/auth/login");
  76. }
  77. /**
  78. * 验证密码
  79. */
  80. public function valid_password() {
  81. if ($user = session("user")) {
  82. $username = $user["account"];
  83. $usertype = $user["usertype"];
  84. $pwd = $this->request["password"];
  85. $user = new UserApi($username, $pwd, $usertype);
  86. if (!$user->checkPwd()) {
  87. return json()->data(["status" => 1, "msg" => "密码错误"]);
  88. } else {
  89. return json(["code" => 200]);
  90. }
  91. } else {
  92. return json()->data(["status" => 2]);
  93. }
  94. }
  95. public function policy(){
  96. return view("", []);
  97. }
  98. public function policy_list(){
  99. $level = $this->request->post('level');
  100. if($level){
  101. $where[] = ['level','=',$level];
  102. }else{
  103. $where[] = ['level','>',0];
  104. }
  105. $list = Db::table('new_policy')->where($where)->select()->toArray();
  106. $result = [];
  107. foreach ($list as $k => $v){
  108. $check = [];
  109. $condition = [];
  110. if(!empty($v['checks'])){
  111. $check = explode(',',$v['checks']);
  112. }
  113. if(!empty($v['condition'])){
  114. $condition = explode(',',$v['condition']);
  115. }
  116. $item = [
  117. 'id' => $v['id'],
  118. 'tag' => $v['tag'],
  119. 'policy' => $v['policy_name'],
  120. 'checks' => $check,
  121. 'condition' => $condition
  122. ];
  123. array_push($result,$item);
  124. }
  125. return json($result);
  126. }
  127. public function policy_update(){
  128. $res = $this->request->post();
  129. foreach ($res as $k => $v){
  130. $update = [];
  131. if(count($v['checks']) > 0){
  132. $update['checks'] = implode(',',$v['checks']);
  133. }
  134. if(count($v['condition']) > 0){
  135. $update['condition'] = implode(',',$v['condition']);
  136. }
  137. if(count($update) > 0){
  138. Db::table('new_policy')->where('id',$v['id'])->save($update);
  139. }
  140. }
  141. }
  142. }