LoginRecord.php 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. * todo 登录设备暂时不加 数据表字段未加。
  44. * @param int $type
  45. * @param int $platform 登录设备
  46. */
  47. public function createRecord($platform='',$type = 0)
  48. {
  49. $data = [];
  50. $data['type'] = $type;
  51. $data['create_user_id'] = $this->user_id;
  52. $data['create_time'] = time();
  53. $data['ip'] = (new Scan())->get_client_ip();
  54. $data['os'] = getOS();
  55. $data['browser'] = getBrowser();
  56. # todo登录设备暂时不加 数据表字段未加
  57. // $platform=['_mobile'=>'手机','_ding'=>'钉钉','_wechat'=>'微信','_wxwork'=>'企业微信'];
  58. // if(empty($platform)){
  59. // $data['device']='网页';
  60. // }else{
  61. // $data['device']=$platform[$platform];
  62. // }
  63. $ip_address = getAddressById($data['ip']);
  64. $data['address'] = $ip_address['country'];
  65. // 效果图有这个备注字段,不知道存啥,就把UA记录了一下
  66. $data['remark'] = '';
  67. $this->save($data);
  68. }
  69. /**
  70. * 创建人
  71. */
  72. public function getCreateUserInfoAttr($val, $data)
  73. {
  74. return User::getUserById($data['create_user_id']) ?: [];
  75. }
  76. /**
  77. * 获取登录记录类型
  78. */
  79. public function getTypeNameAttr($val, $data)
  80. {
  81. return $this->typeList[$data['type']];
  82. }
  83. /**
  84. *
  85. */
  86. /**
  87. * 固定时间内登录密码错超过限制
  88. *
  89. * @param integer $count 登录出错次数
  90. * @param integer $time 等待时间 (分钟)
  91. * @return bool
  92. */
  93. public function verify($count = 3, $time = 5)
  94. {
  95. $where = [
  96. 'create_user_id' => $this->user_id,
  97. 'create_time' => ['GT', time() - 60 * $time],
  98. 'type' => 1
  99. ];
  100. $last_record = $this->order(['id' => 'DESC'])
  101. ->where($where)
  102. ->find();
  103. // 登录记录
  104. if ($last_record) {
  105. $last_time = strtotime($last_record['create_time']);
  106. $where['create_time'] = [
  107. 'BETWEEN',
  108. [
  109. $last_time - 60 * $time,
  110. $last_time
  111. ]
  112. ];
  113. $list = $this->where($where)
  114. ->order(['id' => 'DESC'])
  115. ->column('type');
  116. if (count($list) >= $count) {
  117. $surplusTime = getTimeBySec(60 * $time - (time()-strtotime($last_record['create_time'])));
  118. $this->error = "密码错误次数过多,请在{$surplusTime}后重试!";
  119. return false;
  120. }
  121. }
  122. return true;
  123. }
  124. }