fix: 修正微信服务号发送失败问题
This commit is contained in:
parent
90a8102ee3
commit
df19957523
@ -31,9 +31,7 @@ import java.util.*;
|
|||||||
/**
|
/**
|
||||||
* 使用javax.mail进行邮件发送
|
* 使用javax.mail进行邮件发送
|
||||||
*
|
*
|
||||||
* @author bsetfeng
|
* @author xueye
|
||||||
* @author zhouhao
|
|
||||||
* @since 1.0
|
|
||||||
**/
|
**/
|
||||||
public class DefaultEmailNotifier implements Notifier<EmailTemplate> {
|
public class DefaultEmailNotifier implements Notifier<EmailTemplate> {
|
||||||
|
|
||||||
|
@ -3,7 +3,8 @@ package com.cicdi.notify.wechat;
|
|||||||
import com.cicdi.notify.Provider;
|
import com.cicdi.notify.Provider;
|
||||||
|
|
||||||
public enum WechatProvider implements Provider {
|
public enum WechatProvider implements Provider {
|
||||||
corpMessage("微信企业消息通知");
|
corpMessage("微信企业消息通知"),
|
||||||
|
subscription("微信公众号");
|
||||||
|
|
||||||
private final String name;
|
private final String name;
|
||||||
|
|
||||||
@ -20,6 +21,4 @@ public enum WechatProvider implements Provider {
|
|||||||
public String getName() {
|
public String getName() {
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,6 @@ import com.cicdi.notify.DefaultNotifyType;
|
|||||||
import com.cicdi.notify.Notifier;
|
import com.cicdi.notify.Notifier;
|
||||||
import com.cicdi.notify.NotifyType;
|
import com.cicdi.notify.NotifyType;
|
||||||
import com.cicdi.notify.Provider;
|
import com.cicdi.notify.Provider;
|
||||||
import com.cicdi.notify.template.TemplateManager;
|
|
||||||
import com.cicdi.notify.wechat.WechatProvider;
|
import com.cicdi.notify.wechat.WechatProvider;
|
||||||
import org.apache.http.NameValuePair;
|
import org.apache.http.NameValuePair;
|
||||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||||
@ -75,11 +74,8 @@ public class WechatCorpNotifier implements Notifier<WechatCorpTemplate> {
|
|||||||
String result = EntityUtils.toString(response.getEntity());
|
String result = EntityUtils.toString(response.getEntity());
|
||||||
System.out.println(result);
|
System.out.println(result);
|
||||||
JSONObject jsonObject = JSON.parseObject(result);
|
JSONObject jsonObject = JSON.parseObject(result);
|
||||||
if ("0".equals(jsonObject.get("errcode").toString())) {
|
if (!"0".equals(jsonObject.get("errcode").toString())) {
|
||||||
// log.info("发送微信企业通知成功");
|
throw new RuntimeException("发送微信企业通知失败:" + jsonObject.get("errmsg"));
|
||||||
} else {
|
|
||||||
// log.warn("发送微信企业通知失败:{}", map);
|
|
||||||
// throw new BusinessException("发送微信企业通知失败:" + map.get("errmsg"), code);
|
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
@ -109,8 +105,7 @@ public class WechatCorpNotifier implements Notifier<WechatCorpTemplate> {
|
|||||||
refreshTokenTime = System.currentTimeMillis();
|
refreshTokenTime = System.currentTimeMillis();
|
||||||
return access_token;
|
return access_token;
|
||||||
} else {
|
} else {
|
||||||
// TODO 自定义异常
|
throw new RuntimeException("获取令牌失败:" + responseJson.getString("errmsg"));
|
||||||
// throw new BusinessException("获取Token失败:" + map.get("errmsg"), String.valueOf(map.get("errcode")));
|
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
@ -3,6 +3,7 @@ package com.cicdi.notify.wechat.subscription;
|
|||||||
import com.alibaba.fastjson.JSON;
|
import com.alibaba.fastjson.JSON;
|
||||||
import com.alibaba.fastjson.JSONObject;
|
import com.alibaba.fastjson.JSONObject;
|
||||||
import com.cicdi.notify.*;
|
import com.cicdi.notify.*;
|
||||||
|
import com.cicdi.notify.wechat.WechatProvider;
|
||||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||||
import org.apache.http.client.methods.HttpGet;
|
import org.apache.http.client.methods.HttpGet;
|
||||||
import org.apache.http.client.methods.HttpPost;
|
import org.apache.http.client.methods.HttpPost;
|
||||||
@ -76,8 +77,7 @@ public class WechatSubscriptionNotifier implements Notifier<WechatSubscriptionTe
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Provider getProvider() {
|
public Provider getProvider() {
|
||||||
// return WechatProvider.subscription;
|
return WechatProvider.subscription;
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -92,7 +92,8 @@ public class WechatSubscriptionNotifier implements Notifier<WechatSubscriptionTe
|
|||||||
httpPost.setEntity(requestBody);
|
httpPost.setEntity(requestBody);
|
||||||
CloseableHttpResponse response = httpClient.execute(httpPost);
|
CloseableHttpResponse response = httpClient.execute(httpPost);
|
||||||
JSONObject responseJson = JSON.parseObject(EntityUtils.toString(response.getEntity()));
|
JSONObject responseJson = JSON.parseObject(EntityUtils.toString(response.getEntity()));
|
||||||
if (!"0".equals(responseJson.get("errcode"))) {
|
System.out.println(responseJson);
|
||||||
|
if (!responseJson.containsKey("errcode") || responseJson.getIntValue("errcode") != 0) {
|
||||||
throw new RuntimeException(responseJson.get("errmsg").toString());
|
throw new RuntimeException(responseJson.get("errmsg").toString());
|
||||||
}
|
}
|
||||||
} catch (URISyntaxException | IOException e) {
|
} catch (URISyntaxException | IOException e) {
|
||||||
|
@ -7,6 +7,8 @@ import com.cicdi.notify.template.TemplateProperties;
|
|||||||
import com.cicdi.notify.template.TemplateProvider;
|
import com.cicdi.notify.template.TemplateProvider;
|
||||||
import com.cicdi.notify.wechat.WechatProvider;
|
import com.cicdi.notify.wechat.WechatProvider;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 微信公众号通知器提供商
|
* 微信公众号通知器提供商
|
||||||
*
|
*
|
||||||
@ -20,16 +22,16 @@ public class WechatSubscriptionNotifierProvider implements NotifierProvider, Tem
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Provider getProvider() {
|
public Provider getProvider() {
|
||||||
return WechatProvider.corpMessage;
|
return WechatProvider.subscription;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Template createTemplate(TemplateProperties properties) {
|
public Template createTemplate(TemplateProperties properties) {
|
||||||
return JSON.parseObject(String.valueOf(properties), WechatSubscriptionTemplate.class);
|
return JSON.parseObject(String.valueOf(properties.getTemplate()), WechatSubscriptionTemplate.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Notifier<? extends Template> createNotifier(NotifierProperties properties) {
|
public Notifier<? extends Template> createNotifier(NotifierProperties properties) {
|
||||||
return null;
|
return new WechatSubscriptionNotifier(properties);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package com.cicdi.notify.wechat;
|
package com.cicdi.notify.wechat;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSON;
|
||||||
import com.alibaba.fastjson.JSONObject;
|
import com.alibaba.fastjson.JSONObject;
|
||||||
import com.cicdi.notify.*;
|
import com.cicdi.notify.*;
|
||||||
import com.cicdi.notify.template.AbstractTemplateManager;
|
import com.cicdi.notify.template.AbstractTemplateManager;
|
||||||
@ -7,6 +8,7 @@ import com.cicdi.notify.template.Template;
|
|||||||
import com.cicdi.notify.template.TemplateManager;
|
import com.cicdi.notify.template.TemplateManager;
|
||||||
import com.cicdi.notify.template.TemplateProperties;
|
import com.cicdi.notify.template.TemplateProperties;
|
||||||
import com.cicdi.notify.wechat.corp.WechatCorpNotifierProvider;
|
import com.cicdi.notify.wechat.corp.WechatCorpNotifierProvider;
|
||||||
|
import com.cicdi.notify.wechat.subscription.WechatSubscriptionNotifierProvider;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
@ -22,13 +24,13 @@ public class WechatSubscriptionTest {
|
|||||||
NotifyConfigManager notifyConfigManager = (notifyType, configId) -> {
|
NotifyConfigManager notifyConfigManager = (notifyType, configId) -> {
|
||||||
NotifierProperties notifierProperties = new NotifierProperties();
|
NotifierProperties notifierProperties = new NotifierProperties();
|
||||||
notifierProperties.setType(DefaultNotifyType.wechat.getId());
|
notifierProperties.setType(DefaultNotifyType.wechat.getId());
|
||||||
notifierProperties.setProvider(WechatProvider.corpMessage.getId());
|
notifierProperties.setProvider(WechatProvider.subscription.getId());
|
||||||
notifierProperties.setId("12");
|
notifierProperties.setId("12");
|
||||||
|
|
||||||
Map<String, Object> config = new HashMap<>();
|
Map<String, Object> config = new HashMap<>();
|
||||||
config.put("corpId", "wwbd49ae2419a55a9f");
|
config.put("grantType", "client_credential");
|
||||||
config.put("corpSecret", "TXDRQw_H8gpVKX0E01EmwMXJ4VooXmj65I-mDe0wQ1k");
|
config.put("appId", "wx080f08f17aab234a");
|
||||||
|
config.put("secret", "2d782a5d9cafecdb1e0448c97e092e24");
|
||||||
notifierProperties.setConfiguration(config);
|
notifierProperties.setConfiguration(config);
|
||||||
|
|
||||||
return notifierProperties;
|
return notifierProperties;
|
||||||
@ -40,17 +42,19 @@ public class WechatSubscriptionTest {
|
|||||||
protected TemplateProperties getProperties(NotifyType type, String id) {
|
protected TemplateProperties getProperties(NotifyType type, String id) {
|
||||||
TemplateProperties templateProperties = new TemplateProperties();
|
TemplateProperties templateProperties = new TemplateProperties();
|
||||||
templateProperties.setType(DefaultNotifyType.wechat.getId());
|
templateProperties.setType(DefaultNotifyType.wechat.getId());
|
||||||
templateProperties.setProvider(WechatProvider.corpMessage.getId());
|
templateProperties.setProvider(WechatProvider.subscription.getId());
|
||||||
|
|
||||||
JSONObject jsonObject = new JSONObject();
|
JSONObject jsonObject = new JSONObject();
|
||||||
jsonObject.put("agentId", "3010084");
|
jsonObject.put("toUser", "oOwtw6GaMogfIsrjV9WGdo2A1t5g");
|
||||||
jsonObject.put("toUser", "XueYe");
|
jsonObject.put("templateId", "DMz3nRo41CUZg4Og7DUN2QHLDwQSJ5hs9eB6oq-V8Uc");
|
||||||
jsonObject.put("toParty", "");
|
Map<String, Object> data = new HashMap<>();
|
||||||
jsonObject.put("toTag", "");
|
Map<String, Object> keyword = new HashMap<>();
|
||||||
jsonObject.put("message", "Hello");
|
keyword.put("value", "xueye");
|
||||||
|
keyword.put("color", "#173177");
|
||||||
// templateProperties.setTemplate(jsonObject.toJSONString());
|
data.put("Hello", keyword);
|
||||||
|
jsonObject.put("data", data);
|
||||||
|
|
||||||
|
templateProperties.setTemplate(JSON.toJSONString(jsonObject));
|
||||||
return templateProperties;
|
return templateProperties;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -58,7 +62,7 @@ public class WechatSubscriptionTest {
|
|||||||
NotifierManager notifierManager = new AbstractNotifierManager(notifyConfigManager){};
|
NotifierManager notifierManager = new AbstractNotifierManager(notifyConfigManager){};
|
||||||
|
|
||||||
// register
|
// register
|
||||||
WechatCorpNotifierProvider provider = new WechatCorpNotifierProvider();
|
WechatSubscriptionNotifierProvider provider = new WechatSubscriptionNotifierProvider();
|
||||||
notifierManager.register(provider);
|
notifierManager.register(provider);
|
||||||
templateManager.register(provider);
|
templateManager.register(provider);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user