zjk
2026-09-29 54dcf0a82e7490fa58b1c056d5c0b9cb739c2931
tcm-training-admin-backend/src/main/java/com/tcm/training/admin/utils/VzanHttpUtil.java
@@ -1,5 +1,6 @@
package com.tcm.training.admin.utils;
import cn.hutool.json.JSONUtil;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
@@ -11,6 +12,7 @@
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestOperations;
import org.springframework.web.client.RestTemplate;
@@ -164,6 +166,100 @@
        }
    }
    /**
     * 创建微赞代理。接口返回顶层code或data.result非成功时抛出运行时异常。
     *
     * @return 微赞返回的代理业务数据
     */
    public JsonNode createAgent(String agentMobile, String agentName, Integer levelCode,
                               String parentMobile, String storeName) {
        Map<String, Object> body = new LinkedHashMap<String, Object>();
        body.put("agent_mobile", requireText(agentMobile, "代理手机号不能为空"));
        body.put("agent_name", requireText(agentName, "代理名称不能为空"));
        if (levelCode == null || levelCode < 1 || levelCode > 3) {
            throw new IllegalArgumentException("代理等级必须是1、2或3");
        }
        body.put("level_code", levelCode);
        putIfHasText(body, "parent_mobile", parentMobile);
        putIfHasText(body, "store_name", storeName);
        return postForSuccessfulData(
                "https://openapi.njyqkj0ksyz.com/api/v2/agent/createAgent", body, "创建代理");
    }
    /**
     * 创建微赞门店。接口返回顶层code或data.result非成功时抛出运行时异常。
     *
     * @return 微赞返回的门店业务数据
     */
    public JsonNode createStore(String title, Integer provinceCode, Integer cityCode,
                               Integer areaCode, String address, String telephone) {
        Map<String, Object> body = new LinkedHashMap<String, Object>();
        body.put("title", requireText(title, "门店名称不能为空"));
        body.put("province_code", requireValue(provinceCode, "省编码不能为空"));
        body.put("city_code", requireValue(cityCode, "市编码不能为空"));
        body.put("area_code", requireValue(areaCode, "区县编码不能为空"));
        body.put("address", requireText(address, "详细地址不能为空"));
        body.put("telephone", requireText(telephone, "联系电话不能为空"));
        return postForSuccessfulData(
                "https://openapi.njyqkj0ksyz.com/api/v2/agent/createStore", body, "创建门店");
    }
    private JsonNode postForSuccessfulData(String url, Map<String, Object> body, String operationName) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.setBearerAuth(getToken());
        try {
            log.info("微赞最终请求,url={}, bearerToken={}, body={}", url, headers.getFirst(HttpHeaders.AUTHORIZATION), JSONUtil.toJsonStr(body));
            ResponseEntity<String> response = restOperations.postForEntity(
                    url, new HttpEntity<Map<String, Object>>(body, headers), String.class);
            log.info("微赞原始响应,url={}, responseBody={}", url, JSONUtil.toJsonStr(response.getBody()));
            return parseBusinessResponse(response.getBody(), operationName);
        } catch (RestClientException ex) {
            throw new IllegalStateException("调用微赞" + operationName + "接口失败", ex);
        }
    }
    private JsonNode parseBusinessResponse(String responseBody, String operationName) {
        if (!StringUtils.hasText(responseBody)) {
            throw new IllegalStateException("微赞" + operationName + "接口返回为空");
        }
        try {
            JsonNode root = objectMapper.readTree(responseBody);
            if (!"0".equals(root.path("code").asText())) {
                throw new IllegalStateException(
                        "微赞" + operationName + "失败: " + root.path("msg").asText("未知错误"));
            }
            JsonNode data = root.path("data");
            if (!data.isObject() || data.path("result").asInt(-1) != 0) {
                throw new IllegalStateException("微赞" + operationName + "失败: 业务处理未成功");
            }
            return data;
        } catch (IllegalStateException ex) {
            throw ex;
        } catch (Exception ex) {
            throw new IllegalStateException("解析微赞" + operationName + "响应失败", ex);
        }
    }
    private static <T> T requireValue(T value, String message) {
        if (value == null) {
            throw new IllegalArgumentException(message);
        }
        return value;
    }
    private static String requireText(String value, String message) {
        if (!StringUtils.hasText(value)) {
            throw new IllegalArgumentException(message);
        }
        return value.trim();
    }
    private static void putIfHasText(Map<String, Object> body, String key, String value) {
        if (StringUtils.hasText(value)) {
            body.put(key, value.trim());
        }
    }
    private static String md5Lowercase(String text) {
        try {
            MessageDigest digest = MessageDigest.getInstance("MD5");