| | |
| | | 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; |
| | |
| | | * @return 微赞返回的代理业务数据 |
| | | */ |
| | | public JsonNode createAgent(String agentMobile, String agentName, Integer levelCode, |
| | | String parentMobile, String storeName) { |
| | | String token = getToken(); |
| | | 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) { |
| | | throw new IllegalArgumentException("代理等级不能为空"); |
| | | 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(token); |
| | | headers.setBearerAuth(getToken()); |
| | | try { |
| | | log.info("微赞最终请求,url={}, bearerToken={}, body={}", url, headers.getFirst(HttpHeaders.AUTHORIZATION), JSONUtil.toJsonStr(body)); |
| | | 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()); |
| | | 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("调用微赞创建代理接口失败", ex); |
| | | throw new IllegalStateException("调用微赞" + operationName + "接口失败", ex); |
| | | } |
| | | } |
| | | |
| | | private JsonNode parseCreateAgentResponse(String responseBody) { |
| | | private JsonNode parseBusinessResponse(String responseBody, String operationName) { |
| | | if (!StringUtils.hasText(responseBody)) { |
| | | throw new IllegalStateException("微赞创建代理接口返回为空"); |
| | | throw new IllegalStateException("微赞" + operationName + "接口返回为空"); |
| | | } |
| | | try { |
| | | JsonNode root = objectMapper.readTree(responseBody); |
| | | if (!"0".equals(root.path("code").asText())) { |
| | | throw new IllegalStateException("微赞创建代理失败: " + root.path("msg").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("微赞创建代理失败: 业务处理未成功"); |
| | | throw new IllegalStateException("微赞" + operationName + "失败: 业务处理未成功"); |
| | | } |
| | | return data; |
| | | } catch (IllegalStateException ex) { |
| | | throw ex; |
| | | } catch (Exception ex) { |
| | | throw new IllegalStateException("解析微赞创建代理响应失败", 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); |