fix feedback webhook

main
wangqing 8 months ago
parent 618d71a139
commit 93557fad2b

@ -1,10 +1,12 @@
package com.luoo.user.controller; package com.luoo.user.controller;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.Charset;
import java.util.*; import java.util.*;
import java.util.function.Function; import java.util.function.Function;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.github.houbb.sensitive.word.bs.SensitiveWordBs; import com.github.houbb.sensitive.word.bs.SensitiveWordBs;
import com.luoo.user.dao.UserMessageDao; import com.luoo.user.dao.UserMessageDao;
@ -25,7 +27,9 @@ import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults; import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.query.Criteria; import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity; import org.springframework.http.*;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
@ -298,11 +302,32 @@ public class MyController extends BaseController {
for (String image:imagesArray) { for (String image:imagesArray) {
robotImage = robotImage + Constants.RESOURCE_PREFIX +"user/feedback/" + image+"\r\n"; robotImage = robotImage + Constants.RESOURCE_PREFIX +"user/feedback/" + image+"\r\n";
} }
Map<String,String> robot = new HashMap<>(); 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("msg_type","text");
robot.put("content","反馈内容:"+content+"\r\n反馈人:"+userLoginDto.getNickName()+"\r\n附件:" + robotImage); 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);
restTemplate.postForEntity(url,JSONObject.toJSON(robot),Map.class); System.out.println(response.toString());
return Result.success(); return Result.success();
} }
@ -318,7 +343,8 @@ public class MyController extends BaseController {
String imageName = feedback.getUserId() + "_" + feedback.getFeedbackId() + "_" + idWorker.nextId() String imageName = feedback.getUserId() + "_" + feedback.getFeedbackId() + "_" + idWorker.nextId()
+ StringTools.getFileSuffix(file.getOriginalFilename()); + StringTools.getFileSuffix(file.getOriginalFilename());
String filePath = Constants.USER_FEEDBACK_IMAGE_DIRECTORY + imageName; String filePath = Constants.USER_FEEDBACK_IMAGE_DIRECTORY + imageName;
s3Service.uploadImage("indie", filePath, file.getBytes()); // s3Service.uploadImage("indie", filePath, file.getBytes());
s3Service.upload("indie",filePath,file);
return imageName; return imageName;
} }

@ -1,5 +1,6 @@
package com.luoo.user.service; package com.luoo.user.service;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import software.amazon.awssdk.core.sync.RequestBody; import software.amazon.awssdk.core.sync.RequestBody;
@ -96,10 +97,39 @@ public class S3Service {
public void uploadImage(String bucket, String key, byte[] buffer) { public void uploadImage(String bucket, String key, byte[] buffer) {
PutObjectRequest putObjectRequest = PutObjectRequest.builder() PutObjectRequest putObjectRequest = PutObjectRequest.builder()
.bucket(bucket) .bucket(bucket)
.contentType(MediaType.IMAGE_PNG_VALUE)
.key(key) .key(key)
.build(); .build();
RequestBody requestBody = RequestBody.fromInputStream(new ByteArrayInputStream(buffer), buffer.length); RequestBody requestBody = RequestBody.fromInputStream(new ByteArrayInputStream(buffer), buffer.length);
s3Client.putObject(putObjectRequest, requestBody); s3Client.putObject(putObjectRequest, requestBody);
} }
/**
* s3
* @param bucket
* @param key
* @param file
* @return
* @throws IOException
*/
public int upload(String bucket, String key, MultipartFile file) {
try {
PutObjectRequest putObjectRequest = PutObjectRequest.builder()
.bucket(bucket)
.contentType(file.getContentType())
.key(key)
.build();
RequestBody requestBody = RequestBody.fromInputStream(file.getInputStream(), file.getSize());
PutObjectResponse putObjectResponse = s3Client.putObject(putObjectRequest, requestBody);
SdkHttpResponse sdkHttpResponse = putObjectResponse.sdkHttpResponse();
if (!sdkHttpResponse.isSuccessful()) {
return -1;
} else {
return 1;
}
} catch (Exception e) {
return -1;
}
}
} }

Loading…
Cancel
Save