LoginAccountValidate.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeadmin快速开发前后端分离管理后台(PHP版)
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
  8. // | github下载:https://github.com/likeshop-github/likeadmin
  9. // | 访问官网:https://www.likeadmin.cn
  10. // | likeadmin团队 版权所有 拥有最终解释权
  11. // +----------------------------------------------------------------------
  12. // | author: likeadminTeam
  13. // +----------------------------------------------------------------------
  14. namespace app\api\validate;
  15. use app\common\cache\UserAccountSafeCache;
  16. use app\common\enum\LoginEnum;
  17. use app\common\enum\notice\NoticeEnum;
  18. use app\common\enum\user\UserTerminalEnum;
  19. use app\common\enum\YesNoEnum;
  20. use app\common\service\ConfigService;
  21. use app\common\service\sms\SmsDriver;
  22. use app\common\validate\BaseValidate;
  23. use app\common\model\user\User;
  24. use think\facade\Config;
  25. /**
  26. * 账号密码登录校验
  27. * Class LoginValidate
  28. * @package app\api\validate
  29. */
  30. class LoginAccountValidate extends BaseValidate
  31. {
  32. protected $rule = [
  33. 'terminal' => 'require|in:' . UserTerminalEnum::WECHAT_MMP . ',' . UserTerminalEnum::WECHAT_OA . ','
  34. . UserTerminalEnum::H5 . ',' . UserTerminalEnum::PC . ',' . UserTerminalEnum::IOS .
  35. ',' . UserTerminalEnum::ANDROID,
  36. 'scene' => 'require|in:' . LoginEnum::ACCOUNT_PASSWORD . ',' . LoginEnum::MOBILE_CAPTCHA . '|checkConfig',
  37. 'account' => 'require',
  38. 'password' => 'require'
  39. ];
  40. protected $message = [
  41. 'terminal.require' => '终端参数缺失',
  42. 'terminal.in' => '终端参数状态值不正确',
  43. 'scene.require' => '场景不能为空',
  44. 'scene.in' => '场景值错误',
  45. 'account.require' => '请输入账号',
  46. 'password.require' => '请输入密码',
  47. ];
  48. /**
  49. * @notes 登录场景相关校验
  50. * @param $scene
  51. * @param $rule
  52. * @param $data
  53. * @return bool|string
  54. * @author 段誉
  55. * @date 2022/9/15 14:37
  56. */
  57. public function checkConfig($scene, $rule, $data)
  58. {
  59. $config = ConfigService::get('login', 'login_way');
  60. if (!in_array($scene, $config)) {
  61. return '不支持的登录方式';
  62. }
  63. // 账号密码登录
  64. if (LoginEnum::ACCOUNT_PASSWORD == $scene) {
  65. if (!isset($data['password'])) {
  66. return '请输入密码';
  67. }
  68. return $this->checkPassword($data['password'], [], $data);
  69. }
  70. // 手机验证码登录
  71. if (LoginEnum::MOBILE_CAPTCHA == $scene) {
  72. if (!isset($data['code'])) {
  73. return '请输入手机验证码';
  74. }
  75. return $this->checkCode($data['code'], [], $data);
  76. }
  77. return true;
  78. }
  79. /**
  80. * @notes 登录密码校验
  81. * @param $password
  82. * @param $other
  83. * @param $data
  84. * @return bool|string
  85. * @author 段誉
  86. * @date 2022/9/15 14:39
  87. */
  88. public function checkPassword($password, $other, $data)
  89. {
  90. //账号安全机制,连续输错后锁定,防止账号密码暴力破解
  91. $userAccountSafeCache = new UserAccountSafeCache();
  92. if (!$userAccountSafeCache->isSafe()) {
  93. return '密码连续' . $userAccountSafeCache->count . '次输入错误,请' . $userAccountSafeCache->minute . '分钟后重试';
  94. }
  95. $where = [];
  96. if ($data['scene'] == LoginEnum::ACCOUNT_PASSWORD) {
  97. // 手机号密码登录
  98. $where = ['account|mobile' => $data['account']];
  99. }
  100. $userInfo = User::where($where)
  101. ->field(['password,is_disable'])
  102. ->findOrEmpty();
  103. if ($userInfo->isEmpty()) {
  104. return '用户不存在';
  105. }
  106. if ($userInfo['is_disable'] === YesNoEnum::YES) {
  107. return '用户已禁用';
  108. }
  109. if (empty($userInfo['password'])) {
  110. $userAccountSafeCache->record();
  111. return '用户不存在';
  112. }
  113. $passwordSalt = Config::get('project.unique_identification');
  114. if ($userInfo['password'] !== create_password($password, $passwordSalt)) {
  115. $userAccountSafeCache->record();
  116. return '密码错误';
  117. }
  118. $userAccountSafeCache->relieve();
  119. return true;
  120. }
  121. /**
  122. * @notes 校验验证码
  123. * @param $code
  124. * @param $rule
  125. * @param $data
  126. * @return bool|string
  127. * @author Tab
  128. * @date 2021/8/25 15:43
  129. */
  130. public function checkCode($code, $rule, $data)
  131. {
  132. $smsDriver = new SmsDriver();
  133. $result = $smsDriver->verify($data['account'], $code, NoticeEnum::LOGIN_CAPTCHA);
  134. if ($result) {
  135. return true;
  136. }
  137. return '验证码错误';
  138. }
  139. }