package com.ruzhoukj.sms.service.impl; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.google.common.collect.Maps; import com.platform.exception.BusinessException; import com.ruzhoukj.sms.dao.CustomerProjectMapper; import com.ruzhoukj.sms.entity.CustomerProjectEntity; import com.ruzhoukj.sms.service.CustomerProjectService; 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.HttpClients; import org.apache.http.util.EntityUtils; import org.springframework.stereotype.Service; import java.util.LinkedHashMap; import java.util.List; /** * @author : wujipeng * @date : 2025/11/4 10:03 * @Version: 1.0 * @Desc : 描述信息 */ @Service public class CustomerProjectServiceImpl extends ServiceImpl implements CustomerProjectService { @Override public Object jg() { LinkedHashMap result = Maps.newLinkedHashMap(); //登录获取token String token = null; try { String loginResponse = postWithApacheHttpClient("http://192.168.10.3:71/api/auth/login", "{\"username\":\"wsadmin\",\"password\":\"cd2350c714eb2cf9b4eacde8d4834823\"}", null); JSONObject jsonObject = JSON.parseObject(loginResponse); int loginCode = jsonObject.getInteger("code"); if (loginCode == 200) { JSONObject jsonObject1 = jsonObject.getJSONObject("data"); token = jsonObject1.getString("access_token"); result.put("登录接口", "成功"); } else { result.put("登录接口", "失败"); return result; } } catch (Exception e) { throw new RuntimeException(e); } //查询接口列表 List customerProjectList = list(new LambdaQueryWrapper().eq(CustomerProjectEntity::getProject, "经管")); //执行接口调用 for (CustomerProjectEntity customerProjectEntity : customerProjectList) { //判断请求方式 if ("POST".equals(customerProjectEntity.getType())) { try { String response = postWithApacheHttpClient(customerProjectEntity.getUrl(), customerProjectEntity.getParam(), token); JSONObject jsonObject = JSON.parseObject(response); int code = jsonObject.getInteger("code"); if (code == 200) { result.put(customerProjectEntity.getName(), "成功"); }else { result.put(customerProjectEntity.getName(), "失败"); } } catch (Exception e) { throw new BusinessException("调用接口失败"); } } else if ("GET".equals(customerProjectEntity.getType())) { try { String response = getWithApacheHttpClient(customerProjectEntity.getUrl(), token); JSONObject jsonObject = JSON.parseObject(response); int code = jsonObject.getInteger("code"); if (code == 200) { result.put(customerProjectEntity.getName(), "成功"); } else { result.put(customerProjectEntity.getName(), "失败"); } } catch (Exception e) { throw new BusinessException("调用接口失败"); } } } //结果反馈 return result; } /** * 使用 Apache HttpClient 发送 GET 请求 */ public static String getWithApacheHttpClient(String url, String token) throws Exception { // 创建可关闭的HttpClient实例 try (CloseableHttpClient client = HttpClients.createDefault()) { HttpGet request = new HttpGet(url); request.setHeader("authorization", token); try (CloseableHttpResponse response = client.execute(request)) { return EntityUtils.toString(response.getEntity()); } } } /** * 使用 Apache HttpClient 发送 POST 请求 */ public static String postWithApacheHttpClient(String url, String jsonBody, String token) throws Exception { try (CloseableHttpClient client = HttpClients.createDefault()) { HttpPost request = new HttpPost(url); request.setHeader("authorization", token); // 设置请求体 StringEntity entity = new StringEntity(jsonBody); entity.setContentType("application/json"); request.setEntity(entity); try (CloseableHttpResponse response = client.execute(request)) { return EntityUtils.toString(response.getEntity()); } } } }