123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- package com.technology.huahai.web.zx;
- import com.alibaba.fastjson2.JSONObject;
- import com.technology.huahai.common.constant.RedisKeyConstant;
- import com.technology.huahai.common.core.controller.domain.AjaxResult;
- import com.technology.huahai.common.core.page.TableDataInfo;
- import com.technology.huahai.common.exception.CsmException;
- import com.technology.huahai.common.utils.JsonUtils;
- import com.technology.huahai.common.utils.RedisUtils;
- import com.technology.huahai.common.utils.StringUtils;
- import com.technology.huahai.web.dto.ChannelDto;
- import com.technology.huahai.web.vo.ZxResultDto;
- 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.http.HttpEntity;
- import org.springframework.http.HttpHeaders;
- import org.springframework.http.MediaType;
- import org.springframework.http.ResponseEntity;
- import org.springframework.stereotype.Service;
- import org.springframework.util.LinkedMultiValueMap;
- import org.springframework.util.MultiValueMap;
- import org.springframework.web.client.RestTemplate;
- @Service
- public class ZxService {
- protected final Logger logger = LoggerFactory.getLogger(this.getClass());
- @Autowired
- private RestTemplate restTemplate;
- @Autowired
- RedisUtils redisUtils;
- @Value("${zx.userId}")
- private String userId;
- @Value("${zx.userKey}")
- private String userKey;
- @Value("${zx.loginUrl}")
- private String loginUrl;
- @Value("${zx.csmCtMethodKey}")
- private String csmCtMethodKey;
- @Value("${zx.outinterfaceUrl}")
- private String outinterfaceUrl;
- /**
- * 场所码内网透传接口(操作类)
- * @param o
- * @param o
- * methodName
- * @return
- */
- public AjaxResult cmsEmsInterfaceOperation(Object o, String methodName){
- String token = getZxTokenByRedis();
- if(StringUtils.isBlank(token)){
- throw new CsmException("总线token获取失败");
- }else{
- ChannelDto channelDto = new ChannelDto();
- channelDto.setMethod(methodName);
- channelDto.setData(o);
- String jsonStr = JsonUtils.writeValueAsString(channelDto);
- MultiValueMap<String, String> formData = new LinkedMultiValueMap<>();
- formData.add("methodkey", csmCtMethodKey);
- formData.add("postDataJson", jsonStr);
- // 4. 构造请求头,指定 Content-Type 为 application/x-www-form-urlencoded
- HttpHeaders headers = new HttpHeaders();
- headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
- headers.set("Authorization","Bearer "+token);
- //headers.set("X-Service-Name",methodName);
- // 5. 构造请求实体(包含头和体)
- HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(formData, headers);
- // 6. 发送请求并获取响应
- logger.info("本次总线请求参数为:{}",jsonStr);
- logger.info("本次总线请求token为:{}",headers.get("Authorization"));
- ResponseEntity<String> zxResult = restTemplate.postForEntity(outinterfaceUrl, requestEntity, String.class);
- logger.info("总线响应结果 {}", zxResult.getBody());
- ZxResultDto zxResultDto = JsonUtils.parseJson(zxResult.getBody(),ZxResultDto.class);
- if(0==zxResultDto.getStatus()){
- String data = zxResultDto.getData();
- AjaxResult ajaxResult = JsonUtils.parseJson(data, AjaxResult.class);
- return ajaxResult;
- }else{
- throw new CsmException(zxResultDto.getMessage());
- }
- }
- }
- /**
- * 场所码内网透传接口(查询列表类)
- * @param o
- * @param methodName
- * @return
- */
- public TableDataInfo cmsEmsInterfaceQueryList(Object o, String methodName){
- String token = getZxTokenByRedis();
- if(StringUtils.isBlank(token)){
- throw new CsmException("总线token获取失败");
- }else{
- ChannelDto channelDto = new ChannelDto();
- channelDto.setMethod(methodName);
- channelDto.setData(o);
- String jsonStr = JsonUtils.writeValueAsString(channelDto);
- MultiValueMap<String, String> formData = new LinkedMultiValueMap<>();
- formData.add("methodkey", csmCtMethodKey);
- formData.add("postDataJson", jsonStr);
- // 4. 构造请求头,指定 Content-Type 为 application/x-www-form-urlencoded
- HttpHeaders headers = new HttpHeaders();
- headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
- headers.set("Authorization","Bearer "+token);
- //headers.set("X-Service-Name",methodName);
- // 5. 构造请求实体(包含头和体)
- HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(formData, headers);
- // 6. 发送请求并获取响应
- logger.info("本次总线请求参数为:{}",jsonStr);
- logger.info("本次总线请求token为:{}",headers.get("Authorization"));
- ResponseEntity<String> zxResult = restTemplate.postForEntity(outinterfaceUrl, requestEntity, String.class);
- logger.info("总线响应结果 {}", zxResult.getBody());
- ZxResultDto zxResultDto = JsonUtils.parseJson(zxResult.getBody(),ZxResultDto.class);
- if(0==zxResultDto.getStatus()){
- String data = zxResultDto.getData();
- TableDataInfo tableDataInfo = JsonUtils.parseJson(data, TableDataInfo.class);
- return tableDataInfo;
- }else{
- throw new CsmException(zxResultDto.getMessage());
- }
- }
- }
- public String getZxTokenByRedis(){
- Object o = redisUtils.get(RedisKeyConstant.zxtokenKey);
- if(null != o && StringUtils.isNotEmpty(String.valueOf(o)) && !"null".equals(String.valueOf(o))){
- return String.valueOf(o);
- }else{
- return getZxTokenByInterface();
- }
- }
- public synchronized String getZxTokenByInterface(){
- Object o = redisUtils.get(RedisKeyConstant.zxtokenKey);
- if(null != o && StringUtils.isNotEmpty(String.valueOf(o)) && !"null".equals(String.valueOf(o))){
- return String.valueOf(o);
- }else{
- MultiValueMap<String, String> formData = new LinkedMultiValueMap<>();
- formData.add("userID", userId);
- formData.add("userKey", userKey);
- // 4. 构造请求头,指定 Content-Type 为 application/x-www-form-urlencoded
- HttpHeaders headers = new HttpHeaders();
- headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
- // 5. 构造请求实体(包含头和体)
- HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(formData, headers);
- // 6. 发送请求并获取响应(假设返回的是 String 类型)
- JSONObject jsonObject = restTemplate.postForObject(loginUrl, requestEntity, JSONObject.class);
- logger.info("总线token获取结果 {}",jsonObject);
- Object access_token = jsonObject.get("access_token");
- if(null == access_token){
- throw new CsmException("获取总线token失败");
- }else{
- String token = String.valueOf(access_token);
- redisUtils.set(RedisKeyConstant.zxtokenKey,token,7000);
- return token;
- }
- }
- }
- }
|