|
|
@@ -1,9 +1,12 @@
|
|
|
package com.ylx.usercenter.service.impl;
|
|
|
|
|
|
+import cn.hutool.core.collection.CollUtil;
|
|
|
import cn.hutool.core.util.ObjUtil;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
import cn.hutool.http.HttpUtil;
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
import com.alibaba.fastjson.TypeReference;
|
|
|
import com.ylx.common.core.domain.R;
|
|
|
import com.ylx.common.exception.ServiceException;
|
|
|
@@ -11,8 +14,8 @@ import com.ylx.lottery.service.LotteryCountService;
|
|
|
import com.ylx.massage.domain.TWxUser;
|
|
|
import com.ylx.massage.service.TWxUserService;
|
|
|
import com.ylx.usercenter.domain.dto.UnifiedUserCenterDTO;
|
|
|
+import com.ylx.usercenter.domain.vo.OneAccountVO;
|
|
|
import com.ylx.usercenter.domain.vo.UnifiedUserCenterResponseVO;
|
|
|
-import com.ylx.usercenter.domain.vo.UnifiedUserClientListVO;
|
|
|
import com.ylx.usercenter.service.UnifiedUserCenterService;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
@@ -22,6 +25,9 @@ import org.springframework.transaction.annotation.Propagation;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
+import java.util.Collections;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
|
|
|
@Slf4j
|
|
|
@Service
|
|
|
@@ -29,13 +35,15 @@ public class UnifiedUserCenterServiceImpl implements UnifiedUserCenterService {
|
|
|
|
|
|
@Value("${remote.user-center.base-url}")
|
|
|
private String userCenterBaseUrl;
|
|
|
- @Value("${remote.local-live.client-id}")
|
|
|
+ @Value("${remote.night-fragrance.client-id}")
|
|
|
private String clientId;
|
|
|
|
|
|
// 定义接口路径常量
|
|
|
private static final String QUERY_BIND_PATH = "/userApp/queryBind";
|
|
|
private static final String BIND_PATH = "/userApp/bind";
|
|
|
private static final String UNBIND_PATH = "/userApp/unbind";
|
|
|
+ private static final String QUERY_CLIENTS_PATH = "/userApp/queryClients";
|
|
|
+ private static final int DEFAULT_TIMEOUT = 5000; // 5秒超时
|
|
|
@Resource
|
|
|
private TWxUserService wxUserService;
|
|
|
@Resource
|
|
|
@@ -43,8 +51,6 @@ public class UnifiedUserCenterServiceImpl implements UnifiedUserCenterService {
|
|
|
|
|
|
@Override
|
|
|
public R<?> queryBind(UnifiedUserCenterDTO dto) {
|
|
|
- validateDto(dto);
|
|
|
-// dto.setClientId(clientId);
|
|
|
// 只需要关注业务路径和日志描述即可
|
|
|
return executePost(QUERY_BIND_PATH, dto, "查询用户绑定信息");
|
|
|
}
|
|
|
@@ -67,7 +73,7 @@ public class UnifiedUserCenterServiceImpl implements UnifiedUserCenterService {
|
|
|
R<?> remoteResult = executePost(BIND_PATH, dto, "添加用户绑定信息");
|
|
|
|
|
|
// 4. 判断绑定成功
|
|
|
- if (!remoteResult.getData().equals(Boolean.TRUE)) {
|
|
|
+ if (ObjUtil.notEqual(remoteResult.getCode(), 200)) {
|
|
|
log.error("远程绑定失败, userId: {}, msg: {}", dto.getSourceUserId(), remoteResult.getMsg());
|
|
|
return R.fail("用户绑定失败:" + remoteResult.getMsg());
|
|
|
}
|
|
|
@@ -95,6 +101,12 @@ public class UnifiedUserCenterServiceImpl implements UnifiedUserCenterService {
|
|
|
return executePost(UNBIND_PATH, dto, "解除用户绑定信息");
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public OneAccountVO queryClients(String uuid) {
|
|
|
+ log.info("调用一账通查询平台绑定信息:{}", uuid);
|
|
|
+ return this.queryClientsByUuid(uuid);
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 通用 HTTP POST 请求执行模板
|
|
|
*/
|
|
|
@@ -127,7 +139,6 @@ public class UnifiedUserCenterServiceImpl implements UnifiedUserCenterService {
|
|
|
return R.fail(response.getData(), response.getMessage());
|
|
|
}
|
|
|
|
|
|
- // 5. 处理 data 字段
|
|
|
Object dataObj = response.getData();
|
|
|
|
|
|
// 情况 A: data 是布尔值 false (虽然 code=0,但没数据)
|
|
|
@@ -136,13 +147,27 @@ public class UnifiedUserCenterServiceImpl implements UnifiedUserCenterService {
|
|
|
return R.ok(response.getData());
|
|
|
}
|
|
|
|
|
|
- // 情况 B: data 是包含列表的对象 { "userClients": [...] }
|
|
|
- if (dataObj instanceof UnifiedUserClientListVO) {
|
|
|
- UnifiedUserClientListVO wrapper = (UnifiedUserClientListVO) dataObj;
|
|
|
- return R.ok(wrapper.getUserClients(), response.getMessage());
|
|
|
+ // 情况 B: data 是 JSONObject (例如: { "userClients": [...] })
|
|
|
+ if (dataObj instanceof JSONObject) {
|
|
|
+ JSONObject jsonObject = (JSONObject) dataObj;
|
|
|
+
|
|
|
+ // 优先尝试获取 "userClients" 字段 (针对当前截图的场景)
|
|
|
+ Object userClientsObj = jsonObject.get("userClients");
|
|
|
+
|
|
|
+ if (userClientsObj instanceof JSONArray) {
|
|
|
+ JSONArray userClientsArray = (JSONArray) userClientsObj;
|
|
|
+ log.info("获取到用户客户端数量: {}", userClientsArray.size());
|
|
|
+ // 如果需要转为具体的 List<UserClientVO>,可以在这里做转换
|
|
|
+ return R.ok(userClientsArray);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 兜底逻辑:如果是其他类型的 JSONObject (例如查询详情),直接返回整个对象
|
|
|
+ // 避免数据丢失
|
|
|
+ return R.ok(jsonObject);
|
|
|
}
|
|
|
|
|
|
- return R.ok();
|
|
|
+ // 情况 C: data 是其他类型 (例如直接是一个 String 或 Number)
|
|
|
+ return R.ok(dataObj);
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
log.error("{}发生异常", logDesc, e);
|
|
|
@@ -161,8 +186,12 @@ public class UnifiedUserCenterServiceImpl implements UnifiedUserCenterService {
|
|
|
if (StrUtil.isEmpty(dto.getTargetUserId())) {
|
|
|
throw new IllegalArgumentException("目标用户ID不能为空");
|
|
|
}
|
|
|
- if (StrUtil.isEmpty(dto.getClientId())) {
|
|
|
- throw new IllegalArgumentException("客户端ID不能为空");
|
|
|
+ if (StrUtil.isEmpty(dto.getSourceClientId())) {
|
|
|
+ throw new IllegalArgumentException("源客户端ID不能为空");
|
|
|
+ }
|
|
|
+
|
|
|
+ if (StrUtil.isEmpty(dto.getTargetClientId())) {
|
|
|
+ throw new IllegalArgumentException("目标客户端ID不能为空");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -241,4 +270,59 @@ public class UnifiedUserCenterServiceImpl implements UnifiedUserCenterService {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+ private OneAccountVO queryClientsByUuid(String uuid) {
|
|
|
+ try {
|
|
|
+ // 1. 构建请求参数
|
|
|
+ String url = userCenterBaseUrl + QUERY_CLIENTS_PATH;
|
|
|
+ Map<String, Object> params = Collections.singletonMap("uuid", uuid);
|
|
|
+
|
|
|
+ // 2. 发送请求
|
|
|
+ String resultJson = HttpUtil.get(url, params, DEFAULT_TIMEOUT);
|
|
|
+
|
|
|
+ // 3. 校验结果
|
|
|
+ if (StrUtil.isEmpty(resultJson)) {
|
|
|
+ throw new RuntimeException("调用一账通查询平台绑定信息接口失败:接口返回空结果");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 4. 解析结果
|
|
|
+ UnifiedUserCenterResponseVO<List<OneAccountVO>> response = JSON.parseObject(
|
|
|
+ resultJson,
|
|
|
+ new TypeReference<UnifiedUserCenterResponseVO<List<OneAccountVO>>>() {
|
|
|
+ }
|
|
|
+ );
|
|
|
+
|
|
|
+ // 5. 处理业务状态
|
|
|
+ if (ObjUtil.isNull(response) || !response.isSuccess()) {
|
|
|
+ String msg = ObjUtil.isNotNull(response) ? response.getMessage() : "未知错误";
|
|
|
+ Integer code = ObjUtil.isNotNull(response) ? response.getCode() : -1;
|
|
|
+
|
|
|
+ log.warn("调用一账通查询平台绑定信息接口失败: code={}, msg={}", code, msg);
|
|
|
+ throw new ServiceException("远程接口返回失败: " + msg);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 6. 获取数据列表
|
|
|
+ List<OneAccountVO> oneAccountVOs = response.getData();
|
|
|
+
|
|
|
+ if (CollUtil.isEmpty(oneAccountVOs)) {
|
|
|
+ log.info("未查询到相关活动数据");
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ OneAccountVO oneAccount = oneAccountVOs.stream().filter(item -> item.getClientId().equals(clientId)).findFirst().orElse(null);
|
|
|
+
|
|
|
+ log.info("成功获取调用一账通查询平台绑定信息: {}", JSON.toJSONString(oneAccount));
|
|
|
+
|
|
|
+ return oneAccount;
|
|
|
+
|
|
|
+ } catch (ServiceException e) {
|
|
|
+ // 透传业务异常,不要重复包装
|
|
|
+ throw e;
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("调用一账通查询平台绑定信息接口发生系统异常", e);
|
|
|
+ // 优化点:只抛出消息,不要直接把整个 Exception 对象 toString() 拼接到字符串里
|
|
|
+ throw new ServiceException("调用一账通查询平台绑定信息接口异常: " + e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|