LoginServer.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. namespace app\admin\server\index;
  3. use app\admin\model\SystemAdmin;
  4. use Tinywan\Jwt\JwtToken;
  5. class LoginServer
  6. {
  7. /**
  8. * Notes:管理端登录
  9. * @param string $user_name
  10. * @param string $password
  11. * @param string $captcha
  12. * @return array
  13. * User: yym
  14. * Date: 2022/8/2
  15. */
  16. public static function login(string $user_name, string $password, string $captcha)
  17. {
  18. $access_token = array();
  19. $token = static::loginIn($user_name, $password);
  20. $access_token['expires_time'] = $token['expires_in'];
  21. $access_token['token'] = $token['access_token'];
  22. $access_token['userInfo'] = $token['userInfo'];
  23. return $access_token;
  24. }
  25. /**
  26. * Notes:手机号验证码登录处理
  27. * @param string $user_name
  28. * @param string $password
  29. * @return array
  30. * @throws \Exception
  31. * User: yym
  32. * Date: 2022/8/2
  33. */
  34. private static function loginIn(string $user_name, string $password)
  35. {
  36. SystemAdmin::affairBegin();
  37. try {
  38. $token = array();
  39. $info = SystemAdmin::getInfo($user_name);
  40. if(!empty($info))
  41. {
  42. if($info['admin_pwd'] != md5(sha1($password)))
  43. {
  44. throw new \Exception('账号/密码错误~');
  45. }
  46. switch ($info['admin_status'])
  47. {
  48. case SystemAdmin::DISABLED://已禁用
  49. throw new \Exception('您的账号已禁用,请联系管理员~');
  50. break;
  51. }
  52. $extend = array(
  53. 'id' => $info['admin_id']
  54. );
  55. $token = JwtToken::generateToken($extend);
  56. }else{
  57. throw new \Exception('账号不存在');
  58. }
  59. $update = array(
  60. 'admin_last_ip' => get_ip(),
  61. 'admin_last_time' => date("Y-m-d H:i:s", time()),
  62. 'admin_login_count' => bcadd((string)$info['admin_login_count'],'1'),
  63. 'admin_update_time' => date('Y-m-d H:i:s', time())
  64. );
  65. SystemAdmin::updateInfo($info['admin_id'], $update);
  66. SystemAdmin::affairCommit();
  67. unset($info['admin_pwd']);
  68. $token['userInfo'] = $info;
  69. return $token;
  70. }catch (\Exception $exception){
  71. SystemAdmin::affairRollback();
  72. throw new \Exception($exception->getMessage());
  73. }
  74. }
  75. /**
  76. * Notes:退出登录
  77. * @param string $token
  78. * @return bool
  79. * User: yym
  80. * Date: 2022/8/3
  81. */
  82. public static function loginOut(string $token)
  83. {
  84. if(empty($token))
  85. {
  86. return true;
  87. }
  88. JwtToken::delKey(1, $token);
  89. return true;
  90. }
  91. }