From c927efe5490eb43b75d5f6d65631951e891c3f8e Mon Sep 17 00:00:00 2001 From: 935090232@qq.com <ak473600000> Date: Thu, 07 Jan 2021 23:42:07 +0800 Subject: [PATCH] 公众号模板消息demo --- zq-erp/src/main/resources/config/system.properties | 6 + 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 | 16 ---- zq-erp/src/main/java/com/matrix/system/wechart/templateMsg/MsgDemo.java | 39 +++++++++ 5 files changed, 183 insertions(+), 18 deletions(-) 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..02d0c65 --- /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()); + String errmsg=json.getString("errmsg"); + if(StringUtils.isNotBlank(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 index ab6742c..3ced9fb 100644 --- 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 @@ -71,22 +71,6 @@ - public static void main(String[] args) { - - GzhTemplateMessagePojo messagePojo=new GzhTemplateMessagePojo(); - messagePojo.setTouser("123"); - messagePojo.setTemplate_id("543"); - messagePojo.setUrl("231"); - messagePojo.setMiniprogram("65475234","/123/234123412"); - messagePojo.setFirst("1231","#453454"); - messagePojo.setKeyWord("1231","#453454"); - System.out.println(JSONObject.toJSON(messagePojo).toString()); - - - - - - } public void setMiniprogram(String appid,String url){ this.setMiniprogram(new Miniprogram(appid,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 index 6a2b858..f0757e4 100644 --- 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 @@ -1,5 +1,10 @@ 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.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; @@ -8,13 +13,43 @@ 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; - public static void main(String[] args) { + @Value("${gzh_secret}") + String gzhSecret; + + + @RequestMapping("/template") + @ResponseBody + public AjaxResult template() { + GzhTemplateMessagePojo messagePojo=new GzhTemplateMessagePojo(); + messagePojo.setTouser("123"); + messagePojo.setTemplate_id("543"); + messagePojo.setUrl("231"); + messagePojo.setMiniprogram("65475234","/123/234123412"); + messagePojo.setFirst("1231","#453454"); + messagePojo.setKeyWord("1231","#453454"); + System.out.println(JSONObject.toJSON(messagePojo).toString()); + String ACCESS_TOKEN=WeChatGzhApiTools.getAccessToken(gzhAppId,gzhSecret); + String url="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+ACCESS_TOKEN; + JSONObject result= HttpClientUtil.sendPostWithJson(url,JSONObject.toJSON(messagePojo).toString()); + System.out.println(result.toString()); + return AjaxResult.buildSuccessInstance("1"); + + } } diff --git a/zq-erp/src/main/resources/config/dev/system.properties b/zq-erp/src/main/resources/config/dev/system.properties index 78a6818..6baf382 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=123 + + + #微信支付调试开关 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..bcf3910 100644 --- a/zq-erp/src/main/resources/config/system.properties +++ b/zq-erp/src/main/resources/config/system.properties @@ -51,6 +51,12 @@ xcx_appid =wx3836ab3c1490ff29 xcx_secret =39a3687ec5b2666ed68e7c8b83b26b47 +#公众号 +gzh_appid=wx57e6335559bdbda6 +gzh_secret=123 + + + #微信支付调试开关 wx_pay_debug_onoff = false -- Gitblit v1.9.1