微信公眾號開發(fā)-模板信息推送

微信公眾號開發(fā)-模板信息推送

  • image.png

我們是微信小程序用戶在系統(tǒng)中進(jìn)行用戶綁定,但是需要推送信息到微信公眾中

發(fā)送流程

  • 1.獲取access_token
  • 2.獲取所有的用戶openid
  • 3.獲取所有的用戶的用戶信息
  • 4.獲取用戶的unionid
  • 5.綁定用戶微信公眾號openid,unionid,微信小程序openid
  • 6.通過用戶微信小程序的openid找到用戶微信公眾號的openid
  • 7.拿著微信公眾號openid進(jìn)行模板推送

以下只演示部分代碼

  • 獲取access_token
 /**
     * 獲取公眾號token
     * @param appId
     * @param secret
     * @return
     */
    public String getAccessToken(String appId,String secret){
        String url = "https://api.weixin.qq.com/cgi-bin/token";
        Map<String, String> dataMap = new HashMap<>();
        dataMap.put("grant_type", "client_credential");
        dataMap.put("appid", appId);
        dataMap.put("secret", secret);
        WxGetTokenVo wxGetTokenVo = JSONObject.parseObject(HttpClientUtils.get(url, dataMap), WxGetTokenVo.class);
        return wxGetTokenVo.getAccess_token();
    }
  • 獲取所有的用戶openid
 /**
     * 獲取全部的openID
     * @param acessToken
     * @param nextOpenId
     * @param wxGetOpenIdVos
     * @return
     */
    public void getAllOpenId(String acessToken,String nextOpenId,List<WxGetOpenIdVo> wxGetOpenIdVos){
        String url = "https://api.weixin.qq.com/cgi-bin/user/get";
        Map<String, String> dataMap = new HashMap<>(2);
        dataMap.put("access_token", acessToken);
        if(StringUtils.isNotBlank(nextOpenId)){
            dataMap.put("next_openid", nextOpenId);
        }
        String openidUsersStr = HttpClientUtils.get(url,dataMap);
        WxGetOpenIdVo wxGetOpenIdVo = JSONObject.parseObject(openidUsersStr, WxGetOpenIdVo.class);
        if(wxGetOpenIdVo != null){
            if(wxGetOpenIdVo.getData() != null) {
                wxGetOpenIdVos.add(wxGetOpenIdVo);
            }
            if(StringUtils.isNotBlank(wxGetOpenIdVo.getNext_openid())){
                getAllOpenId(acessToken, wxGetOpenIdVo.getNext_openid(), wxGetOpenIdVos);
            }
        }
    }
  • 獲取用戶的用戶數(shù)據(jù)
 /**
     * 獲取用戶詳情
     * @param acessToken
     * @param openId
     * @param wxGetUserInfoVos
     */
    public void getUserInfo(String acessToken,String openId,List<WxGetUserInfoVo> wxGetUserInfoVos){
        String url = "https://api.weixin.qq.com/cgi-bin/user/info";
        Map<String, String> dataMap = new HashMap<>();
        dataMap.put("access_token", acessToken);
        dataMap.put("openid", openId);
        dataMap.put("lang", "zh_CN");
        WxGetUserInfoVo wxGetUserInfoVo = JSONObject.parseObject(HttpClientUtils.get(url, dataMap), WxGetUserInfoVo.class);
        wxGetUserInfoVos.add(wxGetUserInfoVo);
    }
  • 發(fā)送模板信息
public static void send(String token){
        String url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + token;
//TemplateMessage 需要封裝 測試代碼沒有進(jìn)行封裝       
        TemplateMessage templateMessage=new TemplateMessage();
        AppletVo appletVo = new AppletVo();//跳轉(zhuǎn)小程序使用
        appletVo.setAppid("****");  
        appletVo.setPagepath("/pages/index/index");
        templateMessage.setMiniprogram(appletVo);
       // templateMessage.setUrl("www.baidu.com");
        templateMessage.setTouser("********");
        templateMessage.setTemplate_id("3IrFxsPYYlutAYqru1ZPtGHCMceMfojGURmVkaWLOZg");
        //設(shè)置模板標(biāo)題
        Content first=new Content();
        first.setValue("感謝您進(jìn)行實(shí)名認(rèn)證!");
        first.setColor("");
        //設(shè)置模板內(nèi)容
        Content keyword1=new Content();
        keyword1.setValue("認(rèn)證失敗");
        keyword1.setColor("#FF0000");
        //設(shè)置模板位置
        Content keyword2=new Content();
        keyword2.setValue("2019-08-26");
        keyword2.setColor("");
        //設(shè)置設(shè)備
        //設(shè)置跳轉(zhuǎn)內(nèi)容
        Content remark=new Content();
        remark.setValue("請?zhí)峤徽鎸?shí)的信息,重新提交,謝謝!");
        remark.setColor("#FF0000");
        //創(chuàng)建模板信息數(shù)據(jù)對象
        Data data=new Data();
        data.setFirst(first);
        data.setKeyword1(keyword1);
        data.setKeyword2(keyword2);
        data.setRemark(remark);
        templateMessage.setData(data);
        //將封裝的數(shù)據(jù)轉(zhuǎn)成JSON
        String jsonString = JSON.toJSONString(templateMessage);
        System.out.println("templateMessage = "+templateMessage);
        JSONObject post = doPost(url, JSONObject.parseObject(jsonString));
        System.out.println(post);


    }
  • main方法
 public static void main(String[] args) {
         WeChatUtil weChatUtil = new WeChatUtil();
        String accessToken = weChatUtil.getAccessToken(WXAPPID, KEY);
        //send(accessToken);
        List<WxGetOpenIdVo> wxGetOpenIdVos = new ArrayList<>();
        weChatUtil.getAllOpenId(accessToken, null, wxGetOpenIdVos);
        List<WxGetUserInfoVo> dataUserInfo = new ArrayList<>();
        wxGetOpenIdVos.forEach(wxGetOpenIdVo ->{
            wxGetOpenIdVo.getData().getOpenid().forEach(openid ->
                    weChatUtil.getUserInfo(accessToken, openid, dataUserInfo)
            );
         }
        );
        dataUserInfo.forEach(dataUser->
                System.out.println(dataUser.getOpenid() + "---" +dataUser.getUnionid())
        ); 
    }
image.png

如果需要獲取到微信用的unionid需要在微信開放平臺綁定公眾號,否則不返回該字段。部分封裝代碼沒有貼在代碼中,自行封裝。

示例代碼 沒有整理

[https://www.lanzous.com/i5t9bcb](https://www.lanzous.com/i5t9bcb)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容