LoginValidate.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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\adminapi\validate;
  20. use app\adminapi\logic\settings\shop\ShopSettingLogic;
  21. use app\common\enum\AdminTerminalEnum;
  22. use app\common\model\Admin;
  23. use app\common\cache\AdminAccountSafeCache;
  24. use app\common\service\ConfigService;
  25. use app\common\validate\BaseValidate;
  26. use think\facade\Config;
  27. class LoginValidate extends BaseValidate
  28. {
  29. protected $rule = [
  30. 'terminal' => 'require|in:' . AdminTerminalEnum::PC . ',' . AdminTerminalEnum::MOBILE,
  31. 'account' => 'require',
  32. 'password' => 'require|password',
  33. ];
  34. protected $message = [
  35. 'account.require' => '请输入账号',
  36. 'password.require' => '请输入密码'
  37. ];
  38. /**
  39. * @notes @notes 密码验证
  40. * @param $password
  41. * @param $other
  42. * @param $data
  43. * @return bool|string
  44. * @throws \think\db\exception\DataNotFoundException
  45. * @throws \think\db\exception\DbException
  46. * @throws \think\db\exception\ModelNotFoundException
  47. * @author 令狐冲
  48. * @date 2021/7/2 14:00
  49. */
  50. public function password($password, $other, $data)
  51. {
  52. // 登录限制
  53. $config = [
  54. 'login_restrictions' => ConfigService::get('shop', 'login_restrictions'),
  55. 'password_error_times' => ConfigService::get('shop', 'password_error_times'),
  56. 'limit_login_time' => ConfigService::get('shop', 'limit_login_time'),
  57. ];
  58. $adminAccountSafeCache = new AdminAccountSafeCache();
  59. if ($config['login_restrictions'] == 1) {
  60. $adminAccountSafeCache->count = $config['password_error_times'];
  61. $adminAccountSafeCache->minute = $config['limit_login_time'];
  62. }
  63. //后台账号安全机制,连续输错后锁定,防止账号密码暴力破解
  64. if ($config['login_restrictions'] == 1 && !$adminAccountSafeCache->isSafe()) {
  65. return '密码连续' . $adminAccountSafeCache->count . '次输入错误,请' . $adminAccountSafeCache->minute . '分钟后重试';
  66. }
  67. $adminInfo = Admin::where('account', '=', $data['account'])
  68. ->field(['password,disable'])
  69. ->find();
  70. if (empty($adminInfo)) {
  71. return '账号不存在';
  72. }
  73. if ($adminInfo['disable'] === 1) {
  74. return '账号已禁用';
  75. }
  76. if (empty($adminInfo['password'])) {
  77. $adminAccountSafeCache->record();
  78. return '账号不存在';
  79. }
  80. $passwordSalt = Config::get('project.unique_identification');
  81. if ($adminInfo['password'] !== create_password($password, $passwordSalt)) {
  82. $adminAccountSafeCache->record();
  83. return '密码错误';
  84. }
  85. $adminAccountSafeCache->relieve();
  86. return true;
  87. }
  88. }