LoginAccountValidate.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. ];
  39. protected $message = [
  40. 'terminal.require' => '终端参数缺失',
  41. 'terminal.in' => '终端参数状态值不正确',
  42. 'scene.require' => '场景不能为空',
  43. 'scene.in' => '场景值错误',
  44. 'account.require' => '请输入账号',
  45. 'password.require' => '请输入密码',
  46. ];
  47. /**
  48. * @notes 登录场景相关校验
  49. * @param $scene
  50. * @param $rule
  51. * @param $data
  52. * @return bool|string
  53. * @author 段誉
  54. * @date 2022/9/15 14:37
  55. */
  56. public function checkConfig($scene, $rule, $data)
  57. {
  58. $config = ConfigService::get('login', 'login_way');
  59. if (!in_array($scene, $config)) {
  60. return '不支持的登录方式';
  61. }
  62. // 账号密码登录
  63. if (LoginEnum::ACCOUNT_PASSWORD == $scene) {
  64. if (!isset($data['password'])) {
  65. return '请输入密码';
  66. }
  67. return $this->checkPassword($data['password'], [], $data);
  68. }
  69. // 手机验证码登录
  70. if (LoginEnum::MOBILE_CAPTCHA == $scene) {
  71. if (!isset($data['code'])) {
  72. return '请输入手机验证码';
  73. }
  74. return $this->checkCode($data['code'], [], $data);
  75. }
  76. return true;
  77. }
  78. /**
  79. * @notes 登录密码校验
  80. * @param $password
  81. * @param $other
  82. * @param $data
  83. * @return bool|string
  84. * @author 段誉
  85. * @date 2022/9/15 14:39
  86. */
  87. public function checkPassword($password, $other, $data)
  88. {
  89. //账号安全机制,连续输错后锁定,防止账号密码暴力破解
  90. $userAccountSafeCache = new UserAccountSafeCache();
  91. if (!$userAccountSafeCache->isSafe()) {
  92. return '密码连续' . $userAccountSafeCache->count . '次输入错误,请' . $userAccountSafeCache->minute . '分钟后重试';
  93. }
  94. $where = [];
  95. if ($data['scene'] == LoginEnum::ACCOUNT_PASSWORD) {
  96. // 手机号密码登录
  97. $where = ['account|mobile' => $data['account']];
  98. }
  99. $userInfo = User::where($where)
  100. ->field(['password,is_disable'])
  101. ->findOrEmpty();
  102. if ($userInfo->isEmpty()) {
  103. return '用户不存在';
  104. }
  105. if ($userInfo['is_disable'] === YesNoEnum::YES) {
  106. return '用户已禁用';
  107. }
  108. if (empty($userInfo['password'])) {
  109. $userAccountSafeCache->record();
  110. return '用户不存在';
  111. }
  112. $passwordSalt = Config::get('project.unique_identification');
  113. if ($userInfo['password'] !== create_password($password, $passwordSalt)) {
  114. $userAccountSafeCache->record();
  115. return '密码错误';
  116. }
  117. $userAccountSafeCache->relieve();
  118. return true;
  119. }
  120. /**
  121. * @notes 校验验证码
  122. * @param $code
  123. * @param $rule
  124. * @param $data
  125. * @return bool|string
  126. * @author Tab
  127. * @date 2021/8/25 15:43
  128. */
  129. public function checkCode($code, $rule, $data)
  130. {
  131. $smsDriver = new SmsDriver();
  132. $result = $smsDriver->verify($data['account'], $code, NoticeEnum::LOGIN_CAPTCHA);
  133. if ($result) {
  134. return true;
  135. }
  136. return '验证码错误';
  137. }
  138. }