Auth.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. namespace app\index\controller;
  3. use app\BaseController;
  4. use app\common\api\UserApi;
  5. /**
  6. * Description of Login
  7. *
  8. * @author sgq
  9. */
  10. class Auth extends BaseController {
  11. /**
  12. * 登录
  13. * @return type
  14. */
  15. public function login() {
  16. $msg = "";
  17. if ($this->request->isPost()) {
  18. $username = $this->request["username"];
  19. $pwd = $this->request["password"];
  20. $usertype = $this->request["usertype"];
  21. $user = new UserApi($username, $pwd, $usertype);
  22. if (!$userinfo = $user->getUserInfo()) {
  23. $msg = "用户不存在";
  24. }
  25. if (!$user->checkPwd()) {
  26. $msg = "用户名或者密码错误";
  27. }
  28. $url = "/admin";
  29. switch ($usertype) {
  30. case 1:
  31. //验证错误
  32. break;
  33. case 2:
  34. //验证错误
  35. $url = "/enterprise";
  36. break;
  37. case 3:
  38. //验证错误
  39. $url = "/person";
  40. break;
  41. }
  42. if (!$msg) {
  43. $user->setSession();
  44. if (session("user")) {
  45. return redirect($url);
  46. }
  47. }
  48. }
  49. return view("", ["msg" => $msg]);
  50. }
  51. /**
  52. * 退出
  53. * @return type
  54. */
  55. public function logout() {
  56. session("user", null);
  57. return redirect("/index/auth/login");
  58. }
  59. /**
  60. * 验证密码
  61. */
  62. public function valid_password() {
  63. if ($user = session("user")) {
  64. $username = $user["account"];
  65. $usertype = $user["usertype"];
  66. $pwd = $this->request["password"];
  67. $user = new UserApi($username, $pwd, $usertype);
  68. if (!$user->checkPwd()) {
  69. return json()->data(["status" => 1, "msg" => "密码错误"]);
  70. } else {
  71. return json(["code" => 200]);
  72. }
  73. } else {
  74. return json()->data(["status" => 2]);
  75. }
  76. }
  77. }