me.chanjar.weixin.common.session.WxSessionManager Java Examples

The following examples show how to use me.chanjar.weixin.common.session.WxSessionManager. 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: MenuHandler.java    From black-shop with Apache License 2.0 6 votes vote down vote up
@Override
public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage,
                                Map<String, Object> context, WxMpService weixinService,
                                WxSessionManager sessionManager) {

    String msg = String.format("type:%s, event:%s, key:%s",
        wxMessage.getMsgType(), wxMessage.getEvent(),
        wxMessage.getEventKey());
    if (MenuButtonType.VIEW.equals(wxMessage.getEvent())) {
        return null;
    }

    return WxMpXmlOutMessage.TEXT().content(msg)
        .fromUser(wxMessage.getToUser()).toUser(wxMessage.getFromUser())
        .build();
}
 
Example #2
Source File: FocusMeMessage.java    From mywx with Apache License 2.0 6 votes vote down vote up
@Override
public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, Map<String, Object> context, WxMpService wxMpService, WxSessionManager sessionManager) throws WxErrorException {
   // String msg = source.getMessage("message.welcome", null, Locale.getDefault());
    String event = StringUtils.isBlank(wxMessage.getEvent()) ? StringUtils.EMPTY : wxMessage.getEvent();
    WxMpXmlOutTextMessage m = null;
    if (WxConsts.EVT_SUBSCRIBE.equals(event)) {
        m
                = WxMpXmlOutMessage
                .TEXT()
                .content("message.welcome")
                .fromUser(wxMessage.getToUserName())
                .toUser(wxMessage.getFromUserName())
                .build();
    } else if (WxConsts.EVT_UNSUBSCRIBE.equals(event)) {
    }
    log.info("{} ---> {}", event, ToStringBuilder.reflectionToString(wxMessage));
    return m;
}
 
Example #3
Source File: WxMaDemoServer.java    From weixin-java-tools with Apache License 2.0 6 votes vote down vote up
@Override
public void handle(WxMaMessage wxMessage, Map<String, Object> context,
                   WxMaService service, WxSessionManager sessionManager) throws WxErrorException {
  try {
    WxMediaUploadResult uploadResult = service.getMediaService()
      .uploadMedia(WxMaConstants.MediaType.IMAGE, "png",
        ClassLoader.getSystemResourceAsStream("tmp.png"));
    service.getMsgService().sendKefuMsg(
      WxMaKefuMessage
        .newImageBuilder()
        .mediaId(uploadResult.getMediaId())
        .toUser(wxMessage.getFromUser())
        .build());
  } catch (WxErrorException e) {
    e.printStackTrace();
  }
}
 
Example #4
Source File: MenuHandler.java    From sdb-mall with Apache License 2.0 6 votes vote down vote up
@Override
public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage,
                                Map<String, Object> context, WxMpService weixinService,
                                WxSessionManager sessionManager) {

    String msg = String.format("type:%s, event:%s, key:%s",
        wxMessage.getMsgType(), wxMessage.getEvent(),
        wxMessage.getEventKey());
    if (MenuButtonType.VIEW.equals(wxMessage.getEvent())) {
        return null;
    }

    return WxMpXmlOutMessage.TEXT().content(msg)
        .fromUser(wxMessage.getToUser()).toUser(wxMessage.getFromUser())
        .build();
}
 
Example #5
Source File: LocationHandler.java    From sdb-mall with Apache License 2.0 6 votes vote down vote up
@Override
public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage,
                                Map<String, Object> context, WxMpService wxMpService,
                                WxSessionManager sessionManager) {
    if (wxMessage.getMsgType().equals(XmlMsgType.LOCATION)) {
        //TODO 接收处理用户发送的地理位置消息
        try {
            String content = "感谢反馈,您的的地理位置已收到!";
            return new TextBuilder().build(content, wxMessage, null);
        } catch (Exception e) {
            this.logger.error("位置消息接收处理失败", e);
            return null;
        }
    }

    //上报地理位置事件
    this.logger.info("上报地理位置,纬度 : {},经度 : {},精度 : {}",
        wxMessage.getLatitude(), wxMessage.getLongitude(), String.valueOf(wxMessage.getPrecision()));

    //TODO  可以将用户地理位置信息保存到本地数据库,以便以后使用

    return null;
}
 
Example #6
Source File: DemoImageHandler.java    From weixin-java-tools with Apache License 2.0 6 votes vote down vote up
@Override
public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, Map<String, Object> context,
                                WxMpService wxMpService, WxSessionManager sessionManager) {
  try {
    WxMediaUploadResult wxMediaUploadResult = wxMpService.getMaterialService()
      .mediaUpload(WxConsts.MediaFileType.IMAGE, TestConstants.FILE_JPG, ClassLoader.getSystemResourceAsStream("mm.jpeg"));
    WxMpXmlOutImageMessage m
      = WxMpXmlOutMessage
      .IMAGE()
      .mediaId(wxMediaUploadResult.getMediaId())
      .fromUser(wxMessage.getToUser())
      .toUser(wxMessage.getFromUser())
      .build();
    return m;
  } catch (WxErrorException e) {
    e.printStackTrace();
  }

  return null;
}
 
Example #7
Source File: LocationHandler.java    From yshopmall with Apache License 2.0 6 votes vote down vote up
@Override
public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage,
                                Map<String, Object> context, WxMpService wxMpService,
                                WxSessionManager sessionManager) {
    if (wxMessage.getMsgType().equals(XmlMsgType.LOCATION)) {
        //TODO 接收处理用户发送的地理位置消息
        try {
            String content = "感谢反馈,您的的地理位置已收到!";
            return new TextBuilder().build(content, wxMessage, null);
        } catch (Exception e) {
            this.logger.error("位置消息接收处理失败", e);
            return null;
        }
    }

    //上报地理位置事件
    this.logger.info("上报地理位置,纬度 : {},经度 : {},精度 : {}",
        wxMessage.getLatitude(), wxMessage.getLongitude(), String.valueOf(wxMessage.getPrecision()));

    //TODO  可以将用户地理位置信息保存到本地数据库,以便以后使用

    return null;
}
 
Example #8
Source File: MenuHandler.java    From yshopmall with Apache License 2.0 6 votes vote down vote up
@Override
public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage,
                                Map<String, Object> context, WxMpService weixinService,
                                WxSessionManager sessionManager) {

    String msg = String.format("type:%s, event:%s, key:%s",
        wxMessage.getMsgType(), wxMessage.getEvent(),
        wxMessage.getEventKey());
    if (MenuButtonType.VIEW.equals(wxMessage.getEvent())) {
        return null;
    }

    return WxMpXmlOutMessage.TEXT().content(msg)
        .fromUser(wxMessage.getToUser()).toUser(wxMessage.getFromUser())
        .build();
}
 
Example #9
Source File: WxMaDemoServer.java    From weixin-java-tools with Apache License 2.0 6 votes vote down vote up
@Override
public void handle(WxMaMessage wxMessage, Map<String, Object> context,
                   WxMaService service, WxSessionManager sessionManager) throws WxErrorException {
  try {
    final File file = service.getQrcodeService().createQrcode("123", 430);
    WxMediaUploadResult uploadResult = service.getMediaService().uploadMedia(WxMaConstants.MediaType.IMAGE, file);
    service.getMsgService().sendKefuMsg(
      WxMaKefuMessage
        .newImageBuilder()
        .mediaId(uploadResult.getMediaId())
        .toUser(wxMessage.getFromUser())
        .build());
  } catch (WxErrorException e) {
    e.printStackTrace();
  }
}
 
Example #10
Source File: MenuHandler.java    From mall4j with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage,
                                Map<String, Object> context, WxMpService weixinService,
                                WxSessionManager sessionManager) {

    String msg = String.format("type:%s, event:%s, key:%s",
        wxMessage.getMsgType(), wxMessage.getEvent(),
        wxMessage.getEventKey());
    if (MenuButtonType.VIEW.equals(wxMessage.getEvent())) {
        return null;
    }

    return WxMpXmlOutMessage.TEXT().content(msg)
        .fromUser(wxMessage.getToUser()).toUser(wxMessage.getFromUser())
        .build();
}
 
Example #11
Source File: MsgHandler.java    From fw-cloud-framework with MIT License 6 votes vote down vote up
@Override
public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, Map<String, Object> context,
		WxMpService weixinService, WxSessionManager sessionManager) {

	if (!wxMessage.getMsgType().equals(XmlMsgType.EVENT)) {
		// TODO 可以选择将消息保存到本地
	}

	// 当用户输入关键词如“你好”,“客服”等,并且有客服在线时,把消息转发给在线客服
	try {
		if (StringUtils.startsWithAny(wxMessage.getContent(), "你好", "客服")
				&& weixinService.getKefuService().kfOnlineList().getKfOnlineList().size() > 0) { return WxMpXmlOutMessage
				.TRANSFER_CUSTOMER_SERVICE().fromUser(wxMessage.getToUser()).toUser(
						wxMessage.getFromUser()).build(); }
	} catch (WxErrorException e) {
		e.printStackTrace();
	}

	// TODO 组装回复消息
	String content = "收到信息内容:" + JsonUtils.toJson(wxMessage);

	return new TextBuilder().build(content, wxMessage, weixinService);

}
 
Example #12
Source File: LocationHandler.java    From black-shop with Apache License 2.0 6 votes vote down vote up
@Override
public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, Map<String, Object> context, WxMpService wxMpService,
		WxSessionManager sessionManager) {
	if (wxMessage.getMsgType().equals(XmlMsgType.LOCATION)) {
		// TODO 接收处理用户发送的地理位置消息
		try {
			String content = "感谢反馈,您的的地理位置已收到!";
			return new TextBuilder().build(content, wxMessage, null);
		} catch (Exception e) {
			this.logger.error("位置消息接收处理失败", e);
			return null;
		}
	}

	// 上报地理位置事件
	this.logger.info("上报地理位置,纬度 : {},经度 : {},精度 : {}", wxMessage.getLatitude(), wxMessage.getLongitude(),
			String.valueOf(wxMessage.getPrecision()));

	// TODO 可以将用户地理位置信息保存到本地数据库,以便以后使用

	return null;
}
 
Example #13
Source File: WxCpMessageRouterRule.java    From weixin-java-tools with Apache License 2.0 5 votes vote down vote up
/**
 * 处理微信推送过来的消息
 *
 * @param wxMessage
 * @return true 代表继续执行别的router,false 代表停止执行别的router
 */
protected WxCpXmlOutMessage service(WxCpXmlMessage wxMessage,
    WxCpService wxCpService,
    WxSessionManager sessionManager,
    WxErrorExceptionHandler exceptionHandler) {

  try {

    Map<String, Object> context = new HashMap<String, Object>();
    // 如果拦截器不通过
    for (WxCpMessageInterceptor interceptor : this.interceptors) {
      if (!interceptor.intercept(wxMessage, context, wxCpService, sessionManager)) {
        return null;
      }
    }

    // 交给handler处理
    WxCpXmlOutMessage res = null;
    for (WxCpMessageHandler handler : this.handlers) {
      // 返回最后handler的结果
      res = handler.handle(wxMessage, context, wxCpService, sessionManager);
    }
    return res;

  } catch (WxErrorException e) {
    exceptionHandler.handle(e);
  }

  return null;

}
 
Example #14
Source File: MsgHandler.java    From black-shop with Apache License 2.0 5 votes vote down vote up
@Override
public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, Map<String, Object> context, WxMpService weixinService,
								WxSessionManager sessionManager) {

	if (!wxMessage.getMsgType().equals(XmlMsgType.EVENT)) {
		// TODO 可以选择将消息保存到本地
	}

	// 当用户输入关键词如“你好”,“客服”等,并且有客服在线时,把消息转发给在线客服
	try {
		if (StringUtils.startsWithAny(wxMessage.getContent(), "你好", "客服")
				&& weixinService.getKefuService().kfOnlineList().getKfOnlineList().size() > 0) {
			return WxMpXmlOutMessage.TRANSFER_CUSTOMER_SERVICE().fromUser(wxMessage.getToUser())
					.toUser(wxMessage.getFromUser()).build();
		}
	} catch (WxErrorException e) {
		e.printStackTrace();
	}
	// 1. 获取微信客户端发送的消息
	String fromContent = wxMessage.getContent();
	// 2.使用正则表达式验证消息是否为手机号码格式
	if (RegexUtils.checkMobile(fromContent)) {
		// 1.根据手机号码调用会员服务接口查询用户信息是否存在
		ResponseResult<Boolean> result = sysUserServiceClient.existMobileNumber(fromContent);
		if (!result.getResult()) {
			return new TextBuilder().build("该手机号码" + fromContent + "已经存在!", wxMessage, weixinService);
		}
		// 3.如果是手机号码格式的话,随机生产4位数字注册码
		int registCode = registCode();
		String content = registrationCodeMessage.format(registrationCodeMessage, registCode);
		// 将注册码存入在redis中 key为手机号码
		//redisUtil.setString(Constants.WECHET_CODE_KEY + fromContent, registCode + "", Constants.WECHET_CODE_TIMEOUT);
		return new TextBuilder().build(content, wxMessage, weixinService);
	}
	// 否则情况下返回默认消息 调用第三方机器人接口
	return new TextBuilder().build(defaultRegistrationCodeMessage, wxMessage, weixinService);

}
 
Example #15
Source File: NoMessage.java    From mywx with Apache License 2.0 5 votes vote down vote up
@Override
    public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, Map<String, Object> context, WxMpService wxMpService, WxSessionManager sessionManager)
            throws WxErrorException {
//        MessageSource source = Environment.getI18n();
//        String msg = source.getMessage("message.noregex", null, Locale.getDefault());
        WxMpXmlOutTextMessage m
                = WxMpXmlOutMessage
                .TEXT()
                .content("message.noregex")
                .fromUser(wxMessage.getToUserName())
                .toUser(wxMessage.getFromUserName())
                .build();
        return m;
    }
 
Example #16
Source File: LogHandler.java    From sdb-mall with Apache License 2.0 5 votes vote down vote up
@Override
public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage,
                                Map<String, Object> context, WxMpService wxMpService,
                                WxSessionManager sessionManager) {
    this.logger.info("\n接收到请求消息,内容:{}", JsonUtils.toJson(wxMessage));
    return null;
}
 
Example #17
Source File: StoreCheckNotifyHandler.java    From sdb-mall with Apache License 2.0 5 votes vote down vote up
@Override
public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage,
                                Map<String, Object> context, WxMpService wxMpService,
                                WxSessionManager sessionManager) {
    // TODO 处理门店审核事件
    return null;
}
 
Example #18
Source File: DemoTextHandler.java    From weixin-java-tools with Apache License 2.0 5 votes vote down vote up
@Override
public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, Map<String, Object> context,
    WxMpService wxMpService, WxSessionManager sessionManager) {
  WxMpXmlOutTextMessage m
      = WxMpXmlOutMessage.TEXT().content("测试加密消息").fromUser(wxMessage.getToUserName())
      .toUser(wxMessage.getFromUserName()).build();
  return m;
}
 
Example #19
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 #20
Source File: UnsubscribeHandler.java    From fw-cloud-framework with MIT License 5 votes vote down vote up
@Override
public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, Map<String, Object> context,
		WxMpService wxMpService, WxSessionManager sessionManager) {
	String openId = wxMessage.getFromUser();
	this.logger.info("取消关注用户 OPENID: " + openId);
	// TODO 可以更新本地数据库为取消关注状态
	return null;
}
 
Example #21
Source File: UnsubscribeHandler.java    From sdb-mall with Apache License 2.0 5 votes vote down vote up
@Override
public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage,
                                Map<String, Object> context, WxMpService wxMpService,
                                WxSessionManager sessionManager) {
    String openId = wxMessage.getFromUser();
    this.logger.info("取消关注用户 OPENID: " + openId);
    // TODO 可以更新本地数据库为取消关注状态
    return null;
}
 
Example #22
Source File: WxCpMessageRouterTest.java    From weixin-java-tools with Apache License 2.0 5 votes vote down vote up
public void testConcurrency() throws InterruptedException {
  final WxCpMessageRouter router = new WxCpMessageRouter(null);
  router.rule().handler(new WxCpMessageHandler() {
    @Override
    public WxCpXmlOutMessage handle(WxCpXmlMessage wxMessage, Map<String, Object> context, WxCpService wxCpService,
        WxSessionManager sessionManager) {
      return null;
    }
  }).end();
  
  final WxCpXmlMessage m = new WxCpXmlMessage();
  Runnable r = new Runnable() {
    @Override
    public void run() {
      router.route(m);
      try {
        Thread.sleep(1000l);
      } catch (InterruptedException e) {
      }
    }
  };
  for (int i = 0; i < 10; i++) {
    new Thread(r).start();
  }
  
  Thread.sleep(1000l * 2);
}
 
Example #23
Source File: WxMpMessageRouterRule.java    From weixin-java-tools with Apache License 2.0 5 votes vote down vote up
/**
 * 处理微信推送过来的消息
 *
 * @param wxMessage
 * @return true 代表继续执行别的router,false 代表停止执行别的router
 */
protected WxMpXmlOutMessage service(WxMpXmlMessage wxMessage,
    WxMpService wxMpService,
    WxSessionManager sessionManager,
    WxErrorExceptionHandler exceptionHandler) {

  try {

    Map<String, Object> context = new HashMap<String, Object>();
    // 如果拦截器不通过
    for (WxMpMessageInterceptor interceptor : this.interceptors) {
      if (!interceptor.intercept(wxMessage, context, wxMpService, sessionManager)) {
        return null;
      }
    }

    // 交给handler处理
    WxMpXmlOutMessage res = null;
    for (WxMpMessageHandler handler : this.handlers) {
      // 返回最后handler的结果
      res = handler.handle(wxMessage, context, wxMpService, sessionManager);
    }
    return res;
  } catch (WxErrorException e) {
    exceptionHandler.handle(e);
  }
  return null;

}
 
Example #24
Source File: MsgHandler.java    From sdb-mall with Apache License 2.0 5 votes vote down vote up
@Override
public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage,
                                Map<String, Object> context, WxMpService weixinService,
                                WxSessionManager sessionManager) {

    if (!wxMessage.getMsgType().equals(XmlMsgType.EVENT)) {
        //TODO 可以选择将消息保存到本地
    }

    //当用户输入关键词如“你好”,“客服”等,并且有客服在线时,把消息转发给在线客服
    try {
        if (StringUtils.startsWithAny(wxMessage.getContent(), "你好", "客服")
            && weixinService.getKefuService().kfOnlineList()
            .getKfOnlineList().size() > 0) {
            return WxMpXmlOutMessage.TRANSFER_CUSTOMER_SERVICE()
                .fromUser(wxMessage.getToUser())
                .toUser(wxMessage.getFromUser()).build();
        }
    } catch (WxErrorException e) {
        e.printStackTrace();
    }

    //TODO 组装回复消息
    String content = "收到信息内容:" + JsonUtils.toJson(wxMessage);

    return new TextBuilder().build(content, wxMessage, weixinService);

}
 
Example #25
Source File: MenuHandler.java    From fw-cloud-framework with MIT License 5 votes vote down vote up
@Override
public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, Map<String, Object> context,
		WxMpService weixinService, WxSessionManager sessionManager) {

	String msg = String.format("type:%s, event:%s, key:%s", wxMessage.getMsgType(), wxMessage
			.getEvent(), wxMessage.getEventKey());
	if (MenuButtonType.VIEW.equals(wxMessage.getEvent())) { return null; }

	return WxMpXmlOutMessage.TEXT().content(msg).fromUser(wxMessage.getToUser()).toUser(
			wxMessage.getFromUser()).build();
}
 
Example #26
Source File: UnsubscribeHandler.java    From black-shop with Apache License 2.0 5 votes vote down vote up
@Override
public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage,
                                Map<String, Object> context, WxMpService wxMpService,
                                WxSessionManager sessionManager) {
    String openId = wxMessage.getFromUser();
    this.logger.info("取消关注用户 OPENID: " + openId);
    // TODO 可以更新本地数据库为取消关注状态
    return null;
}
 
Example #27
Source File: StoreCheckNotifyHandler.java    From black-shop with Apache License 2.0 5 votes vote down vote up
@Override
public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage,
                                Map<String, Object> context, WxMpService wxMpService,
                                WxSessionManager sessionManager) {
    // TODO 处理门店审核事件
    return null;
}
 
Example #28
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().sendKefuMsg(WxMaKefuMessage.newTextBuilder().content("回复文本消息")
    .toUser(wxMessage.getFromUser()).build());
}
 
Example #29
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 {
  System.out.println("收到消息:" + wxMessage.toString());
  service.getMsgService().sendKefuMsg(WxMaKefuMessage.newTextBuilder().content("收到信息为:" + wxMessage.toJson())
    .toUser(wxMessage.getFromUser()).build());
}
 
Example #30
Source File: WxMaMessageRouterRule.java    From weixin-java-tools with Apache License 2.0 5 votes vote down vote up
/**
 * 处理微信推送过来的消息
 */
protected void service(WxMaMessage wxMessage,
                       Map<String, Object> context,
                       WxMaService wxMaService,
                       WxSessionManager sessionManager,
                       WxErrorExceptionHandler exceptionHandler) {
  if (context == null) {
    context = new HashMap<>(16);
  }

  try {
    // 如果拦截器不通过
    for (WxMaMessageInterceptor interceptor : this.interceptors) {
      if (!interceptor.intercept(wxMessage, context, wxMaService, sessionManager)) {
        return;
      }
    }

    // 交给handler处理
    for (WxMaMessageHandler handler : this.handlers) {
      // 返回最后handler的结果
      if (handler == null) {
        continue;
      }
      handler.handle(wxMessage, context, wxMaService, sessionManager);
    }
  } catch (WxErrorException e) {
    exceptionHandler.handle(e);
  }
}