123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- <?php
- namespace app\common\service;
- use app\common\library\Ip;
- use app\common\service\VipShortMsgService;
- use app\main\constants\AdminConstants;
- use think\Log;
- /**
- * 用戶登录验证
- * Class CheckIpCityService
- * @package app\main\service
- */
- class CheckIpCityService
- {
- /**
- * @var CheckIpCityService
- */
- protected static $self = NULL;
- /**
- * @return CheckIpCityService
- */
- public static function instance()
- {
- if (self::$self == NULL) {
- self::$self = new self();
- }
- return self::$self;
- }
- /**
- *
- * @param int $channelId
- * @return bool|mixed
- * @throws \think\Exception
- */
- public function getChannelMobile(int $channelId)
- {
- $res = model('vipIpCityWhitelist')->where('vip_id','=',$channelId)->find();
- if (!empty($res)){
- $data = $res->toArray();
- return $data['contact_mobile'];
- }
- return false;
- }
- /**
- * 登录(已验证账号密码正确)时验证 城市 ip地址
- * @param $admin_id
- * @param $username
- * @param $nickname
- * @return array
- * @throws \think\Exception
- */
- public function checkIpCity($admin_id, $username, $nickname)
- {
- $res = [
- 'code'=>1,
- 'msg'=>'成功'
- ];
- $ip = Ip::ip();
- $cityCode = Ip::citycode($ip);
- /**
- * 内网 IP 或 公司外网 IP 加白
- */
- if ($cityCode == 0 || $ip == '103.121.164.210') {
- return $res;
- }
- $groupId = model('AuthGroupAccess')->getGroupId($admin_id);
- switch ($groupId) {
- case AdminConstants::ADMIN_GROUP_ID_VIP:
- $groupName = 'VIP';
- $vipId = $admin_id;
- break;
- case AdminConstants::ADMIN_GROUP_ID_VIP_OPERATOR:
- $groupName = 'VIP运营';
- $adminExtend = \app\main\service\AdminService::instance()->getAdminExtendModel()->getInfo($admin_id);
- $vipId = $adminExtend['create_by'];
- break;
- case AdminConstants::ADMIN_GROUP_ID_CHANNEL:
- $groupName = '渠道';
- $vipAdminBind = model('VipAdminBind')
- ->field('vip_admin_bind.admin_id_master')
- ->join('auth_group_access ac', 'ac.uid = vip_admin_bind.admin_id_master')
- ->where('vip_admin_bind.admin_id_slave', '=', $admin_id)
- ->where('ac.group_id', AdminConstants::ADMIN_GROUP_ID_VIP)
- ->find();
- if ($vipAdminBind) {
- $vipId = $vipAdminBind['admin_id_master'];
- } else {
- return $res;
- }
- break;
- default:
- return $res;
- }
- $data = model('vipIpCityWhitelist')->where('vip_id', '=', $vipId)->find();
- if (empty($data)){
- return $res;
- }
- $data = $data->toArray();
- $allowIps = explode(',',$data['ip']);
- $allowCitys = explode(',',$data['city_code']);
- if (!empty($data['ip']) && !in_array($ip, $allowIps)) {
- $res['code'] = 0;
- $res['msg'] = 'IP非法,'.$ip;
- } elseif (!empty($data['city_code']) && !in_array($cityCode, $allowCitys)) {
- $res['code'] = 0;
- $res['msg'] = '城市非法,'.$cityCode;
- }
- if ($res['code'] == 0) {
- $msgArr = [
- '异常登录',
- "群组:{$groupName}",
- "ID:{$admin_id}",
- "账号:{$username}",
- "昵称:{$nickname}",
- "msg:{$res['msg']}"
- ];
- $content = implode(',',$msgArr);
- Log::info($content);
- //锁定用户
- model('Admin')->update(['status' => 'hidden','updatetime' => time()], ['id' => $admin_id]);
- //发送短信接口
- VipShortMsgService::instance()->throwNotice($content);
- }
- return $res;
- }
- }
|