Search in sources :

Example 1 with WxMpXmlOutMessage

use of me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage in project summer by foxsugar.

the class SubscribeHandler method handle.

@Override
public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, Map<String, Object> context, WxMpService weixinService, WxSessionManager sessionManager) throws WxErrorException {
    this.logger.info("新关注用户 OPENID: " + wxMessage.getFromUser());
    // 获取微信用户基本信息
    WxMpUser userWxInfo = weixinService.getUserService().userInfo(wxMessage.getFromUser(), null);
    if (userWxInfo != null) {
        // TODO 可以添加关注用户到本地
        GameAgentWx gameAgentWx = new GameAgentWx();
        gameAgentWx.setUnionId(userWxInfo.getUnionId());
        gameAgentWx.setOpenId(userWxInfo.getOpenId());
        gameAgentWxService.getGameAgentWxDao().save(gameAgentWx);
    }
    WxMpXmlOutMessage responseResult = null;
    try {
        responseResult = handleSpecial(wxMessage, userWxInfo, weixinService);
    } catch (Exception e) {
        this.logger.error(e.getMessage(), e);
    }
    if (responseResult != null) {
        return responseResult;
    }
    try {
        return new TextBuilder().build("感谢关注", wxMessage, weixinService);
    } catch (Exception e) {
        this.logger.error(e.getMessage(), e);
    }
    return null;
}
Also used : GameAgentWx(com.code.server.db.model.GameAgentWx) TextBuilder(com.code.server.login.wechat.builder.TextBuilder) WxMpUser(me.chanjar.weixin.mp.bean.result.WxMpUser) WxMpXmlOutMessage(me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage) WxErrorException(me.chanjar.weixin.common.exception.WxErrorException)

Example 2 with WxMpXmlOutMessage

use of me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage in project weixin-java-mp-multi-demo by binarywang.

the class AbstractWxPortalController method post.

@ResponseBody
@PostMapping(produces = "application/xml; charset=UTF-8")
public String post(@RequestBody String requestBody, @RequestParam("timestamp") String timestamp, @RequestParam("nonce") String nonce, @RequestParam("signature") String signature, @RequestParam(name = "encrypt_type", required = false) String encType, @RequestParam(name = "msg_signature", required = false) String msgSignature) {
    this.logger.info("\n接收微信请求:[signature=[{}], encType=[{}], msgSignature=[{}]," + " timestamp=[{}], nonce=[{}], requestBody=[\n{}\n] ", signature, encType, msgSignature, timestamp, nonce, requestBody);
    if (!this.getWxService().checkSignature(timestamp, nonce, signature)) {
        throw new IllegalArgumentException("非法请求,可能属于伪造的请求!");
    }
    String out = null;
    if (encType == null) {
        // 明文传输的消息
        WxMpXmlMessage inMessage = WxMpXmlMessage.fromXml(requestBody);
        WxMpXmlOutMessage outMessage = this.getWxService().route(inMessage);
        if (outMessage == null) {
            return "";
        }
        out = outMessage.toXml();
    } else if ("aes".equals(encType)) {
        // aes加密的消息
        WxMpXmlMessage inMessage = WxMpXmlMessage.fromEncryptedXml(requestBody, this.getWxService().getWxMpConfigStorage(), timestamp, nonce, msgSignature);
        this.logger.debug("\n消息解密后内容为:\n{} ", inMessage.toString());
        WxMpXmlOutMessage outMessage = this.getWxService().route(inMessage);
        if (outMessage == null) {
            return "";
        }
        out = outMessage.toEncryptedXml(this.getWxService().getWxMpConfigStorage());
    }
    this.logger.debug("\n组装回复信息:{}", out);
    return out;
}
Also used : WxMpXmlMessage(me.chanjar.weixin.mp.bean.message.WxMpXmlMessage) WxMpXmlOutMessage(me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage)

Example 3 with WxMpXmlOutMessage

use of me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage in project weixin-java-mp-demo-springboot by binarywang.

the class SubscribeHandler method handle.

@Override
public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, Map<String, Object> context, WxMpService weixinService, WxSessionManager sessionManager) throws WxErrorException {
    this.logger.info("新关注用户 OPENID: " + wxMessage.getFromUser());
    // 获取微信用户基本信息
    try {
        WxMpUser userWxInfo = weixinService.getUserService().userInfo(wxMessage.getFromUser(), null);
        if (userWxInfo != null) {
        // TODO 可以添加关注用户到本地数据库
        }
    } catch (WxErrorException e) {
        if (e.getError().getErrorCode() == 48001) {
            this.logger.info("该公众号没有获取用户信息权限!");
        }
    }
    WxMpXmlOutMessage responseResult = null;
    try {
        responseResult = this.handleSpecial(wxMessage);
    } catch (Exception e) {
        this.logger.error(e.getMessage(), e);
    }
    if (responseResult != null) {
        return responseResult;
    }
    try {
        return new TextBuilder().build("感谢关注", wxMessage, weixinService);
    } catch (Exception e) {
        this.logger.error(e.getMessage(), e);
    }
    return null;
}
Also used : TextBuilder(com.github.binarywang.demo.wx.mp.builder.TextBuilder) WxMpUser(me.chanjar.weixin.mp.bean.result.WxMpUser) WxMpXmlOutMessage(me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage) WxErrorException(me.chanjar.weixin.common.error.WxErrorException) WxErrorException(me.chanjar.weixin.common.error.WxErrorException)

Example 4 with WxMpXmlOutMessage

use of me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage in project fw-cloud-framework by liuweijw.

the class WechatController method post.

@PostMapping(produces = "application/xml; charset=UTF-8")
public String post(@RequestBody String requestBody, @RequestParam("signature") String signature, @RequestParam("timestamp") String timestamp, @RequestParam("nonce") String nonce, @RequestParam(name = "encrypt_type", required = false) String encType, @RequestParam(name = "msg_signature", required = false) String msgSignature) {
    this.logger.info("\n接收微信请求:[signature=[{}], encType=[{}], msgSignature=[{}]," + " timestamp=[{}], nonce=[{}], requestBody=[\n{}\n] ", signature, encType, msgSignature, timestamp, nonce, requestBody);
    if (!this.wxService.checkSignature(timestamp, nonce, signature)) {
        throw new IllegalArgumentException("非法请求,可能属于伪造的请求!");
    }
    String out = null;
    if (encType == null) {
        // 明文传输的消息
        WxMpXmlMessage inMessage = WxMpXmlMessage.fromXml(requestBody);
        WxMpXmlOutMessage outMessage = this.route(inMessage);
        if (outMessage == null) {
            return "";
        }
        out = outMessage.toXml();
    } else if ("aes".equals(encType)) {
        // aes加密的消息
        WxMpXmlMessage inMessage = WxMpXmlMessage.fromEncryptedXml(requestBody, this.wxService.getWxMpConfigStorage(), timestamp, nonce, msgSignature);
        this.logger.debug("\n消息解密后内容为:\n{} ", inMessage.toString());
        WxMpXmlOutMessage outMessage = this.route(inMessage);
        if (outMessage == null) {
            return "";
        }
        out = outMessage.toEncryptedXml(this.wxService.getWxMpConfigStorage());
    }
    this.logger.debug("\n组装回复信息:{}", out);
    return out;
}
Also used : WxMpXmlMessage(me.chanjar.weixin.mp.bean.message.WxMpXmlMessage) WxMpXmlOutMessage(me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Example 5 with WxMpXmlOutMessage

use of me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage in project fw-cloud-framework by liuweijw.

the class SubscribeHandler method handle.

@Override
public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, Map<String, Object> context, WxMpService weixinService, WxSessionManager sessionManager) throws WxErrorException {
    this.logger.info("新关注用户 OPENID: " + wxMessage.getFromUser());
    // 获取微信用户基本信息
    WxMpUser userWxInfo = weixinService.getUserService().userInfo(wxMessage.getFromUser(), null);
    if (userWxInfo != null) {
    // TODO 可以添加关注用户到本地
    }
    WxMpXmlOutMessage responseResult = null;
    try {
        responseResult = handleSpecial(wxMessage);
    } catch (Exception e) {
        this.logger.error(e.getMessage(), e);
    }
    if (responseResult != null) {
        return responseResult;
    }
    try {
        return new TextBuilder().build("感谢关注", wxMessage, weixinService);
    } catch (Exception e) {
        this.logger.error(e.getMessage(), e);
    }
    return null;
}
Also used : TextBuilder(com.github.liuweijw.business.wechat.builder.TextBuilder) WxMpUser(me.chanjar.weixin.mp.bean.result.WxMpUser) WxMpXmlOutMessage(me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage) WxErrorException(me.chanjar.weixin.common.error.WxErrorException)

Aggregations

WxMpXmlOutMessage (me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage)12 WxMpXmlMessage (me.chanjar.weixin.mp.bean.message.WxMpXmlMessage)6 WxMpUser (me.chanjar.weixin.mp.bean.result.WxMpUser)6 WxErrorException (me.chanjar.weixin.common.error.WxErrorException)3 WxErrorException (me.chanjar.weixin.common.exception.WxErrorException)3 TextBuilder (com.github.binarywang.demo.wx.mp.builder.TextBuilder)2 PostMapping (org.springframework.web.bind.annotation.PostMapping)2 GameAgentWx (com.code.server.db.model.GameAgentWx)1 TextBuilder (com.code.server.login.wechat.builder.TextBuilder)1 TextBuilder (com.github.binarywang.demo.spring.builder.TextBuilder)1 BaseWxService (com.github.binarywang.demo.spring.service.BaseWxService)1 TextBuilder (com.github.binarywang.demo.wechat.builder.TextBuilder)1 BaseWxService (com.github.binarywang.demo.wx.mp.service.BaseWxService)1 TextBuilder (com.github.liuweijw.business.wechat.builder.TextBuilder)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1