|
@@ -5,8 +5,10 @@ import com.alibaba.fastjson.JSONObject;
|
|
|
import com.ematou.wxservice.api.WeChatApi;
|
|
|
import com.ematou.wxservice.api.WeChatApiRestTemplate;
|
|
|
import com.ematou.wxservice.common.constant.WeChatConstant;
|
|
|
+import com.ematou.wxservice.common.utils.DateUtil;
|
|
|
import com.ematou.wxservice.common.utils.SignUtil;
|
|
|
import com.ematou.wxservice.config.WeChatGeneralConfig;
|
|
|
+import com.ematou.wxservice.entity.pojo.Material;
|
|
|
import com.ematou.wxservice.entity.vo.AccessToken;
|
|
|
import com.ematou.wxservice.entity.vo.BindingInfo;
|
|
|
import com.ematou.wxservice.entity.vo.TemplateMessage;
|
|
@@ -17,13 +19,12 @@ import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.util.ResourceUtils;
|
|
|
import org.springframework.util.StringUtils;
|
|
|
|
|
|
+import java.io.File;
|
|
|
import java.text.DecimalFormat;
|
|
|
-import java.util.Date;
|
|
|
-import java.util.HashMap;
|
|
|
-import java.util.Map;
|
|
|
-import java.util.Random;
|
|
|
+import java.util.*;
|
|
|
|
|
|
/**
|
|
|
* @author lhm
|
|
@@ -43,6 +44,9 @@ public class WeChatService {
|
|
|
|
|
|
@Autowired
|
|
|
UserInfoMapper userInfoMapper;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private MaterialService materialService;
|
|
|
@Autowired
|
|
|
WeChatMessageService weChatMessageService;
|
|
|
@Value("${wechat}")
|
|
@@ -227,4 +231,73 @@ public class WeChatService {
|
|
|
result.put("signature", sign);
|
|
|
return result;
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+ public String mediaUpload(String eventKey) {
|
|
|
+ try {
|
|
|
+ File file = null;
|
|
|
+ if (eventKey.equals(WeChatConstant.CustomEventKey.BUSINESS_COOPERATION)) {
|
|
|
+ file = ResourceUtils.getFile("classpath:static/images/business-cooperation.png");
|
|
|
+ } else if (eventKey.equals(WeChatConstant.CustomEventKey.AFTER_SALE_SERVICE)) {
|
|
|
+ file = ResourceUtils.getFile("classpath:static/images/after-sale-service.png");
|
|
|
+ }
|
|
|
+ boolean flag = false;
|
|
|
+ String mediaId ="";
|
|
|
+ String fileName = file.getName();
|
|
|
+ logger.info("fileName:{}", fileName);
|
|
|
+ Material material = materialService.queryByFileName(fileName);
|
|
|
+
|
|
|
+ logger.info("查询永久素材数据:{}", JSON.toJSONString(material));
|
|
|
+ String accessToken = this.getAccessToken().getAccessToken();
|
|
|
+ if(!Objects.isNull(material)){
|
|
|
+ mediaId = material.getMediaId();
|
|
|
+ String url = String.format(WeChatApi.MEDIA_GET.getUrl(), accessToken);
|
|
|
+ String requestBody = "{ \"media_id\": \"" + mediaId + "\" }";
|
|
|
+ String res = weChatApiRestTemplate.postForOther(url, requestBody);
|
|
|
+ if(res.contains("errcode")){
|
|
|
+ flag = true;
|
|
|
+ }
|
|
|
+ }else {
|
|
|
+ flag = true;
|
|
|
+ }
|
|
|
+
|
|
|
+ if(flag) {
|
|
|
+ //在数据库和公众号服务器上未查到图片素材
|
|
|
+ String url = String.format(WeChatApi.MEDIA_UPLOAD.getUrl(), accessToken);
|
|
|
+ //String result = weChatApiRestTemplate.doPostByFile(url, null, file, "");
|
|
|
+ String result = weChatApiRestTemplate.postByStream(url, file);
|
|
|
+
|
|
|
+ JSONObject resObj = JSON.parseObject(result);
|
|
|
+ mediaId = resObj.getString("media_id");
|
|
|
+ }
|
|
|
+
|
|
|
+ if(Objects.isNull(material)){
|
|
|
+ long length = file.length();
|
|
|
+ material = new Material();
|
|
|
+ material.setMediaId(mediaId);
|
|
|
+ material.setFileName(fileName);
|
|
|
+ material.setFilePath(file.getPath());
|
|
|
+ material.setFileSize(length);
|
|
|
+ material.setFileType(WeChatConstant.XmlMsgType.IMAGE);
|
|
|
+ material.setCreateTime(DateUtil.formatDate(new Date()));
|
|
|
+
|
|
|
+ logger.info("添加永久素材数据:{}", JSON.toJSONString(material));
|
|
|
+ materialService.save(material);
|
|
|
+ }else {
|
|
|
+ if(!mediaId.equals(material.getMediaId())) {
|
|
|
+ material.setMediaId(mediaId);
|
|
|
+ material.setModTime(DateUtil.formatDate(new Date()));
|
|
|
+
|
|
|
+ logger.info("修改永久素材数据:{}", JSON.toJSONString(material));
|
|
|
+ materialService.update(material);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return mediaId;
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ logger.error("上传公众号永久素材异常:", e);
|
|
|
+ }
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+
|
|
|
}
|