You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

117 lines
5.1 KiB

7 months ago
package Tools.MaxKb.Service.Impl;
7 months ago
7 months ago
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
7 months ago
import com.jfinal.kit.PropKit;
7 months ago
import java.io.File;
7 months ago
public class MaxKbImpl {
7 months ago
//MaxKB的url
7 months ago
static final String baseUrl = PropKit.get("baseUrl");
7 months ago
//获取身份识别码接口
7 months ago
static final String Authorization_URL = baseUrl + "/api/user/login";
/**
* QA
*
* @param authCode
* @param uploadFile
*/
7 months ago
public static String uploadQA(String authCode, String zskId, String uploadFile) {
7 months ago
//知识库url上传地址
String qaUrl = baseUrl + "/api/dataset/" + zskId + "/document/qa";
7 months ago
//使用hutool工具箱里面的httpPost功能向指定的接口发起上传文件的请求在Header中携带参数authCode
HttpResponse response = HttpRequest.post(qaUrl)
7 months ago
.header("Accept", "application/json, text/plain, */*")
7 months ago
.header("Accept-Encoding", "gzip,deflate")
.header("Accept-Language", "zh-CN,zh;q=0.9")
7 months ago
.header("Authorization", authCode)// 添加header
.header("Connection", "keep-alive")
.header("Content-Type", "multipart/form-data; boundary=----WebKitFormBoundaryhEiEDpZDka6byweA")
.header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36")
.form("file", new File(uploadFile)).execute();// 添加文件
7 months ago
// 获取响应内容
String result = response.body();
7 months ago
JSONObject jo = JSONUtil.parseObj(result);
int code = jo.getInt("code");
if (code != 200) {
System.out.println("请求失败");
return null;
}
return jo.getJSONArray("data").getJSONObject(0).getStr("id");
7 months ago
}
7 months ago
/**
*
*
* @throws Exception
*/
7 months ago
public static String getAuthorization() {
//管理员账号与密码
String username = PropKit.get("MaxKBUsername");
String password = PropKit.get("MaxKBPassword");
7 months ago
JSONObject requestBody = JSONUtil.createObj()
.set("username", username)
.set("password", password);
7 months ago
HttpResponse response = HttpRequest.post(Authorization_URL)
.header("Content-Type", "application/json")
.header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36")
.header("Origin", baseUrl)
.header("Referer", baseUrl + "/ui/login")
.body(requestBody.toString())
.timeout(10000) // 可选:设置超时时间(毫秒)
.execute();
//System.out.println(response);
// 详细的状态处理
if (response.isOk()) {
String body = response.body();
if (StrUtil.isBlank(body)) {
throw new RuntimeException("响应内容为空");
}
7 months ago
7 months ago
try {
JSONObject responseJson = JSONUtil.parseObj(body);
String data = responseJson.getStr("data");
if (StrUtil.isBlank(data)) {
throw new RuntimeException("未获取到data字段");
7 months ago
}
7 months ago
return data;
} catch (Exception e) {
throw new RuntimeException("解析响应JSON失败: " + e.getMessage());
7 months ago
}
7 months ago
} else {
String errorBody = response.body();
throw new RuntimeException(String.format("请求失败 [%d]: %s",
response.getStatus(),
StrUtil.isBlank(errorBody) ? "无错误信息" : errorBody));
7 months ago
}
}
7 months ago
/**
*
7 months ago
*
* @param authCode
* @param zskId id
7 months ago
* @param documentId id
* @return
*/
public static com.alibaba.fastjson.JSONObject delDocument(String authCode, String zskId, String documentId) {
String url = baseUrl + "/api/dataset/" + zskId + "/document/" + documentId + "?dep=[object+Object]&__v_isRef=true&__v_isShallow=false&_rawValue=false&_value=false";
HttpResponse response = HttpRequest.delete(url)
.header("Accept", "application/json, text/plain, */*")
.header("Accept-Encoding", "gzip,deflate")
.header("Accept-Language", "zh-CN,zh;q=0.9")
.header("Authorization", authCode)// 添加header
.header("Connection", "keep-alive")
.header("Content-Type", "application/json")
.header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36")
.execute();
return com.alibaba.fastjson.JSONObject.parseObject(response.body());
}
7 months ago
}