小程序spring boot发送订阅消息

猿份哥 27天前 ⋅ 87 阅读

1.订阅消息实体类

@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class SingleSubscribeMessage implements Serializable {

    private String touser;
    private String template_id;
    private String page;
    private String miniprogram_state;

    private Map<String,SubscribeTemplateData> data;

}

2.订阅消息data类

@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class SubscribeTemplateData implements Serializable {
    private String value;

}

3.发送订阅消息

 @Override
    public void send(){

        Map<String, SubscribeTemplateData> data = new HashMap<>();
        data.put("number1",new SubscribeTemplateData("111111"));
        data.put("amount3",new SubscribeTemplateData("500元"));
        data.put("name2",new SubscribeTemplateData("草莓"));
        data.put("thing4",new SubscribeTemplateData("xxx水果基地"));

        SingleSubscribeMessage message =new SingleSubscribeMessage();
        message.setPage("pages/index");
        message.setMiniprogram_state("developer");
        message.setTouser("用户openid");
        message.setTemplate_id("订阅消息模板id");
        message.setData(data);

        String url = " https://api.weixin.qq.com/cgi-bin/message/subscribe/send" ;
        String accessToken=accessTokenService.getAppletAccessToken();
        url = url+"?access_token="+accessToken;

        ResponseEntity<String> responseEntity = template.postForEntity(url, singleSubscribeMessage, String.class);

        String s = responseEntity.getBody();
        //解析微信返回数据
        JSONObject json = JSONObject.parseObject(s);
        long errcode = json.getLong("errcode");
            
 }

全部评论: 0

    我有话说: