|
@@ -34,6 +34,8 @@ import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
|
|
|
+import static com.management.platform.controller.WeiXinCorpController.GET_CORP_USERINFO_URL;
|
|
|
+
|
|
|
@Controller
|
|
|
public class AuthRedirectController {
|
|
|
@Resource
|
|
@@ -74,7 +76,7 @@ public class AuthRedirectController {
|
|
|
//获取设备类型
|
|
|
String deviceType = UserAgentUtils.getDeviceType(userAgent);
|
|
|
boolean isMobile = "MOBILE".equals(deviceType);
|
|
|
- String url = WeiXinCorpController.GET_CORP_USERINFO_URL.replace("SUITE_ACCESS_TOKEN", getSuiteAccessToken()).replace("CODE", code);
|
|
|
+ String url = GET_CORP_USERINFO_URL.replace("SUITE_ACCESS_TOKEN", getSuiteAccessToken()).replace("CODE", code);
|
|
|
String forObject = this.restTemplate.getForObject(url, String.class);
|
|
|
JSONObject obj = JSONObject.parseObject(forObject);
|
|
|
System.out.println(obj.toString());
|
|
@@ -166,42 +168,47 @@ public class AuthRedirectController {
|
|
|
return modelAndView;
|
|
|
}
|
|
|
|
|
|
+ @RequestMapping("/testGetProviderToken")
|
|
|
+ public HttpRespMsg testGetProviderToken() {
|
|
|
+ try {
|
|
|
+ String providerAccessToken = wxCorpInfoService.getNewProviderAccessToken();
|
|
|
+ System.out.println("providerAccessToken=="+providerAccessToken);
|
|
|
+ HttpRespMsg msg = new HttpRespMsg();
|
|
|
+ msg.setData(providerAccessToken);
|
|
|
+ return msg;
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ return new HttpRespMsg();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
@RequestMapping("/corpWXScanningAuth")
|
|
|
- public ModelAndView scanningAuth(String auth_code, String state)throws Exception {
|
|
|
+ public ModelAndView corpWXScanningAuth(String code, String state)throws Exception {
|
|
|
Map<String,Object> reqParam = new HashMap<String,Object>(16);
|
|
|
String userAgent = request.getHeader("User-Agent");
|
|
|
//获取设备类型
|
|
|
String deviceType = UserAgentUtils.getDeviceType(userAgent);
|
|
|
boolean isMobile = "MOBILE".equals(deviceType);
|
|
|
- String url = WeiXinCorpController.GET_CORP_SCANNING_CODE_LOGININFO_URL.replace("PROVIDER_ACCESS_TOKEN",wxCorpInfoService.getProviderAccessToken());
|
|
|
- HttpHeaders headers = new HttpHeaders();
|
|
|
- RestTemplate restTemplate = new RestTemplate();
|
|
|
- MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
|
|
|
- headers.setContentType(type);
|
|
|
- headers.add("Accept", MediaType.APPLICATION_JSON.toString());
|
|
|
- JSONObject ob = new JSONObject();
|
|
|
- ob.put("auth_code", auth_code);
|
|
|
- HttpEntity<JSONObject> Entity = new HttpEntity<>(ob, headers);
|
|
|
- ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, Entity, String.class);
|
|
|
+ String url = null;
|
|
|
String redirecUrl = null;
|
|
|
- if (responseEntity.getStatusCode() == HttpStatus.OK) {
|
|
|
- String resp = responseEntity.getBody();
|
|
|
- System.err.println(resp);
|
|
|
- JSONObject obj = JSONObject.parseObject(resp);
|
|
|
- if (obj.getIntValue("errcode") == 0) {
|
|
|
- JSONObject userInfo = obj.getJSONObject("user_info");
|
|
|
- System.out.println(obj.toString());
|
|
|
- String wxUserId = userInfo.getString("userid");
|
|
|
- String openUserId = userInfo.getString("open_userid");
|
|
|
- JSONObject corpInfo = obj.getJSONObject("corp_info");
|
|
|
- String corpId = corpInfo.getString("corpid");
|
|
|
- System.out.println("登录 wxUserId="+wxUserId+", openUserId="+openUserId);
|
|
|
- List<User> userList = userMapper.selectList(new QueryWrapper<User>().eq("corpwx_userid", openUserId));
|
|
|
- Integer companyId = 0;
|
|
|
+ try {
|
|
|
+ String suiteAccessToken = getNewSuiteAccessToken();
|
|
|
+ System.out.println("suiteAccessToken=="+suiteAccessToken);
|
|
|
+ url = GET_CORP_USERINFO_URL.replace("SUITE_ACCESS_TOKEN", suiteAccessToken).replace("CODE", code);
|
|
|
+ String forObject = this.restTemplate.getForObject(url, String.class);
|
|
|
+ JSONObject obj = JSONObject.parseObject(forObject);
|
|
|
+ System.out.println("获取授权的用户身份信息:"+obj.toString());
|
|
|
+ String openId = obj.getString("open_userid");
|
|
|
+ String corpId = obj.getString("corpid");
|
|
|
+ WxCorpInfo wxCorpInfo = wxCorpInfoMapper.selectById(corpId);
|
|
|
+ if (wxCorpInfo != null) {
|
|
|
+ User curUser = null;
|
|
|
+ Integer companyId = wxCorpInfo.getCompanyId();
|
|
|
+ List<User> userList = userMapper.selectList(new QueryWrapper<User>().eq("corpwx_userid", openId).eq("company_id", companyId));
|
|
|
if (userList.size() > 0) {
|
|
|
//该用户已存在
|
|
|
- User curUser = userList.get(0);
|
|
|
+ curUser = userList.get(0);
|
|
|
System.out.println("找到用户corpwxUserid=="+curUser.getCorpwxUserid());
|
|
|
companyId = curUser.getCompanyId();
|
|
|
if (curUser.getIsActive() == 1) {
|
|
@@ -212,34 +219,8 @@ public class AuthRedirectController {
|
|
|
reqParam.put("errorMsg", MessageUtils.message("user.inactive"));
|
|
|
}
|
|
|
} else {
|
|
|
- //使用UserId比对,之前有的老用户存的是UserId
|
|
|
- WxCorpInfo wxCorpInfo = wxCorpInfoMapper.selectById(corpId);
|
|
|
- if (wxCorpInfo == null) {
|
|
|
- reqParam.put("errorMsg", MessageUtils.message("user.accountNoExist"));
|
|
|
- } else {
|
|
|
- User curUser = userMapper.selectOne(new QueryWrapper<User>().eq("company_id", wxCorpInfo.getCompanyId()).eq("corpwx_userid", wxUserId));
|
|
|
- if (curUser == null) {
|
|
|
-// reqParam.put("errorMsg", MessageUtils.message("user.accountNoExist"));
|
|
|
- //用户不存在,去生成该用户
|
|
|
- if (wxCorpInfo.getSaasSyncContact() == 1) {
|
|
|
- curUser = wxCorpInfoService.generateUserInfo(wxCorpInfo.getCompanyId(), openUserId);
|
|
|
- }
|
|
|
- }
|
|
|
- if (curUser != null) {
|
|
|
- if (curUser.getIsActive() == 1) {
|
|
|
- companyId = curUser.getCompanyId();
|
|
|
- reqParam.put("userId", curUser.getId());
|
|
|
- } else {
|
|
|
- //提示账号已停用
|
|
|
- //reqParam.put("errorMsg", "您的账号已停用,无法登录");
|
|
|
- reqParam.put("errorMsg", MessageUtils.message("user.inactive"));
|
|
|
- }
|
|
|
- } else {
|
|
|
- System.err.println("==生成企业微信User失败==");
|
|
|
- reqParam.put("errorMsg", "尚未绑定企业微信,请使用账号密码登录。");
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
+ curUser = wxCorpInfoService.generateUserInfo(wxCorpInfo.getCompanyId(), openId);
|
|
|
+ reqParam.put("userId", curUser.getId());
|
|
|
}
|
|
|
if (!StringUtils.isEmpty(state) && state.length() > 1) {
|
|
|
reqParam.put("path", state);
|
|
@@ -255,11 +236,15 @@ public class AuthRedirectController {
|
|
|
}
|
|
|
}
|
|
|
redirecUrl = "https://crm.ttkuaiban.com/#/" + router;
|
|
|
+ } else {
|
|
|
+ reqParam.put("errorMsg", MessageUtils.message("access.grant"));
|
|
|
}
|
|
|
+ } catch (Exception exception) {
|
|
|
+ exception.printStackTrace();
|
|
|
+ reqParam.put("errorMsg",exception.getMessage());
|
|
|
}
|
|
|
ModelAndView modelAndView = new ModelAndView(
|
|
|
new RedirectView(redirecUrl), reqParam);
|
|
|
-
|
|
|
return modelAndView;
|
|
|
}
|
|
|
|
|
@@ -435,7 +420,7 @@ public class AuthRedirectController {
|
|
|
String resp = responseEntity.getBody();
|
|
|
System.out.println("获取企业微信suitAccessToken返回"+resp);
|
|
|
JSONObject obj = JSONObject.parseObject(resp);
|
|
|
- if (obj.getIntValue("errcode") == 0) {
|
|
|
+ if (obj.getIntValue("errcode") == 0 || obj.getString("suite_access_token") != null) {
|
|
|
WeiXinCorpController.SUITE_ACCESS_TOKEN = obj.getString("suite_access_token");
|
|
|
WeiXinCorpController.suiteTokenExpireTime = System.currentTimeMillis() + obj.getIntValue("expires_in")*1000;
|
|
|
}
|
|
@@ -444,6 +429,32 @@ public class AuthRedirectController {
|
|
|
return WeiXinCorpController.SUITE_ACCESS_TOKEN;
|
|
|
}
|
|
|
|
|
|
+ private String getNewSuiteAccessToken() {
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
+ headers.setContentType(MediaType.APPLICATION_JSON);
|
|
|
+ JSONObject reqParam = new JSONObject();
|
|
|
+ reqParam.put("suite_id", "ww19f9868980b9bb43");
|
|
|
+ reqParam.put("suite_secret", "OeOqoMPGJH2X3k5j_wltO54q9mnXqKoJx6GkCLmOXdQ");
|
|
|
+ SysConfig param = sysConfigMapper.selectOne(new QueryWrapper<SysConfig>().eq("param_key", "wx_suite_ticket"));
|
|
|
+ if (param != null) {
|
|
|
+ reqParam.put("suite_ticket",param.getParamValue());
|
|
|
+ }
|
|
|
+
|
|
|
+ HttpEntity<String> requestEntity = new HttpEntity<String>(reqParam.toJSONString(), headers);
|
|
|
+ ResponseEntity<String> responseEntity = this.restTemplate.exchange(WeiXinCorpController.GET_SUITE_ACCESS_TOKEN_URL,
|
|
|
+ HttpMethod.POST, requestEntity, String.class);
|
|
|
+ if (responseEntity.getStatusCode() == HttpStatus.OK) {
|
|
|
+ String resp = responseEntity.getBody();
|
|
|
+ System.out.println("获取企业微信suitAccessToken返回"+resp);
|
|
|
+ JSONObject obj = JSONObject.parseObject(resp);
|
|
|
+ if (obj.getIntValue("errcode") == 0 || obj.getString("suite_access_token") != null) {
|
|
|
+ return obj.getString("suite_access_token");
|
|
|
+// WeiXinCorpController.suiteTokenExpireTime = System.currentTimeMillis() + obj.getIntValue("expires_in")*1000;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
//获取企业通讯录的accessToken,在私有化部署的企业内部服务器上跑
|
|
|
private String getCorpConcactAccessToken(WxCorpInfo corpInfo) throws Exception {
|