wip:add sync kingdee api
This commit is contained in:
parent
92caef1c7a
commit
8dc83dc1dd
@ -55,6 +55,12 @@
|
|||||||
<version>${revision}</version>
|
<version>${revision}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.ensign</groupId>
|
||||||
|
<artifactId>ensign-module-system-biz</artifactId>
|
||||||
|
<version>${revision}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.ensign</groupId>
|
<groupId>com.ensign</groupId>
|
||||||
<artifactId>ensign-spring-boot-starter-redis</artifactId>
|
<artifactId>ensign-spring-boot-starter-redis</artifactId>
|
||||||
|
@ -1,40 +1,20 @@
|
|||||||
package com.ensign.crm.module.crm.controller.crm;
|
package com.ensign.crm.module.crm.controller.crm;
|
||||||
|
|
||||||
import cn.hutool.json.JSONObject;
|
import com.ensign.crm.module.crm.service.ProxyService;
|
||||||
import cn.hutool.json.JSONUtil;
|
|
||||||
import com.ensign.crm.module.crm.exception.AllKingdeeException;
|
|
||||||
import io.swagger.v3.oas.annotations.Operation;
|
import io.swagger.v3.oas.annotations.Operation;
|
||||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.apache.http.HttpEntity;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.apache.http.HttpResponse;
|
|
||||||
import org.apache.http.client.HttpClient;
|
|
||||||
import org.apache.http.client.config.RequestConfig;
|
|
||||||
import org.apache.http.client.methods.HttpPost;
|
|
||||||
import org.apache.http.entity.StringEntity;
|
|
||||||
import org.apache.http.impl.client.DefaultHttpClient;
|
|
||||||
import org.apache.http.util.EntityUtils;
|
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
|
||||||
import org.springframework.data.redis.core.RedisTemplate;
|
|
||||||
import org.springframework.http.HttpMethod;
|
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.http.client.ClientHttpRequest;
|
|
||||||
import org.springframework.http.client.ClientHttpResponse;
|
|
||||||
import org.springframework.http.client.SimpleClientHttpRequestFactory;
|
|
||||||
import org.springframework.security.access.prepost.PreAuthorize;
|
import org.springframework.security.access.prepost.PreAuthorize;
|
||||||
import org.springframework.util.StreamUtils;
|
|
||||||
import org.springframework.validation.annotation.Validated;
|
import org.springframework.validation.annotation.Validated;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.URI;
|
|
||||||
import java.net.URISyntaxException;
|
import java.net.URISyntaxException;
|
||||||
import java.util.*;
|
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @Description: TODO
|
* @Description: TODO
|
||||||
@ -49,210 +29,14 @@ import java.util.concurrent.TimeUnit;
|
|||||||
@Slf4j
|
@Slf4j
|
||||||
public class ProxyController {
|
public class ProxyController {
|
||||||
|
|
||||||
@Value("${ensign.kingdee.is-public}")
|
@Autowired
|
||||||
private Boolean isPublic;
|
private ProxyService proxyService;
|
||||||
|
|
||||||
@Value("${ensign.kingdee.test-public-end-point}")
|
|
||||||
private String testPublicEndPoint;
|
|
||||||
|
|
||||||
@Value("${ensign.kingdee.test-inner-end-point}")
|
|
||||||
private String testInnerEndPoint;
|
|
||||||
|
|
||||||
@Value("${ensign.kingdee.prod-public-end-point}")
|
|
||||||
private String prodPublicEndPoint;
|
|
||||||
|
|
||||||
@Value("${ensign.kingdee.prod-inner-end-point}")
|
|
||||||
private String prodInnerEndPoint;
|
|
||||||
|
|
||||||
@Value("${ensign.kingdee.env}")
|
|
||||||
private String env;
|
|
||||||
|
|
||||||
private final String redisKingdeeKey = "kingdee:accessToken";
|
|
||||||
|
|
||||||
@Resource
|
|
||||||
private RedisTemplate<String, String> redisTemplate;
|
|
||||||
|
|
||||||
@RequestMapping(value = "/do/**", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
|
@RequestMapping(value = "/do/**", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
|
||||||
@Operation(summary = "转发接口")
|
@Operation(summary = "转发接口")
|
||||||
@PreAuthorize("@ss.hasPermission('crm:proxy:all')")
|
@PreAuthorize("@ss.hasPermission('crm:proxy:all')")
|
||||||
public void proxy(HttpServletRequest request, HttpServletResponse response) throws IOException, URISyntaxException {
|
public void proxy(HttpServletRequest request, HttpServletResponse response) throws IOException, URISyntaxException {
|
||||||
URI uri = new URI(request.getRequestURI());
|
proxyService.doProxy(request, response);
|
||||||
String path = uri.getPath();
|
|
||||||
String query = request.getQueryString();
|
|
||||||
String target = initBasePath() + path.replace("/crm-api/proxy/do", "");
|
|
||||||
if (query != null && !query.isEmpty() && !query.equals("null")) {
|
|
||||||
target = target + "?" + query;
|
|
||||||
}
|
|
||||||
URI newUri = new URI(target);
|
|
||||||
|
|
||||||
log.info("================request kingdee start================");
|
|
||||||
log.info("Request Parameters:");
|
|
||||||
log.info("URI: {}", newUri);
|
|
||||||
log.info("Method: {}", request.getMethod());
|
|
||||||
|
|
||||||
// 执行代理查询
|
|
||||||
String methodName = request.getMethod();
|
|
||||||
HttpMethod httpMethod = HttpMethod.resolve(methodName);
|
|
||||||
if (httpMethod == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
ClientHttpRequest delegate = new SimpleClientHttpRequestFactory().createRequest(newUri, httpMethod);
|
|
||||||
Enumeration<String> headerNames = request.getHeaderNames();
|
|
||||||
// 设置请求头
|
|
||||||
while (headerNames.hasMoreElements()) {
|
|
||||||
String headerName = headerNames.nextElement();
|
|
||||||
Enumeration<String> v = request.getHeaders(headerName);
|
|
||||||
List<String> arr = new ArrayList<>();
|
|
||||||
while (v.hasMoreElements()) {
|
|
||||||
arr.add(v.nextElement());
|
|
||||||
}
|
|
||||||
log.info("Header: {}: {}", headerName, arr);
|
|
||||||
delegate.getHeaders().addAll(headerName, arr);
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
String accessToken = initAccessToken();
|
|
||||||
delegate.getHeaders().add("access_token", accessToken);
|
|
||||||
log.info("Header: access_token: {}", accessToken);
|
|
||||||
} catch (AllKingdeeException e) {
|
|
||||||
log.error("获取accessToken时失败:{}", e.getMessage());
|
|
||||||
log.info("================request kingdee end================");
|
|
||||||
response.setStatus(HttpServletResponse.SC_OK);
|
|
||||||
response.setContentType("application/json");
|
|
||||||
response.setCharacterEncoding("UTF-8");
|
|
||||||
Map<String, Object> body = new HashMap<>();
|
|
||||||
body.put("status", false);
|
|
||||||
body.put("message", e.getMessage());
|
|
||||||
response.getWriter().println(JSONUtil.toJsonStr(body));
|
|
||||||
response.getWriter().flush();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
StreamUtils.copy(request.getInputStream(), delegate.getBody());
|
|
||||||
// 执行远程调用
|
|
||||||
ClientHttpResponse clientHttpResponse = delegate.execute();
|
|
||||||
log.info("Response Status: {}", clientHttpResponse.getStatusCode().value());
|
|
||||||
clientHttpResponse.getHeaders().forEach((key, value) -> value.forEach(it -> {
|
|
||||||
log.info("Response Header: {}: {}", key, it);
|
|
||||||
}));
|
|
||||||
|
|
||||||
response.setStatus(clientHttpResponse.getStatusCode().value());
|
|
||||||
// 设置响应头
|
|
||||||
clientHttpResponse.getHeaders().forEach((key, value) -> value.forEach(it -> {
|
|
||||||
response.setHeader(key, it);
|
|
||||||
}));
|
|
||||||
log.info("================request kingdee end================");
|
|
||||||
StreamUtils.copy(clientHttpResponse.getBody(), response.getOutputStream());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private String initAccessToken() throws IOException, AllKingdeeException {
|
|
||||||
String accessToken = redisTemplate.opsForValue().get(redisKingdeeKey);
|
|
||||||
if (accessToken != null && !accessToken.isEmpty()) {
|
|
||||||
return accessToken;
|
|
||||||
}
|
|
||||||
|
|
||||||
final String END_POINT = initBasePath() + "/ierp/api/getAppToken.do";
|
|
||||||
HttpClient client = new DefaultHttpClient();
|
|
||||||
|
|
||||||
HttpPost post = new HttpPost(END_POINT);
|
|
||||||
|
|
||||||
Map<String, String> body = new HashMap<>();
|
|
||||||
|
|
||||||
body.put("appId", "CRM");
|
|
||||||
body.put("appSecuret", "yx.CRM.tksecurit@240924");
|
|
||||||
body.put("tenantid", "yxzg-topview-dev");
|
|
||||||
body.put("accountId", "1878420900609526784");
|
|
||||||
body.put("language", "zh_CN");
|
|
||||||
|
|
||||||
HttpEntity reqEntity = new StringEntity(JSONUtil.toJsonStr(body), "utf-8");
|
|
||||||
|
|
||||||
post.setEntity(reqEntity);
|
|
||||||
post.setConfig(initRequestConfig());
|
|
||||||
HttpResponse response = client.execute(post);
|
|
||||||
if (response.getStatusLine().getStatusCode() == 200) {
|
|
||||||
|
|
||||||
HttpEntity resEntity = response.getEntity();
|
|
||||||
String responseData = EntityUtils.toString(resEntity, "utf-8");
|
|
||||||
JSONObject jsonObject = JSONUtil.parseObj(responseData);
|
|
||||||
Boolean status = (Boolean) jsonObject.get("status");
|
|
||||||
String state = (String) jsonObject.get("state");
|
|
||||||
if (status && "success".equals(state)) {
|
|
||||||
JSONObject data = (JSONObject) jsonObject.get("data");
|
|
||||||
String appToken = (String) data.get("app_token");
|
|
||||||
if (appToken != null && !appToken.isEmpty()) {
|
|
||||||
return doGetAccessToken(appToken);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
throw new AllKingdeeException("获取AppToken失败");
|
|
||||||
}
|
|
||||||
|
|
||||||
private String doGetAccessToken(String appToken) throws IOException, AllKingdeeException {
|
|
||||||
|
|
||||||
HttpClient client = new DefaultHttpClient();
|
|
||||||
|
|
||||||
HttpPost post = new HttpPost(initBasePath() + "/ierp/api/login.do");
|
|
||||||
|
|
||||||
Map<String, String> body = new HashMap<>();
|
|
||||||
|
|
||||||
body.put("user", "COSMIC");
|
|
||||||
body.put("apptoken", appToken);
|
|
||||||
body.put("tenantid", "yxgz-prod");
|
|
||||||
body.put("accountId", "1878420900609526784");
|
|
||||||
body.put("language", "zh_CN");
|
|
||||||
body.put("usertype", "UserName");
|
|
||||||
|
|
||||||
HttpEntity reqEntity = new StringEntity(JSONUtil.toJsonStr(body), "utf-8");
|
|
||||||
|
|
||||||
post.setEntity(reqEntity);
|
|
||||||
post.setConfig(initRequestConfig());
|
|
||||||
HttpResponse response = client.execute(post);
|
|
||||||
|
|
||||||
if (response.getStatusLine().getStatusCode() == 200) {
|
|
||||||
|
|
||||||
HttpEntity resEntity = response.getEntity();
|
|
||||||
String responseData = EntityUtils.toString(resEntity, "utf-8");
|
|
||||||
JSONObject jsonObject = JSONUtil.parseObj(responseData);
|
|
||||||
Boolean status = (Boolean) jsonObject.get("status");
|
|
||||||
String state = (String) jsonObject.get("state");
|
|
||||||
|
|
||||||
if (status && "success".equals(state)) {
|
|
||||||
JSONObject data = (JSONObject) jsonObject.get("data");
|
|
||||||
String accessToken = (String) data.get("access_token");
|
|
||||||
if (accessToken != null && !accessToken.isEmpty()) {
|
|
||||||
|
|
||||||
Long expireTime = (Long) data.get("expire_time");
|
|
||||||
redisTemplate.opsForValue().set(redisKingdeeKey, accessToken, expireTime / 1000 - 3000, TimeUnit.MICROSECONDS);
|
|
||||||
return accessToken;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
throw new AllKingdeeException("获取AccessToken失败");
|
|
||||||
}
|
|
||||||
|
|
||||||
public RequestConfig initRequestConfig() {
|
|
||||||
return RequestConfig.custom()
|
|
||||||
.setConnectTimeout(5000)//一、连接超时:connectionTimeout-->指的是连接一个url的连接等待时间
|
|
||||||
.setSocketTimeout(5000)// 二、读取数据超时:SocketTimeout-->指的是连接上一个url,获取response的返回等待时间
|
|
||||||
.setConnectionRequestTimeout(5000)
|
|
||||||
.build();
|
|
||||||
}
|
|
||||||
|
|
||||||
public String initBasePath() {
|
|
||||||
if ("test".equals(env)) {
|
|
||||||
if (isPublic) {
|
|
||||||
return testPublicEndPoint;
|
|
||||||
} else {
|
|
||||||
return testInnerEndPoint;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if ("prod".equals(env)) {
|
|
||||||
if (isPublic) {
|
|
||||||
return prodPublicEndPoint;
|
|
||||||
} else {
|
|
||||||
return prodInnerEndPoint;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,10 +1,24 @@
|
|||||||
package com.ensign.crm.module.crm.job;
|
package com.ensign.crm.module.crm.job;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||||
import com.ensign.crm.framework.quartz.core.handler.JobHandler;
|
import com.ensign.crm.framework.quartz.core.handler.JobHandler;
|
||||||
import com.ensign.crm.framework.tenant.core.aop.TenantIgnore;
|
import com.ensign.crm.framework.tenant.core.aop.TenantIgnore;
|
||||||
|
import com.ensign.crm.module.crm.exception.AllKingdeeException;
|
||||||
|
import com.ensign.crm.module.crm.service.ProxyService;
|
||||||
|
import com.ensign.crm.module.system.dal.dataobject.user.AdminUserDO;
|
||||||
|
import com.ensign.crm.module.system.dal.mysql.user.AdminUserMapper;
|
||||||
|
import com.ensign.crm.module.system.service.user.AdminUserService;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.checkerframework.checker.units.qual.A;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @Description: TODO
|
* @Description: TODO
|
||||||
* @Date: 2024/9/26 14:06
|
* @Date: 2024/9/26 14:06
|
||||||
@ -16,10 +30,49 @@ import org.springframework.stereotype.Component;
|
|||||||
public class UserSyncKingdeeTask implements JobHandler {
|
public class UserSyncKingdeeTask implements JobHandler {
|
||||||
|
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private AdminUserMapper userMapper;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private ProxyService proxyService;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@TenantIgnore
|
@TenantIgnore
|
||||||
public String execute(String param) throws Exception {
|
public String execute(String param) throws Exception {
|
||||||
return "";
|
LambdaQueryWrapper<AdminUserDO> queryWrapper = new LambdaQueryWrapper<>();
|
||||||
|
queryWrapper.eq(AdminUserDO::getStatus, 0)
|
||||||
|
.eq(AdminUserDO::getIsSyncKingdee, 1);
|
||||||
|
List<AdminUserDO> users = userMapper.selectList(queryWrapper);
|
||||||
|
List<Map<String, Object>> list = new ArrayList<>();
|
||||||
|
|
||||||
|
for (AdminUserDO user : users) {
|
||||||
|
Map<String, Object> addUser = new HashMap<>();
|
||||||
|
addUser.put("number", user.getUsername());
|
||||||
|
addUser.put("name", user.getNickname());
|
||||||
|
addUser.put("webuserid", user.getId());
|
||||||
|
addUser.put("webdeptid", user.getDeptId());
|
||||||
|
addUser.put("function", "B");
|
||||||
|
addUser.put("phone", user.getMobile());
|
||||||
|
list.add(addUser);
|
||||||
|
}
|
||||||
|
Map<String, Object> body = new HashMap<>();
|
||||||
|
body.put("data", list);
|
||||||
|
log.info("请求body:{}", body);
|
||||||
|
log.info("请求{}条", users.size());
|
||||||
|
JSONObject jsonObject = proxyService.doRequest("/ierp/kapi/v2/yem/yem_crmbasic/yem_crm_dealeruser/CRM_yem_crm_dealeruser", "POST", body, null);
|
||||||
|
log.info("响应body:{}", jsonObject);
|
||||||
|
if (jsonObject.containsKey("status") && jsonObject.containsKey("errorCode")) {
|
||||||
|
if (jsonObject.getBoolean("status") && jsonObject.getInteger("errorCode") == 0) {
|
||||||
|
if (jsonObject.containsKey("data")) {
|
||||||
|
JSONObject dataObject = jsonObject.getJSONObject("data");
|
||||||
|
if (dataObject.containsKey("successCount")) {
|
||||||
|
int successCount = dataObject.getInteger("successCount");
|
||||||
|
log.info("成功{}条", successCount);
|
||||||
|
return "success";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
throw new AllKingdeeException("task error:" + jsonObject);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,299 @@
|
|||||||
|
package com.ensign.crm.module.crm.service;
|
||||||
|
|
||||||
|
|
||||||
|
import cn.hutool.json.JSONUtil;
|
||||||
|
import com.alibaba.fastjson.JSON;
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
|
import com.ensign.crm.module.crm.exception.AllKingdeeException;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.apache.http.HttpEntity;
|
||||||
|
import org.apache.http.HttpResponse;
|
||||||
|
import org.apache.http.client.HttpClient;
|
||||||
|
import org.apache.http.client.config.RequestConfig;
|
||||||
|
import org.apache.http.client.methods.HttpPost;
|
||||||
|
import org.apache.http.client.methods.HttpUriRequest;
|
||||||
|
import org.apache.http.client.methods.RequestBuilder;
|
||||||
|
import org.apache.http.entity.StringEntity;
|
||||||
|
import org.apache.http.impl.client.DefaultHttpClient;
|
||||||
|
import org.apache.http.impl.client.HttpClients;
|
||||||
|
import org.apache.http.util.EntityUtils;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.data.redis.core.RedisTemplate;
|
||||||
|
import org.springframework.http.HttpMethod;
|
||||||
|
import org.springframework.http.client.ClientHttpRequest;
|
||||||
|
import org.springframework.http.client.ClientHttpResponse;
|
||||||
|
import org.springframework.http.client.SimpleClientHttpRequestFactory;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.util.StreamUtils;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.URI;
|
||||||
|
import java.net.URISyntaxException;
|
||||||
|
import java.util.*;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Description: TODO
|
||||||
|
* @Date: 2024/9/26 17:56
|
||||||
|
* @Created: by ZZSLL
|
||||||
|
*/
|
||||||
|
|
||||||
|
@Service
|
||||||
|
@Slf4j
|
||||||
|
public class ProxyService {
|
||||||
|
|
||||||
|
@Value("${ensign.kingdee.is-public}")
|
||||||
|
private Boolean isPublic;
|
||||||
|
|
||||||
|
@Value("${ensign.kingdee.test-public-end-point}")
|
||||||
|
private String testPublicEndPoint;
|
||||||
|
|
||||||
|
@Value("${ensign.kingdee.test-inner-end-point}")
|
||||||
|
private String testInnerEndPoint;
|
||||||
|
|
||||||
|
@Value("${ensign.kingdee.prod-public-end-point}")
|
||||||
|
private String prodPublicEndPoint;
|
||||||
|
|
||||||
|
@Value("${ensign.kingdee.prod-inner-end-point}")
|
||||||
|
private String prodInnerEndPoint;
|
||||||
|
|
||||||
|
@Value("${ensign.kingdee.env}")
|
||||||
|
private String env;
|
||||||
|
|
||||||
|
private final String redisKingdeeKey = "kingdee:accessToken";
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private RedisTemplate<String, String> redisTemplate;
|
||||||
|
|
||||||
|
|
||||||
|
public void doProxy(HttpServletRequest request, HttpServletResponse response) throws IOException, URISyntaxException {
|
||||||
|
URI uri = new URI(request.getRequestURI());
|
||||||
|
String path = uri.getPath();
|
||||||
|
String query = request.getQueryString();
|
||||||
|
String target = initBasePath() + path.replace("/crm-api/proxy/do", "");
|
||||||
|
if (query != null && !query.isEmpty() && !query.equals("null")) {
|
||||||
|
target = target + "?" + query;
|
||||||
|
}
|
||||||
|
URI newUri = new URI(target);
|
||||||
|
|
||||||
|
log.info("================request kingdee start================");
|
||||||
|
log.info("Request Parameters:");
|
||||||
|
log.info("URI: {}", newUri);
|
||||||
|
log.info("Method: {}", request.getMethod());
|
||||||
|
|
||||||
|
// 执行代理查询
|
||||||
|
String methodName = request.getMethod();
|
||||||
|
HttpMethod httpMethod = HttpMethod.resolve(methodName);
|
||||||
|
if (httpMethod == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
ClientHttpRequest delegate = new SimpleClientHttpRequestFactory().createRequest(newUri, httpMethod);
|
||||||
|
Enumeration<String> headerNames = request.getHeaderNames();
|
||||||
|
// 设置请求头
|
||||||
|
while (headerNames.hasMoreElements()) {
|
||||||
|
String headerName = headerNames.nextElement();
|
||||||
|
Enumeration<String> v = request.getHeaders(headerName);
|
||||||
|
List<String> arr = new ArrayList<>();
|
||||||
|
while (v.hasMoreElements()) {
|
||||||
|
arr.add(v.nextElement());
|
||||||
|
}
|
||||||
|
log.info("Header: {}: {}", headerName, arr);
|
||||||
|
delegate.getHeaders().addAll(headerName, arr);
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
String accessToken = initAccessToken();
|
||||||
|
delegate.getHeaders().add("access_token", accessToken);
|
||||||
|
log.info("Header: access_token: {}", accessToken);
|
||||||
|
} catch (AllKingdeeException e) {
|
||||||
|
log.error("获取accessToken时失败:{}", e.getMessage());
|
||||||
|
log.info("================request kingdee end================");
|
||||||
|
response.setStatus(HttpServletResponse.SC_OK);
|
||||||
|
response.setContentType("application/json");
|
||||||
|
response.setCharacterEncoding("UTF-8");
|
||||||
|
Map<String, Object> body = new HashMap<>();
|
||||||
|
body.put("status", false);
|
||||||
|
body.put("message", e.getMessage());
|
||||||
|
response.getWriter().println(JSONUtil.toJsonStr(body));
|
||||||
|
response.getWriter().flush();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
StreamUtils.copy(request.getInputStream(), delegate.getBody());
|
||||||
|
// 执行远程调用
|
||||||
|
ClientHttpResponse clientHttpResponse = delegate.execute();
|
||||||
|
log.info("Response Status: {}", clientHttpResponse.getStatusCode().value());
|
||||||
|
clientHttpResponse.getHeaders().forEach((key, value) -> value.forEach(it -> {
|
||||||
|
log.info("Response Header: {}: {}", key, it);
|
||||||
|
}));
|
||||||
|
|
||||||
|
response.setStatus(clientHttpResponse.getStatusCode().value());
|
||||||
|
// 设置响应头
|
||||||
|
clientHttpResponse.getHeaders().forEach((key, value) -> value.forEach(it -> {
|
||||||
|
response.setHeader(key, it);
|
||||||
|
}));
|
||||||
|
log.info("================request kingdee end================");
|
||||||
|
StreamUtils.copy(clientHttpResponse.getBody(), response.getOutputStream());
|
||||||
|
}
|
||||||
|
|
||||||
|
public com.alibaba.fastjson.JSONObject doRequest(String endpoint, String methodName, Map<String, Object> body, Map<String, Object> header) throws AllKingdeeException, IOException, URISyntaxException {
|
||||||
|
log.info("================request kingdee start================");
|
||||||
|
|
||||||
|
HttpClient httpClient = HttpClients.createDefault();
|
||||||
|
HttpMethod httpMethod = HttpMethod.resolve(methodName);
|
||||||
|
|
||||||
|
URI newUri = new URI(initBasePath() + endpoint);
|
||||||
|
|
||||||
|
if (httpMethod == null) {
|
||||||
|
throw new AllKingdeeException("unknown request methodName: " + methodName);
|
||||||
|
}
|
||||||
|
|
||||||
|
HttpUriRequest request = null;
|
||||||
|
if (httpMethod == HttpMethod.GET) {
|
||||||
|
request = RequestBuilder.get(newUri).build();
|
||||||
|
} else if (httpMethod == HttpMethod.POST) {
|
||||||
|
// Assuming body is a JSON object
|
||||||
|
String jsonBody = JSONUtil.toJsonStr(body);
|
||||||
|
request = RequestBuilder.post(newUri).setEntity(new StringEntity(jsonBody)).build();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add headers to the request
|
||||||
|
if (header != null && request != null) {
|
||||||
|
for (Map.Entry<String, Object> entry : header.entrySet()) {
|
||||||
|
request.addHeader(entry.getKey(), entry.getValue().toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (request != null) {
|
||||||
|
request.addHeader("access_token", initAccessToken());
|
||||||
|
log.info("Requesting: {}", request);
|
||||||
|
}
|
||||||
|
|
||||||
|
HttpResponse response = httpClient.execute(request);
|
||||||
|
|
||||||
|
|
||||||
|
if (response.getStatusLine().getStatusCode() == 200) {
|
||||||
|
HttpEntity entity = response.getEntity();
|
||||||
|
String jsonResponse = EntityUtils.toString(entity);
|
||||||
|
log.info("Received response: {}", jsonResponse);
|
||||||
|
log.info("================request kingdee end================");
|
||||||
|
return JSON.parseObject(jsonResponse);
|
||||||
|
} else {
|
||||||
|
throw new AllKingdeeException("request error: " + response.getStatusLine().getReasonPhrase());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private String initAccessToken() throws IOException, AllKingdeeException {
|
||||||
|
String accessToken = redisTemplate.opsForValue().get(redisKingdeeKey);
|
||||||
|
if (accessToken != null && !accessToken.isEmpty()) {
|
||||||
|
return accessToken;
|
||||||
|
}
|
||||||
|
|
||||||
|
final String END_POINT = initBasePath() + "/ierp/api/getAppToken.do";
|
||||||
|
HttpClient client = new DefaultHttpClient();
|
||||||
|
|
||||||
|
HttpPost post = new HttpPost(END_POINT);
|
||||||
|
|
||||||
|
Map<String, String> body = new HashMap<>();
|
||||||
|
|
||||||
|
body.put("appId", "CRM");
|
||||||
|
body.put("appSecuret", "yx.CRM.tksecurit@240924");
|
||||||
|
body.put("tenantid", "yxzg-topview-dev");
|
||||||
|
body.put("accountId", "1878420900609526784");
|
||||||
|
body.put("language", "zh_CN");
|
||||||
|
|
||||||
|
HttpEntity reqEntity = new StringEntity(JSONUtil.toJsonStr(body), "utf-8");
|
||||||
|
|
||||||
|
post.setEntity(reqEntity);
|
||||||
|
post.setConfig(initRequestConfig());
|
||||||
|
HttpResponse response = client.execute(post);
|
||||||
|
if (response.getStatusLine().getStatusCode() == 200) {
|
||||||
|
|
||||||
|
HttpEntity resEntity = response.getEntity();
|
||||||
|
String responseData = EntityUtils.toString(resEntity, "utf-8");
|
||||||
|
com.alibaba.fastjson.JSONObject jsonObject = JSON.parseObject(responseData);
|
||||||
|
Boolean status = (Boolean) jsonObject.get("status");
|
||||||
|
String state = (String) jsonObject.get("state");
|
||||||
|
if (status && "success".equals(state)) {
|
||||||
|
JSONObject data = (JSONObject) jsonObject.get("data");
|
||||||
|
String appToken = (String) data.get("app_token");
|
||||||
|
if (appToken != null && !appToken.isEmpty()) {
|
||||||
|
return doGetAccessToken(appToken);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
throw new AllKingdeeException("获取AppToken失败");
|
||||||
|
}
|
||||||
|
|
||||||
|
private String doGetAccessToken(String appToken) throws IOException, AllKingdeeException {
|
||||||
|
|
||||||
|
HttpClient client = new DefaultHttpClient();
|
||||||
|
|
||||||
|
HttpPost post = new HttpPost(initBasePath() + "/ierp/api/login.do");
|
||||||
|
|
||||||
|
Map<String, String> body = new HashMap<>();
|
||||||
|
|
||||||
|
body.put("user", "COSMIC");
|
||||||
|
body.put("apptoken", appToken);
|
||||||
|
body.put("tenantid", "yxgz-prod");
|
||||||
|
body.put("accountId", "1878420900609526784");
|
||||||
|
body.put("language", "zh_CN");
|
||||||
|
body.put("usertype", "UserName");
|
||||||
|
|
||||||
|
HttpEntity reqEntity = new StringEntity(JSONUtil.toJsonStr(body), "utf-8");
|
||||||
|
|
||||||
|
post.setEntity(reqEntity);
|
||||||
|
post.setConfig(initRequestConfig());
|
||||||
|
HttpResponse response = client.execute(post);
|
||||||
|
|
||||||
|
if (response.getStatusLine().getStatusCode() == 200) {
|
||||||
|
|
||||||
|
HttpEntity resEntity = response.getEntity();
|
||||||
|
String responseData = EntityUtils.toString(resEntity, "utf-8");
|
||||||
|
JSONObject jsonObject = JSON.parseObject(responseData);
|
||||||
|
Boolean status = (Boolean) jsonObject.get("status");
|
||||||
|
String state = (String) jsonObject.get("state");
|
||||||
|
|
||||||
|
if (status && "success".equals(state)) {
|
||||||
|
JSONObject data = (JSONObject) jsonObject.get("data");
|
||||||
|
String accessToken = (String) data.get("access_token");
|
||||||
|
if (accessToken != null && !accessToken.isEmpty()) {
|
||||||
|
|
||||||
|
Long expireTime = (Long) data.get("expire_time");
|
||||||
|
redisTemplate.opsForValue().set(redisKingdeeKey, accessToken, expireTime / 1000 - 3000, TimeUnit.MICROSECONDS);
|
||||||
|
return accessToken;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
throw new AllKingdeeException("获取AccessToken失败");
|
||||||
|
}
|
||||||
|
|
||||||
|
public RequestConfig initRequestConfig() {
|
||||||
|
return RequestConfig.custom()
|
||||||
|
.setConnectTimeout(5000)//一、连接超时:connectionTimeout-->指的是连接一个url的连接等待时间
|
||||||
|
.setSocketTimeout(5000)// 二、读取数据超时:SocketTimeout-->指的是连接上一个url,获取response的返回等待时间
|
||||||
|
.setConnectionRequestTimeout(5000)
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
public String initBasePath() {
|
||||||
|
if ("test".equals(env)) {
|
||||||
|
if (isPublic) {
|
||||||
|
return testPublicEndPoint;
|
||||||
|
} else {
|
||||||
|
return testInnerEndPoint;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ("prod".equals(env)) {
|
||||||
|
if (isPublic) {
|
||||||
|
return prodPublicEndPoint;
|
||||||
|
} else {
|
||||||
|
return prodInnerEndPoint;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
}
|
@ -17,6 +17,7 @@ import org.springframework.validation.annotation.Validated;
|
|||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
|
import javax.annotation.security.PermitAll;
|
||||||
import javax.validation.Valid;
|
import javax.validation.Valid;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@ -66,6 +67,7 @@ public class DeptController {
|
|||||||
|
|
||||||
@GetMapping(value = {"/list-all-simple", "/simple-list"})
|
@GetMapping(value = {"/list-all-simple", "/simple-list"})
|
||||||
@Operation(summary = "获取部门精简信息列表", description = "只包含被开启的部门,主要用于前端的下拉选项")
|
@Operation(summary = "获取部门精简信息列表", description = "只包含被开启的部门,主要用于前端的下拉选项")
|
||||||
|
@PermitAll
|
||||||
public CommonResult<List<DeptSimpleRespVO>> getSimpleDeptList() {
|
public CommonResult<List<DeptSimpleRespVO>> getSimpleDeptList() {
|
||||||
List<DeptDO> list = deptService.getDeptList(
|
List<DeptDO> list = deptService.getDeptList(
|
||||||
new DeptListReqVO().setStatus(CommonStatusEnum.ENABLE.getStatus()));
|
new DeptListReqVO().setStatus(CommonStatusEnum.ENABLE.getStatus()));
|
||||||
|
@ -24,6 +24,7 @@ import org.springframework.web.bind.annotation.*;
|
|||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
|
import javax.annotation.security.PermitAll;
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
import javax.validation.Valid;
|
import javax.validation.Valid;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@ -48,7 +49,7 @@ public class UserController {
|
|||||||
|
|
||||||
@PostMapping("/create")
|
@PostMapping("/create")
|
||||||
@Operation(summary = "新增用户")
|
@Operation(summary = "新增用户")
|
||||||
@PreAuthorize("@ss.hasPermission('system:user:create')")
|
@PermitAll
|
||||||
public CommonResult<Long> createUser(@Valid @RequestBody UserSaveReqVO reqVO) {
|
public CommonResult<Long> createUser(@Valid @RequestBody UserSaveReqVO reqVO) {
|
||||||
Long id = userService.createUser(reqVO);
|
Long id = userService.createUser(reqVO);
|
||||||
return success(id);
|
return success(id);
|
||||||
|
@ -93,4 +93,8 @@ public class AdminUserDO extends TenantBaseDO {
|
|||||||
*/
|
*/
|
||||||
private LocalDateTime loginDate;
|
private LocalDateTime loginDate;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 是否同步苍穹
|
||||||
|
*/
|
||||||
|
private Integer isSyncKingdee;
|
||||||
}
|
}
|
||||||
|
@ -102,6 +102,7 @@ public class AdminUserServiceImpl implements AdminUserService {
|
|||||||
// 2.1 插入用户
|
// 2.1 插入用户
|
||||||
AdminUserDO user = BeanUtils.toBean(createReqVO, AdminUserDO.class);
|
AdminUserDO user = BeanUtils.toBean(createReqVO, AdminUserDO.class);
|
||||||
user.setStatus(CommonStatusEnum.ENABLE.getStatus()); // 默认开启
|
user.setStatus(CommonStatusEnum.ENABLE.getStatus()); // 默认开启
|
||||||
|
user.setIsSyncKingdee(CommonStatusEnum.DISABLE.getStatus());//未同步
|
||||||
user.setPassword(encodePassword(createReqVO.getPassword())); // 加密密码
|
user.setPassword(encodePassword(createReqVO.getPassword())); // 加密密码
|
||||||
userMapper.insert(user);
|
userMapper.insert(user);
|
||||||
// 2.2 插入关联岗位
|
// 2.2 插入关联岗位
|
||||||
|
Loading…
Reference in New Issue
Block a user