14 changed files with 557 additions and 49 deletions
@ -0,0 +1,195 @@ |
|||||
|
package com.zdxt.app.task; |
||||
|
|
||||
|
import cn.hutool.core.map.MapUtil; |
||||
|
import com.alibaba.fastjson.JSONArray; |
||||
|
import com.alibaba.fastjson.JSONObject; |
||||
|
import com.zdxt.auth.dto.UserBean; |
||||
|
import com.zdxt.auth.feign.UserApi; |
||||
|
import com.zdxt.auth.project.system.dept.service.IDeptService; |
||||
|
import com.zdxt.auth.project.system.dict.service.IDictDataService; |
||||
|
import com.zdxt.auth.project.system.user.service.IUserService; |
||||
|
import com.zdxt.domain.auth.DictData; |
||||
|
import org.apache.http.HttpEntity; |
||||
|
import org.apache.http.client.methods.CloseableHttpResponse; |
||||
|
import org.apache.http.client.methods.HttpPost; |
||||
|
import org.apache.http.entity.StringEntity; |
||||
|
import org.apache.http.impl.client.CloseableHttpClient; |
||||
|
import org.apache.http.impl.client.HttpClientBuilder; |
||||
|
import org.apache.http.util.EntityUtils; |
||||
|
import org.slf4j.Logger; |
||||
|
import org.slf4j.LoggerFactory; |
||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||
|
import org.springframework.beans.factory.annotation.Value; |
||||
|
import org.springframework.stereotype.Component; |
||||
|
|
||||
|
import java.io.IOException; |
||||
|
import java.nio.charset.StandardCharsets; |
||||
|
import java.nio.file.Files; |
||||
|
import java.nio.file.Paths; |
||||
|
import java.security.MessageDigest; |
||||
|
import java.util.*; |
||||
|
import java.util.stream.Collectors; |
||||
|
|
||||
|
|
||||
|
/** |
||||
|
* @Author: kylin |
||||
|
* @Date: 2024/5/9 2:58 下午 |
||||
|
* @Version: 1.0 |
||||
|
* @Desc: TODO |
||||
|
*/ |
||||
|
@Component("synDeptTask") |
||||
|
public class SynDeptTask { |
||||
|
|
||||
|
@Value("${srnwg.systemCode}") |
||||
|
private String systemCode; |
||||
|
@Value("${srnwg.integrationKey}") |
||||
|
private String integrationKey; |
||||
|
@Value("${srnwg.url}") |
||||
|
private String znwgUrl; |
||||
|
@Value("${srnwg.paasId}") |
||||
|
private String paasId; |
||||
|
@Value("${srnwg.paasToken}") |
||||
|
private String paasToken; |
||||
|
|
||||
|
@Autowired private UserApi userApi; |
||||
|
@Autowired private IUserService userService; |
||||
|
@Autowired private IDeptService deptService; |
||||
|
@Autowired |
||||
|
private IDictDataService iDictDataService; |
||||
|
|
||||
|
public static Map<String,String> params = new HashMap<>(); |
||||
|
|
||||
|
private static final Logger log = LoggerFactory.getLogger(SynDeptTask.class); |
||||
|
|
||||
|
|
||||
|
public void run(){ |
||||
|
this.dept(); |
||||
|
} |
||||
|
|
||||
|
/** 同步部门数据 */ |
||||
|
public void dept(){ |
||||
|
List<DictData> srz_item = iDictDataService.selectDictDataByType("srz_item"); |
||||
|
Map<String,String> map = srz_item.stream().collect(Collectors.toMap(DictData::getDictLabel,DictData::getDictValue)); |
||||
|
|
||||
|
if(MapUtil.isNotEmpty(map)){ |
||||
|
params = map; |
||||
|
} |
||||
|
String token = ""; |
||||
|
String destroyTken = ""; |
||||
|
try { |
||||
|
// 获取 access_token
|
||||
|
String tokenJson = connect("/ebus/sfrzhqxglmk/sysPullLogin?systemCode="+systemCode+"&integrationKey="+integrationKey); |
||||
|
JSONObject tokenObj = JSONObject.parseObject(tokenJson); |
||||
|
System.out.println("====================== access_token ======================"); |
||||
|
System.out.println(tokenObj); |
||||
|
System.out.println("====================== access_token ======================"); |
||||
|
token = tokenObj.getString("data"); |
||||
|
|
||||
|
// 获取 用户信息
|
||||
|
int page = 0; |
||||
|
int pageSize = 500; |
||||
|
List<Map<String, Integer>> sumMap = new ArrayList<>(); |
||||
|
|
||||
|
int insertNum = 0; |
||||
|
int updateNum = 0; |
||||
|
page = 0; |
||||
|
// 获取 部门信息
|
||||
|
while (true) { |
||||
|
String deptInfoJson = connect("/ebus/sfrzhqxglmk/sysPullOrg?token="+token+"&page="+page+"&pageSize="+pageSize+"&status=0"); |
||||
|
JSONObject sourceObj = JSONObject.parseObject(deptInfoJson); |
||||
|
JSONObject tmpJsonObj = sourceObj.getJSONObject("data"); |
||||
|
if(null==tmpJsonObj) { |
||||
|
log.info("=====================> tmpJsonObj 为空"); |
||||
|
break; |
||||
|
} |
||||
|
JSONArray dataArr = tmpJsonObj.getJSONArray("data"); |
||||
|
if (null == dataArr || dataArr.size()==0) { break; } |
||||
|
// 插入更新数据库
|
||||
|
Map<String, Integer> resMap = deptService.addSrzDept(dataArr,token); |
||||
|
sumMap.add(resMap); |
||||
|
page++; |
||||
|
} |
||||
|
|
||||
|
insertNum = 0; |
||||
|
updateNum = 0; |
||||
|
for (Map<String, Integer> item : sumMap) { |
||||
|
insertNum += item.get("insertNum"); |
||||
|
updateNum += item.get("updateNum"); |
||||
|
} |
||||
|
log.info("新增部门数量 : {} / 更新部门数量 : {}", insertNum, updateNum); |
||||
|
|
||||
|
// 注销token
|
||||
|
destroyTken = connect("/ebus/sfrzhqxglmk/sysPullLogout?token="+token); |
||||
|
System.out.println("====================== destroyTken ======================"); |
||||
|
System.out.println(destroyTken); |
||||
|
System.out.println("====================== destroyTken ======================"); |
||||
|
} catch (Exception e) { |
||||
|
e.printStackTrace(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
private String connect(String apiPath) { |
||||
|
//各种参数
|
||||
|
|
||||
|
try { |
||||
|
// String params = "{\"uid\":\"5b07b3de672c4e999c4b04f4\"}"; //请求接⼝的参数
|
||||
|
String params = ""; //请求接⼝的参数
|
||||
|
|
||||
|
long now = new Date().getTime(); |
||||
|
String timestamp = Long.toString((long) Math.floor(now / 1000)); |
||||
|
String nonce = Long.toHexString(now) + "-" + Long.toHexString((long) Math.floor(Math.random() * 0xFFFFFF)); |
||||
|
String signature = toSHA256(timestamp + paasToken + nonce + timestamp); |
||||
|
|
||||
|
|
||||
|
//创建CloseableHttpClient
|
||||
|
CloseableHttpClient client = HttpClientBuilder.create().build(); |
||||
|
HttpPost httpPost = new HttpPost(znwgUrl + apiPath); |
||||
|
httpPost.setHeader("x-tif-paasid", paasId); |
||||
|
httpPost.setHeader("x-tif-timestamp", timestamp); |
||||
|
httpPost.setHeader("x-tif-signature", signature); |
||||
|
httpPost.setHeader("x-tif-nonce", nonce); |
||||
|
httpPost.setHeader("Content-Type", "application/json"); |
||||
|
httpPost.setHeader("Cache-Control", "no-cache"); |
||||
|
HttpEntity entity = new StringEntity(params); |
||||
|
|
||||
|
|
||||
|
httpPost.setEntity(entity); |
||||
|
|
||||
|
CloseableHttpResponse response = client.execute(httpPost); |
||||
|
|
||||
|
return EntityUtils.toString(response.getEntity()); |
||||
|
} catch (Exception e) { |
||||
|
e.printStackTrace(); |
||||
|
} |
||||
|
return null; |
||||
|
} |
||||
|
|
||||
|
|
||||
|
|
||||
|
private String toSHA256(String str) throws Exception { |
||||
|
MessageDigest messageDigest; |
||||
|
String encodeStr = ""; |
||||
|
try { |
||||
|
messageDigest = MessageDigest.getInstance("SHA-256"); |
||||
|
messageDigest.update(str.getBytes("UTF-8")); |
||||
|
encodeStr = byte2Hex(messageDigest.digest()); |
||||
|
} catch (Exception e) { |
||||
|
throw e; |
||||
|
} |
||||
|
return encodeStr; |
||||
|
} |
||||
|
|
||||
|
// byte转换成16进制
|
||||
|
private String byte2Hex(byte[] bytes) { |
||||
|
StringBuffer stringBuffer = new StringBuffer(); |
||||
|
String temp = null; |
||||
|
for (int i = 0; i < bytes.length; i++) { |
||||
|
temp = Integer.toHexString(bytes[i] & 0xFF); |
||||
|
if (temp.length() == 1) { |
||||
|
stringBuffer.append("0"); |
||||
|
} |
||||
|
stringBuffer.append(temp); |
||||
|
} |
||||
|
return stringBuffer.toString(); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,187 @@ |
|||||
|
package com.zdxt.app.task; |
||||
|
|
||||
|
import cn.hutool.core.map.MapUtil; |
||||
|
import com.alibaba.fastjson.JSONArray; |
||||
|
import com.alibaba.fastjson.JSONObject; |
||||
|
import com.zdxt.auth.dto.UserBean; |
||||
|
import com.zdxt.auth.feign.UserApi; |
||||
|
import com.zdxt.auth.project.system.dept.service.IDeptService; |
||||
|
import com.zdxt.auth.project.system.dict.service.IDictDataService; |
||||
|
import com.zdxt.auth.project.system.user.service.IUserService; |
||||
|
import com.zdxt.domain.auth.DictData; |
||||
|
import org.apache.http.HttpEntity; |
||||
|
import org.apache.http.client.methods.CloseableHttpResponse; |
||||
|
import org.apache.http.client.methods.HttpPost; |
||||
|
import org.apache.http.entity.StringEntity; |
||||
|
import org.apache.http.impl.client.CloseableHttpClient; |
||||
|
import org.apache.http.impl.client.HttpClientBuilder; |
||||
|
import org.apache.http.util.EntityUtils; |
||||
|
import org.slf4j.Logger; |
||||
|
import org.slf4j.LoggerFactory; |
||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||
|
import org.springframework.beans.factory.annotation.Value; |
||||
|
import org.springframework.stereotype.Component; |
||||
|
|
||||
|
import java.io.IOException; |
||||
|
import java.nio.charset.StandardCharsets; |
||||
|
import java.nio.file.Files; |
||||
|
import java.nio.file.Paths; |
||||
|
import java.security.MessageDigest; |
||||
|
import java.util.*; |
||||
|
import java.util.stream.Collectors; |
||||
|
|
||||
|
|
||||
|
/** |
||||
|
* @Author: kylin |
||||
|
* @Date: 2024/5/9 2:58 下午 |
||||
|
* @Version: 1.0 |
||||
|
* @Desc: TODO |
||||
|
*/ |
||||
|
@Component("synUserInfoTask") |
||||
|
public class SynUserInfoTask { |
||||
|
|
||||
|
@Value("${srnwg.systemCode}") |
||||
|
private String systemCode; |
||||
|
@Value("${srnwg.integrationKey}") |
||||
|
private String integrationKey; |
||||
|
@Value("${srnwg.url}") |
||||
|
private String znwgUrl; |
||||
|
@Value("${srnwg.paasId}") |
||||
|
private String paasId; |
||||
|
@Value("${srnwg.paasToken}") |
||||
|
private String paasToken; |
||||
|
|
||||
|
@Autowired private UserApi userApi; |
||||
|
@Autowired private IUserService userService; |
||||
|
@Autowired private IDeptService deptService; |
||||
|
@Autowired |
||||
|
private IDictDataService iDictDataService; |
||||
|
|
||||
|
public static Map<String,String> params = new HashMap<>(); |
||||
|
|
||||
|
private static final Logger log = LoggerFactory.getLogger(SynUserInfoTask.class); |
||||
|
|
||||
|
|
||||
|
public void run(){ |
||||
|
this.userInfo(); |
||||
|
} |
||||
|
|
||||
|
/** 同步用户数据 */ |
||||
|
public void userInfo(){ |
||||
|
List<DictData> srz_item = iDictDataService.selectDictDataByType("srz_item"); |
||||
|
Map<String,String> map = srz_item.stream().collect(Collectors.toMap(DictData::getDictLabel,DictData::getDictValue)); |
||||
|
|
||||
|
if(MapUtil.isNotEmpty(map)){ |
||||
|
params = map; |
||||
|
} |
||||
|
String token = ""; |
||||
|
String destroyTken = ""; |
||||
|
try { |
||||
|
// 获取 access_token
|
||||
|
String tokenJson = connect("/ebus/sfrzhqxglmk/sysPullLogin?systemCode="+systemCode+"&integrationKey="+integrationKey); |
||||
|
JSONObject tokenObj = JSONObject.parseObject(tokenJson); |
||||
|
System.out.println("====================== access_token ======================"); |
||||
|
System.out.println(tokenObj); |
||||
|
System.out.println("====================== access_token ======================"); |
||||
|
token = tokenObj.getString("data"); |
||||
|
|
||||
|
// 获取 用户信息
|
||||
|
int page = 0; |
||||
|
int pageSize = 500; |
||||
|
List<Map<String, Integer>> sumMap = new ArrayList<>(); |
||||
|
while (true) { |
||||
|
String userInfoJson = connect("/ebus/sfrzhqxglmk/sysPullTacc?token="+token+"&page="+page+"&pageSize="+pageSize+"&status=0"); |
||||
|
JSONObject sourceObj = JSONObject.parseObject(userInfoJson); |
||||
|
JSONObject tmpJsonObj = sourceObj.getJSONObject("data"); |
||||
|
if(null==tmpJsonObj) { |
||||
|
log.info("=====================> tmpJsonObj 为空"); |
||||
|
break; |
||||
|
} |
||||
|
JSONArray dataArr = tmpJsonObj.getJSONArray("data"); |
||||
|
if (null == dataArr || dataArr.size()==0) { break; } |
||||
|
Map<String, Integer> resMap = userService.addSrzUser(dataArr, token); |
||||
|
sumMap.add(resMap); |
||||
|
page++; |
||||
|
} |
||||
|
|
||||
|
int insertNum = 0; |
||||
|
int updateNum = 0; |
||||
|
for (Map<String, Integer> item : sumMap) { |
||||
|
insertNum += item.get("insertNum"); |
||||
|
updateNum += item.get("updateNum"); |
||||
|
} |
||||
|
log.info("新增用户数量 : {} / 更新用户数量 : {}", insertNum, updateNum); |
||||
|
|
||||
|
// 注销token
|
||||
|
destroyTken = connect("/ebus/sfrzhqxglmk/sysPullLogout?token="+token); |
||||
|
System.out.println("====================== destroyTken ======================"); |
||||
|
System.out.println(destroyTken); |
||||
|
System.out.println("====================== destroyTken ======================"); |
||||
|
} catch (Exception e) { |
||||
|
e.printStackTrace(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
private String connect(String apiPath) { |
||||
|
//各种参数
|
||||
|
|
||||
|
try { |
||||
|
// String params = "{\"uid\":\"5b07b3de672c4e999c4b04f4\"}"; //请求接⼝的参数
|
||||
|
String params = ""; //请求接⼝的参数
|
||||
|
|
||||
|
long now = new Date().getTime(); |
||||
|
String timestamp = Long.toString((long) Math.floor(now / 1000)); |
||||
|
String nonce = Long.toHexString(now) + "-" + Long.toHexString((long) Math.floor(Math.random() * 0xFFFFFF)); |
||||
|
String signature = toSHA256(timestamp + paasToken + nonce + timestamp); |
||||
|
|
||||
|
|
||||
|
//创建CloseableHttpClient
|
||||
|
CloseableHttpClient client = HttpClientBuilder.create().build(); |
||||
|
HttpPost httpPost = new HttpPost(znwgUrl + apiPath); |
||||
|
httpPost.setHeader("x-tif-paasid", paasId); |
||||
|
httpPost.setHeader("x-tif-timestamp", timestamp); |
||||
|
httpPost.setHeader("x-tif-signature", signature); |
||||
|
httpPost.setHeader("x-tif-nonce", nonce); |
||||
|
httpPost.setHeader("Content-Type", "application/json"); |
||||
|
httpPost.setHeader("Cache-Control", "no-cache"); |
||||
|
HttpEntity entity = new StringEntity(params); |
||||
|
|
||||
|
|
||||
|
httpPost.setEntity(entity); |
||||
|
|
||||
|
CloseableHttpResponse response = client.execute(httpPost); |
||||
|
|
||||
|
return EntityUtils.toString(response.getEntity()); |
||||
|
} catch (Exception e) { |
||||
|
e.printStackTrace(); |
||||
|
} |
||||
|
return null; |
||||
|
} |
||||
|
|
||||
|
private String toSHA256(String str) throws Exception { |
||||
|
MessageDigest messageDigest; |
||||
|
String encodeStr = ""; |
||||
|
try { |
||||
|
messageDigest = MessageDigest.getInstance("SHA-256"); |
||||
|
messageDigest.update(str.getBytes("UTF-8")); |
||||
|
encodeStr = byte2Hex(messageDigest.digest()); |
||||
|
} catch (Exception e) { |
||||
|
throw e; |
||||
|
} |
||||
|
return encodeStr; |
||||
|
} |
||||
|
|
||||
|
// byte转换成16进制
|
||||
|
private String byte2Hex(byte[] bytes) { |
||||
|
StringBuffer stringBuffer = new StringBuffer(); |
||||
|
String temp = null; |
||||
|
for (int i = 0; i < bytes.length; i++) { |
||||
|
temp = Integer.toHexString(bytes[i] & 0xFF); |
||||
|
if (temp.length() == 1) { |
||||
|
stringBuffer.append("0"); |
||||
|
} |
||||
|
stringBuffer.append(temp); |
||||
|
} |
||||
|
return stringBuffer.toString(); |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue