|
@@ -0,0 +1,150 @@
|
|
|
|
|
+package mingsoft.client;
|
|
|
|
|
+
|
|
|
|
|
+import cn.hutool.core.bean.BeanUtil;
|
|
|
|
|
+import cn.hutool.core.io.FileUtil;
|
|
|
|
|
+import cn.hutool.core.lang.Assert;
|
|
|
|
|
+import cn.hutool.core.map.MapUtil;
|
|
|
|
|
+import cn.hutool.http.HttpResponse;
|
|
|
|
|
+import cn.hutool.http.HttpStatus;
|
|
|
|
|
+import cn.hutool.http.HttpUtil;
|
|
|
|
|
+import cn.hutool.http.Method;
|
|
|
|
|
+import cn.hutool.json.JSONArray;
|
|
|
|
|
+import cn.hutool.json.JSONObject;
|
|
|
|
|
+
|
|
|
|
|
+import java.util.HashMap;
|
|
|
|
|
+import java.util.Map;
|
|
|
|
|
+import java.util.Optional;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * 客户端操作
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param token 登录标识
|
|
|
|
|
+ * @author hosea
|
|
|
|
|
+ * @date 2025-12-24
|
|
|
|
|
+ */
|
|
|
|
|
+public record MingsoftClient(String token) {
|
|
|
|
|
+ public MingsoftClient {
|
|
|
|
|
+ Assert.notBlank(token, "登录标识不能为空");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 读取配置文件,初始化客户端
|
|
|
|
|
+ */
|
|
|
|
|
+ public static Optional<MingsoftClient> of(String config) {
|
|
|
|
|
+ return Optional.ofNullable(config)
|
|
|
|
|
+ .map(FileUtil::readUtf8String)
|
|
|
|
|
+ .map(MingsoftClient::new);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 查列表
|
|
|
|
|
+ */
|
|
|
|
|
+ public Optional<JSONArray> list() {
|
|
|
|
|
+ String url = "https://code.mingsoft.net/code-proxy/people/mcode/projectModelObject/list.do?projectId=" + Info.PROJECT_ID;
|
|
|
|
|
+ try (HttpResponse response = HttpUtil.createRequest(Method.GET, url).addHeaders(createHeaders()).execute()) {
|
|
|
|
|
+ return getResponseData(response);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 修改
|
|
|
|
|
+ */
|
|
|
|
|
+ public Optional<JSONObject> update(MingsoftData data) {
|
|
|
|
|
+ String url = "https://code.mingsoft.net/code-proxy/people/mcode/projectModelObject/update.do";
|
|
|
|
|
+ Map<String, Object> param = new HashMap<>();
|
|
|
|
|
+ data.setPmoDatetime(null);
|
|
|
|
|
+ BeanUtil.beanToMap(data, param, false, true);
|
|
|
|
|
+ Map<String, String> headers = createHeaders();
|
|
|
|
|
+ headers.put("Content-Type", "application/x-www-form-urlencoded");
|
|
|
|
|
+ try (HttpResponse response = HttpUtil.createRequest(Method.POST, url)
|
|
|
|
|
+ .addHeaders(headers)
|
|
|
|
|
+ .form(param)
|
|
|
|
|
+ .execute()) {
|
|
|
|
|
+ return checkResponse(response);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 下载
|
|
|
|
|
+ */
|
|
|
|
|
+ public Optional<byte[]> download(String... ids) {
|
|
|
|
|
+ String url = "https://code.mingsoft.net/code-proxy/people/download.do";
|
|
|
|
|
+ Map<String, Object> param = new HashMap<>();
|
|
|
|
|
+ param.put("projectModelIds", String.join(",", ids));
|
|
|
|
|
+ param.put("id", Info.PROJECT_ID);
|
|
|
|
|
+ param.put("ctgId", Info.CTG_ID);
|
|
|
|
|
+ try (HttpResponse response = HttpUtil.createRequest(Method.GET, url)
|
|
|
|
|
+ .addHeaders(createHeaders())
|
|
|
|
|
+ .form(param)
|
|
|
|
|
+ .execute()) {
|
|
|
|
|
+ if (response.getStatus() == HttpStatus.HTTP_CREATED) {
|
|
|
|
|
+ return Optional.ofNullable(response.bodyBytes());
|
|
|
|
|
+ }
|
|
|
|
|
+ return Optional.empty();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 预览
|
|
|
|
|
+ */
|
|
|
|
|
+ public Optional<JSONArray> preview(String id) {
|
|
|
|
|
+ String url = "https://code.mingsoft.net/code-proxy/people/mcode/project/view.do";
|
|
|
|
|
+ Map<String, Object> param = new HashMap<>();
|
|
|
|
|
+ param.put("projectModelObjectId", id);
|
|
|
|
|
+ param.put("id", Info.PROJECT_ID);
|
|
|
|
|
+ param.put("ctgId", Info.CTG_ID);
|
|
|
|
|
+ try (HttpResponse response = HttpUtil.createRequest(Method.POST, url)
|
|
|
|
|
+ .addHeaders(createHeaders())
|
|
|
|
|
+ .form(param)
|
|
|
|
|
+ .execute()) {
|
|
|
|
|
+ return getResponseData(response);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 获得响应数据
|
|
|
|
|
+ */
|
|
|
|
|
+ private Optional<JSONArray> getResponseData(HttpResponse response) {
|
|
|
|
|
+ return checkResponse(response).map(json -> json.getJSONArray("data"));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 检查响应
|
|
|
|
|
+ */
|
|
|
|
|
+ private Optional<JSONObject> checkResponse(HttpResponse response) {
|
|
|
|
|
+ return Optional.ofNullable(response.body())
|
|
|
|
|
+ .map(JSONObject::new)
|
|
|
|
|
+ .filter(MapUtil::isNotEmpty)
|
|
|
|
|
+ .filter(json -> {
|
|
|
|
|
+ if (json.getInt("code") == HttpStatus.HTTP_OK) {
|
|
|
|
|
+ return true;
|
|
|
|
|
+ }
|
|
|
|
|
+ System.err.println(json);
|
|
|
|
|
+ return false;
|
|
|
|
|
+ });
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 创建请求头
|
|
|
|
|
+ */
|
|
|
|
|
+ private Map<String, String> createHeaders() {
|
|
|
|
|
+ Map<String, String> headers = new HashMap<>();
|
|
|
|
|
+ headers.put("Accept", "application/json, text/plain, */*");
|
|
|
|
|
+ headers.put("Accept-Encoding", "gzip, deflate, br, zstd");
|
|
|
|
|
+ headers.put("Accept-Language", "zh-CN,zh;q=0.9");
|
|
|
|
|
+ headers.put("Cache-Control", "no-cache");
|
|
|
|
|
+ headers.put("Connection", "keep-alive");
|
|
|
|
|
+ headers.put("Cookie", "token=" + token);
|
|
|
|
|
+ headers.put("Pragma", "no-cache");
|
|
|
|
|
+ headers.put("Referer", "https://code.mingsoft.net/");
|
|
|
|
|
+ headers.put("Sec-Fetch-Dest", "empty");
|
|
|
|
|
+ headers.put("Sec-Fetch-Mode", "cors");
|
|
|
|
|
+ headers.put("Sec-Fetch-Site", "same-origin");
|
|
|
|
|
+ headers.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36");
|
|
|
|
|
+ headers.put("X-Requested-With", "XMLHttpRequest");
|
|
|
|
|
+ headers.put("sec-ch-ua", "\"Chromium\";v=\"142\", \"Google Chrome\";v=\"142\", \"Not_A Brand\";v=\"99\"");
|
|
|
|
|
+ headers.put("sec-ch-ua-mobile", "?0");
|
|
|
|
|
+ headers.put("sec-ch-ua-platform", "\"Windows\"");
|
|
|
|
|
+ return headers;
|
|
|
|
|
+ }
|
|
|
|
|
+}
|