admin
2021-04-02 20f473dc4997e8b8a76f8c75d0a649be0a5ea139
src/main/java/com/yeshi/buwan/controller/parser/ConfigParser.java
@@ -1,91 +1,267 @@
package com.yeshi.buwan.controller.parser;
import java.io.PrintWriter;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import com.yeshi.buwan.domain.DetailSystem;
import com.google.gson.GsonBuilder;
import com.google.gson.annotations.Expose;
import com.google.gson.reflect.TypeToken;
import com.yeshi.buwan.domain.system.DetailSystem;
import com.yeshi.buwan.domain.recommend.FloatAD;
import com.yeshi.buwan.dto.config.ADShieldIPConfig;
import com.yeshi.buwan.dto.log.BaseLog;
import com.yeshi.buwan.log.LogHelper;
import com.yeshi.buwan.service.imp.SystemService;
import com.yeshi.buwan.service.inter.ad.FloatADService;
import com.yeshi.buwan.service.manager.IPManager;
import com.yeshi.buwan.util.IPUtil;
import com.yeshi.buwan.util.StringUtil;
import com.yeshi.buwan.util.log.LoggerUtil;
import com.yeshi.buwan.util.log.UserActiveLogFactory;
import com.yeshi.buwan.vo.AcceptData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import com.google.gson.Gson;
import com.yeshi.buwan.service.imp.ConfigService;
import com.yeshi.buwan.service.imp.DetailSystemConfigService;
import com.yeshi.buwan.util.JsonUtil;
import net.sf.json.JSONObject;
@Controller
public class ConfigParser {
    Logger logger = LoggerFactory.getLogger(ConfigParser.class);
    @Resource
    private ConfigService configService;
    private DetailSystemConfigService configService;
    @Resource
    private SystemService systemService;
    private String getAdShowType(String key, String channel, int version, Map<String, String> map) {
    @Resource
    private FloatADService floatADService;
    @Resource
    private IPManager ipManager;
    public ADConfig getAdShowType(String key, String channel, int version, Map<String, String> map) {
        String splash = map.get(key);
        JSONObject jsonObject = JSONObject.fromObject(splash);
        ADConfig splashAD = null;
        if (jsonObject == null||jsonObject.isEmpty())
            return null;
        ADConfig adConfig = null;
        channel = channel.toLowerCase();
        if (jsonObject.optJSONObject(channel) == null) {
        if (jsonObject == null || jsonObject.optJSONObject(channel) == null) {
            // 默认为应用宝
            channel = "qq";
        }
        String adType = "";
        splashAD = new Gson().fromJson(jsonObject.optJSONObject(channel).toString(), ADConfig.class);
        if (version >= splashAD.getVersion()) {
            adType = "";
        } else {
            adType = splashAD.getType();
        adConfig = new Gson().fromJson(jsonObject.optJSONObject(channel).toString(), ADConfig.class);
        if (version >= adConfig.getVersion()) {
            adConfig = null;
        }
        return adType;
        return adConfig;
    }
    /**
     * 屏蔽所有的广告
     *
     * @param channel
     * @param version
     * @param shieldInfo
     * @param ip
     * @return
     */
    private boolean shieldAllAd(String channel, Integer version, String shieldInfo, String ip) {
        if (StringUtil.isNullOrEmpty(shieldInfo)) {
            return false;
        }
        Type type = new TypeToken<List<ADShieldIPConfig>>() {
        }.getType();
        List<ADShieldIPConfig> list = new Gson().fromJson(shieldInfo, type);
        Map<String, ADShieldIPConfig.IPShieldRule> map = new HashMap<>();
        for (ADShieldIPConfig config : list) {
            map.put(config.getChannel().toLowerCase(), config.getRule());
        }
        if (map.get(channel.toLowerCase()) != null) {
            ADShieldIPConfig.IPShieldRule rule = map.get(channel.toLowerCase());
            if (version >= rule.getVersion()) {
                //请求
                IPUtil.IPInfo ipInfo = null;
                try {
                    ipInfo = ipManager.getIPInfo(ip);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if (ipInfo != null) {
                    //先判断省
                    if (!StringUtil.isNullOrEmpty(rule.getProvince())) {
                        if (ipInfo.getProvince() != null && ipInfo.getProvince().contains(rule.getProvince())) {
                            if (!StringUtil.isNullOrEmpty(rule.getCity())) {//同时判断省和市
                                if (ipInfo.getCity() != null && ipInfo.getCity().contains(rule.getCity())) {
                                    return true;
                                }
                            } else {
                                //只判断省
                                return true;
                            }
                        }
                    } else if (!StringUtil.isNullOrEmpty(rule.getCity())) {
                        //只判断市
                        if (ipInfo.getCity() != null && ipInfo.getCity().contains(rule.getCity())) {
                            return true;
                        }
                    }
                }
            }
        }
        return false;
    }
    public void getConfig(AcceptData acceptData, HttpServletRequest request, PrintWriter out) {
        LoggerUtil.getUserActiveLogger().info(UserActiveLogFactory.createAppSplash(new BaseLog(acceptData, "")));
        DetailSystem detailSystem = systemService.getDetailSystemByPackage(acceptData.getPackageName());
        Map<String, String> map = configService.getConfigAsMap(detailSystem, acceptData.getVersion());
        JSONObject data = new JSONObject();
        JSONObject ad = new JSONObject();
        JSONObject adNew = new JSONObject();
        String ip = IPUtil.getRemotIP(request);
        String splash = getAdShowType("ad_splash_config", acceptData.getChannel(), acceptData.getVersion(), map);
        //会员链接
        String vipLink = map.get("vip_link");
        data.put("vipLink", vipLink);
        ADConfig splashConfig = getAdShowType("ad_splash_config", acceptData.getChannel(), acceptData.getVersion(), map);
        //开屏的地区屏蔽规则
        String shieldProvince = map.get("shield_province");
        if(!StringUtil.isNullOrEmpty(shieldProvince)) {
            JSONObject json = JSONObject.fromObject(shieldProvince);
        String shieldInfo = map.get("shield_province");
        boolean hiddenSplash = false;
        if (!StringUtil.isNullOrEmpty(shieldInfo)) {
            JSONObject json = JSONObject.fromObject(shieldInfo);
            //获取渠道需要屏蔽的城市
            String province = json.optString(acceptData.getChannel().toLowerCase());
            if (!StringUtil.isNullOrEmpty(splash) && !StringUtil.isNullOrEmpty(province)) {
                String pro = IPUtil.getIPProvince(ip);
                if (pro != null && pro.contains(province)) {//屏蔽IP
                    splash = "";
            if (splashConfig != null && !StringUtil.isNullOrEmpty(province)) {
                IPUtil.IPInfo ipInfo = null;
                try {
                    ipInfo = ipManager.getIPInfo(ip);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if (ipInfo != null && ipInfo.getProvince().contains(province)) {//屏蔽IP
                    hiddenSplash = true;
                    LogHelper.print("城市屏蔽:" + province);
                }
            }
        }
        //广告全部屏蔽区域
        String shieldAdLocationInfo = map.get("shield_ad_ip_location");
        boolean hiddenAll = shieldAllAd(acceptData.getChannel(), acceptData.getVersion(), shieldAdLocationInfo, ip);
        if (hiddenSplash)
            splashConfig = null;
        String splash = "";
        if (splashConfig != null)
            splash = splashConfig.getType();
        Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
        if (hiddenAll)
            splashConfig = null;
        //开屏
        ad.put("splash", splash);
        adNew.put("splash", gson.toJson(splashConfig));
        ADConfig adConfig = null;
        //视频前贴
        ad.put("videoPlayPre", getAdShowType("ad_play_video_pre", acceptData.getChannel(), acceptData.getVersion(), map));
        adConfig = getAdShowType("ad_play_video_pre", acceptData.getChannel(), acceptData.getVersion(), map);
        if (hiddenAll)
            adConfig = null;
        ad.put("videoPlayPre", adConfig == null ? "" : adConfig.getType());
        adNew.put("videoPlayPre", gson.toJson(adConfig));
        //APP退出
        ad.put("exitApp", getAdShowType("ad_exit_app", acceptData.getChannel(), acceptData.getVersion(), map));
        adConfig = getAdShowType("ad_exit_app", acceptData.getChannel(), acceptData.getVersion(), map);
        if (hiddenAll)
            adConfig = null;
        ad.put("exitApp", adConfig == null ? "" : adConfig.getType());
        adNew.put("exitApp", gson.toJson(adConfig));
        //全屏广告控制
        adConfig = getAdShowType("ad_video_detail_full_video", acceptData.getChannel(), acceptData.getVersion(), map);
        if (hiddenAll)
            adConfig = null;
        ad.put("videoDetailFullVideo", adConfig == null ? "" : adConfig.getType());
        adNew.put("videoDetailFullVideo", gson.toJson(adConfig));
        //搜索页广告控制
        adConfig = getAdShowType("ad_video_search", acceptData.getChannel(), acceptData.getVersion(), map);
        if (hiddenAll)
            adConfig = null;
        ad.put("videoSearch", adConfig == null ? "" : adConfig.getType());
        adNew.put("videoSearch", gson.toJson(adConfig));
        //pptv免广告激励视频
        adConfig = getAdShowType("pptv_no_ad_reward", acceptData.getChannel(), acceptData.getVersion(), map);
        if (hiddenAll)
            adConfig = null;
        adNew.put("pptvNoAdReward", gson.toJson(adConfig));
        //热启动开屏
        adConfig = getAdShowType("ad_splash_hot_start", acceptData.getChannel(), acceptData.getVersion(), map);
        if (hiddenAll)
            adConfig = null;
        adNew.put("splashHotStart", gson.toJson(adConfig));
        data.put("ad", ad);
        data.put("adNew", adNew);
        //PPTV免广告权益时长,暂时设置为3小时
        data.put("pptvNoAdRewardHour", 3);
        //设置网页加速域名
        data.put("webSpeedUpHost", map.get("web_speed_up_host"));
        out.print(JsonUtil.loadTrueJson(data.toString()));
    }
    /**
     * 获取首页配置信息
     *
     * @param acceptData
     * @param request
     * @param out
     */
    public void getHomeConfig(AcceptData acceptData, HttpServletRequest request, PrintWriter out) {
        LoggerUtil.getUserActiveLogger().info(UserActiveLogFactory.createAppMainPage(new BaseLog(acceptData, "")));
        List<FloatAD> adList = floatADService.listShowAD(1, 1);
        Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
        JSONObject data = new JSONObject();
        if (adList != null && adList.size() > 0) {
            FloatAD ad = adList.get(0);
            data.put("floatAD", gson.toJson(ad));
        }
        out.print(JsonUtil.loadTrueJson(data.toString()));
    }
    class ADConfig {
        int version;
        @Expose
        String type;
        @Expose
        String pid;
        public int getVersion() {
            return version;
@@ -103,6 +279,13 @@
            this.type = type;
        }
        public String getPid() {
            return pid;
        }
        public void setPid(String pid) {
            this.pid = pid;
        }
    }
}