|
@@ -3,10 +3,12 @@ package cn.jx.wechat.app.wechatrobot.web.business;
|
|
|
import cn.jx.wechat.app.wechatrobot.api.SendMessageBody;
|
|
|
import cn.jx.wechat.app.wechatrobot.config.BusinessAccessTokenConfig;
|
|
|
import cn.jx.wechat.app.wechatrobot.config.BusinessVerifyProperties;
|
|
|
+import cn.jx.wechat.app.wechatrobot.service.face.AppApi;
|
|
|
import cn.jx.wechat.app.wechatrobot.service.face.BusinessVerify;
|
|
|
import cn.jx.wechat.app.wechatrobot.util.SpringBeanHelper;
|
|
|
import com.google.gson.*;
|
|
|
import com.google.gson.reflect.TypeToken;
|
|
|
+import org.checkerframework.checker.units.qual.A;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
@@ -24,101 +26,16 @@ public class AppApiCtrl {
|
|
|
|
|
|
private static Logger log= LoggerFactory.getLogger(AppApiCtrl.class);
|
|
|
|
|
|
- private final static String Send_Message_Url="https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=%s";
|
|
|
+ private AppApi appApi;
|
|
|
+ @Autowired
|
|
|
+ public AppApiCtrl(AppApi appApi) {
|
|
|
+ this.appApi = appApi;
|
|
|
+ }
|
|
|
|
|
|
@PostMapping("/sendMassage")
|
|
|
public JsonObject sendMessage(@RequestBody SendMessageBody sendMessageBody, @RequestParam String toUser, @RequestParam(required = false) String msgType) {
|
|
|
-
|
|
|
- JsonObject result = new JsonObject();
|
|
|
- result.addProperty("code", 0);
|
|
|
- result.addProperty("mark", "success");
|
|
|
- result.addProperty("msg", "");
|
|
|
- result.add("data", null);
|
|
|
-
|
|
|
- String accessToken = null;
|
|
|
-
|
|
|
- BusinessAccessTokenConfig businessAccessTokenConfig = SpringBeanHelper.getBean(BusinessAccessTokenConfig.class);
|
|
|
- BusinessVerifyProperties businessVerifyProp = SpringBeanHelper.getBean(BusinessVerifyProperties.class);
|
|
|
- RestTemplate rest = SpringBeanHelper.getBean("defaultAppRest", RestTemplate.class);
|
|
|
- try {
|
|
|
- accessToken = businessAccessTokenConfig.currentAccessToken();
|
|
|
- }catch (Exception exp){
|
|
|
- log.info("发送信息异常,{}", exp.getMessage());
|
|
|
- result.addProperty("code", 500);
|
|
|
- result.addProperty("mark", "error");
|
|
|
- result.addProperty("msg", "消息发送异常:"+exp.getMessage() );
|
|
|
- return result;
|
|
|
- }
|
|
|
- String sendMessageUrl= String.format(Send_Message_Url, accessToken);
|
|
|
String msg= null!= sendMessageBody.getMsg()?sendMessageBody.getMsg():"";
|
|
|
-
|
|
|
-
|
|
|
- // 创建请求实体
|
|
|
- try {
|
|
|
- // 设置请求Header
|
|
|
- HttpHeaders headers = new HttpHeaders();
|
|
|
- headers.setContentType(MediaType.APPLICATION_JSON);
|
|
|
- Gson messageGson = new GsonBuilder().setPrettyPrinting().create();
|
|
|
- JsonObject messageObj = new JsonObject();
|
|
|
-
|
|
|
- messageObj.addProperty("agentid",Integer.parseInt(businessVerifyProp.getAgentId()));
|
|
|
- if(null!=msgType&&msgType.equals("text")){
|
|
|
- messageObj.addProperty("msgtype","text");
|
|
|
- JsonObject contentObj = new JsonObject();
|
|
|
- contentObj.addProperty("content",msg);
|
|
|
- messageObj.add("text",contentObj);
|
|
|
- }else if(null==msgType||msgType.isEmpty()){
|
|
|
- messageObj.addProperty("msgtype","text");
|
|
|
- JsonObject contentObj = new JsonObject();
|
|
|
- contentObj.addProperty("content",msg);
|
|
|
- messageObj.add("text",contentObj);
|
|
|
- }else{
|
|
|
- messageObj.addProperty("msgtype",msgType);
|
|
|
- }
|
|
|
-
|
|
|
- messageObj.addProperty("touser",toUser);
|
|
|
-
|
|
|
- HttpEntity<String> requestEntity = new HttpEntity<>(messageGson.toJson(messageObj), headers);
|
|
|
- ResponseEntity<String> responseEntity = rest.exchange(sendMessageUrl, HttpMethod.POST, requestEntity, String.class);
|
|
|
- if (responseEntity.getStatusCode().is2xxSuccessful()) {
|
|
|
- result.addProperty("msg", "消息发送成功" );
|
|
|
- String body = responseEntity.getBody();
|
|
|
- if(null!=body&&!body.isEmpty()){
|
|
|
- Gson gson = new GsonBuilder().setPrettyPrinting().create();
|
|
|
- JsonObject jsonObject = gson.fromJson(responseEntity.getBody(), JsonObject.class);
|
|
|
- if(jsonObject.has("errcode")&&jsonObject.getAsInt()==60020){
|
|
|
- log.info("发送信息失败,{},{}", responseEntity.getStatusCode(), "60020");
|
|
|
- result.addProperty("msg", "消息发送失败");
|
|
|
- result.addProperty("code", 500);
|
|
|
- result.addProperty("mark", "error");
|
|
|
- result.add("data", jsonObject);
|
|
|
- }else{
|
|
|
- log.info("发送信息成功");
|
|
|
- result.addProperty("msg", "发送信息成功");
|
|
|
- result.add("data", jsonObject);
|
|
|
- }
|
|
|
- }else{
|
|
|
- log.info("发送信息失败,{},{}", responseEntity.getStatusCode(), "响应为空");
|
|
|
- result.addProperty("code", 500);
|
|
|
- result.addProperty("mark", "error");
|
|
|
- result.addProperty("msg", "消息发送失败" );
|
|
|
- }
|
|
|
- } else {
|
|
|
- log.info("发送信息失败,{},{}", responseEntity.getStatusCode(), responseEntity.getBody());
|
|
|
- result.addProperty("code", 500);
|
|
|
- result.addProperty("mark", "error");
|
|
|
- result.addProperty("msg", "消息发送失败" );
|
|
|
- }
|
|
|
-
|
|
|
- return result;
|
|
|
- }catch (Exception exp){
|
|
|
- log.info("发送信息异常,{}", exp.getMessage());
|
|
|
- result.addProperty("code", 500);
|
|
|
- result.addProperty("mark", "error");
|
|
|
- result.addProperty("msg", "消息发送异常" );
|
|
|
- return result;
|
|
|
- }
|
|
|
-
|
|
|
+ return this.appApi.sendMessage(msg,msgType,toUser);
|
|
|
}
|
|
|
|
|
|
|