Browse Source

微信接口访问,日趋量

Administrator 2 years ago
parent
commit
d2fe7352b6

+ 11 - 1
mhotel/src/com/happy/Until/DateUtil.java

@@ -23,8 +23,18 @@ public class DateUtil
     }
 
     public static String Time_Formatter_Day = "yyyy-MM-dd";
+    public static String Time_Formatter_yyyyMMdd = "yyyyMMdd";
     public static String Time_Formatter_Second = "yyyy-MM-dd HH:mm:ss";
 
+    // 获取昨天时间
+    public static String getYesturday(Date date, String format) {
+        Calendar nowTime2 = Calendar.getInstance();
+        nowTime2.setTime(date);
+        nowTime2.add(Calendar.DATE, -1);
+        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
+        return simpleDateFormat.format(nowTime2.getTime());
+    }
+
     public static String getCurrentYear() {
         SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
         Date date = new Date();
@@ -374,7 +384,7 @@ public class DateUtil
     }
 
     /**
-     * 转换日期
+     * 转换日期 yyyy/MM/dd
      * @param str 对象
      * @return boolean
      */

+ 54 - 0
mhotel/src/com/happy/Until/Func.java

@@ -1,5 +1,11 @@
 package com.happy.Until;
 
+import org.apache.commons.lang.StringUtils;
+
+import java.beans.BeanInfo;
+import java.beans.IntrospectionException;
+import java.beans.Introspector;
+import java.beans.PropertyDescriptor;
 import java.io.File;
 import java.lang.reflect.Field;
 import java.lang.reflect.Modifier;
@@ -35,6 +41,54 @@ public class Func {
 
     private static DecimalFormat df = new DecimalFormat("#.##########");
 
+
+    /**
+     * map转java对象
+     * @param clazz
+     * @param map
+     * @return
+     * @throws Exception
+     */
+    public static Object convertToObject(Class clazz, Map<String, Object> map)  {
+        if (clazz == null || map == null || map.isEmpty())
+            return null;
+
+        BeanInfo bi = null;
+        Object obj = null;
+        try {
+                bi = Introspector.getBeanInfo(clazz);
+
+                obj = clazz.newInstance();
+
+
+                PropertyDescriptor[] pds = bi.getPropertyDescriptors();
+
+                    String pName;
+                    for (PropertyDescriptor pd : pds) {
+                        pName = pd.getName();
+                        if (map.containsKey(pName)) {
+                            try {
+                                if (pd.getPropertyType().getName().equals("java.lang.Long")) {
+                                    if (StringUtils.isNotEmpty(map.get(pName).toString())) {
+                                        pd.getWriteMethod().invoke(obj, Long.parseLong(map.get(pName).toString()));
+                                    }
+                                } else {
+                                    pd.getWriteMethod().invoke(obj, map.get(pName));
+                                }
+                            } catch (Exception ex) {
+
+                            }
+                        }
+                    }
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+
+        return obj;
+
+    }
+
+
     /**
      * 加法运算(准确计算精度)
      *

+ 2 - 0
mhotel/src/com/happy/Until/TimeExchange.java

@@ -226,6 +226,8 @@ public class TimeExchange {
         return simpleDateFormat.format(nowTime2.getTime());
     }
 
+
+
     public static String getTomorrowTime() throws ParseException {
         Calendar nowTime2 = Calendar.getInstance();
         nowTime2.setTime(StringToDate(getTime()));

+ 6 - 0
mhotel/src/com/happy/common/http/HttpsClient.java

@@ -441,6 +441,12 @@ public class HttpsClient {
         return result;
     }
 
+    /**
+     * post请求
+     * @param request_url
+     * @param json
+     * @return
+     */
     public static String sendJson2(String request_url, JSONObject json) {
         OutputStreamWriter out = null;
         InputStream is = null;

+ 3 - 4
mhotel/src/com/happy/common/util/WechatUtil.java

@@ -6,6 +6,7 @@ import com.alibaba.fastjson.JSONObject;
 // vo实体类参数
 
 import com.happy.Model.weixin.CodeEntityVo;
+import com.happy.Model.weixin.WeiXinUtil;
 import org.bouncycastle.util.encoders.Base64;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.http.util.TextUtils;
@@ -26,8 +27,6 @@ import java.security.Security;
 import java.util.*;
 
 public class WechatUtil {
-    private static final String appId = "wx2fc3f45732fae5d3";
-    private static final String secret = "7eee4a49a4470a77f9222995e8511547";
 
     /**
      * 获取小程序code换取openid、session_key
@@ -37,8 +36,8 @@ public class WechatUtil {
      */
     public static JSONObject getOpenId(String code) {
 
-        String url = "https://api.weixin.qq.com/sns/jscode2session?appid=" + appId
-                + "&secret=" + secret + "&js_code=" + code + "&grant_type=authorization_code";
+        String url = "https://api.weixin.qq.com/sns/jscode2session?appid=" + WeiXinUtil.appid_c
+                + "&secret=" + WeiXinUtil.screct_c + "&js_code=" + code + "&grant_type=authorization_code";
         PrintWriter out = null;
         BufferedReader in = null;
         String line;

+ 0 - 1
mhotel/src/com/happy/common/wx/WxConfig.java

@@ -17,7 +17,6 @@ public class WxConfig {
      */
     public static String apicode = "CS0001";
 
-
     /**
      * 商户号
      */

+ 83 - 0
mhotel/src/com/happy/common/wx/WxDailyVisitInfo.java

@@ -0,0 +1,83 @@
+package com.happy.common.wx;
+
+/**
+ *  微信日访问量趋势
+ * @Date
+ * @Author xieli
+ **/
+public class WxDailyVisitInfo {
+
+    public String ref_date; // 日期,格式为 yyyymmdd
+    public int session_cnt; // 打开次数
+    public int visit_pv;  // 访问次数
+    public int visit_uv; //访问人数
+    public int visit_uv_new;// 新用户数
+    public double stay_time_uv;// 人均停留时长 (浮点型,单位:秒)
+    public double stay_time_session;// 次均停留时长 (浮点型,单位:秒)
+    public double visit_depth;// 平均访问深度 (浮点型)
+
+
+    public String getRef_date() {
+        return ref_date;
+    }
+
+    public void setRef_date(String ref_date) {
+        this.ref_date = ref_date;
+    }
+
+    public int getSession_cnt() {
+        return session_cnt;
+    }
+
+    public void setSession_cnt(int session_cnt) {
+        this.session_cnt = session_cnt;
+    }
+
+    public int getVisit_pv() {
+        return visit_pv;
+    }
+
+    public void setVisit_pv(int visit_pv) {
+        this.visit_pv = visit_pv;
+    }
+
+    public int getVisit_uv() {
+        return visit_uv;
+    }
+
+    public void setVisit_uv(int visit_uv) {
+        this.visit_uv = visit_uv;
+    }
+
+    public int getVisit_uv_new() {
+        return visit_uv_new;
+    }
+
+    public void setVisit_uv_new(int visit_uv_new) {
+        this.visit_uv_new = visit_uv_new;
+    }
+
+    public double getStay_time_uv() {
+        return stay_time_uv;
+    }
+
+    public void setStay_time_uv(double stay_time_uv) {
+        this.stay_time_uv = stay_time_uv;
+    }
+
+    public double getStay_time_session() {
+        return stay_time_session;
+    }
+
+    public void setStay_time_session(double stay_time_session) {
+        this.stay_time_session = stay_time_session;
+    }
+
+    public double getVisit_depth() {
+        return visit_depth;
+    }
+
+    public void setVisit_depth(double visit_depth) {
+        this.visit_depth = visit_depth;
+    }
+}

+ 77 - 0
mhotel/src/com/happy/common/wx/WxServerInterface.java

@@ -0,0 +1,77 @@
+package com.happy.common.wx;
+
+import com.alibaba.druid.support.json.JSONUtils;
+import com.alibaba.fastjson.JSONObject;
+import com.google.gson.Gson;
+import com.google.gson.reflect.TypeToken;
+import com.happy.Model.weixin.WeiXinUtil;
+import com.happy.Until.DateUtil;
+import com.happy.Until.Func;
+import com.happy.common.http.HttpsClient;
+
+import java.text.ParseException;
+import java.util.*;
+
+/**
+ * @Date
+ * @Author xieli
+ **/
+public class WxServerInterface {
+
+
+    /**
+     * 获取access_token
+     * @return
+     */
+    public static String getAccessToken()
+    {
+        JSONObject jsonObject = new JSONObject();
+        jsonObject.put("grant_type", "client_credential");
+        jsonObject.put("appid", WeiXinUtil.appid_c);
+        jsonObject.put("secret",WeiXinUtil.screct_c);
+        String msg3 = HttpsClient.sendJson2("https://api.weixin.qq.com/cgi-bin/stable_token",jsonObject);
+        System.out.println(msg3);
+        Map map = (Map) JSONUtils.parse(msg3);
+        return Func.parseStr(map.get("access_token"));
+    }
+
+
+    /**
+     * 昨天访问量
+     * @param dayTime 20230815 yyyymmdd 传入时间为当日时间
+     * @return 返回为昨日的访问量
+     */
+    public static WxDailyVisitInfo getDailyRetain(Date dayTime) {
+        if (dayTime == null)
+            throw new RuntimeException("未传入查询访问量时间");
+
+        String token = getAccessToken();
+        if (Func.checkNull(token))
+            return null;
+
+        String begin_date = DateUtil.getYesturday(dayTime,DateUtil.Time_Formatter_yyyyMMdd);
+        JSONObject message = new JSONObject();
+        message.put("access_token",token);
+        message.put("begin_date",begin_date);
+        message.put("end_date", begin_date);
+
+        String result = HttpsClient.sendJson("https://api.weixin.qq.com/datacube/getweanalysisappiddailyvisittrend?access_token="+token, message);
+        System.out.println(result);
+        Map map = (Map) JSONUtils.parse(result);
+        List arrayList = (ArrayList) map.get("list");
+        if (arrayList == null || arrayList.size() <= 0)
+            return null;
+
+        Map mapInfo = (Map) arrayList.get(0);
+        WxDailyVisitInfo info = (WxDailyVisitInfo) Func.convertToObject(WxDailyVisitInfo.class,mapInfo);
+        return info;
+    }
+
+    public static void main(String[] args) {
+
+        WxDailyVisitInfo info = getDailyRetain(new Date());
+        System.out.println(info);
+    }
+
+
+}