|
| 1 | +package cn.binarywang.wx.miniapp.api.impl; |
| 2 | + |
| 3 | +import cn.binarywang.wx.miniapp.api.WxMaAnalysisService; |
| 4 | +import cn.binarywang.wx.miniapp.api.WxMaService; |
| 5 | +import cn.binarywang.wx.miniapp.bean.analysis.WxMaRetainInfo; |
| 6 | +import cn.binarywang.wx.miniapp.bean.analysis.WxMaSummaryTrend; |
| 7 | +import cn.binarywang.wx.miniapp.bean.analysis.WxMaUserPortrait; |
| 8 | +import cn.binarywang.wx.miniapp.bean.analysis.WxMaVisitDistribution; |
| 9 | +import cn.binarywang.wx.miniapp.bean.analysis.WxMaVisitPage; |
| 10 | +import cn.binarywang.wx.miniapp.bean.analysis.WxMaVisitTrend; |
| 11 | +import cn.binarywang.wx.miniapp.util.json.WxMaGsonBuilder; |
| 12 | +import com.google.gson.JsonObject; |
| 13 | +import com.google.gson.JsonParser; |
| 14 | +import com.google.gson.reflect.TypeToken; |
| 15 | +import me.chanjar.weixin.common.exception.WxErrorException; |
| 16 | +import org.apache.commons.lang3.time.DateFormatUtils; |
| 17 | + |
| 18 | +import java.lang.reflect.Type; |
| 19 | +import java.util.Date; |
| 20 | +import java.util.List; |
| 21 | + |
| 22 | +/** |
| 23 | + * @author <a href="https://github.com/charmingoh">Charming</a> |
| 24 | + * @since 2018-04-28 |
| 25 | + */ |
| 26 | +public class WxMaAnalysisServiceImpl implements WxMaAnalysisService { |
| 27 | + private static final JsonParser JSON_PARSER = new JsonParser(); |
| 28 | + private WxMaService wxMaService; |
| 29 | + |
| 30 | + public WxMaAnalysisServiceImpl(WxMaService wxMaService) { |
| 31 | + this.wxMaService = wxMaService; |
| 32 | + } |
| 33 | + |
| 34 | + @Override |
| 35 | + public List<WxMaSummaryTrend> getDailySummaryTrend(Date beginDate, Date endDate) throws WxErrorException { |
| 36 | + return getAnalysisResultAsList(GET_DAILY_SUMMARY_TREND_URL, beginDate, endDate, |
| 37 | + new TypeToken<List<WxMaSummaryTrend>>() { |
| 38 | + }.getType()); |
| 39 | + } |
| 40 | + |
| 41 | + @Override |
| 42 | + public List<WxMaVisitTrend> getDailyVisitTrend(Date beginDate, Date endDate) throws WxErrorException { |
| 43 | + return getAnalysisResultAsList(GET_DAILY_VISIT_TREND_URL, beginDate, endDate, |
| 44 | + new TypeToken<List<WxMaVisitTrend>>() { |
| 45 | + }.getType()); |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public List<WxMaVisitTrend> getWeeklyVisitTrend(Date beginDate, Date endDate) throws WxErrorException { |
| 50 | + return getAnalysisResultAsList(GET_WEEKLY_VISIT_TREND_URL, beginDate, endDate, |
| 51 | + new TypeToken<List<WxMaVisitTrend>>() { |
| 52 | + }.getType()); |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public List<WxMaVisitTrend> getMonthlyVisitTrend(Date beginDate, Date endDate) throws WxErrorException { |
| 57 | + return getAnalysisResultAsList(GET_MONTHLY_VISIT_TREND_URL, beginDate, endDate, |
| 58 | + new TypeToken<List<WxMaVisitTrend>>() { |
| 59 | + }.getType()); |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public WxMaVisitDistribution getVisitDistribution(Date beginDate, Date endDate) throws WxErrorException { |
| 64 | + String responseContent = this.wxMaService.post(GET_VISIT_DISTRIBUTION_URL, toJson(beginDate, endDate)); |
| 65 | + return WxMaVisitDistribution.fromJson(responseContent); |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public WxMaRetainInfo getDailyRetainInfo(Date beginDate, Date endDate) throws WxErrorException { |
| 70 | + return getRetainInfo(beginDate, endDate, GET_DAILY_RETAIN_INFO_URL); |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public WxMaRetainInfo getWeeklyRetainInfo(Date beginDate, Date endDate) throws WxErrorException { |
| 75 | + return getRetainInfo(beginDate, endDate, GET_WEEKLY_RETAIN_INFO_URL); |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public WxMaRetainInfo getMonthlyRetainInfo(Date beginDate, Date endDate) throws WxErrorException { |
| 80 | + return getRetainInfo(beginDate, endDate, GET_MONTHLY_RETAIN_INFO_URL); |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public List<WxMaVisitPage> getVisitPage(Date beginDate, Date endDate) throws WxErrorException { |
| 85 | + return getAnalysisResultAsList(GET_VISIT_PAGE_URL, beginDate, endDate, |
| 86 | + new TypeToken<List<WxMaVisitPage>>() { |
| 87 | + }.getType()); |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public WxMaUserPortrait getUserPortrait(Date beginDate, Date endDate) throws WxErrorException { |
| 92 | + String responseContent = this.wxMaService.post(GET_USER_PORTRAIT_URL, toJson(beginDate, endDate)); |
| 93 | + return WxMaUserPortrait.fromJson(responseContent); |
| 94 | + } |
| 95 | + |
| 96 | + private WxMaRetainInfo getRetainInfo(Date beginDate, Date endDate, String url) throws WxErrorException { |
| 97 | + String responseContent = this.wxMaService.post(url, toJson(beginDate, endDate)); |
| 98 | + return WxMaRetainInfo.fromJson(responseContent); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * 获取数据分析结果并返回 List,returnType 类型 |
| 103 | + * |
| 104 | + * @param url 链接 |
| 105 | + * @param returnType 返回的类型 |
| 106 | + * @param <T> 返回的类型 |
| 107 | + * @return List 类型的数据 |
| 108 | + */ |
| 109 | + private <T> List<T> getAnalysisResultAsList(String url, Date beginDate, Date endDate, Type returnType) throws WxErrorException { |
| 110 | + String responseContent = this.wxMaService.post(url, toJson(beginDate, endDate)); |
| 111 | + JsonObject response = JSON_PARSER.parse(responseContent).getAsJsonObject(); |
| 112 | + boolean hasList = response.has("list"); |
| 113 | + if (hasList) { |
| 114 | + return WxMaGsonBuilder.create().fromJson(response.getAsJsonArray("list"), returnType); |
| 115 | + } else { |
| 116 | + return null; |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + private static String toJson(Date beginDate, Date endDate) { |
| 121 | + JsonObject param = new JsonObject(); |
| 122 | + param.addProperty("begin_date", DateFormatUtils.format(beginDate, "yyyyMMdd")); |
| 123 | + param.addProperty("end_date", DateFormatUtils.format(endDate, "yyyyMMdd")); |
| 124 | + return param.toString(); |
| 125 | + } |
| 126 | +} |
0 commit comments