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.ruzhoukj.sms.dao.CustomerProjectMapper; import com.ruzhoukj.sms.entity.CustomerProjectEntity; import com.ruzhoukj.sms.model.vo.CustomerProjectVO; 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 org.springframework.util.ObjectUtils; import java.util.ArrayList; 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 List jg() { List result = new ArrayList<>(); //登录获取token String token; try { // String loginResponse = postWithApacheHttpClient("http://172.24.33.190/api/auth/login", // "{\"username\":\"wsadmin\",\"password\":\"cced9dbff1f6b878702a7c9bc60dd125\"}", null); 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.add(CustomerProjectVO.builder().name("登录接口").type("POST").result("成功").url("http://172.24.33.190/api/auth/login").build()); result.add(CustomerProjectVO.builder().name("登录接口").type("POST").result("成功").url("http://192.168.10.3:71/api/auth/login").build()); } else { // result.add(CustomerProjectVO.builder().name("登录接口").type("POST").result("失败").url("http://172.24.33.190/api/auth/login").build()); result.add(CustomerProjectVO.builder().name("登录接口").type("POST").result("失败").url("http://192.168.10.3/api/auth/login").build()); return result; } } catch (Exception e) { throw new RuntimeException(e); } //查询接口列表 List customerProjectList = list(new LambdaQueryWrapper().eq(CustomerProjectEntity::getProject, "经管")); //执行接口调用 for (CustomerProjectEntity customerProjectEntity : customerProjectList) { String response = null; //判断请求方式 try { if ("POST".equals(customerProjectEntity.getType())) { response = postWithApacheHttpClient(customerProjectEntity.getUrl(), customerProjectEntity.getParam(), token); } else if ("GET".equals(customerProjectEntity.getType())) { response = getWithApacheHttpClient(customerProjectEntity.getUrl(), token); } } catch (Exception e) { result.add(CustomerProjectVO.builder() .name(customerProjectEntity.getName()) .type(customerProjectEntity.getType()) .result("失败") .url(customerProjectEntity.getUrl()).build()); } JSONObject jsonObject = JSON.parseObject(response); int code = jsonObject.getInteger("code"); if (code == 200) { result.add(CustomerProjectVO.builder() .name(customerProjectEntity.getName()) .type(customerProjectEntity.getType()) .result("成功") .url(customerProjectEntity.getUrl()).build()); } else { result.add(CustomerProjectVO.builder() .name(customerProjectEntity.getName()) .type(customerProjectEntity.getType()) .result("失败") .url(customerProjectEntity.getUrl()).build()); } } //结果反馈 return result; } @Override public List sc() { List result = new ArrayList<>(); //登录获取token String token; try { // String loginResponse = postWithApacheHttpClient("http://172.24.33.190/api/auth/login", // "{\"username\":\"wsadmin\",\"password\":\"cced9dbff1f6b878702a7c9bc60dd125\"}", null); String loginResponse = postWithApacheHttpClient("http://192.168.10.3:76/api/business/sys/user/userLogin", "{\"userName\":\"admin\",\"password\":\"Ruzhoukj@2025\"}", null); JSONObject jsonObject = JSON.parseObject(loginResponse); int loginCode = jsonObject.getInteger("code"); if (loginCode == 200) { JSONObject jsonObject1 = jsonObject.getJSONObject("data"); token = jsonObject1.getString("token"); // result.add(CustomerProjectVO.builder().name("登录接口").type("POST").result("成功").url("http://172.24.33.190/api/auth/login").build()); result.add(CustomerProjectVO.builder().name("登录接口").type("POST").result("成功").url("http://192.168.10.3:76/api/business/sys/user/userLogin").build()); } else { // result.add(CustomerProjectVO.builder().name("登录接口").type("POST").result("失败").url("http://172.24.33.190/api/auth/login").build()); result.add(CustomerProjectVO.builder().name("登录接口").type("POST").result("失败").url("http://192.168.10.3:76/api/business/sys/user/userLogin").build()); return result; } } catch (Exception e) { throw new RuntimeException(e); } //查询接口列表 List customerProjectList = list(new LambdaQueryWrapper().eq(CustomerProjectEntity::getProject, "生产")); //执行接口调用 for (CustomerProjectEntity customerProjectEntity : customerProjectList) { String response = null; //判断请求方式 try { if ("POST".equals(customerProjectEntity.getType())) { response = postWithApacheHttpClient(customerProjectEntity.getUrl(), customerProjectEntity.getParam(), "Bearer " + token); } else if ("GET".equals(customerProjectEntity.getType())) { response = getWithApacheHttpClient(customerProjectEntity.getUrl(), "Bearer " + token); } } catch (Exception e) { result.add(CustomerProjectVO.builder() .name(customerProjectEntity.getName()) .type(customerProjectEntity.getType()) .result("失败") .url(customerProjectEntity.getUrl()).build()); } if (ObjectUtils.isEmpty(response)) { result.add(CustomerProjectVO.builder() .name(customerProjectEntity.getName()) .type(customerProjectEntity.getType()) .result("响应体为空") .url(customerProjectEntity.getUrl()).build()); continue; } try { JSONObject jsonObject = JSON.parseObject(response); int code = jsonObject.getInteger("code"); if (code == 200) { result.add(CustomerProjectVO.builder() .name(customerProjectEntity.getName()) .type(customerProjectEntity.getType()) .result("成功") .url(customerProjectEntity.getUrl()).build()); } else { result.add(CustomerProjectVO.builder() .name(customerProjectEntity.getName()) .type(customerProjectEntity.getType()) .result("失败") .url(customerProjectEntity.getUrl()).build()); } } catch (Exception e) { result.add(CustomerProjectVO.builder() .name(customerProjectEntity.getName()) .type(customerProjectEntity.getType()) .result("失败") .url(customerProjectEntity.getUrl()).build()); } } //结果反馈 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()); } } } }