| | |
| | | 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; |
| | | |
| | |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 创建微赞代理。接口返回顶层code或data.result非成功时抛出运行时异常。 |
| | | * |
| | | * @return 微赞返回的代理业务数据 |
| | | */ |
| | | public JsonNode createAgent(String agentMobile, String agentName, Integer levelCode, |
| | | String parentMobile, String storeName) { |
| | | String token = getToken(); |
| | | Map<String, Object> body = new LinkedHashMap<String, Object>(); |
| | | body.put("agent_mobile", requireText(agentMobile, "代理手机号不能为空")); |
| | | body.put("agent_name", requireText(agentName, "代理名称不能为空")); |
| | | if (levelCode == null) { |
| | | throw new IllegalArgumentException("代理等级不能为空"); |
| | | } |
| | | body.put("level_code", levelCode); |
| | | putIfHasText(body, "parent_mobile", parentMobile); |
| | | putIfHasText(body, "store_name", storeName); |
| | | |
| | | HttpHeaders headers = new HttpHeaders(); |
| | | headers.setContentType(MediaType.APPLICATION_JSON); |
| | | headers.setBearerAuth(token); |
| | | try { |
| | | ResponseEntity<String> response = restOperations.postForEntity( |
| | | "https://openapi.njyqkj0ksyz.com/api/v2/agent/createAgent", |
| | | new HttpEntity<Map<String, Object>>(body, headers), String.class); |
| | | return parseCreateAgentResponse(response.getBody()); |
| | | } catch (RestClientException ex) { |
| | | throw new IllegalStateException("调用微赞创建代理接口失败", ex); |
| | | } |
| | | } |
| | | |
| | | private JsonNode parseCreateAgentResponse(String responseBody) { |
| | | if (!StringUtils.hasText(responseBody)) { |
| | | throw new IllegalStateException("微赞创建代理接口返回为空"); |
| | | } |
| | | try { |
| | | JsonNode root = objectMapper.readTree(responseBody); |
| | | if (!"0".equals(root.path("code").asText())) { |
| | | throw new IllegalStateException("微赞创建代理失败: " + root.path("msg").asText("未知错误")); |
| | | } |
| | | JsonNode data = root.path("data"); |
| | | if (!data.isObject() || data.path("result").asInt(-1) != 0) { |
| | | throw new IllegalStateException("微赞创建代理失败: 业务处理未成功"); |
| | | } |
| | | return data; |
| | | } catch (IllegalStateException ex) { |
| | | throw ex; |
| | | } catch (Exception ex) { |
| | | throw new IllegalStateException("解析微赞创建代理响应失败", ex); |
| | | } |
| | | } |
| | | |
| | | 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"); |