Esempio n. 1
0
  /**
   * @Description: 发送文本消息
   *
   * @param @param toUser 成员ID列表(消息接收者,多个接收者用‘|’分隔,最多支持1000个)。特殊情况:指定为@all,则向关注该企业应用的全部成员发送
   * @param @param toParty 部门ID列表,多个接收者用‘|’分隔,最多支持100个。当touser为@all时忽略本参数
   * @param @param toTag 标签ID列表,多个接收者用‘|’分隔。当touser为@all时忽略本参数
   * @param @param agentID 企业应用的id,整型。可在应用的设置页面查看
   * @param @param content 消息内容
   * @param @param safe 表示是否是保密消息,0表示否,1表示是,默认0
   * @param @return
   * @param @throws Exception
   * @return String {"errcode": 0,"errmsg": "ok", "invaliduser":
   *     "UserID1","invalidparty":"PartyID1","invalidtag":"TagID1"}
   * @throws
   * @author yaoly
   * @date 2015-6-30
   */
  public static String sendWxTextMsg(
      String toUser, String toParty, String toTag, String agentID, String content, String safe)
      throws Exception {
    String pToUser = toUser != null ? toUser : "";
    String pToParty = toParty != null ? toParty : "";
    String pToTag = toTag != null ? toTag : "";
    String pAgentID = agentID != null ? agentID : "";
    String pContent = content != null ? content : "";
    String pSafe = safe != null ? safe : "0";

    if (ConfigStorage.getIsTestEnv()) {
      // 如果是测试环境
      pToUser = "******";
    }

    String postJson =
        "{\"touser\":\"#{touser}\",\"toparty\":"
            + "\"#{toparty}\",\"totag\":\"#{totag}\""
            + ",\"msgtype\":\"text\",\"agentid\":\"#{agentid}\","
            + "\"text\":{\"content\":\"#{content}\"},\"safe\":\"#{safe}\"}";

    postJson =
        postJson
            .replace("#{touser}", pToUser)
            .replace("#{toparty}", pToParty)
            .replace("#{totag}", pToTag)
            .replace("#{agentid}", pAgentID)
            .replace("#{content}", pContent)
            .replace("#{safe}", pSafe);

    return sendWxMsg(postJson);
  }
Esempio n. 2
0
 /**
  * @Description: 微信消息发送
  *
  * @param @return
  * @return String
  * @throws
  * @author yaoly
  * @date 2015-6-29
  */
 private static String sendWxMsg(String json) throws Exception {
   AccessToken accessToken = TokenApi.getAccessToken();
   if (accessToken == null) {
     log.error("获取accessToken失败,信息发送失败。信息明细:" + json);
     String errStr = "{\"errcode\":-9999,\"errmsg\":\"获取accessToken失败,信息发送失败\"}";
     return errStr;
   }
   String sendWxMsgUrl = ConfigStorage.getSendMsgUrl(); // 微信发送URL
   sendWxMsgUrl = sendWxMsgUrl.replace("#{ACCESSTOKEN}", accessToken.getAccessTokenValue());
   String msgJsonStr = json;
   HttpUtil httpUtil = HttpUtil.getInstance();
   log.info("发送消息Json:" + msgJsonStr);
   String returnValue = httpUtil.doJsonPostForString(sendWxMsgUrl, msgJsonStr);
   log.info("微信返回信息为:" + returnValue);
   return returnValue;
 }