java微信公众号 推送消息

发布时间 2023-06-07 15:54:54作者: Aoul
WxConfig.java
 

@Slf4j
@Service
public class WxConfig {
@Resource
private WxClient wxClient;

    // appId
    private static final String appId = "xxxx";

    // appIdSecret
    private static final String appIdSecret = "xxxx";

    private static final String templateId = "xxxxx";


    /**
    * 获取WxToken
    *
    * @return
    */

    public String getWxToken() {
      Map<String, Object> clientCredential = new HashMap<>();
      try {
      clientCredential = wxClient.getUserToken("client_credential", appId, appIdSecret);
      } catch (Exception e) {
        log.info("获取微信token异常,{}", e);
      }
        return clientCredential.get("access_token") != null ? clientCredential.get("access_token").toString() : "500";
      }

      /**
      * 获取 公众号下的 opnid集合
      *
      * @return
      */
      public String getUserList() {
        String wxToken = getWxToken();
        if (wxToken.equals("500")) {
        throw new BaseAppException("获取WxToken失败!");
          }
          Map<String, Object> wxUser = new HashMap<>();
        try {
          wxUser = wxClient.getWxUser(wxToken, "");
          } catch (Exception e) {
            log.info("获取微信openid异常,{}", e);
          }
            JSONObject data = JSONUtil.parseObj(wxUser.get("data"));
            return data.get("openid") != null ? data.get("openid").toString() : "500";
          }


          /**
          * 获取微信openid信息
          *
          * @param opnid
          * @return
          */
          public Map<String, Object> getWxUserInfo(String opnid) {
            String wxToken = getWxToken();
            if (wxToken.equals("500")) {
              throw new BaseAppException("获取WxToken失败!");
              }
              Map<String, Object> resultMap = new HashMap<>();
            try {
            resultMap = wxClient.getWxUserInfo(wxToken, opnid, "zh_CN");
            } catch (Exception e) {
            log.info("获取微信用户信息异常,{}", e);
            }
              return resultMap;
            }


             public void sendWxMsg(String openId) {
                String wxToken = getWxToken();
                if (wxToken.equals("500")) {
                throw new BaseAppException("获取WxToken失败!");
                }
              Map<String, Object> wxUser = wxClient.getWxUser(wxToken, "");
              JSONArray jsonArray = JSONUtil.parseArray(JSONUtil.parseObj(wxUser.get("data")).get("openid"));
              //模板参数
                Map<String, WeChatTemplateMsg> sendMag = new HashMap<String, WeChatTemplateMsg>();
              sendMag.put("body", new WeChatTemplateMsg("测试微信下发接口!!!!!"));
              //请求参数
              Map<String, Object> paramMap = new HashMap<>();
              paramMap.put("touser", jsonArray.get(0)); // openId
              paramMap.put("url", "http://125.46.78.163:15002/fs/notice/17677/031527"); // 点击模板信息跳转地址
              paramMap.put("topcolor", "#FF0000"); // 顶色
              paramMap.put("data", sendMag); // 模板参数
              paramMap.put("template_id", templateId); // 模板Id
              Map<String, Object> resultMap = new HashMap<>();
              try {
                resultMap = wxClient.sendTemplateMsg(wxToken, paramMap);
              } catch (Exception e) {
                log.info("微信消息推送异常,{}", e);
              }
              }

            }
 
 
WxClient.java
 

public interface WxClient {

/**
* 获取token
*
* @param grant_type
* @param appid
* @param secret
* @return
*/

@Get("https://api.weixin.qq.com/cgi-bin/token")
Map<String, Object> getUserToken(@Query("grant_type") String grant_type, @Query("appid") String appid, @Query("secret") String secret);

/**
* 获取用户openId
*
* @param access_token
* @param next_openid
* @return
*/

@Get("https://api.weixin.qq.com/cgi-bin/user/get")
Map<String, Object> getWxUser(@Query("access_token") String access_token, @Query("next_openid") String next_openid);


/**
* 默认值 lang=zh_CN
*
* @param access_token
* @param openid
* @param lang
* @return
*/
@Get("https://api.weixin.qq.com/cgi-bin/user/info")
Map<String, Object> getWxUserInfo(@Query("access_token") String access_token, @Query("openid") String openid, @Query("lang") String lang);


/**
* 发送模板消息
* @param access_token
* @param paramMap
* @return
*/
@Post("https://api.weixin.qq.com/cgi-bin/message/template/send")
Map<String, Object> sendTemplateMsg(@Query("access_token") String access_token, @JSONBody Map<String, Object> paramMap);

}