微信消息通知动态获取微信公众平台使用的消息模板id 等模板信息

小功能 : 动态获取微信消息模板。 把appid & secret 拿捏一下就可以使用了(getTempId()函数中)


public String getAccess_token(String appid, String appsecret) {
        String url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+appid+"&secret="+appsecret;
        JSONObject jsonObject = doGet1(url);
        System.out.println(jsonObject.toString());
        String errCode = jsonObject.getString("expires_in");
        if (!StringUtils.isEmpty(errCode)  && !StringUtils.isEmpty(jsonObject.getString("access_token").toString())) {
            String token = jsonObject.get("access_token").toString();
            return token;
        } else {
            return null;
        }
    }

public List<Map> getTempId(){
        WxUserQueryVo user = wxUserMapper.getWxUserById("用户在本平台的id(本平台用户表中本用户所对应的id号)");
        ///获取appid secret//
        String appid = user.getPlatform();//小程序appid
        BackgroundAppletQueryVo appidinfo = bm.SelectAppId(new BackgroundAppletPageParam().setAppId(appid)); //表中保存了scret appid 等信息 (这里可以通过传参或者什么方式获取appid 与 secret 就行)
        String secret = appidinfo.getScret();
        ///获取appid secret END//
        String acctoken=  getAccess_token(appid,secret);
         
       String url = "https://api.weixin.qq.com/wxaapi/newtmpl/gettemplate?access_token="+acctoken;
       HttpGet httpGet = new HttpGet(url);
       String sret = "";
       CloseableHttpClient get = HttpClients.createDefault();
       try{
           Map<String,String> retm = new HashMap<>();
           CloseableHttpResponse execute = get.execute(httpGet);
           if(execute.getStatusLine().getStatusCode()==200){
              HttpEntity httpEntity = execute.getEntity();
               sret +=EntityUtils.toString(httpEntity);
               Map<String,Object> m  = JSON.parseObject(sret);
               List<Map> ol = JSONArray.parseArray(m.get("data").toString(),Map.class);
               //retm.put(m.get("title").toString(),m.get("priTmplId").toString());
               //System.out.println(EntityUtils.toString(httpEntity));
               return ol;
           }
       }catch (Exception e){

       }
        return null;
    }
上一篇:使用python进行钉钉机器人的消息发送


下一篇:IdentityServer4实战:持久化 Client