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();
}
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));
}
Aggregations