Skip to content

Instantly share code, notes, and snippets.

@greenyleaf
Created June 19, 2020 13:27
Show Gist options
  • Save greenyleaf/b42352ce935d56cfa0833f2581540c73 to your computer and use it in GitHub Desktop.
Save greenyleaf/b42352ce935d56cfa0833f2581540c73 to your computer and use it in GitHub Desktop.
a Spring service for Weixin(wechat) oauth2
package top.sdrkyj.custom.service;
import com.alibaba.fastjson.JSON;
import top.sdrkyj.custom.dao.weixin.WxUserInfoDao;
import top.sdrkyj.custom.entity.weixin.WxAccessToken;
import top.sdrkyj.custom.entity.weixin.WxUserInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.client.WebClient;
@Service
public class WxService {
private static final Logger logger = LoggerFactory.getLogger(WxService.class);
private final String appid;
private final String secret;
private final WebClient webClient;
private final WxUserInfoDao wxUserInfoDao;
public WxService(@Value("${weixin.appid}") String appid, @Value("${weixin.secret}") String secret, WxUserInfoDao wxUserInfoDao) {
this.appid = appid;
this.secret = secret;
this.wxUserInfoDao = wxUserInfoDao;
webClient = WebClient.builder().baseUrl("https://api.weixin.qq.com").build();
}
public WxUserInfo doGetAuthenticationInfo(String code) {
logger.debug("doGetAuthenticationInfo entered");
BodyInserters.FormInserter<String> inserter = BodyInserters.fromFormData("appid", appid)
.with("secret", secret)
.with("code", code)
.with("grant_type", "authorization_code");
String block = webClient
.post()
.uri("/sns/oauth2/access_token")
.body(inserter)
.retrieve().bodyToMono(String.class)
.block();
logger.debug("doGetAuthenticationInfo stage 1");
if (block == null) {
logger.warn("获取访问令牌时遇到 null 返回");
return null;
}
WxAccessToken token = JSON.parseObject(block, WxAccessToken.class);
logger.debug("doGetAuthenticationInfo stage 2");
if (token.getErrcode() != null) {
logger.warn("获取访问令牌时遇到错误, {}, {}", token.getErrcode(), token.getErrmsg());
return null;
}
inserter = BodyInserters
.fromFormData("access_token", token.getAccessToken())
.with("openid", token.getOpenid())
.with("lang", "zh_CN");
block = webClient
.post()
.uri("/sns/userinfo")
.body(inserter)
.retrieve().bodyToMono(String.class)
.block();
logger.debug("doGetAuthenticationInfo stage 3");
if (block == null) {
logger.warn("获取用户信息时遇到 null 返回");
return null;
}
WxUserInfo userInfo = JSON.parseObject(block, WxUserInfo.class);
logger.debug("doGetAuthenticationInfo stage 4");
if (userInfo.getErrcode() != null) {
logger.warn("获取用户信息时遇到错误, {}, {}", userInfo.getErrcode(), userInfo.getErrmsg());
return null;
}
if (!wxUserInfoDao.createIgnore(userInfo)) {
logger.debug("doGetAuthenticationInfo stage 5");
wxUserInfoDao.update(userInfo);
}
logger.debug("doGetAuthenticationInfo stage 6");
logger.debug("doGetAuthenticationInfo exit");
return userInfo;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment