From e3ccb13fdd386d41ea2f721924861f6bb64e6708 Mon Sep 17 00:00:00 2001
From: Helius <wangdoubleone@gmail.com>
Date: Sat, 09 Jan 2021 14:46:09 +0800
Subject: [PATCH] Merge branch 'api' of https://gitee.com/jyyforjava/zq-erp into api

---
 zq-erp/src/main/resources/config/system.properties                                     |   10 
 zq-erp/src/main/resources/config/test/system.properties                                |    6 
 zq-erp/src/main/java/com/matrix/system/wechart/templateMsg/MsgDemo2.java               |   69 +++++++
 zq-erp/src/main/resources/config/dev/system.properties                                 |    6 
 zq-erp/src/main/java/com/matrix/system/shopXcx/api/WeChatGzhApiTools.java              |  134 ++++++++++++++
 zq-erp/src/main/java/com/matrix/system/wechart/templateMsg/GzhTemplateMessagePojo.java |  134 ++++++++++++++
 zq-erp/src/main/java/com/matrix/system/wechart/templateMsg/MsgDemo.java                |   77 ++++++++
 zq-erp/src/main/java/com/matrix/component/tools/HttpClientUtil.java                    |   55 ++++++
 zq-erp/src/main/java/com/matrix/system/wechart/templateMsg/UniformMsgPojo.java         |   32 +++
 9 files changed, 519 insertions(+), 4 deletions(-)

diff --git a/zq-erp/src/main/java/com/matrix/component/tools/HttpClientUtil.java b/zq-erp/src/main/java/com/matrix/component/tools/HttpClientUtil.java
new file mode 100644
index 0000000..cbc64d4
--- /dev/null
+++ b/zq-erp/src/main/java/com/matrix/component/tools/HttpClientUtil.java
@@ -0,0 +1,55 @@
+package com.matrix.component.tools;
+
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONObject;
+import com.matrix.core.exception.GlobleException;
+import com.matrix.core.tools.LogUtil;
+import org.apache.http.client.ResponseHandler;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.impl.client.BasicResponseHandler;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClients;
+
+import java.io.IOException;
+
+/**
+ * POST 请求工具
+ */
+public class HttpClientUtil {
+
+    public static JSONObject sendPostWithJson(String url, String json) {
+
+        CloseableHttpClient httpClient = HttpClients.createDefault();
+        ResponseHandler<String> responseHandler = new BasicResponseHandler();
+        try {
+            //第一步:创建HttpClient对象
+            httpClient = HttpClients.createDefault();
+
+            //第二步:创建httpPost对象
+            HttpPost httpPost = new HttpPost(url);
+
+            //第三步:给httpPost设置JSON格式的参数
+            StringEntity requestEntity = new StringEntity(json, "utf-8");
+            requestEntity.setContentEncoding("UTF-8");
+            httpPost.setHeader("Content-type", "application/json");
+            httpPost.setEntity(requestEntity);
+            //第四步:发送HttpPost请求,获取返回值
+            String returnValue = httpClient.execute(httpPost, responseHandler); //调接口获取返回值时,必须用此方法
+            return JSON.parseObject(returnValue);
+        } catch (Exception e) {
+            e.printStackTrace();
+        } finally {
+            try {
+                httpClient.close();
+            } catch (IOException e) {
+                LogUtil.error("http请求发送失败", e);
+                throw new GlobleException(e.getMessage());
+            }
+        }
+        //第五步:处理返回值
+        return null;
+    }
+
+
+}
diff --git a/zq-erp/src/main/java/com/matrix/system/shopXcx/api/WeChatGzhApiTools.java b/zq-erp/src/main/java/com/matrix/system/shopXcx/api/WeChatGzhApiTools.java
new file mode 100644
index 0000000..7c74df6
--- /dev/null
+++ b/zq-erp/src/main/java/com/matrix/system/shopXcx/api/WeChatGzhApiTools.java
@@ -0,0 +1,134 @@
+package com.matrix.system.shopXcx.api;
+
+import com.matrix.component.tools.HttpRequest;
+import com.matrix.component.tools.HttpResponse;
+import com.matrix.core.exception.GlobleException;
+import com.matrix.core.tools.LogUtil;
+import com.matrix.core.tools.PropertiesUtil;
+import com.matrix.core.tools.StringUtils;
+import net.sf.json.JSONObject;
+
+import java.io.IOException;
+
+/***
+ * 公众号api工具
+ */
+public class WeChatGzhApiTools {
+	/**
+	 * 公众号秘钥
+	 */
+	private static final String GZH_SECRET = "gzh_secret";
+	/**
+	 * 公众号appid
+	 */
+	private static final String GZH_APPID = "gzh_appid";
+	/**
+	 * 微信登录url
+	 */
+	private static final String WECHAT_LOGIN_URL = "wechar_login_url";
+
+	/**
+	 * 上一次获取时间
+	 */
+	private static Long preTime;
+
+	/**
+	 * 当前有效的微信token
+	 */
+	private static String accessToken = "";
+
+	private static String appid = "";
+	private static String secret = "";
+
+
+
+
+	/**
+	 * 获取公众号APPId
+	 * @return
+	 */
+	public static String  getAppid(){
+		if(StringUtils.isBlank(appid)){
+			appid = PropertiesUtil.getString(GZH_APPID);
+		}
+		return  appid;
+	}
+
+	/**
+	 * 获取公众号秘钥
+	 * @return
+	 */
+	public static String  getSecret(){
+		if(StringUtils.isBlank(secret)){
+			secret = PropertiesUtil.getString(GZH_SECRET);
+		}
+		return  secret;
+	}
+
+
+
+	/**
+	 * 清空token
+	 */
+	public static void cleanAccessToken() {
+		preTime = null;
+		accessToken = "";
+	}
+
+	public static String getAccessToken(String gzhAppId, String gzhSecret) {
+
+		if (isTokenInvalid()) {
+			synchronized (accessToken) {
+				if (isTokenInvalid()) {
+					LogUtil.info("刷新微信accessToken");
+					HttpRequest reqObj = new HttpRequest();
+					HttpResponse result = null;
+					try {
+						result = reqObj
+								.sendHttpGet("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="
+										+ gzhAppId+ "&secret=" + gzhSecret, null);
+					} catch (IOException e) {
+						LogUtil.error("获取公众号accessToken失败"+e.getMessage(),e);
+						throw new GlobleException(e.getMessage());
+					}
+					JSONObject json = JSONObject.fromObject(result.getDataString());
+
+					if(json.has("errmsg")){
+						throw new GlobleException("获取返回刷新的accessToken失败"+json.toString());
+					}else{
+						String access_token = json.getString("access_token");
+						accessToken = access_token;
+						preTime = System.currentTimeMillis();
+						LogUtil.info("返回刷新的accessToken={}", accessToken);
+						return access_token;
+					}
+
+				} else {
+					return accessToken;
+				}
+			}
+		} else {
+			LogUtil.info("返回现有accessToken={}", accessToken);
+			return accessToken;
+		}
+	}
+
+	/**
+	 * token是否无效
+	 *  TODO 由于现在有多个环境,为了防止冲突每次都获取新的token
+	 * @return
+	 */
+	private static boolean isTokenInvalid() {
+		/**		if (StringUtils.isNotBlank(accessToken) && preTime != null) {
+		 Long now = System.currentTimeMillis() / 1000;
+		 Long pre = preTime / 1000;
+		 boolean invalid = (now - pre) > 6200;
+		 return invalid;
+		 } else {
+		 return true;
+		 }*/
+		return true;
+	}
+
+
+}
diff --git a/zq-erp/src/main/java/com/matrix/system/wechart/templateMsg/GzhTemplateMessagePojo.java b/zq-erp/src/main/java/com/matrix/system/wechart/templateMsg/GzhTemplateMessagePojo.java
new file mode 100644
index 0000000..d6c6517
--- /dev/null
+++ b/zq-erp/src/main/java/com/matrix/system/wechart/templateMsg/GzhTemplateMessagePojo.java
@@ -0,0 +1,134 @@
+package com.matrix.system.wechart.templateMsg;
+
+import com.alibaba.fastjson.JSONObject;
+import com.aliyuncs.utils.HttpsUtils;
+import org.apache.commons.collections.map.HashedMap;
+
+import java.util.Map;
+
+/**
+ * 公众号模板消息对象
+ */
+public class GzhTemplateMessagePojo {
+
+    private  String appid;
+    private  String touser;
+    private  String template_id;
+    private  String url;
+    private  Miniprogram miniprogram;
+    public static class Miniprogram{
+        private  String appid;
+        private  String pagepath;
+
+        public Miniprogram(String appid, String pagepath) {
+            this.appid = appid;
+            this.pagepath = pagepath;
+        }
+
+        public String getAppid() {
+            return appid;
+        }
+
+        public void setAppid(String appid) {
+            this.appid = appid;
+        }
+
+        public String getPagepath() {
+            return pagepath;
+        }
+
+        public void setPagepath(String pagepath) {
+            this.pagepath = pagepath;
+        }
+    }
+    public Map<String ,Item> data=new HashedMap();
+    public static class Item{
+        private String value;
+        private String color;
+
+        public Item(String value, String color) {
+            this.value = value;
+            this.color = color;
+        }
+
+        public String getValue() {
+            return value;
+        }
+
+        public void setValue(String value) {
+            this.value = value;
+        }
+
+        public String getColor() {
+            return color;
+        }
+
+        public void setColor(String color) {
+            this.color = color;
+        }
+    }
+
+
+    public String getAppid() {
+        return appid;
+    }
+
+    public void setAppid(String appid) {
+        this.appid = appid;
+    }
+
+    public void setMiniprogram(String appid, String url){
+        this.setMiniprogram(new Miniprogram(appid,url));
+    }
+    public void setFirst(String value, String color){
+        this.data.put("first",new Item(value,color));
+    }
+
+    public void setKeyWord(String value, String color){
+        this.data.put("keyword"+(this.data.size()),new Item(value,color));
+    }
+    public void setRemark(String value, String color){
+        this.data.put("remark",new Item(value,color));
+    }
+
+    public Miniprogram getMiniprogram() {
+        return miniprogram;
+    }
+
+    public void setMiniprogram(Miniprogram miniprogram) {
+        this.miniprogram = miniprogram;
+    }
+
+    public Map<String, Item> getData() {
+        return data;
+    }
+
+    public void setData(Map<String, Item> data) {
+        this.data = data;
+    }
+
+    public String getTouser() {
+        return touser;
+    }
+
+    public void setTouser(String touser) {
+        this.touser = touser;
+    }
+
+    public String getTemplate_id() {
+        return template_id;
+    }
+
+    public void setTemplate_id(String template_id) {
+        this.template_id = template_id;
+    }
+
+    public String getUrl() {
+        return url;
+    }
+
+    public void setUrl(String url) {
+        this.url = url;
+    }
+
+}
diff --git a/zq-erp/src/main/java/com/matrix/system/wechart/templateMsg/MsgDemo.java b/zq-erp/src/main/java/com/matrix/system/wechart/templateMsg/MsgDemo.java
new file mode 100644
index 0000000..e5c650d
--- /dev/null
+++ b/zq-erp/src/main/java/com/matrix/system/wechart/templateMsg/MsgDemo.java
@@ -0,0 +1,77 @@
+package com.matrix.system.wechart.templateMsg;
+
+import com.alibaba.fastjson.JSONObject;
+import com.matrix.component.tools.HttpClientUtil;
+import com.matrix.core.pojo.AjaxResult;
+import com.matrix.system.hive.plugin.util.HttpUtils;
+import com.matrix.system.shopXcx.api.WeChatGzhApiTools;
+import com.matrix.system.shopXcx.bean.ShopAdvertisType;
+import org.apache.commons.httpclient.HttpClient;
+import org.apache.commons.httpclient.HttpException;
+import org.apache.commons.httpclient.NameValuePair;
+import org.apache.commons.httpclient.methods.PostMethod;
+import org.dom4j.Document;
+import org.dom4j.DocumentException;
+import org.dom4j.DocumentHelper;
+import org.dom4j.Element;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Controller;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.ResponseBody;
+
+import javax.swing.text.html.HTML;
+import java.io.IOException;
+@Controller
+@RequestMapping(value = "test")
+public class MsgDemo {
+
+    @Value("${gzh_appid}")
+    String gzhAppId;
+
+    @Value("${gzh_secret}")
+    String gzhSecret;
+
+
+    @RequestMapping("/template")
+    @ResponseBody
+    public AjaxResult template() {
+        GzhTemplateMessagePojo messagePojo=new GzhTemplateMessagePojo();
+        messagePojo.setTouser("o8EB656NQYwhUuKPMhVVwP_wtLJI");
+        messagePojo.setTemplate_id("Mqu9xPYj_JFhXNj7nLJS7LESQUy6Z7FoCOmVO66Oxe8");
+        //messagePojo.setUrl("www.baidu.com");
+       // messagePojo.setMiniprogram(gzhAppId,"/123/234123412");
+        messagePojo.setFirst("尊敬的谭娅:","#453454");
+        messagePojo.setKeyWord("活细胞肩部护理","#453454");
+        messagePojo.setKeyWord("2014年7月21日 18:36","#453454");
+        messagePojo.setRemark("您的护理课程剩余3次。","#453454");
+
+        String ACCESS_TOKEN=WeChatGzhApiTools.getAccessToken(gzhAppId,gzhSecret);
+        String url="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+ACCESS_TOKEN;
+
+        System.out.println(JSONObject.toJSON(messagePojo).toString());
+        JSONObject result= HttpClientUtil.sendPostWithJson(url,JSONObject.toJSON(messagePojo).toString());
+        System.out.println(result.toString());
+        return AjaxResult.buildSuccessInstance("1");
+
+
+    }
+
+
+    @RequestMapping("/getUserList")
+    @ResponseBody
+    public AjaxResult getUserList() {
+
+        String ACCESS_TOKEN=WeChatGzhApiTools.getAccessToken(gzhAppId,gzhSecret);
+
+        String url="https://api.weixin.qq.com/cgi-bin/user/get?access_token=ACCESS_TOKEN&next_openid=NEXT_OPENID".replaceAll("ACCESS_TOKEN",ACCESS_TOKEN);
+
+
+        String result= HttpUtils.sendGet(url,"");
+        System.out.println(result.toString());
+        return AjaxResult.buildSuccessInstance("1");
+
+
+    }
+
+}
diff --git a/zq-erp/src/main/java/com/matrix/system/wechart/templateMsg/MsgDemo2.java b/zq-erp/src/main/java/com/matrix/system/wechart/templateMsg/MsgDemo2.java
new file mode 100644
index 0000000..b50626a
--- /dev/null
+++ b/zq-erp/src/main/java/com/matrix/system/wechart/templateMsg/MsgDemo2.java
@@ -0,0 +1,69 @@
+package com.matrix.system.wechart.templateMsg;
+
+import com.alibaba.fastjson.JSONObject;
+import com.matrix.component.tools.HttpClientUtil;
+import com.matrix.core.pojo.AjaxResult;
+import com.matrix.system.hive.plugin.util.HttpUtils;
+import com.matrix.system.shopXcx.api.WeChatGzhApiTools;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Controller;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.ResponseBody;
+
+@Controller
+@RequestMapping(value = "test2")
+public class MsgDemo2 {
+
+
+    String xcxAppId="wx5cc58f796224af61";
+
+
+    String xcxSecret="facea088aae414e5c2ee86b459887721";
+
+
+    @RequestMapping("/template")
+    @ResponseBody
+    public AjaxResult template() {
+        UniformMsgPojo uniformMsgPojo=new UniformMsgPojo();
+        GzhTemplateMessagePojo messagePojo=new GzhTemplateMessagePojo();
+        uniformMsgPojo.setTouser("oJkRK4yelehsY4S7I6Ee1ydWtQMI");
+        messagePojo.setTemplate_id("Mqu9xPYj_JFhXNj7nLJS7LESQUy6Z7FoCOmVO66Oxe8");
+        //messagePojo.setUrl("www.baidu.com");
+       // messagePojo.setMiniprogram(gzhAppId,"/123/234123412");
+        messagePojo.setAppid("wx57e6335559bdbda6");
+        messagePojo.setFirst("尊敬的谭娅:","#453454");
+        messagePojo.setKeyWord("活细胞肩部护理","#453454");
+        messagePojo.setKeyWord("2014年7月21日 18:36","#453454");
+        messagePojo.setRemark("您的护理课程剩余3次。","#453454");
+
+        String ACCESS_TOKEN=WeChatGzhApiTools.getAccessToken(xcxAppId,xcxSecret);
+        String url="https://api.weixin.qq.com/cgi-bin/message/wxopen/template/uniform_send?access_token=ACCESS_TOKEN".replaceAll("ACCESS_TOKEN",ACCESS_TOKEN);
+
+        uniformMsgPojo.setMp_template_msg(messagePojo);
+
+        System.out.println(JSONObject.toJSON(uniformMsgPojo).toString());
+        JSONObject result= HttpClientUtil.sendPostWithJson(url,JSONObject.toJSON(uniformMsgPojo).toString());
+        System.out.println(result.toString());
+        return AjaxResult.buildSuccessInstance("1");
+
+
+    }
+
+
+    @RequestMapping("/getUserList")
+    @ResponseBody
+    public AjaxResult getUserList() {
+
+        String ACCESS_TOKEN=WeChatGzhApiTools.getAccessToken(xcxAppId,xcxSecret);
+
+        String url="https://api.weixin.qq.com/cgi-bin/user/get?access_token=ACCESS_TOKEN&next_openid=NEXT_OPENID".replaceAll("ACCESS_TOKEN",ACCESS_TOKEN);
+
+
+        String result= HttpUtils.sendGet(url,"");
+        System.out.println(result.toString());
+        return AjaxResult.buildSuccessInstance("1");
+
+
+    }
+
+}
diff --git a/zq-erp/src/main/java/com/matrix/system/wechart/templateMsg/UniformMsgPojo.java b/zq-erp/src/main/java/com/matrix/system/wechart/templateMsg/UniformMsgPojo.java
new file mode 100644
index 0000000..63678fc
--- /dev/null
+++ b/zq-erp/src/main/java/com/matrix/system/wechart/templateMsg/UniformMsgPojo.java
@@ -0,0 +1,32 @@
+package com.matrix.system.wechart.templateMsg;
+
+public class UniformMsgPojo {
+
+    private  String touser;
+    private GzhTemplateMessagePojo mp_template_msg;
+    private String weapp_template_msg;
+
+    public String getTouser() {
+        return touser;
+    }
+
+    public void setTouser(String touser) {
+        this.touser = touser;
+    }
+
+    public GzhTemplateMessagePojo getMp_template_msg() {
+        return mp_template_msg;
+    }
+
+    public void setMp_template_msg(GzhTemplateMessagePojo mp_template_msg) {
+        this.mp_template_msg = mp_template_msg;
+    }
+
+    public String getWeapp_template_msg() {
+        return weapp_template_msg;
+    }
+
+    public void setWeapp_template_msg(String weapp_template_msg) {
+        this.weapp_template_msg = weapp_template_msg;
+    }
+}
diff --git a/zq-erp/src/main/resources/config/dev/system.properties b/zq-erp/src/main/resources/config/dev/system.properties
index 78a6818..015c690 100644
--- a/zq-erp/src/main/resources/config/dev/system.properties
+++ b/zq-erp/src/main/resources/config/dev/system.properties
@@ -58,6 +58,12 @@
 xcx_appid =wx3836ab3c1490ff29
 xcx_secret =39a3687ec5b2666ed68e7c8b83b26b47
 
+#公众号
+gzh_appid=wx57e6335559bdbda6
+gzh_secret=ecb408af170e3890e6544290cad33760
+
+
+
 #微信支付调试开关
 wx_pay_debug_onoff = false
 
diff --git a/zq-erp/src/main/resources/config/system.properties b/zq-erp/src/main/resources/config/system.properties
index 9bf9768..8a17d20 100644
--- a/zq-erp/src/main/resources/config/system.properties
+++ b/zq-erp/src/main/resources/config/system.properties
@@ -48,8 +48,14 @@
 
 
 wechar_login_url =https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code
-xcx_appid =wx3836ab3c1490ff29
-xcx_secret =39a3687ec5b2666ed68e7c8b83b26b47
+xcx_appid =wx5cc58f796224af61
+xcx_secret =facea088aae414e5c2ee86b459887721
+
+#公众号
+gzh_appid=wx57e6335559bdbda6
+gzh_secret=ecb408af170e3890e6544290cad33760
+
+
 
 #微信支付调试开关
 wx_pay_debug_onoff = false
diff --git a/zq-erp/src/main/resources/config/test/system.properties b/zq-erp/src/main/resources/config/test/system.properties
index 0bf1388..a6ae4c0 100644
--- a/zq-erp/src/main/resources/config/test/system.properties
+++ b/zq-erp/src/main/resources/config/test/system.properties
@@ -46,8 +46,10 @@
 
 
 wechar_login_url =https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code
-xcx_appid =wx3836ab3c1490ff29
-xcx_secret =39a3687ec5b2666ed68e7c8b83b26b47
+xcx_appid =wx5cc58f796224af61
+xcx_secret =facea088aae414e5c2ee86b459887721
+gzh_appid=wx57e6335559bdbda6
+gzh_secret=ecb408af170e3890e6544290cad33760
 
 #微信支付调试开关
 wx_pay_debug_onoff = false

--
Gitblit v1.9.1