mirror of
https://github.com/luoye663/e5.git
synced 2024-12-26 11:48:50 +00:00
~
This commit is contained in:
parent
3a9d8ba204
commit
10eb60b14f
@ -21,7 +21,7 @@ public interface IOutlookService extends IService<Outlook> {
|
||||
|
||||
boolean saveRandomTime(int github_id,int cron_time,int cron_time_random_start,int cron_time_random_end);
|
||||
|
||||
boolean getMailList(Outlook outlook);
|
||||
int getMailList(Outlook outlook) throws Exception;
|
||||
|
||||
List<Outlook> findAll();
|
||||
|
||||
|
@ -11,12 +11,10 @@ import com.google.gson.JsonObject;
|
||||
import io.qyi.e5.outlook.entity.Outlook;
|
||||
import io.qyi.e5.outlook.mapper.OutlookMapper;
|
||||
import io.qyi.e5.outlook.service.IOutlookService;
|
||||
import io.qyi.e5.outlook_log.service.IOutlookLogService;
|
||||
import io.qyi.e5.util.netRequest.OkHttpClientUtil;
|
||||
import io.qyi.e5.util.netRequest.OkHttpRequestUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@ -37,8 +35,6 @@ import java.util.Map;
|
||||
public class OutlookServiceImpl extends ServiceImpl<OutlookMapper, Outlook> implements IOutlookService {
|
||||
private final Logger logger = LoggerFactory.getLogger(this.getClass());
|
||||
|
||||
@Autowired
|
||||
IOutlookLogService outlookLogService;
|
||||
|
||||
@Value("${outlook.errorMsg}")
|
||||
private String[] errorMsg;
|
||||
@ -134,12 +130,13 @@ public class OutlookServiceImpl extends ServiceImpl<OutlookMapper, Outlook> impl
|
||||
|
||||
/**
|
||||
* 删除用户outlook
|
||||
* @Description:
|
||||
* @param: * @param github_id
|
||||
* @return: int
|
||||
* @Author: 落叶随风
|
||||
* @Date: 2020/4/17
|
||||
*/
|
||||
*
|
||||
* @Description:
|
||||
* @param: * @param github_id
|
||||
* @return: int
|
||||
* @Author: 落叶随风
|
||||
* @Date: 2020/4/17
|
||||
*/
|
||||
@Override
|
||||
public int deleteInfo(int github_id) {
|
||||
QueryWrapper<Outlook> outlookQueryWrapper = new QueryWrapper<>();
|
||||
@ -148,42 +145,35 @@ public class OutlookServiceImpl extends ServiceImpl<OutlookMapper, Outlook> impl
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean getMailList(Outlook outlook) {
|
||||
try {
|
||||
String s = MailList(outlook.getAccessToken());
|
||||
JSONObject json = JSON.parseObject(s);
|
||||
/*错误情况,一般是令牌过期*/
|
||||
if (json.containsKey("error")) {
|
||||
String code = json.getJSONObject("error").getString("code");
|
||||
String message = json.getJSONObject("error").getString("message");
|
||||
/*如果出现得错误是没有message中收集的,那么就认为是无法刷新的情况。比如 用户取消了授权、删除了key*/
|
||||
if (!errorCheck(message)) {
|
||||
outlookLogService.addLog(outlook.getGithubId(), "无法刷新令牌!code:3", 0, message);
|
||||
return false;
|
||||
}
|
||||
logger.info("令牌过期!");
|
||||
/*刷新令牌*/
|
||||
String token = refresh_token(outlook);
|
||||
if (token == null) {
|
||||
return false;
|
||||
}
|
||||
/*再次获取邮件列表*/
|
||||
s = MailList(token);
|
||||
json = JSON.parseObject(s);
|
||||
if (json.containsKey("error")) {
|
||||
outlookLogService.addLog(outlook.getGithubId(), "无法刷新令牌!code:2", 1, json.getJSONObject("error").getString("message"));
|
||||
return false;
|
||||
}
|
||||
public int getMailList(Outlook outlook) throws Exception {
|
||||
String s = MailList(outlook.getAccessToken());
|
||||
JSONObject json = JSON.parseObject(s);
|
||||
/*错误情况,一般是令牌过期*/
|
||||
if (json.containsKey("error")) {
|
||||
String code = json.getJSONObject("error").getString("code");
|
||||
String message = json.getJSONObject("error").getString("message");
|
||||
/*如果出现的错误是没有message中收集的,那么就认为是无法刷新的情况。比如 用户取消了授权、删除了key*/
|
||||
if (!errorCheck(message)) {
|
||||
throw new Exception("无法刷新令牌!code:3" + message);
|
||||
}
|
||||
logger.info("令牌过期!");
|
||||
/*刷新令牌*/
|
||||
String token = refresh_token(outlook);
|
||||
if (token == null) {
|
||||
throw new Exception("刷新令牌失败! refresh_token 为空!");
|
||||
}
|
||||
/*再次获取邮件列表*/
|
||||
s = MailList(token);
|
||||
json = JSON.parseObject(s);
|
||||
if (json.containsKey("error")) {
|
||||
throw new Exception("无法刷新令牌!code:2,错误消息: "+ json.getJSONObject("error").getString("message"));
|
||||
}
|
||||
logger.info("邮件列表请求成功!" + s);
|
||||
int mail_count = getMailBody(5, s, outlook.getAccessToken());
|
||||
logger.info("读取邮件数量: {}" , mail_count);
|
||||
outlookLogService.addLog(outlook.getGithubId(), "ok", 1, "读取邮件数量:" + mail_count);
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
logger.info("邮件列表请求成功!" + s);
|
||||
int mail_count = getMailBody(5, s, outlook.getAccessToken());
|
||||
logger.info("读取邮件数量: {}", mail_count);
|
||||
|
||||
return mail_count;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -233,7 +223,7 @@ public class OutlookServiceImpl extends ServiceImpl<OutlookMapper, Outlook> impl
|
||||
|
||||
|
||||
// 刷新令牌,同时更新数据库中的令牌
|
||||
public String refresh_token(Outlook outlook) {
|
||||
public String refresh_token(Outlook outlook) throws Exception {
|
||||
Map<String, Object> head = new HashMap<>();
|
||||
head.put("Content-Type", "application/x-www-form-urlencoded");
|
||||
Map<String, Object> par = new HashMap<>();
|
||||
@ -243,33 +233,24 @@ public class OutlookServiceImpl extends ServiceImpl<OutlookMapper, Outlook> impl
|
||||
par.put("grant_type", "refresh_token");
|
||||
par.put("refresh_token", outlook.getRefreshToken());
|
||||
String s = null;
|
||||
try {
|
||||
s = OkHttpClientUtil.doPost("https://login.microsoftonline.com/common/oauth2/v2.0/token", head, par);
|
||||
logger.info("请求刷新列表返回数据:" + s);
|
||||
JSONObject jsonObject = JSON.parseObject(s);
|
||||
if (!jsonObject.containsKey("access_token")) {
|
||||
logger.info("返回的access_token字段不存在");
|
||||
outlookLogService.addLog(outlook.getGithubId(), "无法刷新令牌! 需要重新授权!", 0, s);
|
||||
// 字段不存在
|
||||
return null;
|
||||
}
|
||||
outlook.setRefreshToken(jsonObject.getString("refresh_token"));
|
||||
outlook.setAccessToken(jsonObject.getString("access_token"));
|
||||
outlook.setIdToken(jsonObject.getString("id_token"));
|
||||
QueryWrapper<Outlook> queryWrapper = new QueryWrapper<>();
|
||||
queryWrapper.eq("client_id", outlook.getClientId());
|
||||
if (baseMapper.update(outlook, queryWrapper) != 1) {
|
||||
logger.info("返更新行数不为1");
|
||||
outlookLogService.addLog(outlook.getGithubId(), "更新数据库时发现有重复的key", 0, "");
|
||||
return null;
|
||||
}
|
||||
return outlook.getAccessToken();
|
||||
// 更新数据库
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
outlookLogService.addLog(outlook.getGithubId(), e.getMessage(), 0, e.getMessage());
|
||||
return null;
|
||||
s = OkHttpClientUtil.doPost("https://login.microsoftonline.com/common/oauth2/v2.0/token", head, par);
|
||||
logger.info("请求刷新列表返回数据:" + s);
|
||||
JSONObject jsonObject = JSON.parseObject(s);
|
||||
if (!jsonObject.containsKey("access_token")) {
|
||||
logger.info("返回的access_token字段不存在");
|
||||
throw new Exception("返回的access_token字段不存在,无法刷新令牌! 需要重新授权!");
|
||||
}
|
||||
outlook.setRefreshToken(jsonObject.getString("refresh_token"));
|
||||
outlook.setAccessToken(jsonObject.getString("access_token"));
|
||||
outlook.setIdToken(jsonObject.getString("id_token"));
|
||||
QueryWrapper<Outlook> queryWrapper = new QueryWrapper<>();
|
||||
queryWrapper.eq("client_id", outlook.getClientId());
|
||||
if (baseMapper.update(outlook, queryWrapper) != 1) {
|
||||
logger.info("返更新行数不为1");
|
||||
throw new Exception("更新数据库时发现有重复的key");
|
||||
}
|
||||
return outlook.getAccessToken();
|
||||
// 更新数据库
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -73,7 +73,7 @@ public class OutlookLogController {
|
||||
}
|
||||
|
||||
@GetMapping("/exec111111")
|
||||
public void s(){
|
||||
public void s() throws Exception {
|
||||
List<Outlook> list = outlookService.findAll();
|
||||
logger.info(String.valueOf(list.size()));
|
||||
for (Outlook outlook :list) {
|
||||
|
@ -36,7 +36,6 @@ public class ListenerImpl {
|
||||
System.out.println("消费者1开始处理消息:"+System.currentTimeMillis());
|
||||
int github_id = Integer.valueOf(new String(message.getBody()));
|
||||
boolean b = Task.executeE5(github_id);
|
||||
|
||||
channel.basicAck(message.getMessageProperties().getDeliveryTag(), true);
|
||||
/*再次进行添加任务*/
|
||||
if (b) {
|
||||
|
@ -82,11 +82,18 @@ public class TaskImpl implements ITask {
|
||||
logger.warn("未找到此用户,github_id: {}", github_id);
|
||||
return false;
|
||||
}
|
||||
boolean mailList = outlookService.getMailList(Outlook);
|
||||
if (!mailList) {
|
||||
boolean isExecuteE5 ;
|
||||
try {
|
||||
int mail_count = outlookService.getMailList(Outlook);
|
||||
outlookLogService.addLog(github_id, "ok", 1, "读取邮件数量:" + mail_count);
|
||||
isExecuteE5 = true;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
outlookLogService.addLog(github_id, "error", 0, e.getMessage());
|
||||
outlookLogService.addLog(github_id, "error", 0, "检测到错误,下次将不再自动调用,请修正错误后再授权开启续订。" );
|
||||
isExecuteE5 = false;
|
||||
}
|
||||
return mailList;
|
||||
return isExecuteE5;
|
||||
}
|
||||
|
||||
/**
|
||||
|
33
src/main/resources/application-online.properties
Normal file
33
src/main/resources/application-online.properties
Normal file
@ -0,0 +1,33 @@
|
||||
server.port= 8083
|
||||
#通用数据源配置
|
||||
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
|
||||
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/e5?charset=utf8mb4&useSSL=false
|
||||
spring.datasource.username=root
|
||||
spring.datasource.password=123456
|
||||
|
||||
# RabbitMq
|
||||
spring.rabbitmq.host=127.0.0.1
|
||||
spring.rabbitmq.port=5672
|
||||
spring.rabbitmq.username=e5
|
||||
spring.rabbitmq.password=passwd
|
||||
spring.rabbitmq.virtual-host=e5
|
||||
spring.rabbitmq.connection-timeout=15000
|
||||
#消费者最小数量
|
||||
spring.rabbitmq.listener.simple.concurrency=1
|
||||
#消费者最大数量
|
||||
spring.rabbitmq.listener.simple.max-concurrency=10
|
||||
#消费者每次从队列获取的消息数量。写多了,如果长时间得不到消费,数据就一直得不到处理
|
||||
spring.rabbitmq.listener.simple.prefetch=20
|
||||
#设置消费端手动 ack
|
||||
spring.rabbitmq.listener.simple.acknowledge-mode=manual
|
||||
# 限流
|
||||
#spring.rabbitmq.listener.simple.prefetch=1
|
||||
#开启 confirm 确认机制
|
||||
#spring.rabbitmq.publisher-confirms=true
|
||||
#开启 return 确认机制
|
||||
spring.rabbitmq.publisher-returns=true
|
||||
#设置为 true 后 消费者在消息没有被路由到合适队列情况下会被return监听,而不会自动删除
|
||||
spring.rabbitmq.template.mandatory=true
|
||||
|
||||
github.client_id=xxxxxxxxxxxxxxxx
|
||||
github.client_secret=xxxxxxxxxxxxxxxxxxx
|
Loading…
Reference in New Issue
Block a user