1
0

LoginAccountValidate.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. 'code.require' => 'code 字段是必填项',
  35. ];
  36. /**
  37. * @notes 登录场景相关校验
  38. * @param $scene
  39. * @param $rule
  40. * @param $data
  41. * @return bool|string
  42. * @author 段誉
  43. * @date 2022/9/15 14:37
  44. */
  45. public function checkConfig($scene, $rule, $data)
  46. {
  47. // 账号密码登录
  48. if (LoginEnum::ACCOUNT_PASSWORD == $scene) {
  49. if (!isset($data['password'])) {
  50. return '请输入密码';
  51. }
  52. return $this->checkPassword($data['password'], [], $data);
  53. }
  54. // 手机验证码登录
  55. if (LoginEnum::MOBILE_CAPTCHA == $scene) {
  56. if (!isset($data['code'])) {
  57. return '请输入手机验证码';
  58. }
  59. return $this->checkCode($data['code'], [], $data);
  60. }
  61. return true;
  62. }
  63. /**
  64. * @notes 登录密码校验
  65. * @param $password
  66. * @param $other
  67. * @param $data
  68. * @return bool|string
  69. * @author 段誉
  70. * @date 2022/9/15 14:39
  71. */
  72. public function checkPassword($password, $other, $data)
  73. {
  74. //账号安全机制,连续输错后锁定,防止账号密码暴力破解
  75. $userAccountSafeCache = new MasterWokerAccountSafeCache();
  76. if (!$userAccountSafeCache->isSafe()) {
  77. return '密码连续' . $userAccountSafeCache->count . '次输入错误,请' . $userAccountSafeCache->minute . '分钟后重试';
  78. }
  79. $where = [];
  80. if ($data['scene'] == LoginEnum::ACCOUNT_PASSWORD) {
  81. // 手机号密码登录
  82. $where = ['account|mobile' => $data['account']];
  83. }
  84. $userInfo = MasterWorker::where($where)
  85. ->field(['password,is_disable'])
  86. ->findOrEmpty();
  87. if ($userInfo->isEmpty()) {
  88. $worker_register = MasterWorkerRegister::where('mobile',$data['account'])->findOrEmpty();
  89. if(!$worker_register->isEmpty() && $worker_register->status==0){
  90. return '您的入驻信息正在审核中,客服将在1-2个工作日内联系您进行入驻操作';
  91. }
  92. return true;
  93. }
  94. if ($userInfo['is_disable'] === YesNoEnum::YES) {
  95. return '用户已禁用';
  96. }
  97. if (empty($userInfo['password'])) {
  98. $userAccountSafeCache->record();
  99. return '用户不存在';
  100. }
  101. $passwordSalt = Config::get('project.unique_identification');
  102. if ($userInfo['password'] !== create_password($password, $passwordSalt)) {
  103. $userAccountSafeCache->record();
  104. return '密码错误';
  105. }
  106. $userAccountSafeCache->relieve();
  107. return true;
  108. }
  109. /**
  110. * @notes 校验验证码
  111. * @param $code
  112. * @param $rule
  113. * @param $data
  114. * @return bool|string
  115. * @author Tab
  116. * @date 2021/8/25 15:43
  117. */
  118. public function checkCode($code, $rule, $data)
  119. {
  120. $smsDriver = new SmsDriver();
  121. $result = $smsDriver->verify($data['account'], $code, NoticeEnum::LOGIN_CAPTCHA);
  122. if ($result) {
  123. return true;
  124. }
  125. return '验证码错误';
  126. }
  127. public function sceneTemporary()
  128. {
  129. $this->only(['account', 'code'])->append('code', 'require');
  130. }
  131. }