微信公众号
目录 一、登录微信公众平台二、开通模板消息三、增加及选择模板四、在项目中配置使用五、遇见的问题 一、登录微信公众平台
1、搜索微信公众平台 2、扫码登录后即可跳转
二、开通模板消息1、在左侧菜单栏找到模板消息 若找不到则点击新的功能 2、点击新的功能后,找到模板消息 若已开通则如下图,在已开通中可查看 开通需要费用以及验证,开通后如下图
三、增加及选择模板1、点击模板消息后点击从历史模板库中添加即可选择模板 2、添加后会有显示
四、在项目中配置使用后台代码如下,用到的字段根据自己的项目进行修改,各类id自行获取(AppID可在基本配置中查看)
//推送公众号模板消息String template_id = "";//模板idString sh = "";if(aa.equals("Y")){sh = "已审核";}else if(aa.equals("N")){sh = "审核未通过";}else{sh = "待审核";}try {//if(bb.equals("")){}String firstDATA="你好,你提交的审批已审核";String keyword1DATA=sh;String keyword2DATA=aud_ex5+aud_ex6+aud_ex8;String remarkDATA="请进入公众号查看";//跳转路径String url = "";Map mapParam = new HashMap();mapParam.put("firstDATA", firstDATA);mapParam.put("keyword1DATA", keyword1DATA);mapParam.put("keyword2DATA", keyword2DATA);mapParam.put("remarkDATA", remarkDATA);mapParam.put("url", url);mapParam.put("OPENID", "oyOK25vBHQKEsrodfCPDS2OiUrbQ");//微信用户openidString jsonParam = "{"touser":""+mapParam.get("OPENID")+"","template_id":""+template_id+"","+ ""url":""+mapParam.get("url")+"","+ ""data": {"first":{"value": ""+mapParam.get("firstDATA")+"","color":"#555e94""+"},"+ ""keyword1":{"value": ""+mapParam.get("keyword1DATA")+"","color":"#555e94""+"},"+ ""keyword2":{"value": ""+mapParam.get("keyword2DATA")+"","color":"#555e94""+"},"+ ""remark":{"value": ""+mapParam.get("remarkDATA")+"","color":"#555e94""+"}"+ "}}";//公众号模版消息接口请求地址String sendMsgUrl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=";//String String access_token = "";//静态缓存存储令牌2小时过期Cache c = CacheManager.getCacheInfo("access_token");System.out.print("令牌获取"+(c!=null));if(c!=null&&!c.isExpired()){access_token = c.getValue().toString();System.out.print("令牌获取"+access_token);}else{System.out.print("令牌过期重新获取");access_token = OAuthUtil.getNewsAccessToken("","");//前者为appId,后者为secret//设置过期时间c = new Cache("access_token",access_token,2*60*60*1000,false);CacheManager.putCache("access_token",c);}//System.out.print("uuuuuuuuuuuuu");sendMsgUrl = sendMsgUrl + access_token;//System.out.print("sendMsgUrl="+sendMsgUrl);Map headerMap = new HashMap();headerMap.put("Content-Type", "application/json");String result = HttpClientUtil.doHttpsPost(sendMsgUrl, headerMap, jsonParam, "utf-8");System.out.print("result="+result);} catch (Exception e) {System.out.println(e.getMessage());} 五、遇见的问题1、遇见了40001错误 官网提示为AppSecret错误或者AppSecret不属于这个公众号,请开发者确认AppSecret的正确性 我的解决方式为: 配置白名单 找到基本配置 查看ip白名单 将自己电脑的ip地址加上去即可