LoginAccountValidate.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. namespace app\workerapi\validate;
  3. use app\common\cache\MasterWokerAccountSafeCache;
  4. use app\common\enum\LoginEnum;
  5. use app\common\enum\notice\NoticeEnum;
  6. use app\common\enum\user\UserTerminalEnum;
  7. use app\common\enum\YesNoEnum;
  8. use app\common\model\master_worker_register\MasterWorkerRegister;
  9. use app\common\service\sms\SmsDriver;
  10. use think\facade\Config;
  11. use app\common\model\master_worker\MasterWorker;
  12. use app\common\validate\BaseValidate;
  13. /**
  14. * @author 林海涛
  15. * @date ${DATA}
  16. */
  17. Class LoginAccountValidate extends BaseValidate
  18. {
  19. protected $rule = [
  20. 'account' => 'require',
  21. //'password' => 'require',
  22. 'terminal' => 'require|in:' . UserTerminalEnum::WECHAT_MMP . ',' . UserTerminalEnum::WECHAT_OA . ','
  23. . UserTerminalEnum::H5 . ',' . UserTerminalEnum::PC . ',' . UserTerminalEnum::IOS .
  24. ',' . UserTerminalEnum::ANDROID,
  25. 'scene' => 'require|in:' . LoginEnum::ACCOUNT_PASSWORD . ',' . LoginEnum::MOBILE_CAPTCHA . '|checkConfig',
  26. ];
  27. protected $message = [
  28. 'account.require' => '请输入手机号或账号',
  29. //'password.require' => '请输入密码',
  30. 'terminal.require' => '终端参数缺失',
  31. 'terminal.in' => '终端参数状态值不正确',
  32. 'scene.require' => '场景不能为空',
  33. 'scene.in' => '场景值错误',
  34. ];
  35. /**
  36. * @notes 登录场景相关校验
  37. * @param $scene
  38. * @param $rule
  39. * @param $data
  40. * @return bool|string
  41. * @author 段誉
  42. * @date 2022/9/15 14:37
  43. */
  44. public function checkConfig($scene, $rule, $data)
  45. {
  46. // 账号密码登录
  47. if (LoginEnum::ACCOUNT_PASSWORD == $scene) {
  48. if (!isset($data['password'])) {
  49. return '请输入密码';
  50. }
  51. return $this->checkPassword($data['password'], [], $data);
  52. }
  53. // 手机验证码登录
  54. if (LoginEnum::MOBILE_CAPTCHA == $scene) {
  55. if (!isset($data['code'])) {
  56. return '请输入手机验证码';
  57. }
  58. return $this->checkCode($data['code'], [], $data);
  59. }
  60. return true;
  61. }
  62. /**
  63. * @notes 登录密码校验
  64. * @param $password
  65. * @param $other
  66. * @param $data
  67. * @return bool|string
  68. * @author 段誉
  69. * @date 2022/9/15 14:39
  70. */
  71. public function checkPassword($password, $other, $data)
  72. {
  73. //账号安全机制,连续输错后锁定,防止账号密码暴力破解
  74. $userAccountSafeCache = new MasterWokerAccountSafeCache();
  75. if (!$userAccountSafeCache->isSafe()) {
  76. return '密码连续' . $userAccountSafeCache->count . '次输入错误,请' . $userAccountSafeCache->minute . '分钟后重试';
  77. }
  78. $where = [];
  79. if ($data['scene'] == LoginEnum::ACCOUNT_PASSWORD) {
  80. // 手机号密码登录
  81. $where = ['account|mobile' => $data['account']];
  82. }
  83. $userInfo = MasterWorker::where($where)
  84. ->field(['password,is_disable'])
  85. ->findOrEmpty();
  86. if ($userInfo->isEmpty()) {
  87. $worker_register = MasterWorkerRegister::where('mobile',$data['account'])->findOrEmpty();
  88. if(!$worker_register->isEmpty() && $worker_register->status==0){
  89. return '您的入驻信息正在审核中,客服将在1-2个工作日内联系您进行入驻操作';
  90. }
  91. return true;
  92. }
  93. if ($userInfo['is_disable'] === YesNoEnum::YES) {
  94. return '用户已禁用';
  95. }
  96. if (empty($userInfo['password'])) {
  97. $userAccountSafeCache->record();
  98. return '用户不存在';
  99. }
  100. $passwordSalt = Config::get('project.unique_identification');
  101. if ($userInfo['password'] !== create_password($password, $passwordSalt)) {
  102. $userAccountSafeCache->record();
  103. return '密码错误';
  104. }
  105. $userAccountSafeCache->relieve();
  106. return true;
  107. }
  108. /**
  109. * @notes 校验验证码
  110. * @param $code
  111. * @param $rule
  112. * @param $data
  113. * @return bool|string
  114. * @author Tab
  115. * @date 2021/8/25 15:43
  116. */
  117. public function checkCode($code, $rule, $data)
  118. {
  119. $smsDriver = new SmsDriver();
  120. $result = $smsDriver->verify($data['account'], $code, NoticeEnum::LOGIN_CAPTCHA);
  121. if ($result) {
  122. return true;
  123. }
  124. return '验证码错误';
  125. }
  126. }