|
|
@@ -0,0 +1,346 @@
|
|
|
+package com.takai.system.service.impl;
|
|
|
+
|
|
|
+import com.alibaba.fastjson2.JSON;
|
|
|
+import com.alibaba.fastjson2.JSONArray;
|
|
|
+import com.alibaba.fastjson2.JSONObject;
|
|
|
+import com.auth0.jwt.JWTCreator;
|
|
|
+import com.auth0.jwt.algorithms.Algorithm;
|
|
|
+import com.takai.common.utils.StringUtils;
|
|
|
+import com.takai.system.service.IJkApiService;
|
|
|
+import com.takai.common.annotation.DataSource;
|
|
|
+import com.takai.common.config.JkConfig;
|
|
|
+import com.takai.common.core.domain.entity.SysDept;
|
|
|
+import com.takai.common.core.domain.entity.SysUser;
|
|
|
+import com.takai.common.core.redis.RedisCache;
|
|
|
+import com.takai.common.enums.DataSourceType;
|
|
|
+import com.takai.system.domain.SysPost;
|
|
|
+import com.takai.system.service.ISysDeptService;
|
|
|
+import com.takai.system.service.ISysPostService;
|
|
|
+import com.takai.system.service.ISysUserService;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import okhttp3.*;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.data.redis.core.RedisTemplate;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
+import com.auth0.jwt.JWT;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 高井 业务层处理
|
|
|
+ *
|
|
|
+ * @author takai
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+@DataSource(DataSourceType.MASTER)
|
|
|
+public class JkApiServiceImpl implements IJkApiService {
|
|
|
+ private static final Logger logger = LoggerFactory.getLogger(JkApiServiceImpl.class);
|
|
|
+ @Autowired
|
|
|
+ private ISysUserService sysUserService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private ISysDeptService sysDeptService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private ISysPostService sysPostService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private RedisCache redisCache;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ public RedisTemplate redisTemplate;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ JkConfig jkConfig;
|
|
|
+
|
|
|
+ public static final String BEARER = "Bearer";
|
|
|
+
|
|
|
+ public static final String PARAM_ST = "startTime";
|
|
|
+
|
|
|
+ public static final String PARAM_PAGE = "page";
|
|
|
+
|
|
|
+ public static final String PARAM_SIZE = "size";
|
|
|
+
|
|
|
+ public static final String PARAM_GRANT_TYPE = "grant_type";
|
|
|
+
|
|
|
+ public static final String PARAM_CLIENT_ID = "client_id";
|
|
|
+
|
|
|
+ public static final String PARAM_CLIENT_SECRET = "client_secret";
|
|
|
+
|
|
|
+ public static final String PARAM_CODE = "code";
|
|
|
+
|
|
|
+ public static final String PARAM_ACCESS_TOKEN = "access_token";
|
|
|
+
|
|
|
+ public static final int DEFAULT_SIZE = 100;
|
|
|
+
|
|
|
+ public static final String CODE_SUCCESS = "0";
|
|
|
+
|
|
|
+ public static final String DATA = "data";
|
|
|
+
|
|
|
+ public static final String TOTAL = "total";
|
|
|
+
|
|
|
+ public static final String LIST = "list";
|
|
|
+
|
|
|
+ public static final String GRANT_TYPE_AC = "authorization_code";
|
|
|
+
|
|
|
+ private static final MediaType JSON_UTF8 = MediaType.get("application/json; charset=utf-8");
|
|
|
+
|
|
|
+ private static final OkHttpClient client = new OkHttpClient();
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 通过建科接口获取建科用户信息
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public List<SysUser> getJkUsers() throws IOException {
|
|
|
+ List<SysUser> userList = new ArrayList<>();
|
|
|
+ List<JSONObject> dataList = getApiData(jkConfig.getIamUserUrl(),"用户");
|
|
|
+ for(JSONObject jsonObject : dataList) {
|
|
|
+ List<SysUser> users = buildSysUserList(jsonObject.getJSONArray(LIST));
|
|
|
+ //保存用户到用户表
|
|
|
+ sysUserService.batchUser(users);
|
|
|
+ userList.addAll(users);
|
|
|
+ }
|
|
|
+ return userList;
|
|
|
+ }
|
|
|
+
|
|
|
+ private List<SysUser> buildSysUserList(JSONArray jsonArray) {
|
|
|
+ List<SysUser> userList = new ArrayList<>();
|
|
|
+ for(int i = 0; i < jsonArray.size(); i++) {
|
|
|
+ SysUser sysUser = new SysUser();
|
|
|
+ JSONObject json = jsonArray.getJSONObject(i);
|
|
|
+ sysUser.setUserId(json.getLong("app_account__id"));
|
|
|
+ sysUser.setUserName(json.getString("app_account__account_no"));
|
|
|
+ sysUser.setNickName(json.getString("app_account__account_name"));
|
|
|
+ //建科用户状态,启用:1,停用:0,删除:-1
|
|
|
+ String status = json.getString("app_account__status");
|
|
|
+ status = status.equals("1") ? "0" : "1";
|
|
|
+ sysUser.setStatus("1");
|
|
|
+ sysUser.setPhonenumber(json.getString("idt_user__mobile"));
|
|
|
+ sysUser.setWorkNo(json.getString("idt_user__work_no"));
|
|
|
+ sysUser.setEmail(json.getString("idt_user__email"));
|
|
|
+ JSONArray orgs = json.getJSONArray("orgs");
|
|
|
+ if(orgs.size() > 0) {
|
|
|
+ sysUser.setDeptId(orgs.getJSONObject(0).getLong("idt_org__id"));
|
|
|
+ }
|
|
|
+ userList.add(sysUser);
|
|
|
+ }
|
|
|
+ return userList;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 通过建科接口获取建科部门信息
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public List<SysDept> getJkDepts() throws IOException {
|
|
|
+ List<SysDept> deptList = new ArrayList<>();
|
|
|
+ List<JSONObject> dataList = getApiData(jkConfig.getIamDeptUrl(),"部门");
|
|
|
+ for(JSONObject jsonObject : dataList) {
|
|
|
+ List<SysDept> depts = buildSysDeptList(jsonObject.getJSONArray(LIST));
|
|
|
+ //保存用户到用户表
|
|
|
+ sysDeptService.batchDept(depts);
|
|
|
+ deptList.addAll(depts);
|
|
|
+ }
|
|
|
+ return deptList;
|
|
|
+ }
|
|
|
+
|
|
|
+ private List<SysDept> buildSysDeptList(JSONArray jsonArray) {
|
|
|
+ List<SysDept> deptList = new ArrayList<>();
|
|
|
+ for(int i = 0; i < jsonArray.size(); i++) {
|
|
|
+ SysDept sysDept = new SysDept();
|
|
|
+ JSONObject json = jsonArray.getJSONObject(i);
|
|
|
+ sysDept.setDeptId(json.getLong("idt_org__id"));
|
|
|
+ sysDept.setDeptName(json.getString("idt_org__org_name"));
|
|
|
+ sysDept.setParentId(json.getLong("idt_org__parent_id"));
|
|
|
+ sysDept.setAncestors(json.getString("idt_org__org_path").substring(1).replaceAll("/",","));
|
|
|
+ //部门状态,启用:1,停用:0
|
|
|
+ String status = json.getString("idt_org__status");
|
|
|
+ status = status.equals("1") ? "0" : "1";
|
|
|
+ sysDept.setStatus("1");
|
|
|
+ deptList.add(sysDept);
|
|
|
+ }
|
|
|
+ return deptList;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 通过建科接口获取建科岗位信息
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public List<SysPost> getJkPosts() throws IOException {
|
|
|
+ List<SysPost> postList = new ArrayList<>();
|
|
|
+ List<JSONObject> dataList = getApiData(jkConfig.getIamPostUrl(),"岗位");
|
|
|
+ for(JSONObject jsonObject : dataList) {
|
|
|
+ List<SysPost> posts = buildSysPostList(jsonObject.getJSONArray(LIST));
|
|
|
+ //保存用户到用户表
|
|
|
+ sysPostService.batchPost(posts);
|
|
|
+ postList.addAll(posts);
|
|
|
+ }
|
|
|
+ return postList;
|
|
|
+ }
|
|
|
+
|
|
|
+ private List<SysPost> buildSysPostList(JSONArray jsonArray) {
|
|
|
+ List<SysPost> postList = new ArrayList<>();
|
|
|
+ for(int i = 0; i < jsonArray.size(); i++) {
|
|
|
+ SysPost sysPost = new SysPost();
|
|
|
+ JSONObject json = jsonArray.getJSONObject(i);
|
|
|
+ sysPost.setPostCode(json.getString("idt_job__code"));
|
|
|
+ sysPost.setPostName(json.getString("idt_job__name"));
|
|
|
+ //部门状态,启用:1,停用:0
|
|
|
+ String status = json.getString("idt_job__status");
|
|
|
+ status = status.equals("1") ? "0" : "1";
|
|
|
+ sysPost.setStatus("1");
|
|
|
+ postList.add(sysPost);
|
|
|
+ }
|
|
|
+ return postList;
|
|
|
+ }
|
|
|
+
|
|
|
+ public List<JSONObject> getApiData(String url,String objectName) throws IOException {
|
|
|
+ List<JSONObject> objectList = new ArrayList<>();
|
|
|
+ int page = 1;
|
|
|
+ int total = 1;
|
|
|
+ while(total >= page){
|
|
|
+ Request request = buildRequest(url,page);
|
|
|
+ // 执行请求并获取响应
|
|
|
+ try {
|
|
|
+ Response response = client.newCall(request).execute();
|
|
|
+ logger.info("获取建科"+objectName+"返回:" + response.body().string());
|
|
|
+
|
|
|
+ if (!response.isSuccessful()) {
|
|
|
+ throw new IOException("获取建科"+objectName+"信息失败 " + response.body().string());
|
|
|
+ }
|
|
|
+ JSONObject jsonObject =JSON.parseObject(response.body().string());
|
|
|
+ logger.info("获取建科"+objectName+"返回信息:" + jsonObject.toJSONString());
|
|
|
+ //“0”为成功其余均为失败
|
|
|
+ String code = jsonObject.getString("code");
|
|
|
+ if (CODE_SUCCESS.equals(code)) {
|
|
|
+ JSONObject dataObject = jsonObject.getJSONObject(DATA);
|
|
|
+ objectList.add(dataObject);
|
|
|
+ total = dataObject.getInteger(TOTAL);
|
|
|
+ page++;
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ throw new IOException("获取建科用"+objectName+"信息失败 ", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return objectList;
|
|
|
+ }
|
|
|
+
|
|
|
+ public Map<String, Object> getJkToken(String code) throws IOException {
|
|
|
+ String token = "";
|
|
|
+ JSONObject userJson = null;
|
|
|
+ try {
|
|
|
+ Request request = buildTokenRequest(code);
|
|
|
+ Response response = client.newCall(request).execute();
|
|
|
+ if (!response.isSuccessful()) {
|
|
|
+ throw new IOException("获取建科token信息失败 " + response.toString());
|
|
|
+ }
|
|
|
+ JSONObject jsonObject =JSON.parseObject(response.body().string());
|
|
|
+ logger.info("获取建科token返回信息:" + jsonObject.toJSONString());
|
|
|
+ token = jsonObject.getString("access_token");
|
|
|
+ if(!StringUtils.isEmpty(token)) {
|
|
|
+ //token 换取用户信息
|
|
|
+ Request pReq = buildProfileRequest(token);
|
|
|
+ Response pResp = client.newCall(request).execute();
|
|
|
+ if (!pResp.isSuccessful()) {
|
|
|
+ throw new IOException("token换取建科用户信息失败 " + pResp.toString());
|
|
|
+ }
|
|
|
+ userJson =JSON.parseObject(pResp.body().string());
|
|
|
+ logger.info("获取建科用户返回信息:" + userJson.toJSONString());
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ throw new IOException(code + "获取建科token息失败 ", e);
|
|
|
+ }
|
|
|
+
|
|
|
+ if(userJson == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ JSONObject attrs = userJson.getJSONObject("attributes");
|
|
|
+ Map<String, Object> map = new HashMap<>();
|
|
|
+ map.put("userId", attrs.getString("sysadmintest"));
|
|
|
+ map.put("nickName", attrs.getString("sysadmintest"));
|
|
|
+ map.put("token", token);
|
|
|
+ return map;
|
|
|
+ }
|
|
|
+
|
|
|
+ private String createJkToken() {
|
|
|
+ JWTCreator.Builder builder = JWT.create().withIssuer(jkConfig.getIamAppid());
|
|
|
+ String sign = builder.withIssuedAt(new Date()).withJWTId(UUID.randomUUID().toString()).sign(Algorithm.HMAC256(jkConfig.getIamAppsecret()));
|
|
|
+ String token = String.join(" ", BEARER, sign);
|
|
|
+ return token;
|
|
|
+ }
|
|
|
+
|
|
|
+ private Request buildRequest(String url,int page) {
|
|
|
+ Map<String, Object> requestBodyMap = new HashMap<>();
|
|
|
+ requestBodyMap.put(PARAM_ST, getStartTime());
|
|
|
+ requestBodyMap.put(PARAM_PAGE, page);
|
|
|
+ requestBodyMap.put(PARAM_SIZE, DEFAULT_SIZE);
|
|
|
+ // 将 Map 转换为 JSON 字符串
|
|
|
+ String requestBodyJson = JSON.toJSONString(requestBodyMap);
|
|
|
+
|
|
|
+ // 构建请求体
|
|
|
+ RequestBody body = RequestBody.create(JSON_UTF8,requestBodyJson);
|
|
|
+ Request request = new Request.Builder()
|
|
|
+ .addHeader("accept", "*/*")
|
|
|
+ .addHeader("Authorization", createJkToken())
|
|
|
+ .addHeader("Content-Type", "application/json")
|
|
|
+ .post(body)
|
|
|
+ .url(url).get().build();
|
|
|
+ logger.info("请求head:{}", request.headers().toString());
|
|
|
+// logger.info("请求body:{}", request.body().toString());
|
|
|
+ return request;
|
|
|
+ }
|
|
|
+
|
|
|
+ private Request buildTokenRequest(String code) {
|
|
|
+ Map<String, Object> requestBodyMap = new HashMap<>();
|
|
|
+ requestBodyMap.put(PARAM_GRANT_TYPE, GRANT_TYPE_AC);
|
|
|
+ requestBodyMap.put(PARAM_CLIENT_ID, jkConfig.getIamAppid());
|
|
|
+ requestBodyMap.put(PARAM_CLIENT_SECRET, jkConfig.getIamAppsecret());
|
|
|
+ requestBodyMap.put(PARAM_CODE, code);
|
|
|
+ // 将 Map 转换为 JSON 字符串
|
|
|
+ String requestBodyJson = JSON.toJSONString(requestBodyMap);
|
|
|
+
|
|
|
+ // 构建请求体
|
|
|
+ RequestBody body = RequestBody.create(JSON_UTF8,requestBodyJson);
|
|
|
+ Request request = new Request.Builder()
|
|
|
+ .addHeader("accept", "*/*")
|
|
|
+ .addHeader("Content-Type", "application/json")
|
|
|
+ .post(body)
|
|
|
+ .url(jkConfig.getIamTokenUrl()).get().build();
|
|
|
+ logger.info("请求head:{}", request.headers().toString());
|
|
|
+ return request;
|
|
|
+ }
|
|
|
+
|
|
|
+ private Request buildProfileRequest(String token) {
|
|
|
+ // 构建带参数的 URL
|
|
|
+ HttpUrl.Builder urlBuilder = HttpUrl.parse(jkConfig.getIamProfileUrl()).newBuilder();
|
|
|
+ urlBuilder.addQueryParameter(PARAM_ACCESS_TOKEN, token);
|
|
|
+ String url = urlBuilder.build().toString();
|
|
|
+ Request request = new Request.Builder()
|
|
|
+ .url(url)
|
|
|
+ .build();
|
|
|
+
|
|
|
+ logger.info("token获取用户信息请求url", url);
|
|
|
+ return request;
|
|
|
+ }
|
|
|
+
|
|
|
+ private String getStartTime() {
|
|
|
+ return String.valueOf(System.currentTimeMillis());
|
|
|
+ }
|
|
|
+
|
|
|
+ private boolean isJsonObject(String data) {
|
|
|
+ try {
|
|
|
+ JSON.parseObject(data);
|
|
|
+ return true;
|
|
|
+ } catch (Exception e) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|