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