|
|
|
package com.ruoyi.common.utils;
|
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
import com.google.gson.JsonObject;
|
|
|
|
import com.ruoyi.common.utils.html.HttpUtils;
|
|
|
|
import okhttp3.Request;
|
|
|
|
import okhttp3.Response;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
public class FeishuUtil {
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(FeishuUtil.class);
|
|
|
|
|
|
|
|
private static Map<String, TenantAccessToken> tenant_access_token_map = new HashMap<>();
|
|
|
|
|
|
|
|
public static String gettenant_access_token(String yy_app_id,String yy_app_secret) {
|
|
|
|
if(tenant_access_token_map.containsKey(yy_app_id))
|
|
|
|
{
|
|
|
|
TenantAccessToken tenant_access_token = tenant_access_token_map.get(yy_app_id);
|
|
|
|
// 判断token是否未超时
|
|
|
|
if (tenant_access_token.getTenant_access_token() != null && System.currentTimeMillis() < tenant_access_token.getTokenExpireTime()) {
|
|
|
|
return tenant_access_token.getTenant_access_token();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
String url = "https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal";
|
|
|
|
try {
|
|
|
|
Response response = HttpUtils.postJsonBody(url, builder -> builder.addHeader("Content-Type", "application/json; charset=utf-8"), jsonObject -> {
|
|
|
|
jsonObject.put("app_id", yy_app_id); // 替换为实际的 app_id
|
|
|
|
jsonObject.put("app_secret", yy_app_secret); // 替换为实际的 app_secret
|
|
|
|
});
|
|
|
|
String str = response.body().string();
|
|
|
|
JSONObject responseMap = JSONObject.parseObject(str, JSONObject.class);
|
|
|
|
if (responseMap != null && responseMap.getInteger("code")==0) {
|
|
|
|
TenantAccessToken tenant_access_token = new TenantAccessToken();
|
|
|
|
tenant_access_token.setTenant_access_token(responseMap.getString("tenant_access_token"));
|
|
|
|
// 设置token过期时间,假设token有效期为7200秒
|
|
|
|
tenant_access_token.setTokenExpireTime(System.currentTimeMillis() + responseMap.getInteger("expire") * 1000);
|
|
|
|
tenant_access_token_map.put(yy_app_id, tenant_access_token);
|
|
|
|
return tenant_access_token.getTenant_access_token();
|
|
|
|
}else {
|
|
|
|
throw new RuntimeException("Failed to get tenant_access_token: " + str);
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
throw new RuntimeException("Failed to get tenant_access_token: " ,e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* HTTP URL
|
|
|
|
* https://open.feishu.cn/open-apis/bitable/v1/apps/:app_token/tables/:table_id/records
|
|
|
|
* HTTP Method
|
|
|
|
* POST
|
|
|
|
* 接口频率限制
|
|
|
|
* 50 次/秒
|
|
|
|
*
|
|
|
|
* 请求头 :Authorization、Content-Type
|
|
|
|
*/
|
|
|
|
public static void subFeishuTables(String tenant_access_token,String app_token,String table_id, JSONObject field )
|
|
|
|
{
|
|
|
|
String url = "https://open.feishu.cn/open-apis/bitable/v1/apps/"+app_token+"/tables/"+table_id+"/records";
|
|
|
|
|
|
|
|
JSONObject params = new JSONObject();
|
|
|
|
params.put("fields",field);
|
|
|
|
|
|
|
|
try {
|
|
|
|
Response response = HttpUtils.postJsonBody(url, builder -> {
|
|
|
|
builder.addHeader("Content-Type", "application/json");
|
|
|
|
builder.addHeader("Authorization", "Bearer "+tenant_access_token);
|
|
|
|
}, jsonObject -> {
|
|
|
|
jsonObject.put("fields", field);
|
|
|
|
});
|
|
|
|
String str = response.body().string();
|
|
|
|
JSONObject responseMap = JSONObject.parseObject(str, JSONObject.class);
|
|
|
|
if (responseMap != null && responseMap.getInteger("code")!=0)
|
|
|
|
{
|
|
|
|
logger.error("提交飞书表数据返回失败:{}",str);
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
logger.error("提交飞书表数据异常",e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} |
...
|
...
|
|