AdminUser.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. /**
  3. * Created by PhpStorm
  4. * User:林志杰
  5. * Email:[email protected]
  6. * Motto:纵有疾风起,人生不言弃!
  7. * Time:2020/1/10 20:46
  8. */
  9. namespace app\admin\business;
  10. use app\common\model\mysql\AdminUser as AdminUserModel;
  11. use think\db\exception\DataNotFoundException;
  12. use think\db\exception\DbException;
  13. use think\db\exception\ModelNotFoundException;
  14. use think\Exception;
  15. class AdminUser
  16. {
  17. public static function login($data)
  18. {
  19. $adminUser = self::getAdminUserByUsername($data['username']);
  20. if (empty($adminUser)) {
  21. throw new Exception('不存在该用户');
  22. }
  23. // 判断密码是否正确
  24. if ($adminUser['password'] !== md5($data['password'] . '_singwa_abc')) {
  25. throw new Exception('密码错误');
  26. // return show(config('status.error'), '密码错误');
  27. }
  28. $adminUserObj = new AdminUserModel();
  29. // 记录信息到mysql中
  30. $updateData = [
  31. 'last_login_time' => time(),
  32. 'last_login_ip' => request()->ip(),
  33. 'update_time' => time(),
  34. ];
  35. $res = $adminUserObj->updateById($adminUser['id'], $updateData);
  36. if (empty($res)) {
  37. throw new Exception('登陆失败');
  38. // return show(config('status.error'), '登陆失败');
  39. }
  40. // 记录session
  41. session(config('admin.session_admin'), $adminUser);
  42. return true;
  43. }
  44. /**
  45. * 通过用户名获取用户数据
  46. * @param $username
  47. * @return array|bool|\think\Model|null
  48. * @throws DataNotFoundException
  49. * @throws DbException
  50. * @throws ModelNotFoundException
  51. */
  52. public static function getAdminUserByUsername($username)
  53. {
  54. $adminUserObj = new AdminUserModel();
  55. $adminUser = $adminUserObj->getAdminUserByUsername($username);
  56. if (empty($adminUser) || $adminUser->status != config('status.mysql.table_normal')) {
  57. return false;
  58. }
  59. $adminUser = $adminUser->toArray();
  60. return $adminUser;
  61. }
  62. }