微信小程序向公众号推送消息超详细教程

时间:2025-03-19 20:25:14
package com.hk.frame.controller.push; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import lombok.extern.slf4j.Slf4j; import org.apache.commons.collections.MapUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; import org.springframework.web.client.RestTemplate; import java.util.HashMap; import java.util.Map; /** * @Description: 小程序推送公众号消息测试 */ @Slf4j @Controller public class MsgPushTest { /** * 获取token * @return */ public String getAccessToken() { RestTemplate restTemplate = new RestTemplate(); Map<String, String> params = new HashMap<>(); params.put("APPID", WxAppletConfig.APPLET_APPID); params.put("APPSECRET", WxAppletConfig.APPLET_SECRET); params.put("grant_type", "client_credential"); String tokenUrl="/cgi-bin/token?appid={APPID}&secret={APPSECRET}&grant_type={grant_type}"; ResponseEntity<String> responseEntity = restTemplate.getForEntity(tokenUrl, String.class, params); String body = responseEntity.getBody(); JSONObject object = JSON.parseObject(body); String access_Token = object.getString("access_token"); System.err.println("access_Token:"+access_Token); return access_Token; } /** * 推送 * @param content * @return */ @ResponseBody @RequestMapping(value = "/testPush", method = RequestMethod.POST,produces = "application/json;charset=UTF-8") public String push(@RequestBody Map<String, String> content) { //获取需要推送的用户openid String openid= content.get("openid"); //获取用户token String token = getAccessToken(); String resultStatus = "0";//0:失败,1:成功 try { //小程序统一消息推送 String path = "/cgi-bin/message/wxopen/template/uniform_send?access_token=" + token; //封装参数 JSONObject jsonData = new JSONObject(); //小程序用户的openid jsonData.put("touser", openid); JSONObject jsonObject = new JSONObject(); //公众号APPID jsonObject.put("appid", WxAppletConfig.OFFICIAL_ACCOUNT_APPID); //公众号模板ID jsonObject.put("template_id", WxAppletConfig.OFFICIAL_ACCOUNT_TEMPLATE_ID); //公众号模板消息所要跳转的url jsonObject.put("url", "/qq_46122292/article/details/124961251"); //公众号模板消息所要跳转的小程序,小程序的必须与公众号具有绑定关系 JSONObject miniprogram = new JSONObject(); //小程序APPID miniprogram.put("appid",WxAppletConfig.APPLET_APPID); //跳转到小程序的页面路径 miniprogram.put("pagepath","pages/main_Page/mainPage"); jsonObject.put("miniprogram", miniprogram); //公众号消息数据封装 JSONObject data = new JSONObject(); //此处的参数key,需要对照模板中的key来设置 data.put("first", getValue("亲爱的王先生/女士您好,您于2022年07月25日新增加一个客户。")); data.put("keyword1", getValue(content.get("phone")));//联系方式 data.put("keyword2", getValue(content.get("time")));//时间 data.put("remark", getValue("请前往小程序查看!")); jsonObject.put("data", data); jsonData.put("mp_template_msg", jsonObject); System.out.println("请求参数:"+jsonData); String s = HttpUtils.post(path, jsonData.toJSONString()); System.out.println("返回结果:"+s); resultStatus="1"; } catch (Exception e) { log.error("微信公众号发送消息失败!",e.getMessage()); resultStatus="0"; } return resultStatus; } /** * 获取data * @param value * @return */ private JSONObject getValue(String value) { // TODO Auto-generated method stub JSONObject json = new JSONObject(); json.put("value", value); json.put("color", "#173177"); return json; } }