LoginValidate.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. namespace app\kefuapi\validate;
  3. use app\common\basics\Validate;
  4. use app\common\enum\KefuEnum;
  5. use app\common\logic\ChatLogic;
  6. use app\common\model\Admin;
  7. use app\common\model\shop\ShopAdmin;
  8. use think\facade\Cache;
  9. /**
  10. * 客服登录验证
  11. * Class LoginValidate
  12. * @package app\shopapi\validate
  13. */
  14. class LoginValidate extends Validate
  15. {
  16. protected $rule = [
  17. 'type' => 'require',
  18. 'client' => 'require',
  19. 'account' => 'require',
  20. 'password' => 'require|checkPassword',
  21. ];
  22. protected $message = [
  23. 'type.require' => '参数缺失',
  24. 'account.require' => '请输入账号',
  25. 'password.require' => '请输入密码',
  26. 'password.checkPassword' => '账号或密码错误',
  27. 'client.require' => '请输入客户端'
  28. ];
  29. /**
  30. * @notes 校验密码
  31. * @param $password
  32. * @param $other
  33. * @param $data
  34. * @return bool|string
  35. * @author 段誉
  36. * @date 2021/11/9 16:02
  37. */
  38. protected function checkPassword($password, $other, $data)
  39. {
  40. $field = 'k.id, k.shop_id, a.account, a.salt, a.password,
  41. k.disable as kefu_disable, a.disable as admin_disable';
  42. $condition = ['a.account' => $data['account'], 'k.del' => 0, 'a.del' => 0];
  43. if (KefuEnum::TYPE_SHOP == $data['type']) {
  44. $isPlatform = false;
  45. $chat = (new ShopAdmin())->alias('a')
  46. ->field($field)
  47. ->join('kefu k', 'a.id = k.admin_id and a.shop_id = k.shop_id')
  48. ->where($condition)
  49. ->findOrEmpty();
  50. } else {
  51. $isPlatform = true;
  52. $chat = (new Admin())->alias('a')
  53. ->field($field)
  54. ->join('kefu k', 'k.admin_id = a.id')
  55. ->where(['k.shop_id' => 0])
  56. ->where($condition)
  57. ->findOrEmpty();
  58. }
  59. if (false === $this->safe(false, $isPlatform)) {
  60. $this->message['password.password'] .= ':多次输入错误';
  61. return false;
  62. }
  63. if ($chat->isEmpty()) {
  64. $this->safe(true, $isPlatform);
  65. return '账号不存在';
  66. }
  67. if ($chat['kefu_disable'] || $chat['admin_disable']) {
  68. return '账号被禁用';
  69. }
  70. $password = generatePassword($password, $chat['salt']);
  71. if ($password != $chat['password']) {
  72. $this->safe(true, $isPlatform);
  73. return false;
  74. }
  75. // 检查后台配置是否开启,当前缓存驱动是否redis
  76. if (false === ChatLogic::checkConfig($chat['shop_id'])) {
  77. return ChatLogic::getError() ?: '请联系管理员设置后台配置';
  78. }
  79. return true;
  80. }
  81. /**
  82. * 连续30分钟内15次输错密码,无法登录
  83. * @param bool $add
  84. * @return bool
  85. */
  86. protected function safe($status = false, $isPlatform = false)
  87. {
  88. if ($isPlatform) {
  89. $errorCount = 'platform_kefu_error_count' . request()->ip();
  90. } else {
  91. $errorCount = 'shop_kefu_error_count' . request()->ip();
  92. }
  93. if ($status) {
  94. $loginErrorCount = Cache::get($errorCount);
  95. $loginErrorCount++;
  96. Cache::tag('kefu_login_error_count')->set($errorCount, $loginErrorCount, 1800);
  97. }
  98. $count = Cache::get($errorCount);
  99. if (!empty($count) && $count >= 15) {
  100. return false;
  101. }
  102. return true;
  103. }
  104. }