1.fix NPE, emoji encode issue

main
Gary 5 months ago
parent d37579aa39
commit 2faa608070

@ -12,6 +12,7 @@ import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.Environment;
import org.springframework.context.annotation.Bean;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import util.IdWorker;
import util.JwtUtil;
@ -20,6 +21,7 @@ import util.JwtUtil;
@SpringBootApplication
@EnableEurekaClient
@EnableJpaAuditing
@EnableAsync
public class UserApplication {
static Logger logger= LoggerFactory.getLogger(UserApplication.class);
public static void main(String[] args) throws UnknownHostException {

@ -0,0 +1,13 @@
package com.luoo.user.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class RestTemplateConfig {
@Bean
RestTemplate restTemplate() {
return new RestTemplate();
}
}

@ -1,13 +1,10 @@
package com.luoo.user.controller;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.houbb.sensitive.word.bs.SensitiveWordBs;
import com.luoo.user.dao.UserMessageDao;
import com.luoo.user.dto.TotalCommentVo;
@ -27,9 +24,6 @@ import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.*;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
@ -41,7 +35,6 @@ import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;
import constants.Constants;
import controller.BaseController;
@ -79,8 +72,6 @@ import util.StringTools;
@Api(tags = "MyController")
public class MyController extends BaseController {
private static final String REDIS_KEY_THANKS = "thanks";
private static final String url = "https://open.feishu.cn/open-apis/bot/v2/hook/84316603-355a-4dea-bb86-65f4b8919431";
@Autowired
private S3Service s3Service;
@Autowired
@ -107,7 +98,7 @@ public class MyController extends BaseController {
@Autowired
private RedisTemplate redisTemplate;
@Autowired
private UserMessageDao userMessageDao;
@ -290,45 +281,10 @@ public class MyController extends BaseController {
feedback.setFeedbackId(String.valueOf(idWorker.nextId()));
feedback.setType(type);
feedback.setUserId(userLoginDto.getUserId());
feedback.setNickName(userLoginDto.getNickName());
feedback.setContent(content);
feedback.setNickName(EmojiConverterUtil.encode(userLoginDto.getNickName()));
feedback.setContent(EmojiConverterUtil.encode(content));
feedback.setImages(uploadImages(feedback, files));
feedbackService.save(feedback);
RestTemplate restTemplate = new RestTemplate();
// ResponseEntity<String> response = restTemplate.getForEntity(url,String.class);
String images = uploadImages(feedback, files);
String[] imagesArray = images.split(",");
String robotImage = "";
for (String image:imagesArray) {
robotImage = robotImage + Constants.RESOURCE_PREFIX +"user/feedback/" + image+"\r\n";
}
Map<String,Object> robot = new HashMap<>();
Map<String,String> contentMap = new HashMap<>();
contentMap.put("text","反馈内容:"+content+"\r\n反馈人:"+userLoginDto.getNickName()+"\r\n附件:" + robotImage);
robot.put("msg_type","text");
robot.put("content",contentMap);
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
JSONObject jsonObject = new JSONObject();
jsonObject.put("msg_type","text");
jsonObject.put("content",contentMap);
HttpEntity<String> requestEntity = new HttpEntity<>(jsonObject.toString(),httpHeaders);
// restTemplate.exchange(url, HttpMethod.POST,requestEntity,Map.class);
// JSON json = JSONObject.toJSON(robot);
// String json =JSONObject.toJSONString(robot);
// 中文乱码,主要是 StringHttpMessageConverter的默认编码为ISO导致的
List<HttpMessageConverter<?>> list = restTemplate.getMessageConverters();
for (HttpMessageConverter converter : list) {
if (converter instanceof StringHttpMessageConverter) {
((StringHttpMessageConverter) converter).setDefaultCharset(Charset.forName("UTF-8"));
break;
}
}
ResponseEntity<Map> response = restTemplate.postForEntity(url,requestEntity,Map.class);
System.out.println(response.toString());
feedbackService.send(feedback);
return Result.success();
}
@ -344,11 +300,10 @@ public class MyController extends BaseController {
String imageName = feedback.getUserId() + "_" + feedback.getFeedbackId() + "_" + idWorker.nextId()
+ StringTools.getFileSuffix(file.getOriginalFilename());
String filePath = Constants.USER_FEEDBACK_IMAGE_DIRECTORY + imageName;
// s3Service.uploadImage("indie", filePath, file.getBytes());
s3Service.upload("indie",filePath,file);
s3Service.upload("indie", filePath, file);
return imageName;
}
private Result<PageResult<UserRespDTO>> getCollectedUserInfo(String userId, Integer pageNum, Integer pageSize,
CollectTypeEnum collectTypeEnum) {
Pageable pageable = PageRequest.of(pageNum-1,pageSize);

@ -1,17 +1,67 @@
package com.luoo.user.service;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import com.alibaba.fastjson.JSONObject;
import com.luoo.user.dao.FeedbackDao;
import com.luoo.user.pojo.Feedback;
import com.luoo.user.util.EmojiConverterUtil;
import constants.Constants;
@Service
public class FeedbackService {
private static final String[] EMPTY_IMAGE_ARRAY = new String[] {};
private static final String FEI_SHU_URL = "https://open.feishu.cn/open-apis/bot/v2/hook/84316603-355a-4dea-bb86-65f4b8919431";
@Autowired
private FeedbackDao feedbackDao;
@Autowired
private RestTemplate restTemplate;
@Async
public void send(Feedback feedback) {
feedbackDao.save(feedback);
seedToFeishu(feedback);
}
private void seedToFeishu(Feedback feedback) {
String images = feedback.getImages();
String[] imagesArray = null == images ? EMPTY_IMAGE_ARRAY : images.split(",");
String robotImage = "";
for (String image : imagesArray) {
robotImage = robotImage + Constants.RESOURCE_PREFIX + "user/feedback/" + image + "\r\n";
}
Map<String, Object> robot = new HashMap<>();
Map<String, String> contentMap = new HashMap<>();
contentMap.put("text", "反馈内容:" + EmojiConverterUtil.decode(feedback.getContent()) + "\r\n反馈人:"
+ EmojiConverterUtil.decode(feedback.getNickName()) + "\r\n附件:" + robotImage);
robot.put("msg_type", "text");
robot.put("content", contentMap);
public void save(Feedback appFeedback) {
feedbackDao.save(appFeedback);
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
JSONObject jsonObject = new JSONObject();
jsonObject.put("msg_type", "text");
jsonObject.put("content", contentMap);
HttpEntity<String> requestEntity = new HttpEntity<>(jsonObject.toString(), httpHeaders);
List<HttpMessageConverter<?>> list = restTemplate.getMessageConverters();
for (HttpMessageConverter<?> converter : list) {
if (converter instanceof StringHttpMessageConverter) {
((StringHttpMessageConverter) converter).setDefaultCharset(Charset.forName("UTF-8"));
break;
}
}
restTemplate.postForEntity(FEI_SHU_URL, requestEntity, Map.class);
}
}

Loading…
Cancel
Save