接入 个推,刷新token

pull/11/head
3y 3 years ago
parent 754309b1aa
commit d39fc34c34

@ -12,6 +12,13 @@ package com.java3y.austin.common.constant;
*/
public class SendAccountConstant {
/**
* 1010
*/
public static final Integer START = 10;
public static final Integer STEP = 10;
/**
*
*/
@ -19,6 +26,14 @@ public class SendAccountConstant {
public static final String DING_DING_WORK_NOTICE_PREFIX = "ding_ding_work_notice_";
public static final String DING_DING_ACCESS_TOKEN_PREFIX = "ding_ding_access_token_";
/**
* PUSH
*/
public static final String GE_TUI_ACCOUNT_KEY = "geTuiAccount";
public static final String GE_TUI_ACCOUNT_PREFIX = "ge_tui_account_";
public static final String GE_TUI_ACCESS_TOKEN_PREFIX = "ge_tui_access_token_";
/**
*
@ -46,9 +61,5 @@ public class SendAccountConstant {
public static final String SMS_ACCOUNT_KEY = "smsAccount";
public static final String SMS_PREFIX = "sms_";
/**
* 1010
*/
public static final Integer START = 10;
public static final Integer STEP = 10;
}

@ -0,0 +1,30 @@
package com.java3y.austin.common.dto.account;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
*
*
* @author 3y
* <p>
* api
* <p>
* https://docs.getui.com/getui/start/devcenter/
*/
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class GeTuiAccount {
private String appId;
private String appKey;
private String masterSecret;
}

@ -0,0 +1,37 @@
package com.java3y.austin.cron.dto.getui;
import com.alibaba.fastjson.annotation.JSONField;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @author 3y
* @date 2022/5/8
* <p>
* https://docs.getui.com/getui/server/rest_v2/token/
*/
@NoArgsConstructor
@Data
@AllArgsConstructor
@Builder
public class GeTuiTokenResultDTO {
@JSONField(name = "msg")
private String msg;
@JSONField(name = "code")
private Integer code;
@JSONField(name = "data")
private DataDTO data;
@NoArgsConstructor
@Data
public static class DataDTO {
@JSONField(name = "expire_time")
private String expireTime;
@JSONField(name = "token")
private String token;
}
}

@ -0,0 +1,40 @@
package com.java3y.austin.cron.dto.getui;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.http.ContentType;
import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONField;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* token
* @author 3y
* https://docs.getui.com/getui/server/rest_v2/token/
*/
@NoArgsConstructor
@Data
@Builder
@AllArgsConstructor
public class QueryTokenParamDTO {
/**
* sign
*/
@JSONField(name = "sign")
private String sign;
/**
* timestamp
*/
@JSONField(name = "timestamp")
private String timestamp;
/**
* appkey
*/
@JSONField(name = "appkey")
private String appKey;
}

@ -0,0 +1,91 @@
package com.java3y.austin.cron.handler;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.http.ContentType;
import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSON;
import com.google.common.base.Throwables;
import com.java3y.austin.common.constant.SendAccountConstant;
import com.java3y.austin.common.dto.account.GeTuiAccount;
import com.java3y.austin.cron.dto.getui.QueryTokenParamDTO;
import com.java3y.austin.cron.dto.getui.GeTuiTokenResultDTO;
import com.java3y.austin.support.config.SupportThreadPoolConfig;
import com.java3y.austin.support.utils.AccountUtils;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
/**
* token
* <p>
* https://docs.getui.com/getui/server/rest_v2/token/
*
* @author 3y
*/
@Service
@Slf4j
public class RefreshGeTuiAccessTokenHandler {
@Autowired
private StringRedisTemplate redisTemplate;
@Autowired
private AccountUtils accountUtils;
/**
* )
*/
@XxlJob("refreshGeTuiAccessTokenJob")
public void execute() {
log.info("refreshGeTuiAccessTokenJob#execute!");
SupportThreadPoolConfig.getPendingSingleThreadPool().execute(() -> {
for (int index = SendAccountConstant.START; true; index = index + SendAccountConstant.STEP) {
GeTuiAccount account = accountUtils.getAccount(index, SendAccountConstant.GE_TUI_ACCOUNT_KEY, SendAccountConstant.GE_TUI_ACCOUNT_PREFIX, new GeTuiAccount());
if (account == null) {
break;
}
String accessToken = getAccessToken(account);
if (StrUtil.isNotBlank(accessToken)) {
redisTemplate.opsForValue().set(SendAccountConstant.GE_TUI_ACCESS_TOKEN_PREFIX + index, accessToken);
}
}
});
}
/**
* access_token
*
* @param account
* @return
*/
private String getAccessToken(GeTuiAccount account) {
String accessToken = "";
try {
String url = "https://restapi.getui.com/v2/" + account.getAppId() + "/auth";
String time = String.valueOf(System.currentTimeMillis());
String digest = SecureUtil.sha256().digestHex(account.getAppKey() + time + account.getMasterSecret());
QueryTokenParamDTO param = QueryTokenParamDTO.builder()
.timestamp(time)
.appKey(account.getAppKey())
.sign(digest).build();
String body = HttpRequest.post(url).header(Header.CONTENT_TYPE.getValue(), ContentType.JSON.getValue())
.body(JSON.toJSONString(param))
.timeout(20000)
.execute().body();
GeTuiTokenResultDTO geTuiTokenResultDTO = JSON.parseObject(body, GeTuiTokenResultDTO.class);
if (geTuiTokenResultDTO.getCode().equals(0)) {
accessToken = geTuiTokenResultDTO.getData().getToken();
}
} catch (Exception e) {
log.error("RefreshGeTuiAccessTokenHandler#getAccessToken fail:{}", Throwables.getStackTraceAsString(e));
}
return accessToken;
}
}

@ -0,0 +1,115 @@
package com.java3y.austin.handler.domain.getui;
import com.alibaba.fastjson.annotation.JSONField;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.List;
/**
* param
* @author 3y
* https://docs.getui.com/getui/server/rest_v2/push/
*/
@NoArgsConstructor
@AllArgsConstructor
@Data
@Builder
public class SendPushParam {
/**
* requestId
*/
@JSONField(name = "request_id")
private String requestId;
/**
* settings
*/
@JSONField(name = "settings")
private SettingsVO settings;
/**
* audience
*/
@JSONField(name = "audience")
private AudienceVO audience;
/**
* pushMessage
*/
@JSONField(name = "push_message")
private PushMessageVO pushMessage;
/**
* SettingsVO
*/
@NoArgsConstructor
@Data
public static class SettingsVO {
/**
* ttl
*/
@JSONField(name = "ttl")
private Integer ttl;
}
/**
* AudienceVO
*/
@NoArgsConstructor
@Data
@AllArgsConstructor
@Builder
public static class AudienceVO {
/**
* cid
*/
@JSONField(name = "cid")
private List<String> cid;
}
/**
* PushMessageVO
*/
@NoArgsConstructor
@Data
@AllArgsConstructor
@Builder
public static class PushMessageVO {
/**
* notification
*/
@JSONField(name = "notification")
private NotificationVO notification;
/**
* NotificationVO
*/
@NoArgsConstructor
@Data
@AllArgsConstructor
@Builder
public static class NotificationVO {
/**
* title
*/
@JSONField(name = "title")
private String title;
/**
* body
*/
@JSONField(name = "body")
private String body;
/**
* clickType
*/
@JSONField(name = "click_type")
private String clickType;
/**
* url
*/
@JSONField(name = "url")
private String url;
}
}
}

@ -0,0 +1,38 @@
package com.java3y.austin.handler.domain.getui;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
*
* @author 3y
* https://docs.getui.com/getui/server/rest_v2/common_args/?id=doc-title-1
*/
@Builder
@Data
@AllArgsConstructor
@NoArgsConstructor
public class SendPushResult {
/**
* msg
*/
@JSONField(name = "msg")
private String msg;
/**
* code
*/
@JSONField(name = "code")
private Integer code;
/**
* data
*/
@JSONField(name = "data")
private JSONObject data;
}
Loading…
Cancel
Save