Merge pull request #6 from dqget/master

优化getAccount方法,通过class进行类型转换
pull/11/head
Java3y 3 years ago committed by GitHub
commit 83c66f4fe8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -46,7 +46,7 @@ public class RefreshDingDingAccessTokenHandler {
log.info("refreshAccessTokenJob#execute!"); log.info("refreshAccessTokenJob#execute!");
SupportThreadPoolConfig.getPendingSingleThreadPool().execute(() -> { SupportThreadPoolConfig.getPendingSingleThreadPool().execute(() -> {
for (int index = SendAccountConstant.START; true; index = index + SendAccountConstant.STEP) { for (int index = SendAccountConstant.START; true; index = index + SendAccountConstant.STEP) {
DingDingWorkNoticeAccount account = accountUtils.getAccount(index, SendAccountConstant.DING_DING_WORK_NOTICE_ACCOUNT_KEY, SendAccountConstant.DING_DING_WORK_NOTICE_PREFIX, new DingDingWorkNoticeAccount()); DingDingWorkNoticeAccount account = accountUtils.getAccount(index, SendAccountConstant.DING_DING_WORK_NOTICE_ACCOUNT_KEY, SendAccountConstant.DING_DING_WORK_NOTICE_PREFIX, DingDingWorkNoticeAccount.class);
if (account == null) { if (account == null) {
break; break;
} }

@ -45,7 +45,7 @@ public class DingDingRobotHandler extends BaseHandler implements Handler {
@Override @Override
public boolean handler(TaskInfo taskInfo) { public boolean handler(TaskInfo taskInfo) {
try { try {
DingDingRobotAccount account = accountUtils.getAccount(taskInfo.getSendAccount(), SendAccountConstant.DING_DING_ROBOT_ACCOUNT_KEY, SendAccountConstant.DING_DING_ROBOT_PREFIX, new DingDingRobotAccount()); DingDingRobotAccount account = accountUtils.getAccount(taskInfo.getSendAccount(), SendAccountConstant.DING_DING_ROBOT_ACCOUNT_KEY, SendAccountConstant.DING_DING_ROBOT_PREFIX, DingDingRobotAccount.class);
DingDingRobotParam dingDingRobotParam = assembleParam(taskInfo); DingDingRobotParam dingDingRobotParam = assembleParam(taskInfo);
String httpResult = HttpUtil.post(assembleParamUrl(account), JSON.toJSONString(dingDingRobotParam)); String httpResult = HttpUtil.post(assembleParamUrl(account), JSON.toJSONString(dingDingRobotParam));
DingDingRobotResult dingDingRobotResult = JSON.parseObject(httpResult, DingDingRobotResult.class); DingDingRobotResult dingDingRobotResult = JSON.parseObject(httpResult, DingDingRobotResult.class);

@ -50,7 +50,7 @@ public class DingDingWorkNoticeHandler extends BaseHandler implements Handler {
@Override @Override
public boolean handler(TaskInfo taskInfo) { public boolean handler(TaskInfo taskInfo) {
try { try {
DingDingWorkNoticeAccount account = accountUtils.getAccount(taskInfo.getSendAccount(), SendAccountConstant.DING_DING_WORK_NOTICE_ACCOUNT_KEY, SendAccountConstant.DING_DING_WORK_NOTICE_PREFIX, new DingDingWorkNoticeAccount()); DingDingWorkNoticeAccount account = accountUtils.getAccount(taskInfo.getSendAccount(), SendAccountConstant.DING_DING_WORK_NOTICE_ACCOUNT_KEY, SendAccountConstant.DING_DING_WORK_NOTICE_PREFIX, DingDingWorkNoticeAccount.class);
OapiMessageCorpconversationAsyncsendV2Request request = assembleParam(account, taskInfo); OapiMessageCorpconversationAsyncsendV2Request request = assembleParam(account, taskInfo);
String accessToken = redisTemplate.opsForValue().get(SendAccountConstant.DING_DING_ACCESS_TOKEN_PREFIX + taskInfo.getSendAccount()); String accessToken = redisTemplate.opsForValue().get(SendAccountConstant.DING_DING_ACCESS_TOKEN_PREFIX + taskInfo.getSendAccount());
OapiMessageCorpconversationAsyncsendV2Response response = new DefaultDingTalkClient(URL).execute(request, accessToken); OapiMessageCorpconversationAsyncsendV2Response response = new DefaultDingTalkClient(URL).execute(request, accessToken);

@ -62,7 +62,7 @@ public class EmailHandler extends BaseHandler implements Handler {
* @return * @return
*/ */
private MailAccount getAccountConfig(Integer sendAccount) { private MailAccount getAccountConfig(Integer sendAccount) {
MailAccount account = accountUtils.getAccount(sendAccount, SendAccountConstant.EMAIL_ACCOUNT_KEY, SendAccountConstant.EMAIL_ACCOUNT_PREFIX, new MailAccount()); MailAccount account = accountUtils.getAccount(sendAccount, SendAccountConstant.EMAIL_ACCOUNT_KEY, SendAccountConstant.EMAIL_ACCOUNT_PREFIX, MailAccount.class);
try { try {
MailSSLSocketFactory sf = new MailSSLSocketFactory(); MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true); sf.setTrustAllHosts(true);

@ -46,7 +46,7 @@ public class EnterpriseWeChatHandler extends BaseHandler implements Handler {
@Override @Override
public boolean handler(TaskInfo taskInfo) { public boolean handler(TaskInfo taskInfo) {
try { try {
WxCpDefaultConfigImpl accountConfig = accountUtils.getAccount(taskInfo.getSendAccount(), SendAccountConstant.ENTERPRISE_WECHAT_ACCOUNT_KEY, SendAccountConstant.ENTERPRISE_WECHAT_PREFIX, new WxCpDefaultConfigImpl()); WxCpDefaultConfigImpl accountConfig = accountUtils.getAccount(taskInfo.getSendAccount(), SendAccountConstant.ENTERPRISE_WECHAT_ACCOUNT_KEY, SendAccountConstant.ENTERPRISE_WECHAT_PREFIX, WxCpDefaultConfigImpl.class);
WxCpMessageServiceImpl messageService = new WxCpMessageServiceImpl(initService(accountConfig)); WxCpMessageServiceImpl messageService = new WxCpMessageServiceImpl(initService(accountConfig));
WxCpMessageSendResult result = messageService.send(buildWxCpMessage(taskInfo, accountConfig.getAgentId())); WxCpMessageSendResult result = messageService.send(buildWxCpMessage(taskInfo, accountConfig.getAgentId()));
if (Integer.valueOf(WxMpErrorMsgEnum.CODE_0.getCode()).equals(result.getErrCode())) { if (Integer.valueOf(WxMpErrorMsgEnum.CODE_0.getCode()).equals(result.getErrCode())) {

@ -44,7 +44,7 @@ public class TencentSmsScript implements SmsScript {
@Override @Override
public List<SmsRecord> send(SmsParam smsParam) throws Exception { public List<SmsRecord> send(SmsParam smsParam) throws Exception {
TencentSmsAccount tencentSmsAccount = accountUtils.getAccount(smsParam.getSendAccount(), SendAccountConstant.SMS_ACCOUNT_KEY, SendAccountConstant.SMS_PREFIX, TencentSmsAccount.builder().build()); TencentSmsAccount tencentSmsAccount = accountUtils.getAccount(smsParam.getSendAccount(), SendAccountConstant.SMS_ACCOUNT_KEY, SendAccountConstant.SMS_PREFIX, TencentSmsAccount.class);
SmsClient client = init(tencentSmsAccount); SmsClient client = init(tencentSmsAccount);
SendSmsRequest request = assembleReq(smsParam, tencentSmsAccount); SendSmsRequest request = assembleReq(smsParam, tencentSmsAccount);
SendSmsResponse response = client.SendSms(request); SendSmsResponse response = client.SendSms(request);

@ -27,14 +27,14 @@ public class AccountUtils {
* (key:dingDingRobotAccount) [{"ding_ding_robot_10":{"secret":"SEC996d8d9d4768aded74114faae924f229229de444475a1c295d64fedf","webhook":"https://oapi.dingtalk.com/robot/send?access_token=8d03b644ffb6534b203d87333367328b0c3003d164715d2c6c6e56"}}] * (key:dingDingRobotAccount) [{"ding_ding_robot_10":{"secret":"SEC996d8d9d4768aded74114faae924f229229de444475a1c295d64fedf","webhook":"https://oapi.dingtalk.com/robot/send?access_token=8d03b644ffb6534b203d87333367328b0c3003d164715d2c6c6e56"}}]
* (key:dingDingWorkNoticeAccount) [{"ding_ding_work_notice_10":{"appKey":"dingh6yyyyyyycrlbx","appSecret":"tQpvmkR863333yyyyyHP3QHyyyymy9Ao1yoL1oQX5Nlx_fYLLLlpPJWHvWKbTu","agentId":"152333383622"}}] * (key:dingDingWorkNoticeAccount) [{"ding_ding_work_notice_10":{"appKey":"dingh6yyyyyyycrlbx","appSecret":"tQpvmkR863333yyyyyHP3QHyyyymy9Ao1yoL1oQX5Nlx_fYLLLlpPJWHvWKbTu","agentId":"152333383622"}}]
*/ */
public <T> T getAccount(Integer sendAccount, String apolloKey, String prefix, T t) { public <T> T getAccount(Integer sendAccount, String apolloKey, String prefix, Class<T> clazz) {
String accountValues = config.getProperty(apolloKey, AustinConstant.APOLLO_DEFAULT_VALUE_JSON_ARRAY); String accountValues = config.getProperty(apolloKey, AustinConstant.APOLLO_DEFAULT_VALUE_JSON_ARRAY);
JSONArray jsonArray = JSON.parseArray(accountValues); JSONArray jsonArray = JSON.parseArray(accountValues);
for (int i = 0; i < jsonArray.size(); i++) { for (int i = 0; i < jsonArray.size(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i); JSONObject jsonObject = jsonArray.getJSONObject(i);
Object object = jsonObject.getObject(prefix + sendAccount, t.getClass()); T object = jsonObject.getObject(prefix + sendAccount, clazz);
if (object != null) { if (object != null) {
return (T) object; return object;
} }
} }
return null; return null;

Loading…
Cancel
Save