Search in sources :

Example 1 with WxErrorException

use of me.chanjar.weixin.common.error.WxErrorException 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 2 with WxErrorException

use of me.chanjar.weixin.common.error.WxErrorException 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)

Example 3 with WxErrorException

use of me.chanjar.weixin.common.error.WxErrorException in project fw-cloud-framework by liuweijw.

the class MessageServiceImpl method sendWeixinCurrencyTemplateMessage.

@Override
public boolean sendWeixinCurrencyTemplateMessage(MsgBean msgBean) throws WxErrorException {
    if (null == msgBean)
        throw new WxErrorException(WxError.builder().errorCode(500).errorMsg("消息内容不能为空").build());
    if (StringHelper.isBlank(msgBean.getTemplateId()))
        throw new WxErrorException(WxError.builder().errorCode(500).errorMsg("消息内容不能为空").build());
    WxMpTemplateMessageBuilder templateMessage = WxMpTemplateMessage.builder();
    templateMessage.templateId(msgBean.getTemplateId());
    if (!StringHelper.isBlank(msgBean.getLinkUrl()))
        templateMessage.url(msgBean.getLinkUrl());
    List<WxMpTemplateData> data = new ArrayList<WxMpTemplateData>();
    if (StringHelper.isNotBlank(msgBean.getFirst()))
        data.add(new WxMpTemplateData("first", msgBean.getFirst(), "#173177"));
    if (StringHelper.isNotBlank(msgBean.getKeyword1()))
        data.add(new WxMpTemplateData("keyword1", msgBean.getKeyword1(), "#173177"));
    if (StringHelper.isNotBlank(msgBean.getKeyword2()))
        data.add(new WxMpTemplateData("keyword2", msgBean.getKeyword2(), "#173177"));
    if (StringHelper.isNotBlank(msgBean.getKeyword3()))
        data.add(new WxMpTemplateData("keyword3", msgBean.getKeyword3(), "#173177"));
    if (StringHelper.isNotBlank(msgBean.getKeyword4()))
        data.add(new WxMpTemplateData("keyword4", msgBean.getKeyword4(), "#173177"));
    if (StringHelper.isNotBlank(msgBean.getKeyword5()))
        data.add(new WxMpTemplateData("keyword5", msgBean.getKeyword5(), "#173177"));
    if (StringHelper.isNotBlank(msgBean.getRemark()))
        data.add(new WxMpTemplateData("remark", msgBean.getRemark(), "#173177"));
    templateMessage.data(data);
    WxMpTemplateMessage toMessage = null;
    List<String> toUsers = msgBean.getToUser();
    if (null != toUsers && toUsers.size() > 0) {
        for (String toUser : toUsers) {
            templateMessage.toUser(toUser.trim());
            toMessage = templateMessage.build();
            log.info("|send template msg start|" + toMessage.toString());
            String returnMsg = wxMpService.getTemplateMsgService().sendTemplateMsg(toMessage);
            log.info("|send template msg end  |" + returnMsg);
        }
    }
    return true;
}
Also used : WxMpTemplateData(me.chanjar.weixin.mp.bean.template.WxMpTemplateData) WxMpTemplateMessage(me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage) WxMpTemplateMessageBuilder(me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage.WxMpTemplateMessageBuilder) ArrayList(java.util.ArrayList) WxErrorException(me.chanjar.weixin.common.error.WxErrorException)

Example 4 with WxErrorException

use of me.chanjar.weixin.common.error.WxErrorException in project weixin-java-mp-demo-springboot by binarywang.

the class WxRedirectController method greetUser.

@RequestMapping("/greet")
public String greetUser(@PathVariable String appid, @RequestParam String code, ModelMap map) {
    if (!this.wxService.switchover(appid)) {
        throw new IllegalArgumentException(String.format("未找到对应appid=[%s]的配置,请核实!", appid));
    }
    try {
        WxOAuth2AccessToken accessToken = wxService.getOAuth2Service().getAccessToken(code);
        WxOAuth2UserInfo user = wxService.getOAuth2Service().getUserInfo(accessToken, null);
        map.put("user", user);
    } catch (WxErrorException e) {
        e.printStackTrace();
    }
    return "greet_user";
}
Also used : WxOAuth2AccessToken(me.chanjar.weixin.common.bean.oauth2.WxOAuth2AccessToken) WxOAuth2UserInfo(me.chanjar.weixin.common.bean.WxOAuth2UserInfo) WxErrorException(me.chanjar.weixin.common.error.WxErrorException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 5 with WxErrorException

use of me.chanjar.weixin.common.error.WxErrorException in project weixin-java-mp-multi-demo by binarywang.

the class SubscribeHandler method handle.

@Override
public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, Map<String, Object> context, WxMpService wxMpService, WxSessionManager sessionManager) throws WxErrorException {
    this.logger.info("新关注用户 OPENID: " + wxMessage.getFromUser());
    BaseWxService weixinService = (BaseWxService) wxMpService;
    // 获取微信用户基本信息
    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.binarywang.demo.wx.mp.builder.TextBuilder) WxMpUser(me.chanjar.weixin.mp.bean.result.WxMpUser) BaseWxService(com.github.binarywang.demo.wx.mp.service.BaseWxService) WxMpXmlOutMessage(me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage) WxErrorException(me.chanjar.weixin.common.error.WxErrorException)

Aggregations

WxErrorException (me.chanjar.weixin.common.error.WxErrorException)11 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)6 WxMpUser (me.chanjar.weixin.mp.bean.result.WxMpUser)4 HttpResult (com.github.liuweijw.commons.pay.beans.HttpResult)3 WxMpXmlOutMessage (me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage)3 WxMpOAuth2AccessToken (me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken)3 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)3 TextBuilder (com.github.binarywang.demo.wx.mp.builder.TextBuilder)2 WechatInfo (com.github.liuweijw.business.wechat.domain.WechatInfo)2 MsgBean (com.github.liuweijw.commons.pay.beans.MsgBean)2 BaseWxService (com.github.binarywang.demo.wx.mp.service.BaseWxService)1 UrlInfoBean (com.github.liuweijw.business.wechat.beans.UrlInfoBean)1 WechatNotifyBean (com.github.liuweijw.business.wechat.beans.WechatNotifyBean)1 TextBuilder (com.github.liuweijw.business.wechat.builder.TextBuilder)1 AuthInfo (com.github.liuweijw.business.wechat.domain.AuthInfo)1 R (com.github.liuweijw.commons.base.R)1 WxTemplateEnum (com.github.liuweijw.commons.pay.enums.WxTemplateEnum)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 WxJsapiSignature (me.chanjar.weixin.common.bean.WxJsapiSignature)1