LoginRecord.php 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Description: 应用配置
  4. // +----------------------------------------------------------------------
  5. // | Author: Michael_xu | gengxiaoxu@5kcrm.com
  6. // +----------------------------------------------------------------------
  7. namespace app\admin\model;
  8. use com\Scan;
  9. class LoginRecord extends Common
  10. {
  11. /**
  12. * 为了数据库的整洁,同时又不影响Model和Controller的名称
  13. * 我们约定每个模块的数据表都加上相同的前缀,比如CRM模块用crm作为数据表前缀
  14. */
  15. protected $name = 'admin_login_record';
  16. protected $autoWriteTimestamp = true;
  17. protected $createTime = 'create_time';
  18. protected $updateTime = false;
  19. /**
  20. * 登录成功
  21. */
  22. const TYPE_SUCCESS = 0;
  23. /**
  24. * 密码错误
  25. */
  26. const TYPE_PWD_ERROR = 1;
  27. /**
  28. * 账号被禁用
  29. */
  30. const TYPE_USER_BANNED = 2;
  31. // 类型
  32. public $typeList = [
  33. self::TYPE_SUCCESS => '登录成功',
  34. self::TYPE_PWD_ERROR => '密码错误',
  35. self::TYPE_USER_BANNED => '账号被禁用',
  36. ];
  37. /**
  38. * 登录员工ID
  39. */
  40. public $user_id = 0;
  41. /**
  42. * 添加登录记录
  43. *
  44. * @param int $type
  45. */
  46. public function createRecord($type = 0)
  47. {
  48. $data = [];
  49. $data['type'] = $type;
  50. $data['create_user_id'] = $this->user_id;
  51. $data['create_time'] = time();
  52. $data['ip'] = (new Scan())->get_client_ip();
  53. $data['os'] = getOS();
  54. $data['browser'] = getBrowser();
  55. $ip_address = getAddressById($data['ip']);
  56. $data['address'] = $ip_address['country'];
  57. // 效果图有这个备注字段,不知道存啥,就把UA记录了一下
  58. $data['remark'] = $_SERVER['HTTP_USER_AGENT'];
  59. $this->save($data);
  60. }
  61. /**
  62. * 创建人
  63. */
  64. public function getCreateUserInfoAttr($val, $data)
  65. {
  66. return User::getUserById($data['create_user_id']) ?: [];
  67. }
  68. /**
  69. * 获取登录记录类型
  70. */
  71. public function getTypeNameAttr($val, $data)
  72. {
  73. return $this->typeList[$data['type']];
  74. }
  75. /**
  76. *
  77. */
  78. /**
  79. * 固定时间内登录密码错超过限制
  80. *
  81. * @param integer $count 登录出错次数
  82. * @param integer $time 等待时间 (分钟)
  83. * @return bool
  84. */
  85. public function verify($count = 3, $time = 5)
  86. {
  87. $where = [
  88. 'create_user_id' => $this->user_id,
  89. 'create_time' => ['GT', time() - 60 * $time],
  90. 'type' => 1
  91. ];
  92. $last_record = $this->order(['id' => 'DESC'])
  93. ->where($where)
  94. ->find();
  95. // 登录记录
  96. if ($last_record) {
  97. $last_time = strtotime($last_record['create_time']);
  98. $where['create_time'] = [
  99. 'BETWEEN',
  100. [
  101. $last_time - 60 * $time,
  102. $last_time
  103. ]
  104. ];
  105. $list = $this->where($where)
  106. ->order(['id' => 'DESC'])
  107. ->column('type');
  108. if (count($list) >= $count) {
  109. $surplusTime = getTimeBySec(60 * $time - (time()-strtotime($last_record['create_time'])));
  110. $this->error = "密码错误次数过多,请在{$surplusTime}后重试!";
  111. return false;
  112. }
  113. }
  114. return true;
  115. }
  116. }