| | |
| | | @Slf4j |
| | | public class WeChatSubscribeMessageSender { |
| | | private static final String ACCESS_TOKEN_HOST = "https://api.weixin.qq.com/cgi-bin/token"; |
| | | private static final String STABLE_ACCESS_TOKEN_HOST = "https://api.weixin.qq.com/cgi-bin/stable_token"; |
| | | private static final String API_URL = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send"; |
| | | |
| | | private static final String WX_APPID = "wxb7f0ea286fc4e535"; |
| | |
| | | params.put("touser", touser); |
| | | // 构建订阅消息内容的JSON对象 |
| | | JSONObject messageData = new JSONObject(); |
| | | messageData.put("thing1", createDataItem("直播主题", title)); |
| | | messageData.put("thing4", createDataItem("直播间名称", shopName)); |
| | | messageData.put("time5", createDataItem("直播时间", DateUtil.format(aliveStartAt, "MM-dd HH:mm:ss"))); |
| | | params.put("data", messageData.toJSONString()); |
| | | messageData.put("thing4", createDataItem("直播间名称", shopName)); |
| | | messageData.put("thing1", createDataItem("直播主题", title)); |
| | | params.put("data", messageData); |
| | | params.put("miniprogram_state", "trial"); |
| | | params.put("lang", "zh_CN"); |
| | | try { |
| | | log.info("发送消息参数:{}", JSONObject.toJSONString(params)); |
| | | String post = HttpUtil.post(url, JSONObject.toJSONString(params)); |
| | | log.info("发送消息返回结果:{}", post); |
| | | } catch (Exception e) { |
| | |
| | | * 获取access_token |
| | | * @return |
| | | */ |
| | | public static String getAccessTokenByWX() { |
| | | String host = ACCESS_TOKEN_HOST + "?appid=" + WX_APPID + "&secret=" + WX_SECRET + "&grant_type=client_credential"; |
| | | log.info("host:{}", host); |
| | | return HttpUtil.get(host); |
| | | private static String getAccessTokenByWX() { |
| | | Map<String, Object> params = new HashMap<>(); |
| | | params.put("appid", WX_APPID); |
| | | params.put("secret", WX_SECRET); |
| | | params.put("grant_type", "client_credential"); |
| | | String token = HttpUtil.post(STABLE_ACCESS_TOKEN_HOST,JSONObject.toJSONString(params)); |
| | | log.info("token:{}", token); |
| | | JSONObject jsonObject = JSONObject.parseObject(token); |
| | | return jsonObject.getString("access_token"); |
| | | } |
| | | private static Map<String, Object> createDataItem(String name, String value) { |
| | | Map<String, Object> item = new HashMap<>(); |