|
@@ -249,6 +249,7 @@ public class WechatServiceImpl implements IWechatService { |
|
@@ -249,6 +249,7 @@ public class WechatServiceImpl implements IWechatService { |
249
|
|
249
|
|
250
|
//处理文案内容若为图片,则需要上传图片至微信素材中心,获取 media_id 拿来发消息 获取失败,直接返回
|
250
|
//处理文案内容若为图片,则需要上传图片至微信素材中心,获取 media_id 拿来发消息 获取失败,直接返回
|
251
|
if(customMsgBO.getMsgType().equals(WechatCustomMsgTypeEnum.IMAGE.getText()) && !getMediaId(customMsgBO, accessToken)){
|
251
|
if(customMsgBO.getMsgType().equals(WechatCustomMsgTypeEnum.IMAGE.getText()) && !getMediaId(customMsgBO, accessToken)){
|
|
|
252
|
+ logger.warn("customMsgSend getMediaId error, with sceneKey is {}",customMsgBO.getSendKey());
|
252
|
return new ArrayList<>();
|
253
|
return new ArrayList<>();
|
253
|
}
|
254
|
}
|
254
|
|
255
|
|
|
@@ -274,24 +275,29 @@ public class WechatServiceImpl implements IWechatService { |
|
@@ -274,24 +275,29 @@ public class WechatServiceImpl implements IWechatService { |
274
|
private boolean getMediaId(WechatCustomMsgBO customMsgBO, AccessToken accessToken) {
|
275
|
private boolean getMediaId(WechatCustomMsgBO customMsgBO, AccessToken accessToken) {
|
275
|
try {
|
276
|
try {
|
276
|
//下载网络图片
|
277
|
//下载网络图片
|
277
|
- String fileName = String.valueOf(System.currentTimeMillis()) + ".png";
|
278
|
+ String fileName = String.valueOf(System.currentTimeMillis()) + MessageSenderConstant.IMG_SUFFIX;
|
|
|
279
|
+ String pathFile = MessageSenderConstant.TEMP_FILE_PATH + fileName;
|
278
|
HttpClientUtil.downFromPicUrl(customMsgBO.getSendContent(), MessageSenderConstant.TEMP_FILE_PATH, fileName);
|
280
|
HttpClientUtil.downFromPicUrl(customMsgBO.getSendContent(), MessageSenderConstant.TEMP_FILE_PATH, fileName);
|
279
|
//上传微信 获取 media_id
|
281
|
//上传微信 获取 media_id
|
280
|
- String addMateralUrl = Consts.ADD_MATERIAL_URL.replace("ACCESSTOKEN","sdjfwoeijowen").replace("TYPE", WechatCustomMsgTypeEnum.IMAGE.getText());
|
|
|
281
|
- String mediaIdResult = HttpClientUtil.connectHttpsByPost(addMateralUrl,new File(MessageSenderConstant.TEMP_FILE_PATH + fileName));
|
282
|
+ String addMediaUrl = getMediaUrl(accessToken);
|
|
|
283
|
+ //发送微信请求
|
|
|
284
|
+ String mediaIdResult = HttpClientUtil.connectHttpsByPost(addMediaUrl,new File(pathFile));
|
282
|
if(StringUtils.isEmpty(mediaIdResult)){
|
285
|
if(StringUtils.isEmpty(mediaIdResult)){
|
283
|
- logger.warn("getMediaId with get mediaId null,imgUrl is {}",customMsgBO.getSendContent());
|
286
|
+ logger.warn("getMediaId with get result null,sceneKey is {},imgUrl is {}",customMsgBO.getSendKey(),customMsgBO.getSendContent());
|
284
|
return false;
|
287
|
return false;
|
285
|
}
|
288
|
}
|
286
|
JSONObject resultObj = JSONObject.parseObject(mediaIdResult);
|
289
|
JSONObject resultObj = JSONObject.parseObject(mediaIdResult);
|
287
|
|
290
|
|
|
|
291
|
+ //校验返回结果
|
288
|
if (!checkResult(customMsgBO, resultObj,fileName)) return false;
|
292
|
if (!checkResult(customMsgBO, resultObj,fileName)) return false;
|
289
|
|
293
|
|
290
|
//删除下载的图片
|
294
|
//删除下载的图片
|
291
|
- new File(MessageSenderConstant.TEMP_FILE_PATH).delete();
|
295
|
+ deleteFile(pathFile);
|
|
|
296
|
+
|
292
|
JSONObject object = new JSONObject();
|
297
|
JSONObject object = new JSONObject();
|
293
|
object.put("media_id",resultObj.get("media_id"));
|
298
|
object.put("media_id",resultObj.get("media_id"));
|
294
|
customMsgBO.setSendContent(object.toJSONString());
|
299
|
customMsgBO.setSendContent(object.toJSONString());
|
|
|
300
|
+ logger.info("getMediaId success end,sceneKey is {},result is {}",customMsgBO.getSendKey(),customMsgBO.getSendContent());
|
295
|
} catch (IOException | NoSuchAlgorithmException | KeyManagementException | NoSuchProviderException e) {
|
301
|
} catch (IOException | NoSuchAlgorithmException | KeyManagementException | NoSuchProviderException e) {
|
296
|
logger.error("customMsgSend with IO post error,e is {}",e);
|
302
|
logger.error("customMsgSend with IO post error,e is {}",e);
|
297
|
return false;
|
303
|
return false;
|
|
@@ -299,6 +305,16 @@ public class WechatServiceImpl implements IWechatService { |
|
@@ -299,6 +305,16 @@ public class WechatServiceImpl implements IWechatService { |
299
|
return true;
|
305
|
return true;
|
300
|
}
|
306
|
}
|
301
|
|
307
|
|
|
|
308
|
+ private void deleteFile(String pathFile) {
|
|
|
309
|
+ if(!new File(pathFile).delete()){
|
|
|
310
|
+ logger.warn("delete file error,file is {}",pathFile);
|
|
|
311
|
+ }
|
|
|
312
|
+ }
|
|
|
313
|
+
|
|
|
314
|
+ private String getMediaUrl(AccessToken accessToken) {
|
|
|
315
|
+ return Consts.ADD_MATERIAL_URL.replace("ACCESSTOKEN",accessToken.getToken()).replace("TYPE", WechatCustomMsgTypeEnum.IMAGE.getText());
|
|
|
316
|
+ }
|
|
|
317
|
+
|
302
|
private boolean checkResult(WechatCustomMsgBO customMsgBO, JSONObject resultObj,String fileName) throws KeyManagementException, NoSuchAlgorithmException, NoSuchProviderException, IOException {
|
318
|
private boolean checkResult(WechatCustomMsgBO customMsgBO, JSONObject resultObj,String fileName) throws KeyManagementException, NoSuchAlgorithmException, NoSuchProviderException, IOException {
|
303
|
if(resultObj.get("errcode") != null){
|
319
|
if(resultObj.get("errcode") != null){
|
304
|
//这里重新获取token再调用一次
|
320
|
//这里重新获取token再调用一次
|
|
@@ -306,6 +322,7 @@ public class WechatServiceImpl implements IWechatService { |
|
@@ -306,6 +322,7 @@ public class WechatServiceImpl implements IWechatService { |
306
|
String addMateralUrl = Consts.ADD_MATERIAL_URL.replace("ACCESSTOKEN",newAccessToken).replace("TYPE", WechatCustomMsgTypeEnum.IMAGE.getText());
|
322
|
String addMateralUrl = Consts.ADD_MATERIAL_URL.replace("ACCESSTOKEN",newAccessToken).replace("TYPE", WechatCustomMsgTypeEnum.IMAGE.getText());
|
307
|
String mediaIdResult = HttpClientUtil.connectHttpsByPost(addMateralUrl,new File(MessageSenderConstant.TEMP_FILE_PATH + fileName));
|
323
|
String mediaIdResult = HttpClientUtil.connectHttpsByPost(addMateralUrl,new File(MessageSenderConstant.TEMP_FILE_PATH + fileName));
|
308
|
if(StringUtils.isEmpty(mediaIdResult) || JSONObject.parseObject(mediaIdResult).get("errcode") != null){
|
324
|
if(StringUtils.isEmpty(mediaIdResult) || JSONObject.parseObject(mediaIdResult).get("errcode") != null){
|
|
|
325
|
+ logger.warn("checkResult with try again error,sceneKey is {},error is {}",customMsgBO.getSendKey(),mediaIdResult);
|
309
|
return false;
|
326
|
return false;
|
310
|
}
|
327
|
}
|
311
|
resultObj.put("media_id",JSONObject.parseObject(mediaIdResult).get("media_id"));
|
328
|
resultObj.put("media_id",JSONObject.parseObject(mediaIdResult).get("media_id"));
|