com.zopen.wechat.work.action.AtoWechatWorkAction 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.action;
import com.zcj.util.UtilString;
import com.zcj.util.UtilUri;
import com.zopen.ato.properties.WechatWorkProperties;
import com.zopen.wechat.exception.WechatException;
import com.zopen.wechat.work.dto.user.UserInfo;
import com.zopen.wechat.work.service.Oauth2WorkInterface;
import com.zopen.wechat.work.service.WechatWorkHttpUrl;
import com.zopen.wechat.work.service.WechatWorkUserService;
import com.zopen.wechat.work.task.WechatWorkInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import java.util.Objects;
@Controller
@RequestMapping("/ato/wechat/work")
public class AtoWechatWorkAction {
private static final Logger logger = LoggerFactory.getLogger(AtoWechatWorkAction.class);
@Autowired
private WechatWorkProperties wechatWorkProperties;
@Autowired
private WechatWorkUserService wechatWorkUserService;
@Autowired(required = false)
private Oauth2WorkInterface oauth2WorkInterface;
// 企业微信网页授权登录
@RequestMapping("/login")
public String login(HttpServletRequest request, String agentId, String page, String param1, String param2) {
if (UtilString.isBlank(agentId)) {
logger.warn("企业微信授权登录的地址中缺少 agentId 参数,无法执行登录跳转");
throw new WechatException("缺少 agentId 参数");
}
String corpId = WechatWorkInfo.getCorpId(agentId);
if (UtilString.isBlank(corpId)) {
logger.warn("通过 agentId 获取 corpId 失败,可能是 agentId 不存在:agentId[{}]", agentId);
throw new WechatException("agentId 错误");
}
String redirectUri = wechatWorkProperties.getDomainUrl() + "/ato/wechat/work/callback";
redirectUri = UtilUri.urlAddParam(redirectUri, "agentId", agentId);
redirectUri = UtilUri.urlAddParam(redirectUri, "page", UtilUri.urlEncode(page));
redirectUri = UtilUri.urlAddParam(redirectUri, "param1", UtilUri.urlEncode(param1));
redirectUri = UtilUri.urlAddParam(redirectUri, "param2", UtilUri.urlEncode(param2));
redirectUri = UtilUri.urlEncode(redirectUri);
String state = oauth2WorkInterface.setState(request, page, agentId);
state = Objects.toString(state, "");
String callbackUrl = String.format(WechatWorkHttpUrl.OAUTH2_AUTHORIZE_BASE, corpId, redirectUri, state);
logger.debug("企业微信菜单进去后的跳转地址:[{}]", callbackUrl);
return "redirect:" + callbackUrl;
}
@RequestMapping("/callback")
public String callback(HttpServletRequest request, String agentId, String page, String param1, String param2) {
if (UtilString.isBlank(agentId)) {
logger.warn("企业微信授权登录的地址中缺少 agentId 参数,无法执行登录跳转");
throw new WechatException("agentId 参数缺失");
}
String accessToken = WechatWorkInfo.getAccessToken(agentId);
if (UtilString.isBlank(accessToken)) {
logger.warn("通过 agentId 获取 accessToken 失败,无法执行登录跳转:agentId[{}]", agentId);
throw new WechatException("agentId 错误");
}
String code = request.getParameter("code");
String state = request.getParameter("state");
oauth2WorkInterface.validState(request, page, agentId, state);
// 获取用户信息
UserInfo userInfo = wechatWorkUserService.getUserInfo(accessToken, code);
// 获取跳转地址
String redirectPath = oauth2WorkInterface.redirect(userInfo.getUserId(), userInfo.getOpenId(), agentId, page, param1, param2);
if (UtilString.isNotBlank(redirectPath)) {
logger.debug("企业微信菜单回调后的请求地址:[{}]", redirectPath);
return "redirect:" + redirectPath;
}
throw new WechatException("未配置 redirect 地址,跳转失败");
}
}