src/main/java/com/yeshi/buwan/controller/parser/ConfigParser.java
@@ -1,6 +1,8 @@ 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; @@ -9,15 +11,19 @@ import com.google.gson.GsonBuilder; import com.google.gson.annotations.Expose; import com.google.gson.reflect.TypeToken; import com.yeshi.buwan.domain.Config; import com.yeshi.buwan.domain.DetailSystem; import com.yeshi.buwan.domain.recommend.FloatAD; import com.yeshi.buwan.dto.config.ADShieldIPConfig; 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.vo.AcceptData; import net.sf.json.JSONArray; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Controller; @@ -40,6 +46,9 @@ @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); @@ -57,6 +66,61 @@ 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) { DetailSystem detailSystem = systemService.getDetailSystemByPackage(acceptData.getPackageName()); Map<String, String> map = configService.getConfigAsMap(detailSystem, acceptData.getVersion()); @@ -65,27 +129,38 @@ JSONObject adNew = new JSONObject(); String ip = IPUtil.getRemotIP(request); ADConfig splashConfig = getAdShowType("ad_splash_config", acceptData.getChannel(), acceptData.getVersion(), map); //开屏的地区屏蔽规则 String shieldProvince = map.get("shield_province"); boolean hidden = false; 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 (splashConfig != null && !StringUtil.isNullOrEmpty(province)) { String pro = IPUtil.getIPProvince(ip); LogHelper.print("城市屏蔽:" + ip + ":" + pro); if (pro != null && pro.contains(province)) {//屏蔽IP hidden = true; LogHelper.print("城市屏蔽:" + pro); } } } if (hidden) 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 = ""; @@ -94,6 +169,8 @@ Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create(); if (hiddenAll) splashConfig = null; //开屏 ad.put("splash", splash); adNew.put("splash", gson.toJson(splashConfig)); @@ -102,19 +179,25 @@ ADConfig adConfig = null; //视频前贴 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退出 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 (hidden) if (hiddenAll) adConfig = null; ad.put("videoSearch", adConfig == null ? "" : adConfig.getType()); adNew.put("videoSearch", gson.toJson(adConfig)); src/main/java/com/yeshi/buwan/domain/user/LoginUserExtra.java
@@ -24,10 +24,22 @@ private String wxNickName; private String wxPortrait; private Integer wxSex; //PPTV的用户ID private String pptvUid; private Date createTime; private Date updateTime; public String getPptvUid() { return pptvUid; } public void setPptvUid(String pptvUid) { this.pptvUid = pptvUid; } public Integer getWxSex() { return wxSex; } src/main/java/com/yeshi/buwan/domain/vip/VIPOrderRecord.java
New file @@ -0,0 +1,142 @@ package com.yeshi.buwan.domain.vip; import java.math.BigDecimal; import java.util.Date; //会员订单记录 public class VIPOrderRecord { //未支付 public final static int STATE_NOT_PAY = 0; //已支付 public final static int STATE_PAY = 1; //支付宝 public final static int PAY_WAY_ALIPAY = 1; //微信 public final static int PAY_WAY_WX = 2; private String id; //用户ID private String uid; //状态 private Integer state; //类型 private VIPPrice.VIPPriceType type; //资金 private BigDecimal money; //支付方式 private Integer payWay; //支付金额 private BigDecimal payMoney; //付款时间 private Date payTime; private Date createTime; private Date updateTime; //会员开始时间 private Date vipStartTime; //会员结束时间 private Date vipEndTime; public Date getVipStartTime() { return vipStartTime; } public void setVipStartTime(Date vipStartTime) { this.vipStartTime = vipStartTime; } public Date getVipEndTime() { return vipEndTime; } public void setVipEndTime(Date vipEndTime) { this.vipEndTime = vipEndTime; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getUid() { return uid; } public void setUid(String uid) { this.uid = uid; } public Integer getState() { return state; } public void setState(Integer state) { this.state = state; } public VIPPrice.VIPPriceType getType() { return type; } public void setType(VIPPrice.VIPPriceType type) { this.type = type; } public BigDecimal getMoney() { return money; } public void setMoney(BigDecimal money) { this.money = money; } public Integer getPayWay() { return payWay; } public void setPayWay(Integer payWay) { this.payWay = payWay; } public BigDecimal getPayMoney() { return payMoney; } public void setPayMoney(BigDecimal payMoney) { this.payMoney = payMoney; } public Date getPayTime() { return payTime; } public void setPayTime(Date payTime) { this.payTime = payTime; } public Date getCreateTime() { return createTime; } public void setCreateTime(Date createTime) { this.createTime = createTime; } public Date getUpdateTime() { return updateTime; } public void setUpdateTime(Date updateTime) { this.updateTime = updateTime; } } src/main/java/com/yeshi/buwan/domain/vip/VIPPrice.java
New file @@ -0,0 +1,26 @@ package com.yeshi.buwan.domain.vip; import org.springframework.data.mongodb.core.mapping.Document; import java.math.BigDecimal; import java.util.Date; @Document(collection = "vipPrice") public class VIPPrice { public enum VIPPriceType { week("周会员"), month("包月"), season("包季"), year("包年"); private String name; private VIPPriceType(String name) { this.name = name; } } private String id; private VIPPriceType type; private BigDecimal price; private Date createTime; private Date updateTime; } src/main/java/com/yeshi/buwan/dto/config/ADShieldIPConfig.java
New file @@ -0,0 +1,59 @@ package com.yeshi.buwan.dto.config; /** * 广告屏蔽IP设置 */ public class ADShieldIPConfig { //渠道 private String channel; private IPShieldRule rule; public String getChannel() { return channel; } public void setChannel(String channel) { this.channel = channel; } public IPShieldRule getRule() { return rule; } public void setRule(IPShieldRule rule) { this.rule = rule; } public static class IPShieldRule { private String province; private String city; private Integer version; public String getProvince() { return province; } public void setProvince(String province) { this.province = province; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public Integer getVersion() { return version; } public void setVersion(Integer version) { this.version = version; } } } src/main/java/com/yeshi/buwan/service/imp/ConfigService.java
@@ -1,12 +1,7 @@ package com.yeshi.buwan.service.imp; import java.io.Serializable; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.annotation.Resource; import com.yeshi.buwan.dao.ConfigDao; import com.yeshi.buwan.domain.Config; import com.yeshi.buwan.domain.DetailSystem; import org.hibernate.HibernateException; import org.hibernate.Session; @@ -14,8 +9,10 @@ import org.springframework.orm.hibernate4.HibernateCallback; import org.springframework.stereotype.Service; import com.yeshi.buwan.dao.ConfigDao; import com.yeshi.buwan.domain.Config; import javax.annotation.Resource; import java.util.HashMap; import java.util.List; import java.util.Map; @Service public class ConfigService { src/main/java/com/yeshi/buwan/service/imp/UserService.java
@@ -186,6 +186,7 @@ lu.setOpenid(openid); lu.setPortrait(portrait); lu.setIpinfo(ipInfo); lu.setState(LoginUser.STATE_NORMAL); session.save(lu); session.flush(); session.getTransaction().commit(); src/main/java/com/yeshi/buwan/service/manager/IPManager.java
New file @@ -0,0 +1,19 @@ package com.yeshi.buwan.service.manager; import com.yeshi.buwan.util.IPUtil; import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Component; @Component public class IPManager { @Cacheable(value = "ipCache", key = "'getIPInfo-'+#ip") public IPUtil.IPInfo getIPInfo(String ip) throws Exception { IPUtil.IPInfo ipInfo = IPUtil.getIPInfo(ip); if (ipInfo == null) { throw new Exception("获取IP信息失败"); } return ipInfo; } } src/main/java/com/yeshi/buwan/util/IPUtil.java
@@ -261,42 +261,17 @@ return ip; } public static Map<String, String> getIPInfo(String ip) { Map<String, String> map = new HashMap<String, String>(); HttpClient client = new HttpClient(); GetMethod method = new GetMethod("http://ip.taobao.com/service/getIpInfo.php?ip=" + ip); try { client.executeMethod(method); String result = method.getResponseBodyAsString(); JSONObject object = JSONObject.fromObject(result); if (object.optInt("code") == 0) { map.put("city", object.optJSONObject("data").optString("city")); map.put("country", object.optJSONObject("data").optString("country")); return map; } } catch (HttpException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return map; } public static String getIPContry(String ip) { String res = new ShowApiRequest("http://route.showapi.com/2152-1", "49053", "983e020ce60042cd95f39b3ebd4b9563") .addTextPara("ip", ip) .post(); JSONObject json = JSONObject.fromObject(res); if (json.optInt("showapi_res_code") == 0) { JSONObject body = json.optJSONObject("showapi_res_body"); if (body != null) return body.optString("country"); } return ""; IPInfo ipInfo = getIPInfo(ip); return ipInfo == null ? "" : ipInfo.getCountry(); } public static String getIPProvince(String ip) { IPInfo ipInfo = getIPInfo(ip); return ipInfo == null ? "" : ipInfo.getProvince(); } public static IPInfo getIPInfo(String ip) { try { String res = new ShowApiRequest("http://route.showapi.com/2152-1", "49053", "983e020ce60042cd95f39b3ebd4b9563") .addTextPara("ip", ip) @@ -304,13 +279,54 @@ JSONObject json = JSONObject.fromObject(res); if (json.optInt("showapi_res_code") == 0) { JSONObject body = json.optJSONObject("showapi_res_body"); if (body != null) return body.optString("state"); if (body != null) { String country = body.optString("country"); String state = body.optString("state"); String city = body.optString("city"); return new IPInfo(country, state, city); } } } catch (Exception e) { } return ""; return null; } public static class IPInfo { private String province; private String city; private String country; public IPInfo(String country, String province, String city) { this.province = province; this.city = city; this.country = country; } public String getProvince() { return province; } public void setProvince(String province) { this.province = province; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getCountry() { return country; } public void setCountry(String country) { this.country = country; } } } src/main/java/com/yeshi/buwan/util/Utils.java
@@ -323,15 +323,15 @@ public static int getType(HttpServletRequest request, UserInfo user, String detailSystemId) { String ip = getIP(request); if (!(ip + "").equalsIgnoreCase(user.getIp())) { Map<String, String> map = IPUtil.getIPInfo(ip); IPUtil.IPInfo ipInfo = IPUtil.getIPInfo(ip); user.setIp(ip); if (!StringUtil.isNullOrEmpty(map.get("city"))) { user.setCity(map.get("city")); if (ipInfo != null && !StringUtil.isNullOrEmpty(ipInfo.getCity())) { user.setCity(ipInfo.getCity()); } if (!StringUtil.isNullOrEmpty(map.get("country"))) { user.setCountry(map.get("country")); if (ipInfo != null && !StringUtil.isNullOrEmpty(ipInfo.getCountry())) { user.setCountry(ipInfo.getCountry()); } new UserService().updateUserInfo(user); } src/main/resources/ehcache.xml
@@ -95,5 +95,10 @@ timeToLiveSeconds="1200" overflowToDisk="true" memoryStoreEvictionPolicy="LRU" /> <cache name="ipCache" maxElementsInMemory="1000" maxElementsOnDisk="10000" eternal="false" timeToIdleSeconds="1200" timeToLiveSeconds="1200" overflowToDisk="true" memoryStoreEvictionPolicy="LRU" /> </ehcache> src/test/java/com/hxh/spring/test/IPTest.java
@@ -7,7 +7,7 @@ @Test public void test1() { for (int i = 0; i < 10; i++) System.out.println(IPUtil.getIPProvince("113.249.192.217")); // for (int i = 0; i < 10; i++) System.out.println(IPUtil.getIPProvince("134.175.68.214")); } }