cn.binarywang.wx.miniapp.bean.WxMaTemplateMessage Java Examples

The following examples show how to use cn.binarywang.wx.miniapp.bean.WxMaTemplateMessage. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: WxTemplateSender.java    From BigDataPlatform with GNU General Public License v3.0 6 votes vote down vote up
private void sendMsg(String touser, String templatId, String[] parms, String page, String color,
                     String emphasisKeyword) {
    LitemallUserFormid userFormid = formIdService.queryByOpenId(touser);
    if (userFormid == null)
        return;


    WxMaTemplateMessage msg = new WxMaTemplateMessage();
    msg.setTemplateId(templatId);
    msg.setToUser(touser);
    msg.setFormId(userFormid.getFormid());
    msg.setPage(page);
    msg.setColor(color);
    msg.setEmphasisKeyword(emphasisKeyword);
    msg.setData(createMsgData(parms));

    try {
        wxMaService.getMsgService().sendTemplateMsg(msg);
        if (formIdService.updateUserFormId(userFormid) == 0) {
            logger.warn("更新数据已失效");
        }
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
    }
}
 
Example #2
Source File: WxTemplateSender.java    From dts-shop with GNU Lesser General Public License v3.0 6 votes vote down vote up
private void sendMsg(String touser, String templatId, String[] parms, String page, String color,
		String emphasisKeyword) {
	DtsUserFormid userFormid = formIdService.queryByOpenId(touser);
	if (userFormid == null)
		return;

	WxMaTemplateMessage msg = new WxMaTemplateMessage();
	msg.setTemplateId(templatId);
	msg.setToUser(touser);
	msg.setFormId(userFormid.getFormid());
	msg.setPage(page);
	msg.setColor(color);
	msg.setEmphasisKeyword(emphasisKeyword);
	msg.setData(createMsgData(parms));

	try {
		wxMaService.getMsgService().sendTemplateMsg(msg);
		if (formIdService.updateUserFormId(userFormid) == 0) {
			logger.warn("更新数据已失效");
		}
	} catch (Exception e) {
		e.printStackTrace();
	}
}
 
Example #3
Source File: WxTemplateSender.java    From mall with MIT License 6 votes vote down vote up
private void sendMsg(String touser, String templatId, String[] parms, String page, String color, String emphasisKeyword) {
    LitemallUserFormid userFormid = formIdService.queryByOpenId(touser);
    if (userFormid == null)
        return;


    WxMaTemplateMessage msg = new WxMaTemplateMessage();
    msg.setTemplateId(templatId);
    msg.setToUser(touser);
    msg.setFormId(userFormid.getFormid());
    msg.setPage(page);
    msg.setColor(color);
    msg.setEmphasisKeyword(emphasisKeyword);
    msg.setData(createMsgData(parms));

    try {
        wxMaService.getMsgService().sendTemplateMsg(msg);
        if (formIdService.updateUserFormId(userFormid) == 0) {
            logger.warn("更新数据已失效");
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example #4
Source File: WxMaMsgServiceImplTest.java    From weixin-java-tools with Apache License 2.0 6 votes vote down vote up
@Test(invocationCount = 5, threadPoolSize = 3)
public void testSendTemplateMsg() throws WxErrorException {
  SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
  TestConfig config = (TestConfig) this.wxService.getWxMaConfig();

  WxMaTemplateMessage templateMessage = WxMaTemplateMessage.builder()
    .toUser(config.getOpenid())
    .formId("FORMID")
    .page("index")
    .data(Lists.newArrayList(
      new WxMaTemplateMessage.Data("keyword1", "339208499", "#173177"),
      new WxMaTemplateMessage.Data("keyword2", dateFormat.format(new Date()), "#173177"),
      new WxMaTemplateMessage.Data("keyword3", "粤海喜来登酒店", "#173177"),
      new WxMaTemplateMessage.Data("keyword4", "广州市天河区天河路208号", "#173177")))
    .templateId(config.getTemplateId())
    .emphasisKeyword("keyword1.DATA")
    .build();
  //templateMessage.addData( new WxMaTemplateMessage.Data("keyword1", "339208499", "#173177"));
  this.wxService.getMsgService().sendTemplateMsg(templateMessage);
}
 
Example #5
Source File: WxMaMsgServiceImpl.java    From weixin-java-tools with Apache License 2.0 5 votes vote down vote up
@Override
public void sendTemplateMsg(WxMaTemplateMessage templateMessage) throws WxErrorException {
  String responseContent = this.wxMaService.post(TEMPLATE_MSG_SEND_URL, templateMessage.toJson());
  JsonObject jsonObject = JSON_PARSER.parse(responseContent).getAsJsonObject();
  if (jsonObject.get(WxMaConstants.ERRCODE).getAsInt() != 0) {
    throw new WxErrorException(WxError.fromJson(responseContent));
  }
}
 
Example #6
Source File: WxMaDemoServer.java    From weixin-java-tools with Apache License 2.0 5 votes vote down vote up
@Override
public void handle(WxMaMessage wxMessage, Map<String, Object> context,
                   WxMaService service, WxSessionManager sessionManager)
  throws WxErrorException {
  service.getMsgService().sendTemplateMsg(WxMaTemplateMessage.builder()
    .templateId(templateId).data(Lists.newArrayList(
      new WxMaTemplateMessage.Data("keyword1", "339208499", "#173177")))
    .toUser(wxMessage.getFromUser())
    .formId("自己替换可用的formid")
    .build());
}
 
Example #7
Source File: WxMaTemplateMessageGsonAdapter.java    From weixin-java-tools with Apache License 2.0 4 votes vote down vote up
@Override
public JsonElement serialize(WxMaTemplateMessage message, Type typeOfSrc, JsonSerializationContext context) {
  JsonObject messageJson = new JsonObject();
  messageJson.addProperty("touser", message.getToUser());
  messageJson.addProperty("template_id", message.getTemplateId());
  if (message.getPage() != null) {
    messageJson.addProperty("page", message.getPage());
  }

  if (message.getFormId() != null) {
    messageJson.addProperty("form_id", message.getFormId());
  }

  if (message.getPage() != null) {
    messageJson.addProperty("page", message.getPage());
  }

  if (message.getColor() != null) {
    messageJson.addProperty("color", message.getColor());
  }

  if (message.getEmphasisKeyword() != null) {
    messageJson.addProperty("emphasis_keyword", message.getEmphasisKeyword());
  }

  JsonObject data = new JsonObject();
  messageJson.add("data", data);

  if (message.getData() == null) {
    return messageJson;
  }

  for (WxMaTemplateMessage.Data datum : message.getData()) {
    JsonObject dataJson = new JsonObject();
    dataJson.addProperty("value", datum.getValue());
    if (datum.getColor() != null) {
      dataJson.addProperty("color", datum.getColor());
    }
    data.add(datum.getName(), dataJson);
  }

  return messageJson;
}
 
Example #8
Source File: WxUniformMsgSender.java    From WePush with MIT License 4 votes vote down vote up
@Override
public SendResult send(String[] msgData) {
    SendResult sendResult = new SendResult();

    try {
        String openId = msgData[0];
        WxMaTemplateMessage wxMaTemplateMessage = wxMaTemplateMsgMaker.makeMsg(msgData);
        WxMpTemplateMessage wxMpTemplateMessage = wxMpTemplateMsgMaker.makeMsg(msgData);

        WxMaUniformMessage wxMaUniformMessage = new WxMaUniformMessage();
        wxMaUniformMessage.setMpTemplateMsg(true);
        wxMaUniformMessage.setToUser(openId);
        wxMaUniformMessage.setAppid(App.config.getMiniAppAppId());
        wxMaUniformMessage.setTemplateId(wxMpTemplateMessage.getTemplateId());
        wxMaUniformMessage.setUrl(wxMpTemplateMessage.getUrl());
        wxMaUniformMessage.setPage(wxMaTemplateMessage.getPage());
        wxMaUniformMessage.setFormId(msgData[1]);
        MiniProgram miniProgram = new MiniProgram();
        miniProgram.setAppid(App.config.getMiniAppAppId());
        miniProgram.setPagePath(wxMaTemplateMessage.getPage());

        wxMaUniformMessage.setMiniProgram(miniProgram);
        wxMaUniformMessage.setData(wxMaTemplateMessage.getData());
        wxMaUniformMessage.setEmphasisKeyword(wxMaTemplateMessage.getEmphasisKeyword());

        if (PushControl.dryRun) {
            sendResult.setSuccess(true);
            return sendResult;
        } else {
            WxMaTemplateMsgSender.getWxMaService().getMsgService().sendUniformMsg(wxMaUniformMessage);
        }
    } catch (Exception e) {
        sendResult.setSuccess(false);
        sendResult.setInfo(e.getMessage());
        log.error(ExceptionUtils.getStackTrace(e));
        return sendResult;
    }

    sendResult.setSuccess(true);
    return sendResult;
}
 
Example #9
Source File: WxMaMsgService.java    From weixin-java-tools with Apache License 2.0 2 votes vote down vote up
/**
 * <pre>
 * 发送模板消息
 * 详情请见: <a href="https://mp.weixin.qq.com/debug/wxadoc/dev/api/notice.html#接口说明">发送模板消息</a>
 * 接口url格式:https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=ACCESS_TOKEN
 * </pre>
 */
void sendTemplateMsg(WxMaTemplateMessage templateMessage) throws WxErrorException;