|
|
@@ -0,0 +1,103 @@
|
|
|
+package mingsoft;
|
|
|
+
|
|
|
+import cn.hutool.core.bean.BeanUtil;
|
|
|
+import cn.hutool.core.io.FileUtil;
|
|
|
+import cn.hutool.core.util.EnumUtil;
|
|
|
+import cn.hutool.http.HttpResponse;
|
|
|
+import cn.hutool.http.HttpUtil;
|
|
|
+import cn.hutool.http.Method;
|
|
|
+import cn.hutool.json.JSONArray;
|
|
|
+import cn.hutool.json.JSONObject;
|
|
|
+import lombok.AllArgsConstructor;
|
|
|
+
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+import java.util.stream.StreamSupport;
|
|
|
+
|
|
|
+@AllArgsConstructor
|
|
|
+public class MingsoftClient {
|
|
|
+ private final String token;
|
|
|
+ private final String sessionId;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 查列表
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public Map<Info, MingsoftData> list() {
|
|
|
+ String url = "https://code.mingsoft.net/code-proxy/people/mcode/projectModelObject/list.do?projectId=1980198434053410817";
|
|
|
+ try (HttpResponse response = HttpUtil.createRequest(Method.GET, url).addHeaders(createHeaders()).execute()) {
|
|
|
+ JSONObject json = new JSONObject(response.body());
|
|
|
+ if (json.isEmpty()) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ if (json.getInt("code") != 200) {
|
|
|
+ System.err.println(json);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ return from(json.getJSONArray("data"));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 加载备份
|
|
|
+ */
|
|
|
+ public static Map<Info, MingsoftData> loadBackup(String jsonFile) {
|
|
|
+ String json = FileUtil.readUtf8String(jsonFile);
|
|
|
+ return from(new JSONObject(json).getJSONArray("data"));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 修改
|
|
|
+ */
|
|
|
+ public boolean 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()) {
|
|
|
+ JSONObject json = new JSONObject(response.body());
|
|
|
+ if (json.isEmpty()) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ if (json.getInt("code") != 200) {
|
|
|
+ System.err.println(json);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private static Map<Info, MingsoftData> from(JSONArray array) {
|
|
|
+ return StreamSupport.stream(array.jsonIter().spliterator(), false)
|
|
|
+ .map(item -> item.toBean(MingsoftData.class))
|
|
|
+ .collect(Collectors.toMap(mingsoftData -> EnumUtil.fromStringQuietly(Info.class, mingsoftData.getPmoTableName().toUpperCase()), v -> v));
|
|
|
+ }
|
|
|
+
|
|
|
+ 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 + "; JSESSIONID=" + sessionId);
|
|
|
+ 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;
|
|
|
+ }
|
|
|
+}
|