1:前期准备工作
因飞书接口与项目在对接时如何定位对应的用户 是通过工号确认的。因此系统会在 添加 飞书工号字段(jobnumber) 然后将提供的人员工号初始化进去。
初始化方法(手动执行):
1.1:新建表 通过姓名连接起来 数据导进去 update
2:单点登录
Function audit(){
获取token:校验存在不存在返回信息;
获取到token 根据fs_enable 配置开关 获取接口是否开启 校验存在不存在返回信息;
Object info= Getjobmuber(token)// 根据接口获取工号 校验存在不存在返回信息
Map userinfo = getuserinfore(info.get(“number”)) //根据工号获取人员信息 校验存在不存在返回信息
信息存在;
调用登录系统方法 login(userinfo.get(“username”),userinfo.get(“password”))
}
// 根据接口获取工号方法
Function Getjobmuber(token){
//调用接口
Return fsinterface(token);
}
Function fsinterface(token){
// http restful的发送方法
Object info = RestUtil.get(fs_url,token);
Return info
}
Function getuserinfore(jobnumber){
Map userinfo = userservice.getuserinfo(jobnumber);
Return userinfo
}
3:消息推送
实现方法:
配置个监听类 在建立一个配置表
通过反射的方法 拿到每个处理类去处理对应的数据
4:接口
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.springframework.http.*;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.Map;
public class RestUtil {
/**
* 以form表单形式发送请求
*
* @param reqData
*/
public static ResultMsg<Object> postForm(Map<String, Object> reqData, String targeturl, String username, String password) {
ResponseEntity<String> response;
try {
HttpHeaders headers = new HttpHeaders();
// 以表单的方式提交
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
//加入basicAuth账号密码认证
headers.setBasicAuth(username, password, StandardCharsets.UTF_8);
//将请求头部和参数合成一个请求
MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
for (String key : reqData.keySet()) {
params.add(key, reqData.get(key));
}
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(params, headers);
//执行HTTP请求,将返回的结构使用String类格式化
RestTemplate restTemplate = (RestTemplate) SpringUtil.getBean("restTemplate");
restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
response = restTemplate.postForEntity(targeturl, requestEntity, String.class);
} catch (HttpClientErrorException e) {
response = new ResponseEntity<>(new String(e.getResponseBodyAsByteArray(), StandardCharsets.UTF_8), e.getStatusCode());
e.printStackTrace();
} catch (Exception e) {
response = new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
e.printStackTrace();
}
if (response.getStatusCode().is2xxSuccessful()) {
String body = response.getBody();
JSONObject jsonObject = JSONObject.parseObject(body);
return ResultMsgHelper.buildResult(jsonObject.getInteger("code"), jsonObject.getString("msg"), jsonObject.get("data"));
} else {
return ResultMsgHelper.error(response.getBody());
}
}
/**
* 以json形式调用网动接口
*
* @param json
*/
public static ResultMsg<Object> postJson(String json, String targeturl, String username, String password) {
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Type", "application/json;charset=UTF-8");
//加入basicAuth账号密码认证
headers.setBasicAuth(username, password, StandardCharsets.UTF_8);
HttpEntity<String> httpEntity = new HttpEntity<>(json, headers);
ResponseEntity<String> response;
try {
RestTemplate restTemplate = (RestTemplate) SpringUtil.getBean("restTemplate");
restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
response = restTemplate.postForEntity(targeturl, httpEntity, String.class);
} catch (HttpClientErrorException e) {
response = new ResponseEntity<>(new String(e.getResponseBodyAsByteArray(), StandardCharsets.UTF_8), e.getStatusCode());
e.printStackTrace();
} catch (Exception e) {
response = new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
e.printStackTrace();
}
if (response.getStatusCode().is2xxSuccessful()) {
String body = response.getBody();
JSONObject jsonObject = JSONObject.parseObject(body);
return ResultMsgHelper.buildResult(jsonObject.getInteger("code"), jsonObject.getString("msg"), jsonObject.get("data"));
} else if (response.getStatusCode() == HttpStatus.FORBIDDEN) {
return ResultMsgHelper.error("您没有授权");
} else {
return ResultMsgHelper.error(response.getBody());
}
}
/**
* 以json形式调用接口
* post方式
* @param json
*/
public static String post(String json, String targeturl) {
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Type", "application/json;charset=UTF-8");
HttpEntity<String> httpEntity = new HttpEntity<>(json, headers);
ResponseEntity<String> response;
try {
RestTemplate restTemplate = (RestTemplate) SpringUtil.getBean("restTemplate");
restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
response = restTemplate.postForEntity(targeturl, httpEntity, String.class);
} catch (HttpClientErrorException e) {
response = new ResponseEntity<>(new String(e.getResponseBodyAsByteArray(), StandardCharsets.UTF_8), e.getStatusCode());
e.printStackTrace();
} catch (Exception e) {
response = new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
e.printStackTrace();
}
if (response.getStatusCode().is2xxSuccessful()) {
String body = response.getBody();
JSONObject jsonObject = JSONObject.parseObject(body);
return jsonObject.toJSONString();
} else {//接口返回失败
return response.getStatusCode().toString();
}
}
/**
*
* get方式
*/
public static String get(String targeturl) {
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Type", "application/json;charset=UTF-8");
ResponseEntity<String> response;
try {
RestTemplate restTemplate = (RestTemplate) SpringUtil.getBean("restTemplate");
restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
response = restTemplate.getForEntity(targeturl, String.class);
} catch (HttpClientErrorException e) {
response = new ResponseEntity<>(new String(e.getResponseBodyAsByteArray(), StandardCharsets.UTF_8), e.getStatusCode());
e.printStackTrace();
} catch (Exception e) {
response = new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
e.printStackTrace();
}
if (response.getStatusCode().is2xxSuccessful()) {
String body = response.getBody();
//JSONObject jsonObject = JSONObject.parseObject(body);
return body;
} else {//接口返回失败
return null;
}
}
public static String requestBodyPost(String requestBody,String jsseuri) {
//请求的body信息
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(jsseuri);
//添加header
httpPost.addHeader("X-Easymi-AppCode", "AppCode");
httpPost.addHeader("X-Easymi-UserName", "UserName");
//添加body
ByteArrayEntity entity = null;
try {
entity = new ByteArrayEntity(requestBody.getBytes("UTF-8"));
entity.setContentType("application/json");
} catch (UnsupportedEncodingException e) {
//logger.error("向服务器承保接口发起http请求,封装请求body时出现异常", e);
throw new RuntimeException("向服务器承保接口发起http请求,封装请求body时出现异常", e);
}
httpPost.setEntity(entity);
//执行post请求
HttpResponse response = null;
try {
response = httpClient.execute(httpPost);
String rs = EntityUtils.toString(response.getEntity(),"utf-8");
return rs;
} catch (ClientProtocolException e) {
throw new RuntimeException("提交给服务器的请求,不符合HTTP协议", e);
} catch (IOException e) {
throw new RuntimeException("向服务器承保接口发起http请求,执行post请求异常", e);
}
}
}