Search in sources :

Example 1 with WechatNotifyBean

use of com.github.liuweijw.business.wechat.beans.WechatNotifyBean in project fw-cloud-framework by liuweijw.

the class WxAuthorizeController method openId.

// 如果用户同意授权,页面将跳转至 redirect_uri/?code=CODE&state=STATE
@RequestMapping(value = "/openId", method = RequestMethod.GET)
public String openId(HttpServletRequest request, @RequestParam("code") String code, @RequestParam("state") String state, @RequestParam("from") Integer from, @RequestParam("wechatId") String wechatId, @RequestParam("t") Long t) {
    long start = System.currentTimeMillis();
    String openId = "";
    try {
        boolean isSopeBase = from.intValue() == 1;
        WxMpOAuth2AccessToken wxMpOAuth2AccessToken = wxService.oauth2getAccessToken(code);
        openId = wxMpOAuth2AccessToken.getOpenId();
        log.info("【wxauth.openId】:state|" + state);
        // 采用异步方式拉取用户信息
        // taskExecutor.execute(() -> {})
        long mqStart = System.currentTimeMillis();
        log.info("【wxauth.openId】发送MQ:" + mqStart);
        WechatNotifyBean wechatNotifyBean = new WechatNotifyBean();
        wechatNotifyBean.setSopeBase(isSopeBase);
        wechatNotifyBean.setWechatId(wechatId);
        wechatNotifyBean.setWxMpOAuth2AccessToken(wxMpOAuth2AccessToken);
        rabbitTemplate.convertAndSend(MqQueueConstant.WECHAT_QUEUE, wechatNotifyBean);
        log.info("【wxauth.openId】发送MQ耗时:" + (System.currentTimeMillis() - mqStart));
        log.info("【wxauth.openId】:openId|" + openId);
    } catch (WxErrorException ex) {
        ex.printStackTrace();
        log.info("【wxauth.openId】exception:" + ex.getError().getErrorMsg());
    }
    String returnUrl = "";
    if (state.length() == 32 && !state.startsWith("http")) {
        // key
        UrlInfoBean urlInfoBean = urlInfoService.findFromCacheByUuid(state);
        returnUrl = urlInfoBean.getUrl();
    } else {
        returnUrl = state;
    }
    String redirectUrl = RequestUtil.buildAppendURLParams(RequestUtil.buildURLParams(returnUrl, OPENID), OPENID + "=" + openId, "t=" + t);
    log.info("【wxauth.openId】:redirect|" + redirectUrl);
    long end = System.currentTimeMillis();
    log.info("【wxauth.openId】耗时:" + (end - start));
    log.info("【wxauth.authorize】请求从第三方应用到跳转授权开始[" + t + "],耗时:" + (end - t));
    return "redirect:" + redirectUrl;
}
Also used : WxMpOAuth2AccessToken(me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken) WechatNotifyBean(com.github.liuweijw.business.wechat.beans.WechatNotifyBean) UrlInfoBean(com.github.liuweijw.business.wechat.beans.UrlInfoBean) WxErrorException(me.chanjar.weixin.common.error.WxErrorException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

UrlInfoBean (com.github.liuweijw.business.wechat.beans.UrlInfoBean)1 WechatNotifyBean (com.github.liuweijw.business.wechat.beans.WechatNotifyBean)1 WxErrorException (me.chanjar.weixin.common.error.WxErrorException)1 WxMpOAuth2AccessToken (me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1