|
@@ -3,6 +3,8 @@ package thyyxxk.wxservice_server.service;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.http.HttpEntity;
|
|
|
+import org.springframework.http.HttpHeaders;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.web.client.RestTemplate;
|
|
|
import thyyxxk.wxservice_server.config.exception.BizException;
|
|
@@ -22,6 +24,7 @@ import java.util.List;
|
|
|
@Service
|
|
|
public class PushWxMessageService {
|
|
|
private final WxApiDao dao;
|
|
|
+ private static final String API = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=";
|
|
|
|
|
|
@Autowired
|
|
|
public PushWxMessageService(WxApiDao dao) {
|
|
@@ -36,24 +39,32 @@ public class PushWxMessageService {
|
|
|
if (null == openidList || openidList.isEmpty()) {
|
|
|
return "ERROR:未找到卡号【" + param.getCardNo() + "】的openid";
|
|
|
}
|
|
|
- RestTemplate template = new RestTemplate();
|
|
|
+
|
|
|
+ String wxUrl = API + PropertiesUtil.getLocalProperty("access_token");
|
|
|
JSONObject content = param.getMsgContext();
|
|
|
- String wxUrl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" +
|
|
|
- PropertiesUtil.getLocalProperty("access_token");
|
|
|
- String res = "ERROR:推送失败,网络异常";
|
|
|
+
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
+ headers.add("content-type", "application/json;charset=UTF-8");
|
|
|
+ RestTemplate template = new RestTemplate();
|
|
|
+
|
|
|
+ String res = "";
|
|
|
for (String openid : openidList) {
|
|
|
content.replace("touser", openid);
|
|
|
- res = template.postForObject(wxUrl, content, String.class);
|
|
|
+ HttpEntity<JSONObject> entity = new HttpEntity<>(content, headers);
|
|
|
+ res = template.postForObject(wxUrl, entity, String.class);
|
|
|
log.info("推送消息1:内容:{},结果:{}", content, res);
|
|
|
}
|
|
|
return res;
|
|
|
}
|
|
|
|
|
|
public String pushMessage2(JSONObject msgContent) {
|
|
|
- RestTemplate template = new RestTemplate();
|
|
|
- String wxUrl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" +
|
|
|
- PropertiesUtil.getLocalProperty("access_token");
|
|
|
- String res = template.postForObject(wxUrl, msgContent, String.class);
|
|
|
+ String wxUrl = API + PropertiesUtil.getLocalProperty("access_token");
|
|
|
+
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
+ headers.add("content-type", "application/json;charset=UTF-8");
|
|
|
+ HttpEntity<JSONObject> entity = new HttpEntity<>(msgContent, headers);
|
|
|
+ String res = new RestTemplate().postForObject(wxUrl, entity, String.class);
|
|
|
+
|
|
|
log.info("推送消息2:内容:{},结果:{}", msgContent, res);
|
|
|
if (StringUtil.isBlank(res)) {
|
|
|
return "推送失败,未知的异常。";
|
|
@@ -71,4 +82,5 @@ public class PushWxMessageService {
|
|
|
}
|
|
|
return resobj.getString("errmsg");
|
|
|
}
|
|
|
+
|
|
|
}
|