|
@@ -2,6 +2,7 @@ package com.chuanxia.mcp.service.impl;
|
|
|
|
|
|
import cn.hutool.core.collection.CollectionUtil;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
|
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
@@ -9,23 +10,29 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
import com.chuanxia.mcp.bean.dto.DomainBindDto;
|
|
|
import com.chuanxia.mcp.bean.dto.DomainQueryDto;
|
|
|
import com.chuanxia.mcp.bean.enums.DefaultDomainEnums;
|
|
|
-import com.chuanxia.mcp.bean.model.CdnDomain;
|
|
|
-import com.chuanxia.mcp.bean.model.CdnDomainCity;
|
|
|
-import com.chuanxia.mcp.bean.model.SystemUser;
|
|
|
+import com.chuanxia.mcp.bean.enums.DeviceUaLevel;
|
|
|
+import com.chuanxia.mcp.bean.model.*;
|
|
|
import com.chuanxia.mcp.bean.vo.Result;
|
|
|
import com.chuanxia.mcp.exception.ResultException;
|
|
|
import com.chuanxia.mcp.mapper.CdnDomainMapper;
|
|
|
+import com.chuanxia.mcp.mapper.CdnDomainOperatorMapper;
|
|
|
+import com.chuanxia.mcp.mapper.CdnUaMapper;
|
|
|
import com.chuanxia.mcp.service.CdnDoaminService;
|
|
|
import com.chuanxia.mcp.service.CdnDomainCityService;
|
|
|
import com.chuanxia.mcp.service.UserService;
|
|
|
+import com.chuanxia.mcp.util.IpUtil;
|
|
|
+import eu.bitwalker.useragentutils.Browser;
|
|
|
+import eu.bitwalker.useragentutils.OperatingSystem;
|
|
|
+import eu.bitwalker.useragentutils.UserAgent;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.List;
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.util.*;
|
|
|
|
|
|
/**
|
|
|
* @author Administrator
|
|
@@ -37,6 +44,8 @@ public class CdnDoaminServiceImpl extends ServiceImpl<CdnDomainMapper, CdnDomain
|
|
|
private final UserService userService;
|
|
|
private final CdnDomainMapper domainMapper;
|
|
|
private final CdnDomainCityService domainCityService;
|
|
|
+ private final CdnUaMapper cdnUaMapper;
|
|
|
+ private final CdnDomainOperatorMapper domainOperatorMapper;
|
|
|
|
|
|
@Override
|
|
|
public Result<IPage<CdnDomain>> listPage(DomainQueryDto queryDto) {
|
|
@@ -63,15 +72,11 @@ public class CdnDoaminServiceImpl extends ServiceImpl<CdnDomainMapper, CdnDomain
|
|
|
if (StringUtils.isBlank(domain.getManufacturer())) {
|
|
|
throw new ResultException(500, "厂商不能为空");
|
|
|
}
|
|
|
- Integer defaultDomain = domain.getDefaultDomain();
|
|
|
- if (DefaultDomainEnums.DEFAULTDOMAIN.getStatus().equals(defaultDomain)) {
|
|
|
- //判断是否已存在默认域名,存在则不允许添加了
|
|
|
- LambdaQueryWrapper<CdnDomain> query = Wrappers.lambdaQuery();
|
|
|
- query.eq(CdnDomain::getDefaultDomain, defaultDomain);
|
|
|
- int count = this.count(query);
|
|
|
- if (count >= 1) {
|
|
|
- throw new ResultException(500, "已存在默认域名,不能再次添加默认域名");
|
|
|
- }
|
|
|
+ long count = this.count();
|
|
|
+ if (count <= 0) {
|
|
|
+ domain.setDefaultDomain(1);
|
|
|
+ } else {
|
|
|
+ domain.setDefaultDomain(0);
|
|
|
}
|
|
|
domain.setCreateBy(loginUser.getId());
|
|
|
this.save(domain);
|
|
@@ -111,12 +116,169 @@ public class CdnDoaminServiceImpl extends ServiceImpl<CdnDomainMapper, CdnDomain
|
|
|
//判断是否已存在默认域名,存在则不允许添加了
|
|
|
LambdaQueryWrapper<CdnDomain> query = Wrappers.lambdaQuery();
|
|
|
query.eq(CdnDomain::getDefaultDomain, defaultDomain).ne(CdnDomain::getId, domain.getId());
|
|
|
- int count = this.count(query);
|
|
|
+ long count = this.count(query);
|
|
|
if (count >= 1) {
|
|
|
throw new ResultException(500, "已存在默认域名,不能再次添加默认域名");
|
|
|
}
|
|
|
}
|
|
|
this.updateById(domain);
|
|
|
+ domainOperatorMapper.updateDomainByDomainId(domain.getId(),domain.getDomain());
|
|
|
return Result.success("操作成功");
|
|
|
}
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Result getCdnByVideoId(String videoId, BigDecimal internetSpeed, String operatorName, HttpServletRequest request) {
|
|
|
+
|
|
|
+ //根据ip获取归属地
|
|
|
+ String requestIp = IpUtil.getRequestIp(request);
|
|
|
+ try {
|
|
|
+ requestIp = "27.37.0.1";
|
|
|
+ String cityInfo = IpUtil.getCityInfo(requestIp);
|
|
|
+ CdnDomain cdnDomain = new CdnDomain();
|
|
|
+ if (StringUtils.isNotBlank(cityInfo)) {
|
|
|
+ String[] split = cityInfo.split("\\|");
|
|
|
+ if (split.length >= 5) {
|
|
|
+ //根据城市名获取域名信息
|
|
|
+ String city = split[3].replaceAll("市", "");
|
|
|
+ cdnDomain = getDomainByCityName(city);
|
|
|
+ if (cdnDomain == null) {
|
|
|
+ String operator = split[4];
|
|
|
+ if (StringUtils.isBlank(operator)) {
|
|
|
+ //根据城市没有获取到域名,根据运营商获取域名信息
|
|
|
+ operator = operatorName;
|
|
|
+ }
|
|
|
+ cdnDomain = getDomainByOperator(operator);
|
|
|
+ if (cdnDomain == null) {
|
|
|
+ //根据运营商没有获取到域名,根据时间区间获取域名信息
|
|
|
+ cdnDomain = getDomainByTimeRule();
|
|
|
+ if (cdnDomain == null) {
|
|
|
+ //根据时间没有获取到域名,获取默认域名
|
|
|
+ LambdaQueryWrapper<CdnDomain> query = Wrappers.lambdaQuery();
|
|
|
+ query.eq(CdnDomain::getDefaultDomain, 1);
|
|
|
+ cdnDomain = domainMapper.selectOne(query);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //获取ua
|
|
|
+ String header = request.getHeader("user-agent");
|
|
|
+ UserAgent userAgent = UserAgent.parseUserAgentString(header);
|
|
|
+ CdnUa uaByConfig = getUaByConfig(userAgent);
|
|
|
+ String ratio = "1024*768";
|
|
|
+ if (uaByConfig != null) {
|
|
|
+ //根据配置+网速,生成对应分辨率
|
|
|
+ Integer level = uaByConfig.getLevel();
|
|
|
+ //计算分辨率
|
|
|
+ level = countLevel(level, internetSpeed);
|
|
|
+ if (DeviceUaLevel.HIGH.getLevel().equals(level)) {
|
|
|
+
|
|
|
+ ratio = "1980*1080";
|
|
|
+ } else if (DeviceUaLevel.MEDIUM.getLevel().equals(level)) {
|
|
|
+ ratio = "1280*720";
|
|
|
+ } else if (DeviceUaLevel.LOW.getLevel().equals(level)) {
|
|
|
+ ratio = "1024*768";
|
|
|
+ }
|
|
|
+ }
|
|
|
+ String m3u8Url = cdnDomain.getDomain() + "/" + ratio;
|
|
|
+
|
|
|
+ return Result.success(m3u8Url);
|
|
|
+ } else {
|
|
|
+ throw new ResultException(500, "根据ip地址获取城市信息失败");
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ private Integer countLevel(Integer level, BigDecimal internetSpeed) {
|
|
|
+ //网速分为三个档位
|
|
|
+ Integer speedLevel = 1;
|
|
|
+ if (internetSpeed.doubleValue() <= 100) {
|
|
|
+ speedLevel = 3;
|
|
|
+ } else if (100 < internetSpeed.doubleValue() && internetSpeed.doubleValue() < 500) {
|
|
|
+ speedLevel = 2;
|
|
|
+ }
|
|
|
+ if (level > speedLevel) {
|
|
|
+ return speedLevel;
|
|
|
+ } else {
|
|
|
+ return level;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据城市信息获取cdn
|
|
|
+ *
|
|
|
+ * @param city 城市名
|
|
|
+ * @return cdn
|
|
|
+ */
|
|
|
+ private CdnDomain getDomainByCityName(String city) {
|
|
|
+ log.info("根据城市名获取cdn,当前城市:{}", city);
|
|
|
+ return domainMapper.getDomainByCityName(city);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据
|
|
|
+ *
|
|
|
+ * @param operator 运营商
|
|
|
+ * @return cdn
|
|
|
+ */
|
|
|
+ private CdnDomain getDomainByOperator(String operator) {
|
|
|
+ log.info("根据运营商获取cdn,当前运营商:{}", operator);
|
|
|
+ return domainMapper.getDomainByOperator(operator);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据当前时间获取cdn
|
|
|
+ *
|
|
|
+ * @return cdn
|
|
|
+ */
|
|
|
+ private CdnDomain getDomainByTimeRule() {
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ int hours = cal.get(Calendar.HOUR_OF_DAY);
|
|
|
+ log.info("根据时间获取cdn,当前小时:{}", hours);
|
|
|
+ return domainMapper.getDomainByTimeRule(hours);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据当前浏览器ua,获取对应配置
|
|
|
+ *
|
|
|
+ * @param userAgent
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private CdnUa getUaByConfig(UserAgent userAgent) {
|
|
|
+ LambdaQueryWrapper<CdnUa> query = Wrappers.lambdaQuery();
|
|
|
+ //获取浏览器对象
|
|
|
+ Browser browser = userAgent.getBrowser();
|
|
|
+ //获取操作系统对象
|
|
|
+ OperatingSystem operatingSystem = userAgent.getOperatingSystem();
|
|
|
+ Browser group = browser.getGroup();
|
|
|
+ String name = group.getName();
|
|
|
+ if (StringUtils.isNotBlank(name)) {
|
|
|
+ query.eq(CdnUa::getBrowser, name);
|
|
|
+ }
|
|
|
+ String version = userAgent.getBrowserVersion().getVersion();
|
|
|
+ if (StringUtils.isNotBlank(version)) {
|
|
|
+ query.eq(CdnUa::getBrowserVersion, version);
|
|
|
+ }
|
|
|
+ String os = operatingSystem.getGroup().getName();
|
|
|
+ if (StringUtils.isNotBlank(os)) {
|
|
|
+ query.eq(CdnUa::getOs, os);
|
|
|
+ }
|
|
|
+ log.info("获取到用户浏览器:{},浏览器版本:{},操作系统:{}", name, version, os);
|
|
|
+ query.orderByDesc(CdnUa::getCreateTime);
|
|
|
+ query.last("limit 0,1");
|
|
|
+ System.out.println("浏览器名:" + browser.getName());
|
|
|
+ System.out.println("浏览器类型:" + browser.getBrowserType());
|
|
|
+ System.out.println("浏览器家族:" + browser.getGroup());
|
|
|
+ System.out.println("浏览器生产厂商:" + browser.getManufacturer());
|
|
|
+ System.out.println("浏览器使用的渲染引擎:" + browser.getRenderingEngine());
|
|
|
+ System.out.println("浏览器版本:" + userAgent.getBrowserVersion());
|
|
|
+ System.out.println("操作系统名:" + operatingSystem.getName());
|
|
|
+ System.out.println("访问设备类型:" + operatingSystem.getDeviceType());
|
|
|
+ System.out.println("操作系统家族:" + operatingSystem.getGroup());
|
|
|
+ System.out.println("操作系统生产厂商:" + operatingSystem.getManufacturer());
|
|
|
+
|
|
|
+ return cdnUaMapper.selectOne(query);
|
|
|
+ }
|
|
|
}
|