Login.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. namespace app\validate\api\user;
  3. use app\model\User;
  4. use app\service\ConfServiceFacade;
  5. use laytp\library\Str;
  6. use think\facade\Config;
  7. use think\Validate;
  8. class Login extends Validate
  9. {
  10. //数组顺序就是检测的顺序,比如这里,会先检测code验证码的正确性
  11. protected $rule = [
  12. 'account' => 'require',
  13. 'password' => 'require|checkPassword:',
  14. ];
  15. //定义内置方法检验失败后返回的字符
  16. protected $message = [
  17. 'account.require' => '用户名不能为空',
  18. 'password.require' => '密码不能为空',
  19. ];
  20. //自定义验证码检验方法
  21. protected function checkVerifyCode($verifyCode)
  22. {
  23. if(ConfServiceFacade::get("system.basic.loginNeedCaptcha") == 1){
  24. if (!captcha_check($verifyCode)) {
  25. return '验证码错误';
  26. } else {
  27. return true;
  28. }
  29. }
  30. return true;
  31. }
  32. //自定义密码检验方法
  33. protected function checkPassword($password, $rule, $data)
  34. {
  35. $account = $data['account'];
  36. $user = User::where('account', '=', $account)->find();
  37. if (!$user) {
  38. return '用户名或密码错误';
  39. }
  40. $passwordHash = $user->password;
  41. if (!Str::checkPassword($password, $passwordHash)) {
  42. return '用户名或密码错误';
  43. }
  44. $status = $user->status;
  45. if ($status == 2) {
  46. return '用户已被禁用,请联系管理员';
  47. }
  48. return true;
  49. }
  50. }