Search in sources :

Example 1 with AuthInfo

use of com.github.liuweijw.business.wechat.domain.AuthInfo in project fw-cloud-framework by liuweijw.

the class WxInfoController method getWechatInfoByOpenId.

@RequestMapping(value = "/{wechatId}/{openId}")
public HttpResult getWechatInfoByOpenId(@PathVariable("wechatId") String wechatId, @PathVariable("openId") String openId) {
    WechatInfo wechatInfo = wechatInfoService.findByWechatId(wechatId);
    if (null == wechatInfo) {
        log.error("公众号wechatId[" + wechatId + "]不存在");
        return new HttpResult().failure("公众号wechatId[" + wechatId + "]不存在");
    }
    AuthInfo authInfo = authInfoService.findByOpenIdAndWechatId(openId, wechatId);
    if (null == authInfo) {
        log.error("公众号openId[" + openId + "]不存在");
        return new HttpResult().failure("公众号openId[" + openId + "]不存在");
    }
    return new HttpResult().data(authInfo).success();
}
Also used : AuthInfo(com.github.liuweijw.business.wechat.domain.AuthInfo) WechatInfo(com.github.liuweijw.business.wechat.domain.WechatInfo) HttpResult(com.github.liuweijw.commons.pay.beans.HttpResult) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with AuthInfo

use of com.github.liuweijw.business.wechat.domain.AuthInfo 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

AuthInfo (com.github.liuweijw.business.wechat.domain.AuthInfo)2 WechatInfo (com.github.liuweijw.business.wechat.domain.WechatInfo)1 HttpResult (com.github.liuweijw.commons.pay.beans.HttpResult)1 Date (java.util.Date)1 WxErrorException (me.chanjar.weixin.common.error.WxErrorException)1 WxMpOAuth2AccessToken (me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken)1 WxMpUser (me.chanjar.weixin.mp.bean.result.WxMpUser)1 RabbitHandler (org.springframework.amqp.rabbit.annotation.RabbitHandler)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1