|
@@ -0,0 +1,471 @@
|
|
|
+package com.management.platform.controller;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
+import com.management.platform.entity.*;
|
|
|
+import com.management.platform.entity.vo.UserVO;
|
|
|
+import com.management.platform.mapper.*;
|
|
|
+import com.management.platform.service.FeishuInfoService;
|
|
|
+import com.management.platform.service.UserService;
|
|
|
+import com.management.platform.service.WxCorpInfoService;
|
|
|
+import com.management.platform.service.impl.FeishuInfoServiceImpl;
|
|
|
+import com.management.platform.util.HttpRespMsg;
|
|
|
+import com.management.platform.util.MessageUtils;
|
|
|
+import com.management.platform.util.UserAgentUtils;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.springframework.beans.BeanUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.http.*;
|
|
|
+import org.springframework.stereotype.Controller;
|
|
|
+import org.springframework.web.bind.annotation.GetMapping;
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
+import org.springframework.web.bind.annotation.ResponseBody;
|
|
|
+import org.springframework.web.client.RestTemplate;
|
|
|
+import org.springframework.web.servlet.ModelAndView;
|
|
|
+import org.springframework.web.servlet.view.RedirectView;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.time.ZoneOffset;
|
|
|
+import java.time.format.DateTimeFormatter;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+@Controller
|
|
|
+public class AuthRedirectController {
|
|
|
+ @Resource
|
|
|
+ HttpServletRequest request;
|
|
|
+ @Value("${suitId}")
|
|
|
+ private String suitId;
|
|
|
+ @Value("${suitSecret}")
|
|
|
+ private String suitSecret;
|
|
|
+ @Value("${privateDeployURL.pcUrl}")
|
|
|
+ private String pcUrl;
|
|
|
+ @Value("${privateDeployURL.mobUrl}")
|
|
|
+ private String mobUrl;
|
|
|
+ @Value("${corpId}")
|
|
|
+ private String corpId;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ SysConfigMapper sysConfigMapper;
|
|
|
+ @Autowired
|
|
|
+ RestTemplate restTemplate;
|
|
|
+ @Resource
|
|
|
+ UserMapper userMapper;
|
|
|
+ @Resource
|
|
|
+ WxCorpInfoMapper wxCorpInfoMapper;
|
|
|
+ @Resource
|
|
|
+ CompanyMapper companyMapper;
|
|
|
+ @Resource
|
|
|
+ WxCorpInfoService wxCorpInfoService;
|
|
|
+ @Resource
|
|
|
+ FeishuInfoServiceImpl feishuInfoService;
|
|
|
+ @Resource
|
|
|
+ UserLoginInfoMapper userLoginInfoMapper;
|
|
|
+
|
|
|
+
|
|
|
+ @RequestMapping("/corpWXAuth")
|
|
|
+ public ModelAndView auth(String code, String state) {
|
|
|
+ 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_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());
|
|
|
+ String wxUserId = obj.getString("UserId");
|
|
|
+ String openUserId = obj.getString("open_userid");
|
|
|
+ String corpId = obj.getString("CorpId");
|
|
|
+
|
|
|
+ System.out.println("登录 wxUserId="+wxUserId+", openUserId="+openUserId);
|
|
|
+ List<User> userList = userMapper.selectList(new QueryWrapper<User>().eq("corpwx_userid", openUserId));
|
|
|
+ Integer companyId = 0;
|
|
|
+ if (userList.size() > 0) {
|
|
|
+ //该用户已存在
|
|
|
+ User curUser = userList.get(0);
|
|
|
+ System.out.println("找到用户corpwxUserid=="+curUser.getCorpwxUserid());
|
|
|
+ companyId = curUser.getCompanyId();
|
|
|
+ if (curUser.getIsActive() == 1) {
|
|
|
+ reqParam.put("userId", curUser.getId());
|
|
|
+ } else {
|
|
|
+ //提示账号已停用
|
|
|
+ //reqParam.put("errorMsg", "您的账号已停用,无法登录");
|
|
|
+ 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", "尚未绑定企业微信,请使用账号密码登录。");
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ reqParam.put("hasTriedAutoLogin", 1);
|
|
|
+ if (!StringUtils.isEmpty(state) && state.length() > 1) {
|
|
|
+ reqParam.put("path", state);
|
|
|
+ }
|
|
|
+
|
|
|
+ String redirecUrl = null;
|
|
|
+ String router = "index";
|
|
|
+ if (companyId > 0) {
|
|
|
+ HashMap compExpireInfo = getCompExpireInfo(companyId);
|
|
|
+ if (compExpireInfo != null) {
|
|
|
+ //过期了
|
|
|
+ router = "expire";
|
|
|
+ reqParam.put("expDate", compExpireInfo.get("expDate"));
|
|
|
+ reqParam.put("version", compExpireInfo.get("version"));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (isMobile) {
|
|
|
+ redirecUrl = "https://mobworktime.ttkuaiban.com/#/" + router;
|
|
|
+ } else {
|
|
|
+ redirecUrl = "https://worktime.ttkuaiban.com/#/" + router;
|
|
|
+ }
|
|
|
+ //定制化风格
|
|
|
+ if (corpId.equals("wpy9TkCAAAvjmvQuz0IH9iosxBXrqcdA")) {
|
|
|
+ //中辰华典
|
|
|
+ reqParam.put("style", "new");
|
|
|
+ }
|
|
|
+ ModelAndView modelAndView = new ModelAndView(
|
|
|
+ new RedirectView(redirecUrl), reqParam);
|
|
|
+
|
|
|
+ //存储该公司第一次登录所使用的设备
|
|
|
+ if (!reqParam.containsKey("errorMsg")){
|
|
|
+ Integer company = userLoginInfoMapper.selectCount(new QueryWrapper<UserLoginInfo>().eq("company_id",companyId));
|
|
|
+ if (company.equals(0)){
|
|
|
+ UserLoginInfo userLoginInfo = new UserLoginInfo();
|
|
|
+ userLoginInfo.setCompanyId(companyId);
|
|
|
+ userLoginInfo.setFirstLoginDevice(deviceType);
|
|
|
+ userLoginInfo.setWxUserId(wxUserId);
|
|
|
+ userLoginInfoMapper.insert(userLoginInfo);
|
|
|
+ System.err.println(userLoginInfo.toString());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return modelAndView;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @RequestMapping("/corpWXScanningAuth")
|
|
|
+ public ModelAndView scanningAuth(String auth_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 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;
|
|
|
+ if (userList.size() > 0) {
|
|
|
+ //该用户已存在
|
|
|
+ User curUser = userList.get(0);
|
|
|
+ System.out.println("找到用户corpwxUserid=="+curUser.getCorpwxUserid());
|
|
|
+ companyId = curUser.getCompanyId();
|
|
|
+ if (curUser.getIsActive() == 1) {
|
|
|
+ reqParam.put("userId", curUser.getId());
|
|
|
+ } else {
|
|
|
+ //提示账号已停用
|
|
|
+ //reqParam.put("errorMsg", "您的账号已停用,无法登录");
|
|
|
+ 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", "尚未绑定企业微信,请使用账号密码登录。");
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (!StringUtils.isEmpty(state) && state.length() > 1) {
|
|
|
+ reqParam.put("path", state);
|
|
|
+ }
|
|
|
+ String router = "login";
|
|
|
+ if (companyId > 0) {
|
|
|
+ HashMap compExpireInfo = getCompExpireInfo(companyId);
|
|
|
+ if (compExpireInfo != null) {
|
|
|
+ //过期了
|
|
|
+ router = "expire";
|
|
|
+ reqParam.put("expDate", compExpireInfo.get("expDate"));
|
|
|
+ reqParam.put("version", compExpireInfo.get("version"));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ redirecUrl = "https://worktime.ttkuaiban.com/#/" + router;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ ModelAndView modelAndView = new ModelAndView(
|
|
|
+ new RedirectView(redirecUrl), reqParam);
|
|
|
+
|
|
|
+ return modelAndView;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ //企业内部应用登录接口
|
|
|
+ @RequestMapping("/corpInsideWXAuth")
|
|
|
+ public ModelAndView authInside(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);
|
|
|
+ WxCorpInfo corpInfo = wxCorpInfoMapper.selectById(corpId);
|
|
|
+ String url = WeiXinCorpController.GET_CORP_INSIDE_USERINFO_URL.replace("ACCESS_TOKEN",getCorpConcactAccessToken(corpInfo)).replace("CODE", code);
|
|
|
+ String forObject = this.restTemplate.getForObject(url, String.class);
|
|
|
+ JSONObject obj = JSONObject.parseObject(forObject);
|
|
|
+ String wxUserId = obj.getString("userid");
|
|
|
+// String openUserId = obj.getString("open_userid");
|
|
|
+// String corpId = obj.getString("CorpId");
|
|
|
+
|
|
|
+ System.out.println("wxUserId="+wxUserId);
|
|
|
+ System.out.println(obj.toString());
|
|
|
+ List<User> userList = userMapper.selectList(new QueryWrapper<User>().eq("corpwx_userid", wxUserId).eq("company_id", corpInfo.getCompanyId()));
|
|
|
+ Integer companyId = 0;
|
|
|
+ if (userList.size() > 0) {
|
|
|
+ //该用户已存在
|
|
|
+ User curUser = userList.get(0);
|
|
|
+ companyId = curUser.getCompanyId();
|
|
|
+ if (curUser.getIsActive() == 1) {
|
|
|
+ reqParam.put("userId", curUser.getId());
|
|
|
+ } else {
|
|
|
+ //提示账号已停用
|
|
|
+ //reqParam.put("errorMsg", "您的账号已停用,无法登录");
|
|
|
+ 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 (curUser != null) {
|
|
|
+ if (curUser.getIsActive() == 1) {
|
|
|
+ companyId = curUser.getCompanyId();
|
|
|
+ reqParam.put("userId", curUser.getId());
|
|
|
+ } else {
|
|
|
+ //提示账号已停用
|
|
|
+ reqParam.put("errorMsg", MessageUtils.message("user.inactive"));
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ reqParam.put("errorMsg", MessageUtils.message("user.accountNoExist"));
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ reqParam.put("hasTriedAutoLogin", 1);
|
|
|
+ if (!StringUtils.isEmpty(state) && state.length() > 1) {
|
|
|
+ reqParam.put("path", state);
|
|
|
+ }
|
|
|
+
|
|
|
+ String redirecUrl = null;
|
|
|
+ String router = "login";
|
|
|
+ if (companyId > 0) {
|
|
|
+ HashMap compExpireInfo = getCompExpireInfo(companyId);
|
|
|
+ if (compExpireInfo != null) {
|
|
|
+ //过期了
|
|
|
+ router = "expire";
|
|
|
+ reqParam.put("expDate", compExpireInfo.get("expDate"));
|
|
|
+ reqParam.put("version", compExpireInfo.get("version"));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (isMobile) {
|
|
|
+ redirecUrl = mobUrl + router;
|
|
|
+ } else {
|
|
|
+ redirecUrl = pcUrl + router;
|
|
|
+ }
|
|
|
+ ModelAndView modelAndView = new ModelAndView(
|
|
|
+ new RedirectView(redirecUrl), reqParam);
|
|
|
+ reqParam.put("isPrivateCorpWX", 1);
|
|
|
+ if (reqParam.containsKey("errorMsg")) {
|
|
|
+ System.out.println(reqParam.get("errorMsg"));
|
|
|
+ }
|
|
|
+ return modelAndView;
|
|
|
+ }
|
|
|
+
|
|
|
+ private HashMap getCompExpireInfo(Integer companyId) {
|
|
|
+ Company company = companyMapper.selectById(companyId);
|
|
|
+ int version = 1;
|
|
|
+ if (company.getPackageProject() == 1) {
|
|
|
+ version = 2;
|
|
|
+ }
|
|
|
+ if (company.getPackageOa() == 1) {
|
|
|
+ version = 3;
|
|
|
+ }
|
|
|
+ if (company.getPackageEngineering() == 1) {
|
|
|
+ version = 4;
|
|
|
+ }
|
|
|
+ boolean hasExp = false;
|
|
|
+ LocalDateTime expirationDate = company.getExpirationDate();
|
|
|
+ DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
|
|
|
+ if (null != company.getExpirationDate()) {
|
|
|
+ if (expirationDate.isBefore(LocalDateTime.now())) {
|
|
|
+ hasExp = true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (hasExp) {
|
|
|
+ String format = dtf.format(expirationDate);
|
|
|
+ HashMap map = new HashMap();
|
|
|
+ map.put("version", version);
|
|
|
+ map.put("expDate", format);
|
|
|
+ return map;
|
|
|
+ } else {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @GetMapping("testClient")
|
|
|
+ @ResponseBody
|
|
|
+ public String test(HttpServletRequest request) {
|
|
|
+ String userAgent = request.getHeader("user-agent");
|
|
|
+ System.out.println("agent: " + userAgent);
|
|
|
+ if (userAgent.toLowerCase().contains("micromessenger")) {
|
|
|
+ System.out.println("===微信平台");
|
|
|
+ } else if (userAgent.toLowerCase().contains("wxwork")) {
|
|
|
+ System.out.println("===企业微信平台");
|
|
|
+ }
|
|
|
+ System.out.println("浏览器组:" + UserAgentUtils.getBorderGroup(userAgent));
|
|
|
+ System.out.println("浏览器名字:" + UserAgentUtils.getBorderName(userAgent));
|
|
|
+ System.out.println("浏览器类型" + UserAgentUtils.getBorderType(userAgent));
|
|
|
+ System.out.println("浏览器生产商:" + UserAgentUtils.getBrowserManufacturer(userAgent));
|
|
|
+ System.out.println("浏览器版本:" + UserAgentUtils.getBrowserVersion(userAgent));
|
|
|
+ System.out.println("设备生产厂商:" + UserAgentUtils.getDeviceManufacturer(userAgent));
|
|
|
+ System.out.println("设备类型:" + UserAgentUtils.getDeviceType(userAgent));
|
|
|
+ System.out.println("设备操作系统:" + UserAgentUtils.getOs(userAgent));
|
|
|
+ System.out.println("操作系统的名字:" + UserAgentUtils.getOsName(userAgent));
|
|
|
+ System.out.println("操作系统的版本号:" + UserAgentUtils.getOsVersion(userAgent));
|
|
|
+ System.out.println("操作系统浏览器的渲染引擎:" + UserAgentUtils.getBorderRenderingEngine(userAgent));
|
|
|
+ String os = UserAgentUtils.getOs(userAgent);
|
|
|
+ if (os.contains("Windows")) {
|
|
|
+ System.out.println("是Windows");
|
|
|
+ } else if (os.contains("Linux")) {
|
|
|
+ System.out.println("是Linux");
|
|
|
+ } else {
|
|
|
+ System.out.println("无法匹配: {" + os + "}");
|
|
|
+ }
|
|
|
+
|
|
|
+ return "Success";
|
|
|
+ }
|
|
|
+
|
|
|
+ //获取第三方应用临时凭证
|
|
|
+ private String getSuiteAccessToken() {
|
|
|
+ if (WeiXinCorpController.SUITE_ACCESS_TOKEN == null || WeiXinCorpController.suiteTokenExpireTime < System.currentTimeMillis()) {
|
|
|
+ //失效了,需要重新获取
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
+ headers.setContentType(MediaType.APPLICATION_JSON);
|
|
|
+ JSONObject reqParam = new JSONObject();
|
|
|
+ reqParam.put("suite_id", suitId);
|
|
|
+ reqParam.put("suite_secret", suitSecret);
|
|
|
+ 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) {
|
|
|
+ WeiXinCorpController.SUITE_ACCESS_TOKEN = obj.getString("suite_access_token");
|
|
|
+ WeiXinCorpController.suiteTokenExpireTime = System.currentTimeMillis() + obj.getIntValue("expires_in")*1000;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return WeiXinCorpController.SUITE_ACCESS_TOKEN;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ //获取企业通讯录的accessToken,在私有化部署的企业内部服务器上跑
|
|
|
+ private String getCorpConcactAccessToken(WxCorpInfo corpInfo) throws Exception {
|
|
|
+ String url = WeiXinCorpController.GET_CORP_TOKEN.replace("ID", corpInfo.getCorpid()).replace("SECRET", corpInfo.getContactSecret());
|
|
|
+ ResponseEntity<String> responseEntity = this.restTemplate.exchange(url,
|
|
|
+ HttpMethod.GET, null, String.class);
|
|
|
+ if (responseEntity.getStatusCode() == HttpStatus.OK) {
|
|
|
+ String resp = responseEntity.getBody();
|
|
|
+ JSONObject json = JSONObject.parseObject(resp);
|
|
|
+ if (json.getIntValue("errcode") == 0) {
|
|
|
+ String access_token = json.getString("access_token");
|
|
|
+ corpInfo.setAccessToken(access_token);
|
|
|
+ } else {
|
|
|
+ throw new Exception(json.toJSONString());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return corpInfo.getAccessToken();
|
|
|
+ }
|
|
|
+
|
|
|
+}
|