LoginAccountValidate.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. //微信小程序提交版本测试
  71. if($data['account'] == '13545228441' || $data['account'] == '17362953140'){
  72. return true;
  73. }
  74. if (!isset($data['code'])) {
  75. return '请输入手机验证码';
  76. }
  77. return $this->checkCode($data['code'], [], $data);
  78. }
  79. return true;
  80. }
  81. /**
  82. * @notes 登录密码校验
  83. * @param $password
  84. * @param $other
  85. * @param $data
  86. * @return bool|string
  87. * @author 段誉
  88. * @date 2022/9/15 14:39
  89. */
  90. public function checkPassword($password, $other, $data)
  91. {
  92. //账号安全机制,连续输错后锁定,防止账号密码暴力破解
  93. $userAccountSafeCache = new UserAccountSafeCache();
  94. if (!$userAccountSafeCache->isSafe()) {
  95. return '密码连续' . $userAccountSafeCache->count . '次输入错误,请' . $userAccountSafeCache->minute . '分钟后重试';
  96. }
  97. $where = [];
  98. if ($data['scene'] == LoginEnum::ACCOUNT_PASSWORD) {
  99. // 手机号密码登录
  100. $where = ['account|mobile' => $data['account']];
  101. }
  102. $userInfo = User::where($where)
  103. ->field(['password,is_disable'])
  104. ->findOrEmpty();
  105. if ($userInfo->isEmpty()) {
  106. return '用户不存在';
  107. }
  108. if ($userInfo['is_disable'] === YesNoEnum::YES) {
  109. return '用户已禁用';
  110. }
  111. if (empty($userInfo['password'])) {
  112. $userAccountSafeCache->record();
  113. return '用户不存在';
  114. }
  115. $passwordSalt = Config::get('project.unique_identification');
  116. if ($userInfo['password'] !== create_password($password, $passwordSalt)) {
  117. $userAccountSafeCache->record();
  118. return '密码错误';
  119. }
  120. $userAccountSafeCache->relieve();
  121. return true;
  122. }
  123. /**
  124. * @notes 校验验证码
  125. * @param $code
  126. * @param $rule
  127. * @param $data
  128. * @return bool|string
  129. * @author Tab
  130. * @date 2021/8/25 15:43
  131. */
  132. public function checkCode($code, $rule, $data)
  133. {
  134. $smsDriver = new SmsDriver();
  135. $result = $smsDriver->verify($data['account'], $code, NoticeEnum::LOGIN_CAPTCHA);
  136. if ($result) {
  137. return true;
  138. }
  139. return '验证码错误';
  140. }
  141. }