Commit 00b63b88 by danmo

消息发送图片改为发送时换取素材id

1 parent 491eb31e
...@@ -36,4 +36,13 @@ public interface WeMediaClient { ...@@ -36,4 +36,13 @@ public interface WeMediaClient {
@Post(url = "/media/upload") @Post(url = "/media/upload")
WeMediaDto upload(@DataFile(value = "media", fileName = "${1}") InputStream file, String filename, @Query("type") String type); WeMediaDto upload(@DataFile(value = "media", fileName = "${1}") InputStream file, String filename, @Query("type") String type);
/**
* 上传临时素材
* Inputstream 对象
* 使用byte数组和Inputstream对象时一定要定义fileName属性
*/
@Post(url = "/media/upload")
WeMediaDto upload(@DataFile(value = "media") InputStream file, @Query("type") String type);
} }
...@@ -94,6 +94,8 @@ public interface IWeMaterialService { ...@@ -94,6 +94,8 @@ public interface IWeMaterialService {
*/ */
public WeMediaDto uploadTemporaryMaterial(String url, String type,String name); public WeMediaDto uploadTemporaryMaterial(String url, String type,String name);
public WeMediaDto uploadTemporaryMaterial(String url, String type);
/** /**
* 上传素材图片 * 上传素材图片
* @param file * @param file
......
...@@ -13,11 +13,14 @@ import com.linkwechat.wecom.client.WeCustomerMessagePushClient; ...@@ -13,11 +13,14 @@ import com.linkwechat.wecom.client.WeCustomerMessagePushClient;
import com.linkwechat.wecom.domain.WeCustomer; import com.linkwechat.wecom.domain.WeCustomer;
import com.linkwechat.wecom.domain.WeCustomerMessage; import com.linkwechat.wecom.domain.WeCustomerMessage;
import com.linkwechat.wecom.domain.WeGroup; import com.linkwechat.wecom.domain.WeGroup;
import com.linkwechat.wecom.domain.dto.WeMediaDto;
import com.linkwechat.wecom.domain.dto.message.CustomerMessagePushDto; import com.linkwechat.wecom.domain.dto.message.CustomerMessagePushDto;
import com.linkwechat.wecom.domain.dto.message.ImageMessageDto;
import com.linkwechat.wecom.domain.dto.message.SendMessageResultDto; import com.linkwechat.wecom.domain.dto.message.SendMessageResultDto;
import com.linkwechat.wecom.domain.dto.message.WeCustomerMessagePushDto; import com.linkwechat.wecom.domain.dto.message.WeCustomerMessagePushDto;
import com.linkwechat.wecom.mapper.WeCustomerMessageMapper; import com.linkwechat.wecom.mapper.WeCustomerMessageMapper;
import com.linkwechat.wecom.service.IWeCustomerMessageService; import com.linkwechat.wecom.service.IWeCustomerMessageService;
import com.linkwechat.wecom.service.IWeMaterialService;
import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
...@@ -45,6 +48,9 @@ public class WeCustomerMessageServiceImpl extends ServiceImpl<WeCustomerMessageM ...@@ -45,6 +48,9 @@ public class WeCustomerMessageServiceImpl extends ServiceImpl<WeCustomerMessageM
@Autowired @Autowired
private WeCustomerMessagePushClient weCustomerMessagePushClient; private WeCustomerMessagePushClient weCustomerMessagePushClient;
@Autowired
private IWeMaterialService weMaterialService;
@Override @Override
public int updateWeCustomerMessageActualSend(Long messageId, Integer actualSend) { public int updateWeCustomerMessageActualSend(Long messageId, Integer actualSend) {
...@@ -147,9 +153,15 @@ public class WeCustomerMessageServiceImpl extends ServiceImpl<WeCustomerMessageM ...@@ -147,9 +153,15 @@ public class WeCustomerMessageServiceImpl extends ServiceImpl<WeCustomerMessageM
} }
if (customerMessagePushDto.getMessageType().equals(GroupMessageType.IMAGE.getType())) { if (customerMessagePushDto.getMessageType().equals(GroupMessageType.IMAGE.getType())) {
ImageMessageDto imageMessage = customerMessagePushDto.getImageMessage();
try {
WeMediaDto weMediaDto = weMaterialService.uploadTemporaryMaterial(imageMessage.getPic_url(), GroupMessageType.IMAGE.getMessageType());
imageMessage.setMedia_id(weMediaDto.getMedia_id());
} catch (Exception e) {
}
JSONObject jsonObject = new JSONObject(); JSONObject jsonObject = new JSONObject();
jsonObject.put("msgtype","image"); jsonObject.put("msgtype","image");
jsonObject.put("image",customerMessagePushDto.getImageMessage()); jsonObject.put("image",imageMessage);
list.add(jsonObject); list.add(jsonObject);
} }
if (customerMessagePushDto.getMessageType().equals(GroupMessageType.LINK.getType())) { if (customerMessagePushDto.getMessageType().equals(GroupMessageType.LINK.getType())) {
......
...@@ -153,6 +153,27 @@ public class WeMaterialServiceImpl implements IWeMaterialService { ...@@ -153,6 +153,27 @@ public class WeMaterialServiceImpl implements IWeMaterialService {
} }
@Override @Override
public WeMediaDto uploadTemporaryMaterial(String url, String type) {
HttpURLConnection conn = null;
try{
URL materialUrl = new URL(url);
conn = (HttpURLConnection) materialUrl.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(20 * 1000);
InputStream inputStream = conn.getInputStream();
return weMediaClient.upload(inputStream, type);
}catch (Exception e){
e.printStackTrace();
log.error("上传临时文件失败......url:{},type:{},ex:{},st:{}",url,type,e.getMessage(),e.getStackTrace());
}finally {
if (conn !=null){
conn = null;
}
}
return null;
}
@Override
public WeMediaDto uploadImg(MultipartFile file) { public WeMediaDto uploadImg(MultipartFile file) {
return weMediaClient.uploadimg(file); return weMediaClient.uploadimg(file);
} }
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!