com.zopen.wechat.work.service.WechatWorkUserService Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of zopen-ato-starter Show documentation
Show all versions of zopen-ato-starter Show documentation
Alibaba Tencent And Others For Spring Boot.
package com.zopen.wechat.work.service;
import com.google.gson.Gson;
import com.zopen.wechat.exception.WechatAssert;
import com.zopen.wechat.work.dto.user.UserInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
@Component("atoWechatWorkUserService")
public class WechatWorkUserService {
private static final Logger logger = LoggerFactory.getLogger(WechatWorkUserService.class);
@Autowired
private RestTemplate restTemplate;
// 通过 code 获取 用户信息
public UserInfo getUserInfo(String accessToken, String code) {
WechatAssert.notNullAndEmpty(accessToken, "accessToken 不能为空");
WechatAssert.notNullAndEmpty(code, "code 不能为空");
String url = String.format(WechatWorkHttpUrl.WORK_GET_USER_INFO, accessToken, code);
String userInfoRespStr = restTemplate.getForObject(url, String.class);
WechatAssert.notNullAndEmpty(userInfoRespStr, "获取访问用户身份失败:接口返回的内容为空");
logger.debug("获取访问用户身份成功:[{}]", userInfoRespStr);
UserInfo userInfo = new Gson().fromJson(userInfoRespStr, UserInfo.class);
WechatAssert.notNull(userInfo, "企业微信获取访问用户身份失败");
userInfo.valid("获取访问用户身份失败");
logger.debug("解析访问用户身份成功:UserId[{}] OpenId[{}] DeviceId[{}]", userInfo.getUserId(), userInfo.getOpenId(), userInfo.getDeviceId());
return userInfo;
}
}