|
|
@@ -1,28 +1,37 @@
|
|
|
package com.malk.service.aliwork.impl;
|
|
|
|
|
|
-import com.alibaba.fastjson.JSON;
|
|
|
import com.malk.server.aliwork.YDAuth;
|
|
|
-import com.malk.server.aliwork.YDConf;
|
|
|
-import com.malk.server.common.McException;
|
|
|
import com.malk.server.dingtalk.DDR_New;
|
|
|
import com.malk.service.aliwork.YDClient_Form;
|
|
|
import com.malk.service.dingtalk.DDClient;
|
|
|
import com.malk.utils.UtilHttp;
|
|
|
+import com.malk.utils.UtilRespMapper;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
-import org.apache.commons.lang3.StringUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
-import java.util.Collections;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
|
|
|
+import static com.malk.service.aliwork.impl.YDHelpers.assertResult;
|
|
|
+import static com.malk.service.aliwork.impl.YDHelpers.body;
|
|
|
+import static com.malk.service.aliwork.impl.YDHelpers.mergeExt;
|
|
|
+import static com.malk.service.aliwork.impl.YDHelpers.normPage;
|
|
|
+import static com.malk.service.aliwork.impl.YDHelpers.normPageSize;
|
|
|
+import static com.malk.service.aliwork.impl.YDHelpers.normSearchField;
|
|
|
+import static com.malk.service.aliwork.impl.YDHelpers.url;
|
|
|
+import static com.malk.service.aliwork.impl.YDHelpers.urlV2;
|
|
|
+import static com.malk.service.aliwork.impl.YDHelpers.validatePageSize;
|
|
|
+
|
|
|
/**
|
|
|
* 宜搭表单原子接口实现
|
|
|
*
|
|
|
* <p>严格对齐 {@code mjava-baseline §3.4.2}:所有 body_ext 字段透传到 HTTP body,不过滤。
|
|
|
* accessToken 为 null 时回退到 {@code DDClient.initTokenHeader()} 全局 token。</p>
|
|
|
+ *
|
|
|
+ * <p>共用 helper(url/body/mergeExt/assertResult/normXxx)见 {@link YDHelpers};
|
|
|
+ * 响应类型守卫见 {@link UtilRespMapper}。</p>
|
|
|
*/
|
|
|
@Slf4j
|
|
|
@Service
|
|
|
@@ -31,21 +40,8 @@ public class YDClient_FormImpl implements YDClient_Form {
|
|
|
@Autowired
|
|
|
private DDClient ddClient;
|
|
|
|
|
|
- // ---------------- 共用工具 ----------------
|
|
|
-
|
|
|
- private static final String BASE_V1 = "https://api.dingtalk.com/v1.0/yida";
|
|
|
- private static final String BASE_V2 = "https://api.dingtalk.com/v2.0/yida";
|
|
|
-
|
|
|
- private String url(String uri) {
|
|
|
- return BASE_V1 + uri;
|
|
|
- }
|
|
|
-
|
|
|
- private String urlV2(String uri) {
|
|
|
- return BASE_V2 + uri;
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
- * 构造请求头:accessToken 为 null 时回退到全局 DDClient。
|
|
|
+ * 构造请求头:accessToken 为 null 时回退到全局 DDClient(依赖注入,留在 impl 内)。
|
|
|
*/
|
|
|
private Map<String, String> header(YDAuth auth) {
|
|
|
if (auth.getAccessToken() != null && !auth.getAccessToken().isEmpty()) {
|
|
|
@@ -56,43 +52,6 @@ public class YDClient_FormImpl implements YDClient_Form {
|
|
|
return ddClient.initTokenHeader();
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 构造基础 body:auth 三字段 + body_ext 透传。
|
|
|
- */
|
|
|
- private Map<String, Object> body(YDAuth auth) {
|
|
|
- Map<String, Object> body = new HashMap<>();
|
|
|
- body.put("appType", auth.getAppType());
|
|
|
- body.put("systemToken", auth.getSystemToken());
|
|
|
- body.put("userId", auth.resolvedUserId());
|
|
|
- return body;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 合并 body_ext 透传(不过滤任何字段;优先级:显式参数 > body_ext > auth)
|
|
|
- */
|
|
|
- private Map<String, Object> mergeExt(Map<String, Object> body, Map<String, Object> body_ext) {
|
|
|
- if (body_ext == null || body_ext.isEmpty()) {
|
|
|
- return body;
|
|
|
- }
|
|
|
- // body_ext 先进(优先级低),然后显式参数覆盖
|
|
|
- Map<String, Object> merged = new HashMap<>(body_ext);
|
|
|
- merged.putAll(body);
|
|
|
- return merged;
|
|
|
- }
|
|
|
-
|
|
|
- private DDR_New assertResult(DDR_New ddr, String action) {
|
|
|
- if (ddr == null) {
|
|
|
- throw new McException("YIDA_NULL_RESPONSE", "宜搭接口 [" + action + "] 返回空");
|
|
|
- }
|
|
|
- if (!ddr.isSuccess()) {
|
|
|
- throw new McException(
|
|
|
- ddr.getCode() == null ? "YIDA_ERROR" : ddr.getCode(),
|
|
|
- "宜搭接口 [" + action + "] 失败: " + ddr.getMessage()
|
|
|
- );
|
|
|
- }
|
|
|
- return ddr;
|
|
|
- }
|
|
|
-
|
|
|
// ================================================================
|
|
|
// 表单实例 CRUD
|
|
|
// ================================================================
|
|
|
@@ -104,7 +63,7 @@ public class YDClient_FormImpl implements YDClient_Form {
|
|
|
body.put("formDataJson", formDataJson);
|
|
|
body = mergeExt(body, body_ext);
|
|
|
DDR_New r = assertResult(DDR_New.doPost(url("/forms/instances"), header(auth), null, body), "saveForm");
|
|
|
- return String.valueOf(r.getResult());
|
|
|
+ return UtilRespMapper.asString(r.getResult());
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@@ -114,8 +73,7 @@ public class YDClient_FormImpl implements YDClient_Form {
|
|
|
body.put("updateFormDataJson", updateFormDataJson);
|
|
|
body = mergeExt(body, body_ext);
|
|
|
DDR_New r = assertResult((DDR_New) UtilHttp.doPut(url("/forms/instances"), header(auth), body, DDR_New.class), "updateForm");
|
|
|
- Object result = r.getResult();
|
|
|
- return result instanceof Map ? (Map<String, Object>) result : Collections.emptyMap();
|
|
|
+ return UtilRespMapper.asMap(r.getResult());
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@@ -126,8 +84,7 @@ public class YDClient_FormImpl implements YDClient_Form {
|
|
|
body.put("formDataJson", formDataJson);
|
|
|
body = mergeExt(body, body_ext);
|
|
|
DDR_New r = assertResult(DDR_New.doPost(urlV2("/forms/instances/insertOrUpdate"), header(auth), null, body), "upsertForm");
|
|
|
- Object result = r.getResult();
|
|
|
- return result instanceof Map ? (Map<String, Object>) result : Collections.emptyMap();
|
|
|
+ return UtilRespMapper.asMap(r.getResult());
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@@ -136,7 +93,7 @@ public class YDClient_FormImpl implements YDClient_Form {
|
|
|
body.put("formInstanceId", formInstanceId);
|
|
|
body = mergeExt(body, body_ext);
|
|
|
DDR_New r = assertResult((DDR_New) UtilHttp.doDelete(url("/forms/instances"), header(auth), body, DDR_New.class), "deleteForm");
|
|
|
- return Boolean.TRUE.equals(r.getResult()) || "true".equalsIgnoreCase(String.valueOf(r.getResult()));
|
|
|
+ return UtilRespMapper.asBool(r.getResult());
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@@ -146,11 +103,7 @@ public class YDClient_FormImpl implements YDClient_Form {
|
|
|
body.put("searchFieldJson", searchFieldJson);
|
|
|
body = mergeExt(body, body_ext);
|
|
|
DDR_New r = assertResult(DDR_New.doPost(url("/forms/instances/batchRemove"), header(auth), null, body), "deleteFormByCondition");
|
|
|
- Object result = r.getResult();
|
|
|
- if (result instanceof Number) {
|
|
|
- return ((Number) result).intValue();
|
|
|
- }
|
|
|
- return 0;
|
|
|
+ return UtilRespMapper.asInt(r.getResult(), 0);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@@ -160,8 +113,7 @@ public class YDClient_FormImpl implements YDClient_Form {
|
|
|
body.put("updateFormDataJson", updateFormDataJson);
|
|
|
body = mergeExt(body, body_ext);
|
|
|
DDR_New r = assertResult((DDR_New) UtilHttp.doPut(url("/forms/instances/components"), header(auth), body, DDR_New.class), "updateFormComponents");
|
|
|
- Object result = r.getResult();
|
|
|
- return result instanceof Map ? (Map<String, Object>) result : Collections.emptyMap();
|
|
|
+ return UtilRespMapper.asMap(r.getResult());
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@@ -171,11 +123,7 @@ public class YDClient_FormImpl implements YDClient_Form {
|
|
|
body.put("formDataListJson", formDataListJson);
|
|
|
body = mergeExt(body, body_ext);
|
|
|
DDR_New r = assertResult(DDR_New.doPost(url("/forms/instances/batchSave"), header(auth), null, body), "batchSaveForm");
|
|
|
- Object result = r.getResult();
|
|
|
- if (result instanceof List) {
|
|
|
- return JSON.parseArray(JSON.toJSONString(result), String.class);
|
|
|
- }
|
|
|
- return Collections.emptyList();
|
|
|
+ return UtilRespMapper.asStringList(r.getResult());
|
|
|
}
|
|
|
|
|
|
// ================================================================
|
|
|
@@ -184,51 +132,38 @@ public class YDClient_FormImpl implements YDClient_Form {
|
|
|
|
|
|
@Override
|
|
|
public Map<String, Object> getForm(YDAuth auth, String formInstanceId, Map<String, Object> body_ext) {
|
|
|
- Map<String, Object> param = body(auth);
|
|
|
- if (body_ext != null) {
|
|
|
- param.putAll(body_ext);
|
|
|
- // body_ext 优先级低,auth 字段覆盖
|
|
|
- param.putAll(body(auth));
|
|
|
- }
|
|
|
+ Map<String, Object> param = mergeExt(body(auth), body_ext);
|
|
|
DDR_New r = assertResult(DDR_New.doGet(url("/forms/instances/" + formInstanceId), header(auth), param), "getForm");
|
|
|
- Object result = r.getResult();
|
|
|
- return result instanceof Map ? (Map<String, Object>) result : Collections.emptyMap();
|
|
|
+ return UtilRespMapper.asMap(r.getResult());
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public Map<String, Object> searchForm(YDAuth auth, String formUuid, String searchFieldJson,
|
|
|
Integer currentPage, Integer pageSize, Map<String, Object> body_ext) {
|
|
|
- if (pageSize != null && pageSize > YDConf.PAGE_SIZE_LIMIT) {
|
|
|
- throw new McException("YIDA_PAGESIZE_EXCEEDED", "pageSize 不能超过 " + YDConf.PAGE_SIZE_LIMIT);
|
|
|
- }
|
|
|
+ validatePageSize(pageSize);
|
|
|
Map<String, Object> body = body(auth);
|
|
|
body.put("formUuid", formUuid);
|
|
|
- body.put("searchFieldJson", StringUtils.isBlank(searchFieldJson) ? "{}" : searchFieldJson);
|
|
|
- body.put("currentPage", currentPage == null ? 1 : currentPage);
|
|
|
- body.put("pageSize", pageSize == null ? YDConf.PAGE_SIZE_LIMIT : pageSize);
|
|
|
+ body.put("searchFieldJson", normSearchField(searchFieldJson));
|
|
|
+ body.put("currentPage", normPage(currentPage));
|
|
|
+ body.put("pageSize", normPageSize(pageSize));
|
|
|
body = mergeExt(body, body_ext);
|
|
|
DDR_New r = assertResult(DDR_New.doPost(url("/forms/instances/search"), header(auth), null, body), "searchForm");
|
|
|
- Object result = r.getResult();
|
|
|
- return result instanceof Map ? (Map<String, Object>) result : Collections.emptyMap();
|
|
|
+ return UtilRespMapper.asMap(r.getResult());
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public List<String> listFormIds(YDAuth auth, String formUuid, String searchFieldJson,
|
|
|
Integer currentPage, Integer pageSize, Map<String, Object> body_ext) {
|
|
|
Map<String, Object> body = body(auth);
|
|
|
- body.put("searchFieldJson", StringUtils.isBlank(searchFieldJson) ? "{}" : searchFieldJson);
|
|
|
- body.put("currentPage", currentPage == null ? 1 : currentPage);
|
|
|
- body.put("pageSize", pageSize == null ? YDConf.PAGE_SIZE_LIMIT : pageSize);
|
|
|
+ body.put("searchFieldJson", normSearchField(searchFieldJson));
|
|
|
+ body.put("currentPage", normPage(currentPage));
|
|
|
+ body.put("pageSize", normPageSize(pageSize));
|
|
|
body = mergeExt(body, body_ext);
|
|
|
DDR_New r = assertResult(
|
|
|
- DDR_New.doPost(url("/forms/instances/ids/" + auth.getAppType() + "/" + formUuid), header(auth), body, body),
|
|
|
+ DDR_New.doPost(url("/forms/instances/ids/" + auth.getAppType() + "/" + formUuid), header(auth), null, body),
|
|
|
"listFormIds"
|
|
|
);
|
|
|
- Object result = r.getResult();
|
|
|
- if (result instanceof List) {
|
|
|
- return JSON.parseArray(JSON.toJSONString(result), String.class);
|
|
|
- }
|
|
|
- return Collections.emptyList();
|
|
|
+ return UtilRespMapper.asStringList(r.getResult());
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@@ -236,33 +171,22 @@ public class YDClient_FormImpl implements YDClient_Form {
|
|
|
Integer currentPage, Integer pageSize, Map<String, Object> body_ext) {
|
|
|
Map<String, Object> body = body(auth);
|
|
|
body.put("formUuid", formUuid);
|
|
|
- body.put("searchFieldJson", StringUtils.isBlank(searchFieldJson) ? "{}" : searchFieldJson);
|
|
|
- body.put("currentPage", currentPage == null ? 1 : currentPage);
|
|
|
- body.put("pageSize", pageSize == null ? YDConf.PAGE_SIZE_LIMIT : pageSize);
|
|
|
+ body.put("searchFieldJson", normSearchField(searchFieldJson));
|
|
|
+ body.put("currentPage", normPage(currentPage));
|
|
|
+ body.put("pageSize", normPageSize(pageSize));
|
|
|
body = mergeExt(body, body_ext);
|
|
|
DDR_New r = assertResult(DDR_New.doPost(url("/forms/instances/advances/queryAll"), header(auth), null, body), "listFormsAll");
|
|
|
- Object result = r.getResult();
|
|
|
- return result instanceof Map ? (Map<String, Object>) result : Collections.emptyMap();
|
|
|
+ return UtilRespMapper.asMap(r.getResult());
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public List<Map<String, Object>> listInnerTable(YDAuth auth, String formInstanceId, Map<String, Object> body_ext) {
|
|
|
- Map<String, Object> param = body(auth);
|
|
|
- if (body_ext != null) {
|
|
|
- param.putAll(body_ext);
|
|
|
- param.putAll(body(auth));
|
|
|
- }
|
|
|
+ Map<String, Object> param = mergeExt(body(auth), body_ext);
|
|
|
DDR_New r = assertResult(
|
|
|
DDR_New.doGet(url("/forms/innerTables/" + formInstanceId), header(auth), param),
|
|
|
"listInnerTable"
|
|
|
);
|
|
|
- Object result = r.getResult();
|
|
|
- if (result instanceof List) {
|
|
|
- return JSON.parseArray(JSON.toJSONString(result), Map.class).stream()
|
|
|
- .map(m -> (Map<String, Object>) m)
|
|
|
- .collect(java.util.stream.Collectors.toList());
|
|
|
- }
|
|
|
- return Collections.emptyList();
|
|
|
+ return UtilRespMapper.asMapList(r.getResult());
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@@ -271,30 +195,14 @@ public class YDClient_FormImpl implements YDClient_Form {
|
|
|
body.put("formInstanceId", formInstanceId);
|
|
|
body = mergeExt(body, body_ext);
|
|
|
DDR_New r = assertResult(DDR_New.doPost(url("/forms/operationsLogs/query"), header(auth), null, body), "listFormOperations");
|
|
|
- Object result = r.getResult();
|
|
|
- if (result instanceof List) {
|
|
|
- return JSON.parseArray(JSON.toJSONString(result), Map.class).stream()
|
|
|
- .map(m -> (Map<String, Object>) m)
|
|
|
- .collect(java.util.stream.Collectors.toList());
|
|
|
- }
|
|
|
- return Collections.emptyList();
|
|
|
+ return UtilRespMapper.asMapList(r.getResult());
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public List<Map<String, Object>> listForms(YDAuth auth, Map<String, Object> body_ext) {
|
|
|
- Map<String, Object> param = body(auth);
|
|
|
- if (body_ext != null) {
|
|
|
- param.putAll(body_ext);
|
|
|
- param.putAll(body(auth));
|
|
|
- }
|
|
|
+ Map<String, Object> param = mergeExt(body(auth), body_ext);
|
|
|
DDR_New r = assertResult(DDR_New.doGet(url("/forms"), header(auth), param), "listForms");
|
|
|
- Object result = r.getResult();
|
|
|
- if (result instanceof List) {
|
|
|
- return JSON.parseArray(JSON.toJSONString(result), Map.class).stream()
|
|
|
- .map(m -> (Map<String, Object>) m)
|
|
|
- .collect(java.util.stream.Collectors.toList());
|
|
|
- }
|
|
|
- return Collections.emptyList();
|
|
|
+ return UtilRespMapper.asMapList(r.getResult());
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@@ -321,6 +229,6 @@ public class YDClient_FormImpl implements YDClient_Form {
|
|
|
DDR_New.doGet(url("/apps/temporaryUrls/" + auth.getAppType()), header(auth), param),
|
|
|
"convertTempUrl"
|
|
|
);
|
|
|
- return String.valueOf(r.getResult());
|
|
|
+ return UtilRespMapper.asString(r.getResult());
|
|
|
}
|
|
|
}
|