...
|
...
|
@@ -249,6 +249,7 @@ public class WechatServiceImpl implements IWechatService { |
|
|
|
|
|
//处理文案内容若为图片,则需要上传图片至微信素材中心,获取 media_id 拿来发消息 获取失败,直接返回
|
|
|
if(customMsgBO.getMsgType().equals(WechatCustomMsgTypeEnum.IMAGE.getText()) && !getMediaId(customMsgBO, accessToken)){
|
|
|
logger.warn("customMsgSend getMediaId error, with sceneKey is {}",customMsgBO.getSendKey());
|
|
|
return new ArrayList<>();
|
|
|
}
|
|
|
|
...
|
...
|
@@ -274,24 +275,29 @@ public class WechatServiceImpl implements IWechatService { |
|
|
private boolean getMediaId(WechatCustomMsgBO customMsgBO, AccessToken accessToken) {
|
|
|
try {
|
|
|
//下载网络图片
|
|
|
String fileName = String.valueOf(System.currentTimeMillis()) + ".png";
|
|
|
String fileName = String.valueOf(System.currentTimeMillis()) + MessageSenderConstant.IMG_SUFFIX;
|
|
|
String pathFile = MessageSenderConstant.TEMP_FILE_PATH + fileName;
|
|
|
HttpClientUtil.downFromPicUrl(customMsgBO.getSendContent(), MessageSenderConstant.TEMP_FILE_PATH, fileName);
|
|
|
//上传微信 获取 media_id
|
|
|
String addMateralUrl = Consts.ADD_MATERIAL_URL.replace("ACCESSTOKEN","sdjfwoeijowen").replace("TYPE", WechatCustomMsgTypeEnum.IMAGE.getText());
|
|
|
String mediaIdResult = HttpClientUtil.connectHttpsByPost(addMateralUrl,new File(MessageSenderConstant.TEMP_FILE_PATH + fileName));
|
|
|
String addMediaUrl = getMediaUrl(accessToken);
|
|
|
//发送微信请求
|
|
|
String mediaIdResult = HttpClientUtil.connectHttpsByPost(addMediaUrl,new File(pathFile));
|
|
|
if(StringUtils.isEmpty(mediaIdResult)){
|
|
|
logger.warn("getMediaId with get mediaId null,imgUrl is {}",customMsgBO.getSendContent());
|
|
|
logger.warn("getMediaId with get result null,sceneKey is {},imgUrl is {}",customMsgBO.getSendKey(),customMsgBO.getSendContent());
|
|
|
return false;
|
|
|
}
|
|
|
JSONObject resultObj = JSONObject.parseObject(mediaIdResult);
|
|
|
|
|
|
//校验返回结果
|
|
|
if (!checkResult(customMsgBO, resultObj,fileName)) return false;
|
|
|
|
|
|
//删除下载的图片
|
|
|
new File(MessageSenderConstant.TEMP_FILE_PATH).delete();
|
|
|
JSONObject object = new JSONObject();
|
|
|
deleteFile(pathFile);
|
|
|
|
|
|
JSONObject object = new JSONObject();
|
|
|
object.put("media_id",resultObj.get("media_id"));
|
|
|
customMsgBO.setSendContent(object.toJSONString());
|
|
|
logger.info("getMediaId success end,sceneKey is {},result is {}",customMsgBO.getSendKey(),customMsgBO.getSendContent());
|
|
|
} catch (IOException | NoSuchAlgorithmException | KeyManagementException | NoSuchProviderException e) {
|
|
|
logger.error("customMsgSend with IO post error,e is {}",e);
|
|
|
return false;
|
...
|
...
|
@@ -299,6 +305,16 @@ public class WechatServiceImpl implements IWechatService { |
|
|
return true;
|
|
|
}
|
|
|
|
|
|
private void deleteFile(String pathFile) {
|
|
|
if(!new File(pathFile).delete()){
|
|
|
logger.warn("delete file error,file is {}",pathFile);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private String getMediaUrl(AccessToken accessToken) {
|
|
|
return Consts.ADD_MATERIAL_URL.replace("ACCESSTOKEN",accessToken.getToken()).replace("TYPE", WechatCustomMsgTypeEnum.IMAGE.getText());
|
|
|
}
|
|
|
|
|
|
private boolean checkResult(WechatCustomMsgBO customMsgBO, JSONObject resultObj,String fileName) throws KeyManagementException, NoSuchAlgorithmException, NoSuchProviderException, IOException {
|
|
|
if(resultObj.get("errcode") != null){
|
|
|
//这里重新获取token再调用一次
|
...
|
...
|
@@ -306,6 +322,7 @@ public class WechatServiceImpl implements IWechatService { |
|
|
String addMateralUrl = Consts.ADD_MATERIAL_URL.replace("ACCESSTOKEN",newAccessToken).replace("TYPE", WechatCustomMsgTypeEnum.IMAGE.getText());
|
|
|
String mediaIdResult = HttpClientUtil.connectHttpsByPost(addMateralUrl,new File(MessageSenderConstant.TEMP_FILE_PATH + fileName));
|
|
|
if(StringUtils.isEmpty(mediaIdResult) || JSONObject.parseObject(mediaIdResult).get("errcode") != null){
|
|
|
logger.warn("checkResult with try again error,sceneKey is {},error is {}",customMsgBO.getSendKey(),mediaIdResult);
|
|
|
return false;
|
|
|
}
|
|
|
resultObj.put("media_id",JSONObject.parseObject(mediaIdResult).get("media_id"));
|
...
|
...
|
|