LoginAccountValidate.php 4.9 KB

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