| | |
| | | import com.fasterxml.jackson.databind.ObjectMapper; |
| | | import com.tcm.training.admin.app.auth.config.AppWechatAuthProperties; |
| | | import com.tcm.training.admin.app.auth.dto.AppWechatLoginRequest; |
| | | import java.nio.charset.StandardCharsets; |
| | | import org.slf4j.Logger; |
| | | import org.slf4j.LoggerFactory; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.http.converter.StringHttpMessageConverter; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.web.client.RestOperations; |
| | | import org.springframework.web.client.RestTemplate; |
| | |
| | | */ |
| | | @Autowired |
| | | public AppWechatAuthClient(AppWechatAuthProperties properties, ObjectMapper objectMapper) { |
| | | this(properties, objectMapper, new RestTemplate()); |
| | | this(properties, objectMapper, createUtf8RestTemplate()); |
| | | } |
| | | |
| | | /** |
| | |
| | | } |
| | | |
| | | /** |
| | | * 创建默认按 UTF-8 解码字符串响应的 HTTP 客户端,避免微信中文昵称乱码。 |
| | | */ |
| | | private static RestTemplate createUtf8RestTemplate() { |
| | | RestTemplate restTemplate = new RestTemplate(); |
| | | restTemplate.getMessageConverters().removeIf(StringHttpMessageConverter.class::isInstance); |
| | | restTemplate.getMessageConverters().add(0, new StringHttpMessageConverter(StandardCharsets.UTF_8)); |
| | | return restTemplate; |
| | | } |
| | | |
| | | /** |
| | | * 保存微信换票结果,避免在多个私有方法之间散落参数。 |
| | | */ |
| | | private static class WechatAccessTokenSnapshot { |