1.优化
Showing
3 changed files
with
29 additions
and
127 deletions
| ... | @@ -24,6 +24,7 @@ import com.topdraw.common.ResultInfo; | ... | @@ -24,6 +24,7 @@ import com.topdraw.common.ResultInfo; |
| 24 | import com.topdraw.config.RedisKeyUtil; | 24 | import com.topdraw.config.RedisKeyUtil; |
| 25 | import com.topdraw.exception.BadRequestException; | 25 | import com.topdraw.exception.BadRequestException; |
| 26 | import com.topdraw.exception.EntityNotFoundException; | 26 | import com.topdraw.exception.EntityNotFoundException; |
| 27 | import com.topdraw.resttemplate.RestTemplateClient; | ||
| 27 | import com.topdraw.util.Base64Util; | 28 | import com.topdraw.util.Base64Util; |
| 28 | import com.topdraw.util.JSONUtil; | 29 | import com.topdraw.util.JSONUtil; |
| 29 | import com.topdraw.utils.RedisUtils; | 30 | import com.topdraw.utils.RedisUtils; |
| ... | @@ -34,7 +35,6 @@ import io.swagger.annotations.Api; | ... | @@ -34,7 +35,6 @@ import io.swagger.annotations.Api; |
| 34 | import io.swagger.annotations.ApiOperation; | 35 | import io.swagger.annotations.ApiOperation; |
| 35 | import lombok.extern.slf4j.Slf4j; | 36 | import lombok.extern.slf4j.Slf4j; |
| 36 | import org.apache.commons.collections4.CollectionUtils; | 37 | import org.apache.commons.collections4.CollectionUtils; |
| 37 | import org.assertj.core.util.Arrays; | ||
| 38 | import org.springframework.beans.BeanUtils; | 38 | import org.springframework.beans.BeanUtils; |
| 39 | import org.springframework.beans.factory.annotation.Autowired; | 39 | import org.springframework.beans.factory.annotation.Autowired; |
| 40 | import org.springframework.util.Assert; | 40 | import org.springframework.util.Assert; |
| ... | @@ -43,7 +43,6 @@ import org.springframework.web.bind.annotation.*; | ... | @@ -43,7 +43,6 @@ import org.springframework.web.bind.annotation.*; |
| 43 | 43 | ||
| 44 | import java.io.IOException; | 44 | import java.io.IOException; |
| 45 | import java.net.URLDecoder; | 45 | import java.net.URLDecoder; |
| 46 | import java.time.LocalDateTime; | ||
| 47 | import java.util.*; | 46 | import java.util.*; |
| 48 | import java.util.stream.Collectors; | 47 | import java.util.stream.Collectors; |
| 49 | 48 | ||
| ... | @@ -475,7 +474,9 @@ public class UserOperationController { | ... | @@ -475,7 +474,9 @@ public class UserOperationController { |
| 475 | 474 | ||
| 476 | if (StringUtils.isNotBlank(headimgurl)) { | 475 | if (StringUtils.isNotBlank(headimgurl)) { |
| 477 | String headimgurlDecode = URLDecoder.decode(headimgurl, "UTF-8"); | 476 | String headimgurlDecode = URLDecoder.decode(headimgurl, "UTF-8"); |
| 478 | memberDTO.setAvatarUrl(headimgurlDecode); | 477 | String imageEncode = Base64Util.encode(headimgurlDecode); |
| 478 | String image = RestTemplateClient.chooseImage(imageEncode, "image"); | ||
| 479 | memberDTO.setAvatarUrl(StringUtils.isNotBlank(image) == true ? image:headimgurlDecode); | ||
| 479 | } | 480 | } |
| 480 | }catch (Exception e) { | 481 | }catch (Exception e) { |
| 481 | log.info("headimgurl , nickname ===>> encode error!"); | 482 | log.info("headimgurl , nickname ===>> encode error!"); | ... | ... |
| 1 | package com.topdraw.resttemplate; | 1 | package com.topdraw.resttemplate; |
| 2 | 2 | ||
| 3 | import com.alibaba.fastjson.JSON; | ||
| 3 | import com.alibaba.fastjson.JSONObject; | 4 | import com.alibaba.fastjson.JSONObject; |
| 4 | import com.topdraw.business.module.member.address.domain.MemberAddress; | 5 | import com.topdraw.business.module.member.address.domain.MemberAddress; |
| 5 | import com.topdraw.business.module.member.domain.Member; | 6 | import com.topdraw.business.module.member.domain.Member; |
| 6 | import com.topdraw.business.module.member.relatedinfo.domain.MemberRelatedInfo; | 7 | import com.topdraw.business.module.member.relatedinfo.domain.MemberRelatedInfo; |
| 8 | import lombok.Data; | ||
| 7 | import lombok.extern.slf4j.Slf4j; | 9 | import lombok.extern.slf4j.Slf4j; |
| 10 | import org.apache.commons.lang3.StringUtils; | ||
| 8 | import org.springframework.beans.factory.annotation.Autowired; | 11 | import org.springframework.beans.factory.annotation.Autowired; |
| 9 | import org.springframework.core.env.Environment; | 12 | import org.springframework.core.env.Environment; |
| 10 | import org.springframework.http.ResponseEntity; | 13 | import org.springframework.http.ResponseEntity; |
| ... | @@ -13,7 +16,9 @@ import org.springframework.stereotype.Component; | ... | @@ -13,7 +16,9 @@ import org.springframework.stereotype.Component; |
| 13 | import org.springframework.web.client.RestTemplate; | 16 | import org.springframework.web.client.RestTemplate; |
| 14 | 17 | ||
| 15 | import javax.annotation.PostConstruct; | 18 | import javax.annotation.PostConstruct; |
| 19 | import java.util.List; | ||
| 16 | import java.util.Map; | 20 | import java.util.Map; |
| 21 | import java.util.Objects; | ||
| 17 | 22 | ||
| 18 | @Slf4j | 23 | @Slf4j |
| 19 | @Component | 24 | @Component |
| ... | @@ -28,147 +33,41 @@ public class RestTemplateClient { | ... | @@ -28,147 +33,41 @@ public class RestTemplateClient { |
| 28 | 33 | ||
| 29 | @PostConstruct | 34 | @PostConstruct |
| 30 | private void init() { | 35 | private void init() { |
| 31 | BASE_URL = environment.getProperty("api.baseUrl"); | 36 | BASE_URL = environment.getProperty("api.uc-service"); |
| 32 | SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory(); | 37 | SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory(); |
| 33 | // 设置连接超时 | 38 | // 设置连接超时 |
| 34 | factory.setConnectTimeout(5000); | 39 | factory.setConnectTimeout(5000); |
| 35 | // 设置读取超时 | 40 | // 设置读取超时 |
| 36 | factory.setReadTimeout(8000); | 41 | factory.setReadTimeout(3000); |
| 37 | restTemplate = new RestTemplate(factory); | 42 | restTemplate = new RestTemplate(factory); |
| 38 | } | 43 | } |
| 39 | 44 | ||
| 40 | public static JSONObject getMemberInfo(Long memberId) { | 45 | public static String chooseImage(String base64Url, String entity) { |
| 41 | JSONObject resultSet = null; | 46 | Image image = new Image(base64Url, entity); |
| 42 | String url = BASE_URL + "/api/member/findById/" + memberId; | ||
| 43 | log.info("request uc : url is " + url + ", memberId is " + memberId); | ||
| 44 | ResponseEntity<String> responseEntity = restTemplate.getForEntity(url, String.class); | ||
| 45 | if (responseEntity.getStatusCode().is2xxSuccessful()) { | ||
| 46 | String entityBody = responseEntity.getBody(); | ||
| 47 | JSONObject jsonObject = JSONObject.parseObject(entityBody); | ||
| 48 | if (jsonObject.getInteger("businessCode").equals(200)) { | ||
| 49 | resultSet = jsonObject.getJSONArray("resultSet").getJSONObject(0); | ||
| 50 | } | ||
| 51 | } | ||
| 52 | log.info("uc response: " + resultSet.toJSONString()); | ||
| 53 | return resultSet; | ||
| 54 | } | ||
| 55 | |||
| 56 | public static String createMember(Member member) { | ||
| 57 | String entityBody = ""; | 47 | String entityBody = ""; |
| 58 | String url = BASE_URL + "/api/member/create"; | 48 | String url = BASE_URL + "/ucService/api/upload/chooseImage"; |
| 59 | log.info("request uc : url is " + url + ", memberId is " + com.alibaba.fastjson.JSONObject.toJSONString(member)); | 49 | log.info("request uc : url is " + url + ", memberId is " + com.alibaba.fastjson.JSONObject.toJSONString(image)); |
| 60 | ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, member, String.class); | 50 | ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, image, String.class); |
| 61 | if (responseEntity.getStatusCode().is2xxSuccessful()) { | 51 | if (responseEntity.getStatusCode().is2xxSuccessful()) { |
| 62 | entityBody = responseEntity.getBody(); | 52 | entityBody = responseEntity.getBody(); |
| 63 | 53 | JSONObject parseObject = JSON.parseObject(entityBody); | |
| 64 | } | 54 | List<Object> resultSet = (List)parseObject.get("resultSet"); |
| 65 | return entityBody; | 55 | Object o = resultSet.get(0); |
| 66 | } | 56 | return Objects.nonNull(o)?o.toString():""; |
| 67 | |||
| 68 | public static String updateMember(Member member) { | ||
| 69 | String entityBody = ""; | ||
| 70 | String url = BASE_URL + "/api/member/update"; | ||
| 71 | log.info("request uc : url is " + url + ", memberId is " + com.alibaba.fastjson.JSONObject.toJSONString(member)); | ||
| 72 | restTemplate.put(url, member); | ||
| 73 | |||
| 74 | return entityBody; | ||
| 75 | } | ||
| 76 | |||
| 77 | public static String createMemberAddress(MemberAddress member) { | ||
| 78 | JSONObject resultSet = null; | ||
| 79 | String url = BASE_URL + "/api/MemberAddress/create"; | ||
| 80 | log.info("request uc : url is " + url + ", memberId is " + com.alibaba.fastjson.JSONObject.toJSONString(member)); | ||
| 81 | ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, member, String.class); | ||
| 82 | String entityBody = ""; | ||
| 83 | if (responseEntity.getStatusCode().is2xxSuccessful()) { | ||
| 84 | entityBody = responseEntity.getBody(); | ||
| 85 | } | ||
| 86 | log.info("uc response: " + entityBody); | ||
| 87 | return entityBody; | ||
| 88 | } | ||
| 89 | |||
| 90 | public static void updateMemberAddress(MemberAddress member) { | ||
| 91 | String url = BASE_URL + "/api/MemberAddress/update"; | ||
| 92 | log.info("request uc : url is " + url + ", memberId is " + com.alibaba.fastjson.JSONObject.toJSONString(member)); | ||
| 93 | restTemplate.put(url, member); | ||
| 94 | |||
| 95 | } | ||
| 96 | |||
| 97 | public static void deleteMemberAddress(Long id) { | ||
| 98 | String url = BASE_URL + "/api/MemberAddress/delete/" + id; | ||
| 99 | log.info("request uc : url is " + url + ", memberId is " + id); | ||
| 100 | restTemplate.delete(url); | ||
| 101 | |||
| 102 | } | ||
| 103 | |||
| 104 | public static JSONObject getMemberProfile(Long memberId) { | ||
| 105 | JSONObject resultSet = null; | ||
| 106 | String url = BASE_URL + "/api/MemberRelatedInfo/getMemberRelatedInfos"; | ||
| 107 | log.info("request uc : url is " + url + ", memberId is " + memberId); | ||
| 108 | ResponseEntity<String> responseEntity = restTemplate.getForEntity(url, String.class); | ||
| 109 | if (responseEntity.getStatusCode().is2xxSuccessful()) { | ||
| 110 | String entityBody = responseEntity.getBody(); | ||
| 111 | JSONObject jsonObject = JSONObject.parseObject(entityBody); | ||
| 112 | if (jsonObject.getInteger("businessCode").equals(200)) { | ||
| 113 | resultSet = jsonObject.getJSONArray("resultSet").getJSONObject(0); | ||
| 114 | } | ||
| 115 | } | ||
| 116 | log.info("uc response: " + resultSet.toJSONString()); | ||
| 117 | return resultSet; | ||
| 118 | } | ||
| 119 | |||
| 120 | public static String getMemberAddress(Map<String, Object> param) { | ||
| 121 | String entityBody = ""; | ||
| 122 | String url = BASE_URL + "/api/MemberAddress/pageMemberAddress?page=" + param.get("page") + "&size=" + param.get("size") + "&memberId=" + param.get("memberId"); | ||
| 123 | log.info("request uc : url is " + url + ", param is " + param); | ||
| 124 | ResponseEntity<String> responseEntity = restTemplate.getForEntity(url, String.class); | ||
| 125 | if (responseEntity.getStatusCode().is2xxSuccessful()) { | ||
| 126 | entityBody = responseEntity.getBody(); | ||
| 127 | } | 57 | } |
| 128 | log.info("uc response: " + entityBody); | ||
| 129 | return entityBody; | 58 | return entityBody; |
| 130 | } | 59 | } |
| 131 | 60 | ||
| 132 | public static String getMemberRelatedInfo(Map<String, Object> param) { | 61 | @Data |
| 133 | String entityBody = ""; | 62 | static class Image { |
| 134 | String url = BASE_URL + "/api/MemberRelatedInfo/pageMemberRelatedInfos?page=" + param.get("page") + "&size=" + param.get("size") + "&memberId=" + param.get("memberId"); | ||
| 135 | log.info("request uc : url is " + url + ", memberId is " + param); | ||
| 136 | ResponseEntity<String> responseEntity = restTemplate.getForEntity(url, String.class); | ||
| 137 | if (responseEntity.getStatusCode().is2xxSuccessful()) { | ||
| 138 | entityBody = responseEntity.getBody(); | ||
| 139 | } | ||
| 140 | log.info("uc response: " + entityBody); | ||
| 141 | return entityBody; | ||
| 142 | } | ||
| 143 | 63 | ||
| 144 | public static void deleteMemberRelatedInfo(Long memberRelatedId) { | 64 | private String base64URL; |
| 145 | String url = BASE_URL + "/api/MemberRelatedInfo/delete/" + memberRelatedId; | 65 | private String entity; |
| 146 | log.info("request uc : url is " + url + ", memberId is " + memberRelatedId); | ||
| 147 | restTemplate.delete(url, String.class); | ||
| 148 | } | ||
| 149 | 66 | ||
| 150 | public static String addMemberRelatedInfo(MemberRelatedInfo resources) { | 67 | public Image(String base64Url, String entity){ |
| 151 | String entityBody = null; | 68 | this.base64URL = base64Url; |
| 152 | String url = BASE_URL + "/api/MemberRelatedInfo/create"; | 69 | this.entity = entity; |
| 153 | log.info("request uc : url is " + url + ", memberRelatedId is " + com.alibaba.fastjson.JSONObject.toJSONString(resources)); | ||
| 154 | ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, resources, String.class); | ||
| 155 | if (responseEntity.getStatusCode().is2xxSuccessful()) { | ||
| 156 | entityBody = responseEntity.getBody(); | ||
| 157 | // JSONObject jsonObject = JSONObject.parseObject(entityBody); | ||
| 158 | // if (jsonObject.getInteger("businessCode").equals(200)) { | ||
| 159 | // resultSet = jsonObject.getJSONArray("resultSet").getJSONObject(0); | ||
| 160 | // } | ||
| 161 | } | 70 | } |
| 162 | log.info("uc response: " + entityBody); | ||
| 163 | return entityBody; | ||
| 164 | } | ||
| 165 | |||
| 166 | public static String updateMemberRelatedInfo(MemberRelatedInfo resources) { | ||
| 167 | String resultSet = ""; | ||
| 168 | String url = BASE_URL + "/api/MemberRelatedInfo/update"; | ||
| 169 | log.info("request uc : url is " + url + ", memberRelatedId is " + com.alibaba.fastjson.JSONObject.toJSONString(resources)); | ||
| 170 | restTemplate.put(url, resources); | ||
| 171 | 71 | ||
| 172 | return resultSet; | ||
| 173 | } | 72 | } |
| 174 | } | 73 | } | ... | ... |
-
Please register or sign in to post a comment