CheckIpCityService.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. namespace app\common\service;
  3. use app\common\library\Ip;
  4. use app\common\service\VipShortMsgService;
  5. use app\main\constants\AdminConstants;
  6. use think\Log;
  7. /**
  8. * 用戶登录验证
  9. * Class CheckIpCityService
  10. * @package app\main\service
  11. */
  12. class CheckIpCityService
  13. {
  14. /**
  15. * @var CheckIpCityService
  16. */
  17. protected static $self = NULL;
  18. /**
  19. * @return CheckIpCityService
  20. */
  21. public static function instance()
  22. {
  23. if (self::$self == NULL) {
  24. self::$self = new self();
  25. }
  26. return self::$self;
  27. }
  28. /**
  29. *
  30. * @param int $channelId
  31. * @return bool|mixed
  32. * @throws \think\Exception
  33. */
  34. public function getChannelMobile(int $channelId)
  35. {
  36. $res = model('vipIpCityWhitelist')->where('vip_id','=',$channelId)->find();
  37. if (!empty($res)){
  38. $data = $res->toArray();
  39. return $data['contact_mobile'];
  40. }
  41. return false;
  42. }
  43. /**
  44. * 登录(已验证账号密码正确)时验证 城市 ip地址
  45. * @param $admin_id
  46. * @param $username
  47. * @param $nickname
  48. * @return array
  49. * @throws \think\Exception
  50. */
  51. public function checkIpCity($admin_id, $username, $nickname)
  52. {
  53. $res = [
  54. 'code'=>1,
  55. 'msg'=>'成功'
  56. ];
  57. $ip = Ip::ip();
  58. $cityCode = Ip::citycode($ip);
  59. /**
  60. * 内网 IP 或 公司外网 IP 加白
  61. */
  62. if ($cityCode == 0 || $ip == '103.121.164.210') {
  63. return $res;
  64. }
  65. $groupId = model('AuthGroupAccess')->getGroupId($admin_id);
  66. switch ($groupId) {
  67. case AdminConstants::ADMIN_GROUP_ID_VIP:
  68. $groupName = 'VIP';
  69. $vipId = $admin_id;
  70. break;
  71. case AdminConstants::ADMIN_GROUP_ID_VIP_OPERATOR:
  72. $groupName = 'VIP运营';
  73. $adminExtend = \app\main\service\AdminService::instance()->getAdminExtendModel()->getInfo($admin_id);
  74. $vipId = $adminExtend['create_by'];
  75. break;
  76. case AdminConstants::ADMIN_GROUP_ID_CHANNEL:
  77. $groupName = '渠道';
  78. $vipAdminBind = model('VipAdminBind')
  79. ->field('vip_admin_bind.admin_id_master')
  80. ->join('auth_group_access ac', 'ac.uid = vip_admin_bind.admin_id_master')
  81. ->where('vip_admin_bind.admin_id_slave', '=', $admin_id)
  82. ->where('ac.group_id', AdminConstants::ADMIN_GROUP_ID_VIP)
  83. ->find();
  84. if ($vipAdminBind) {
  85. $vipId = $vipAdminBind['admin_id_master'];
  86. } else {
  87. return $res;
  88. }
  89. break;
  90. default:
  91. return $res;
  92. }
  93. $data = model('vipIpCityWhitelist')->where('vip_id', '=', $vipId)->find();
  94. if (empty($data)){
  95. return $res;
  96. }
  97. $data = $data->toArray();
  98. $allowIps = explode(',',$data['ip']);
  99. $allowCitys = explode(',',$data['city_code']);
  100. if (!empty($data['ip']) && !in_array($ip, $allowIps)) {
  101. $res['code'] = 0;
  102. $res['msg'] = 'IP非法,'.$ip;
  103. } elseif (!empty($data['city_code']) && !in_array($cityCode, $allowCitys)) {
  104. $res['code'] = 0;
  105. $res['msg'] = '城市非法,'.$cityCode;
  106. }
  107. if ($res['code'] == 0) {
  108. $msgArr = [
  109. '异常登录',
  110. "群组:{$groupName}",
  111. "ID:{$admin_id}",
  112. "账号:{$username}",
  113. "昵称:{$nickname}",
  114. "msg:{$res['msg']}"
  115. ];
  116. $content = implode(',',$msgArr);
  117. Log::info($content);
  118. //锁定用户
  119. model('Admin')->update(['status' => 'hidden','updatetime' => time()], ['id' => $admin_id]);
  120. //发送短信接口
  121. VipShortMsgService::instance()->throwNotice($content);
  122. }
  123. return $res;
  124. }
  125. }