10 changed files with 1014 additions and 0 deletions
@ -0,0 +1,187 @@ |
|||
package com.zdxt.enforcementcode.quartz.job; |
|||
|
|||
import cn.hutool.core.map.MapUtil; |
|||
import com.alibaba.fastjson.JSONArray; |
|||
import com.alibaba.fastjson.JSONObject; |
|||
import com.zdxt.enforcementcode.service.ISrzDeptService; |
|||
import lombok.RequiredArgsConstructor; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
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.dromara.system.domain.vo.SysDictDataVo; |
|||
import org.dromara.system.service.ISysDictDataService; |
|||
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.security.MessageDigest; |
|||
import java.util.*; |
|||
import java.util.stream.Collectors; |
|||
|
|||
/** |
|||
* 同步市认证部门组织信息表原始数据 |
|||
*/ |
|||
@Slf4j |
|||
@Component |
|||
@RequiredArgsConstructor |
|||
public class SrzDeptOriginalDataJob { |
|||
|
|||
@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 ISrzDeptService srzDeptService; |
|||
@Autowired |
|||
private ISysDictDataService sysDictDataService; |
|||
|
|||
public static Map<String,String> params = new HashMap<>(); |
|||
|
|||
private static final Logger log = LoggerFactory.getLogger(SrzDeptOriginalDataJob.class); |
|||
|
|||
|
|||
public void run(){ |
|||
this.getData(); |
|||
} |
|||
|
|||
/** 同步部门数据 */ |
|||
public void getData(){ |
|||
List<SysDictDataVo> srz_item = sysDictDataService.selectDictDataByType("srz_item"); |
|||
Map<String,String> map = srz_item.stream().collect(Collectors.toMap(SysDictDataVo::getDictLabel,SysDictDataVo::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; } |
|||
// 插入更新数据库
|
|||
log.info("-------------------> 拉取到组织数量: {}", dataArr.size()); |
|||
Map<String, Integer> resMap = srzDeptService.saveSrzDept(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 = ""; //请求接⼝的参数
|
|||
|
|||
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.enforcementcode.quartz.job; |
|||
|
|||
import cn.hutool.core.map.MapUtil; |
|||
import com.alibaba.fastjson.JSONArray; |
|||
import com.alibaba.fastjson.JSONObject; |
|||
import com.zdxt.enforcementcode.service.ISrzUserService; |
|||
import lombok.RequiredArgsConstructor; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
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.dromara.system.domain.vo.SysDictDataVo; |
|||
import org.dromara.system.service.ISysDictDataService; |
|||
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.security.MessageDigest; |
|||
import java.util.*; |
|||
import java.util.stream.Collectors; |
|||
|
|||
/** |
|||
* 同步市认证用户组织信息原始数据 |
|||
*/ |
|||
@Slf4j |
|||
@Component |
|||
@RequiredArgsConstructor |
|||
public class SrzUserOriginalDataJob { |
|||
|
|||
@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 ISrzUserService srzUserService; |
|||
@Autowired |
|||
private ISysDictDataService sysDictDataService; |
|||
|
|||
public static Map<String,String> params = new HashMap<>(); |
|||
|
|||
private static final Logger log = LoggerFactory.getLogger(SrzUserOriginalDataJob.class); |
|||
|
|||
|
|||
public void run(){ |
|||
this.getData(); |
|||
} |
|||
|
|||
/** |
|||
* 获取市认证用户原始数据 |
|||
*/ |
|||
public void getData(){ |
|||
List<SysDictDataVo> srz_item = sysDictDataService.selectDictDataByType("srz_item"); |
|||
Map<String,String> map = srz_item.stream().collect(Collectors.toMap(SysDictDataVo::getDictLabel,SysDictDataVo::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); |
|||
log.info("====================== access_token ======================"); |
|||
log.info("获取市认证令牌:{}",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 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"); |
|||
log.info("市认证过来的用户数量---> : {}", tmpJsonObj.get("'total'")); |
|||
if (null == dataArr || dataArr.size()==0) { break; } |
|||
Map<String, Integer> resMap = srzUserService.saveSrzUser(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 = ""; //请求接⼝的参数
|
|||
|
|||
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