|
|
@@ -0,0 +1,91 @@
|
|
|
+package com.ylx.usercenter.service.impl;
|
|
|
+
|
|
|
+import cn.hutool.core.util.ObjUtil;
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
+import cn.hutool.http.HttpUtil;
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.ylx.common.exception.ServiceException;
|
|
|
+import com.ylx.usercenter.domain.dto.UnifiedUserCenterDTO;
|
|
|
+import com.ylx.usercenter.domain.vo.UnifiedUserClientVO;
|
|
|
+import com.ylx.usercenter.service.UnifiedUserCenterService;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+public class UnifiedUserCenterServiceImpl implements UnifiedUserCenterService {
|
|
|
+
|
|
|
+ @Value("${remote.user-center.base-url}")
|
|
|
+ private String baseUrl;
|
|
|
+ @Value("${remote.user-center.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";
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public UnifiedUserClientVO queryBind(UnifiedUserCenterDTO dto) {
|
|
|
+ validateDto(dto);
|
|
|
+ // 只需要关注业务路径和日志描述即可
|
|
|
+ return executePost(QUERY_BIND_PATH, dto, "查询用户绑定信息");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public UnifiedUserClientVO bind(UnifiedUserCenterDTO dto) {
|
|
|
+ validateDto(dto);
|
|
|
+ return executePost(BIND_PATH, dto, "添加用户绑定信息");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public UnifiedUserClientVO unbind(UnifiedUserCenterDTO dto) {
|
|
|
+ validateDto(dto);
|
|
|
+ return executePost(UNBIND_PATH, dto, "解除用户绑定信息");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 通用 HTTP POST 请求执行模板
|
|
|
+ */
|
|
|
+ private UnifiedUserClientVO executePost(String path, UnifiedUserCenterDTO dto, String logDesc) {
|
|
|
+ try {
|
|
|
+ // 1. 构建 URL
|
|
|
+ String url = baseUrl + path;
|
|
|
+
|
|
|
+ // 2. 转换参数
|
|
|
+ dto.setClientId(clientId);
|
|
|
+ String jsonParam = JSON.toJSONString(dto);
|
|
|
+ log.info("调用远程接口: {}, 参数: {}", url, jsonParam);
|
|
|
+
|
|
|
+ // 3. 发送请求
|
|
|
+ String result = HttpUtil.post(url, jsonParam);
|
|
|
+
|
|
|
+ // 4. 校验结果
|
|
|
+ if (StrUtil.isEmpty(result)) {
|
|
|
+ throw new RuntimeException(logDesc + "失败:接口返回空结果");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 5. 解析结果
|
|
|
+ UnifiedUserClientVO response = JSON.parseObject(result, UnifiedUserClientVO.class);
|
|
|
+ if (response == null) {
|
|
|
+ throw new RuntimeException(logDesc + "失败:响应对象解析为空");
|
|
|
+ }
|
|
|
+
|
|
|
+ return response;
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("{}发生异常", logDesc, e);
|
|
|
+ throw new ServiceException(logDesc + "异常" + e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 参数校验提取
|
|
|
+ */
|
|
|
+ private void validateDto(UnifiedUserCenterDTO dto) {
|
|
|
+ if (ObjUtil.isNull(dto)) {
|
|
|
+ throw new IllegalArgumentException("请求参数不能为空");
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|