|
|
@@ -0,0 +1,75 @@
|
|
|
+package com.storlead.trade.client;
|
|
|
+
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
+import com.storlead.framework.common.result.Result;
|
|
|
+import com.storlead.framework.core.tenant.TenantConstants;
|
|
|
+import com.storlead.mail.webclient.config.MailRemoteProperties;
|
|
|
+import com.storlead.mail.webclient.constant.MailRemoteApiConstants;
|
|
|
+import com.storlead.mail.webclient.support.MailRemoteHttpSupport;
|
|
|
+import lombok.RequiredArgsConstructor;
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
+import org.springframework.web.reactive.function.client.WebClient;
|
|
|
+
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+/**
|
|
|
+ * CRM 邮箱账户远程客户端,对齐 B 服务 {@code /mkt/**}。
|
|
|
+ */
|
|
|
+@RequiredArgsConstructor
|
|
|
+public class MarketEmailsClient {
|
|
|
+
|
|
|
+ /** 默认连 application-dev.yml 里配的那台邮件服务;要换机器就用 main 的 args[0] 覆盖 */
|
|
|
+ private static final String DEFAULT_BASE_URL = "http://192.168.1.87:10080/router/email";
|
|
|
+
|
|
|
+ private final MailRemoteHttpSupport http;
|
|
|
+
|
|
|
+ public Result<?> emailList(List<Long> ids) {
|
|
|
+ // B 服务是 @RequestParam("ids") List<Long>,必须按 form 重复 key:ids=2&ids=3。
|
|
|
+ // 不能 postJson 直接传 List:body 会变成 [2,3],B 端当成字符串 "[2, 3]" 再按逗号拆,
|
|
|
+ // 得到 "[2" 和 " 3]",转不成 Long,表现为 list size 正确但元素全是 null。
|
|
|
+ // 也不能 postForm(path, ids):toForm 会对 List 做 convertValue(..., Map) 抛异常。
|
|
|
+ return http.postForm(MailRemoteApiConstants.MKT_LIST_BY_IDS, http.formOf("ids", ids));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 本地直连调试入口:不启动 Spring 容器,手工搭一个 MailRemoteHttpSupport 出来。
|
|
|
+ * <p>
|
|
|
+ * 脱离 Web 容器运行时没有登录态和请求上下文,必须关掉 token / 租户透传,
|
|
|
+ * 否则 authHeaders() 会去调 LoginUserUtil / RequestContextHolder,取不到值甚至报错。
|
|
|
+ * 需要鉴权时改用 args 把 token / 租户塞进默认请求头。
|
|
|
+ *
|
|
|
+ * <pre>
|
|
|
+ * 默认连 dev:直接 Run main
|
|
|
+ * 换地址:args[0] = http://127.0.0.1:18090/router/email
|
|
|
+ * 带 token:args[1] = eyJhbGciOi...(B 服务 AuthFilter 用的 header 名由 tokenHeader 决定,默认 token)
|
|
|
+ * 带租户:args[2] = 1234567890123456789(写入 X-Tenant-Id)
|
|
|
+ * </pre>
|
|
|
+ */
|
|
|
+ public static void main(String[] args) {
|
|
|
+ String baseUrl = args.length > 0 ? args[0] : DEFAULT_BASE_URL;
|
|
|
+ String token = args.length > 1 ? args[1] : null;
|
|
|
+ String tenantId = args.length > 2 ? args[2] : null;
|
|
|
+
|
|
|
+ MailRemoteProperties properties = new MailRemoteProperties();
|
|
|
+ properties.setBaseUrl(baseUrl);
|
|
|
+ properties.setForwardToken(false);
|
|
|
+ properties.setForwardTenant(false);
|
|
|
+
|
|
|
+ String tokenHeader = properties.getTokenHeader();
|
|
|
+ WebClient.Builder builder = WebClient.builder()
|
|
|
+ .baseUrl(properties.resolveBaseUrl());
|
|
|
+ // 这里绕开 authHeaders():它依赖 LoginUserUtil / RequestContextHolder,容器外取不到
|
|
|
+ if (StringUtils.hasText(token)) {
|
|
|
+ builder.defaultRequest(req -> req.header(tokenHeader, token));
|
|
|
+ }
|
|
|
+ if (StringUtils.hasText(tenantId)) {
|
|
|
+ builder.defaultRequest(req -> req.header(TenantConstants.HTTP_HEADER_TENANT_ID, tenantId));
|
|
|
+ }
|
|
|
+
|
|
|
+ MailRemoteHttpSupport http = new MailRemoteHttpSupport(
|
|
|
+ builder.build(), properties, new ObjectMapper());
|
|
|
+
|
|
|
+ System.out.println(new MarketEmailsClient(http).emailList(Arrays.asList(2L, 3L)));
|
|
|
+ }
|
|
|
+}
|