10 changed files with 165 additions and 11 deletions
@ -0,0 +1,96 @@ |
|||
package com.qs.serve.common.util; |
|||
|
|||
import com.alibaba.fastjson.JSON; |
|||
import com.alibaba.fastjson.JSONObject; |
|||
import com.qs.serve.common.config.properties.ProjectProperties; |
|||
import com.qs.serve.common.model.consts.GySysConst; |
|||
import com.qs.serve.common.model.dto.R; |
|||
import lombok.experimental.UtilityClass; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.apache.http.ParseException; |
|||
import org.apache.http.client.ClientProtocolException; |
|||
import org.apache.http.client.methods.CloseableHttpResponse; |
|||
import org.apache.http.client.methods.HttpGet; |
|||
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.impl.client.HttpClients; |
|||
import org.apache.http.protocol.HTTP; |
|||
import org.apache.http.util.EntityUtils; |
|||
|
|||
import javax.servlet.http.HttpServletRequest; |
|||
import java.io.IOException; |
|||
import java.util.HashMap; |
|||
import java.util.Map; |
|||
|
|||
/** |
|||
* @author YenHex |
|||
* @since 2022/11/23 |
|||
*/ |
|||
@Slf4j |
|||
@UtilityClass |
|||
public class BirHttpUtil { |
|||
|
|||
/** |
|||
* 判断当前环境是否请求bir |
|||
* @return |
|||
*/ |
|||
public static boolean isRestBir(){ |
|||
ProjectProperties projectProperties = SpringUtils.getBean(ProjectProperties.class); |
|||
return projectProperties.getBirService().equals("true"); |
|||
} |
|||
|
|||
public static String getBaseUrl(){ |
|||
ProjectProperties projectProperties = SpringUtils.getBean(ProjectProperties.class); |
|||
boolean isRestBir = projectProperties.getBirService().equals("true"); |
|||
if(isRestBir){ |
|||
return projectProperties.getBirServiceUrl(); |
|||
} |
|||
return null; |
|||
} |
|||
|
|||
public static R get(String action){ |
|||
return get(action,null); |
|||
} |
|||
|
|||
public static R get(String action,Object query){ |
|||
String baseUrl = getBaseUrl(); |
|||
String url = baseUrl+"/"+action; |
|||
if(query!=null){ |
|||
Map map = JsonUtil.objectToMap(query); |
|||
String params = HttpUtil.createUrl(map); |
|||
if(url.contains("?")){ |
|||
url = url+params; |
|||
}else { |
|||
url = url+"?"+params; |
|||
} |
|||
} |
|||
HashMap<String,String> hearders = new HashMap<>(); |
|||
HttpServletRequest request = ServletUtils.getRequest(); |
|||
String tenant = request.getHeader(GySysConst.TENANT_PROP); |
|||
String token = request.getHeader(GySysConst.AUTHORIZATION_PROP); |
|||
hearders.put(GySysConst.TENANT_PROP,tenant); |
|||
hearders.put(GySysConst.AUTHORIZATION_PROP,token); |
|||
//自动封装翻页参数
|
|||
if(!url.contains("?")){ |
|||
url += "?"; |
|||
} |
|||
if(PageUtil.getPageSize()!=null){ |
|||
url += "&pageSize="+PageUtil.getPageSize(); |
|||
} |
|||
if(PageUtil.getPageNum()!=null){ |
|||
url += "&pageNum="+PageUtil.getPageNum(); |
|||
} |
|||
String result = HttpUtil.doGet(url,hearders); |
|||
JSONObject object = JSON.parseObject(result); |
|||
Integer status = object.getInteger("status"); |
|||
String msg = object.getString("msg"); |
|||
Object data = object.get("data"); |
|||
R r = R.ok(data); |
|||
r.setStatus(status); |
|||
r.setMsg(msg); |
|||
return r; |
|||
} |
|||
|
|||
} |
Loading…
Reference in new issue