Search in sources :

Example 11 with WxErrorException

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

the class WechatRabbitListener method receive.

@RabbitHandler
public void receive(WechatNotifyBean wechatNotifyBean) {
    long start = System.currentTimeMillis();
    log.info("【wxauth.wechatRabbit】:exec receive start|" + start);
    WxMpOAuth2AccessToken wxMpOAuth2AccessToken = wechatNotifyBean.getWxMpOAuth2AccessToken();
    if (null == wxMpOAuth2AccessToken || StringHelper.isBlank(wechatNotifyBean.getWechatId()) || StringHelper.isBlank(wxMpOAuth2AccessToken.getOpenId()))
        return;
    log.info("【wxauth.wechatRabbit】:exec start");
    try {
        WxMpUser wxMpUser = null;
        String openId = wxMpOAuth2AccessToken.getOpenId();
        AuthInfo authInfo = authInfoService.findByOpenIdAndWechatId(openId, wechatNotifyBean.getWechatId());
        if (// 大于2小时
        null == authInfo || System.currentTimeMillis() - authInfo.getUpdateTime().getTime() > 7200000) {
            boolean isSopeBase = wechatNotifyBean.isSopeBase();
            if (isSopeBase) {
                log.info("【wxauth.openId】静默登录");
                wxMpUser = wxService.getUserService().userInfo(openId);
            } else {
                // refresh_token有效期为30天,当refresh_token失效之后,需要用户重新授权。
                String refreshToken = wxMpOAuth2AccessToken.getRefreshToken();
                wxMpOAuth2AccessToken = wxService.oauth2refreshAccessToken(refreshToken);
                log.info("【wxauth.openId】主动登录");
                // 拉取用户信息(需scope为 snsapi_userinfo)
                wxMpUser = wxService.oauth2getUserInfo(wxMpOAuth2AccessToken, null);
            }
            if (null != wxMpUser) {
                if (null == authInfo)
                    authInfo = new AuthInfo();
                authInfo.setOpenId(openId);
                authInfo.setWechatId(wechatNotifyBean.getWechatId());
                authInfo.setNickName(WebUtils.buildURLEncoder(EmojiUtils.toHtml(wxMpUser.getNickname())));
                authInfo.setHeadImgUrl(wxMpUser.getHeadImgUrl());
                authInfo.setCity(wxMpUser.getCity());
                authInfo.setProvince(wxMpUser.getProvince());
                authInfo.setLanguage(wxMpUser.getLanguage());
                authInfo.setRemark(wxMpUser.getRemark());
                authInfo.setSexDesc(wxMpUser.getSexDesc());
                authInfo.setSex(wxMpUser.getSex());
                authInfo.setCountry(wxMpUser.getCountry());
                authInfo.setRefreshToken(wxMpOAuth2AccessToken.getRefreshToken());
                if (null == authInfo.getCreateTime())
                    authInfo.setCreateTime(new Date());
                authInfo.setUpdateTime(new Date());
                authInfoService.saveOrUpdate(authInfo);
            }
        }
        log.info("【wxauth.wechatRabbit】:openId|" + openId);
    } catch (WxErrorException ex) {
        ex.printStackTrace();
        log.info("【wxauth.wechatRabbit】exception:" + ex.getError().getErrorMsg());
    }
    log.info("【wxauth.wechatRabbit】:exec finished 耗时:" + (System.currentTimeMillis() - start));
}
Also used : WxMpOAuth2AccessToken(me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken) AuthInfo(com.github.liuweijw.business.wechat.domain.AuthInfo) WxMpUser(me.chanjar.weixin.mp.bean.result.WxMpUser) Date(java.util.Date) WxErrorException(me.chanjar.weixin.common.error.WxErrorException) RabbitHandler(org.springframework.amqp.rabbit.annotation.RabbitHandler)

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