LoginValidate.php 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeshop100%开源免费商用商城系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | 商业版本务必购买商业授权,以免引起法律纠纷
  8. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  9. // | gitee下载:https://gitee.com/likeshop_gitee
  10. // | github下载:https://github.com/likeshop-github
  11. // | 访问官网:https://www.likeshop.cn
  12. // | 访问社区:https://home.likeshop.cn
  13. // | 访问手册:http://doc.likeshop.cn
  14. // | 微信公众号:likeshop技术社区
  15. // | likeshop团队 版权所有 拥有最终解释权
  16. // +----------------------------------------------------------------------
  17. // | author: likeshopTeam
  18. // +----------------------------------------------------------------------
  19. namespace app\businessapi\validate;
  20. use app\common\cache\AdminAccountSafeCache;
  21. use app\common\enum\AdminTerminalEnum;
  22. use app\common\model\Admin;
  23. use app\common\service\ConfigService;
  24. use app\common\validate\BaseValidate;
  25. use think\facade\Config;
  26. class LoginValidate extends BaseValidate
  27. {
  28. protected $rule = [
  29. 'terminal' => 'require|in:' . AdminTerminalEnum::PC . ',' . AdminTerminalEnum::MOBILE.','.AdminTerminalEnum::BUSINESSEA,
  30. 'account' => 'require',
  31. 'password' => 'require|password',
  32. ];
  33. protected $message = [
  34. 'account.require' => '请输入账号',
  35. 'password.require' => '请输入密码'
  36. ];
  37. /**
  38. * @notes @notes 密码验证
  39. * @param $password
  40. * @param $other
  41. * @param $data
  42. * @return bool|string
  43. * @throws \think\db\exception\DataNotFoundException
  44. * @throws \think\db\exception\DbException
  45. * @throws \think\db\exception\ModelNotFoundException
  46. * @author 令狐冲
  47. * @date 2021/7/2 14:00
  48. */
  49. public function password($password, $other, $data)
  50. {
  51. // 登录限制
  52. $config = [
  53. 'login_restrictions' => ConfigService::get('shop', 'login_restrictions'),
  54. 'password_error_times' => ConfigService::get('shop', 'password_error_times'),
  55. 'limit_login_time' => ConfigService::get('shop', 'limit_login_time'),
  56. ];
  57. $adminAccountSafeCache = new AdminAccountSafeCache();
  58. if ($config['login_restrictions'] == 1) {
  59. $adminAccountSafeCache->count = $config['password_error_times'];
  60. $adminAccountSafeCache->minute = $config['limit_login_time'];
  61. }
  62. //后台账号安全机制,连续输错后锁定,防止账号密码暴力破解
  63. if ($config['login_restrictions'] == 1 && !$adminAccountSafeCache->isSafe()) {
  64. return '密码连续' . $adminAccountSafeCache->count . '次输入错误,请' . $adminAccountSafeCache->minute . '分钟后重试';
  65. }
  66. $adminInfo = Admin::where('account', '=', $data['account'])
  67. ->field(['password,disable'])
  68. ->find();
  69. if (empty($adminInfo)) {
  70. return '账号不存在';
  71. }
  72. if ($adminInfo['disable'] === 1) {
  73. return '账号已禁用';
  74. }
  75. if (empty($adminInfo['password'])) {
  76. $adminAccountSafeCache->record();
  77. return '账号不存在';
  78. }
  79. $passwordSalt = Config::get('project.unique_identification');
  80. if ($adminInfo['password'] !== create_password($password, $passwordSalt)) {
  81. $adminAccountSafeCache->record();
  82. return '密码错误';
  83. }
  84. $adminAccountSafeCache->relieve();
  85. return true;
  86. }
  87. }