User.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. namespace app\controller;
  3. use app\BaseController;
  4. use app\model\TokenModel;
  5. use app\model\UserModel;
  6. use think\facade\Cache;
  7. class User extends BaseController
  8. {
  9. public function login(): \think\response\Json
  10. {
  11. $user = $this->request->post('username', '0');
  12. $pass = $this->request->post('password', '0');
  13. $info = UserModel::where("mail", $user)->field('id,mail,password,login_fail_count,login_ip,login_time')->find();
  14. if (Cache::get('login.' . $user)) {
  15. return $this->error("账号已被安全锁定,您可以修改密码然后登录");
  16. }
  17. if (!$info) {
  18. return $this->error("账号不存在");
  19. }
  20. if ($info['login_fail_count'] == 10) {
  21. Cache::set('login.' . $user, 'lock', 7200);
  22. $info->login_fail_count = 0;
  23. $info->save();
  24. return $this->error("账号已被锁定2小时");
  25. }
  26. if ($info['password'] != md5($pass)) {
  27. $info->login_fail_count += 1;
  28. $info->save();
  29. return $this->error("账号不存在或密码错误");
  30. }
  31. $token = renderToken($user);
  32. $agent = $this->request->header("User-Agent");
  33. $agent = mb_substr($agent, 0, 250);
  34. $auth = ["user_id" => $info['id'], 'token' => $token, 'create_time' => time(), 'ip' => $this->request->ip(), 'user_agent' => $agent];
  35. $add = TokenModel::insert($auth);
  36. unset($auth['user_agent']);
  37. unset($auth['ip']);
  38. $info->login_ip = getRealIp();
  39. $info->login_time = date("Y-m-d H:i:s");
  40. $info->login_fail_count = 0;//登陆成功将失败次数归零
  41. $info->save();
  42. return $this->success("登录成功", $auth);
  43. }
  44. function register(): \think\response\Json
  45. {
  46. $user = $this->request->post('username', false);
  47. $pass = $this->request->post('password', false);
  48. $code = $this->request->post('code', '0000');
  49. if ($user && $pass) {
  50. if (!validateEmail($user)) {
  51. return $this->error("邮箱格式错误");
  52. }
  53. if (strlen($pass) < 6) {
  54. return $this->error("密码过短");
  55. }
  56. $cacheCode = Cache::get("code" . $user);
  57. if (!$cacheCode || $cacheCode != $code) {
  58. return $this->error('验证码错误');
  59. }
  60. if (UserModel::where("mail", $user)->field("id,mail")->find()) {
  61. return $this->error("账号已存在");
  62. }
  63. $add = UserModel::insert(["mail" => $user, "password" => md5($pass), "create_time" => date('Y-m-d H:i:s'),'register_ip'=>getRealIp()]);
  64. if ($add) {
  65. Cache::delete("code" . $user);
  66. return $this->success("ok");
  67. }
  68. }
  69. return $this->error('注册失败');
  70. }
  71. public function forgetPass(): \think\response\Json
  72. {
  73. $user = $this->request->post('username', false);
  74. $pass = $this->request->post('password', false);
  75. $code = $this->request->post('code', '0000');
  76. if ($user && $pass) {
  77. if (!validateEmail($user)) {
  78. return $this->error("邮箱格式错误");
  79. }
  80. if (strlen($pass) < 6) {
  81. return $this->error("密码过短");
  82. }
  83. $info = UserModel::where("mail", $user)->field("id,mail")->find();
  84. if (!$info) {
  85. return $this->error("账号不存在");
  86. }
  87. $cacheCode = Cache::get("code" . $user);
  88. if ($cacheCode && $cacheCode == $code) {
  89. $info->password = md5($pass);
  90. $add = $info->save();
  91. if ($add) {
  92. TokenModel::where("user_id", $info['id'])->delete(); //删除所有登录记录
  93. Cache::delete('login.' . $user);
  94. return $this->success("ok");
  95. }
  96. } else {
  97. return $this->error('验证码错误');
  98. }
  99. }
  100. return $this->error('修改失败');
  101. }
  102. public function get(): \think\response\Json
  103. {
  104. $info = $this->getUser(true);
  105. if ($info) {
  106. $info = UserModel::field('id,mail,manager')->find($info['user_id']);
  107. return $this->success('ok', $info);
  108. }
  109. return $this->error('获取失败');
  110. }
  111. }