This commit is contained in:
LuoYe_MyWork
2020-09-04 17:17:40 +08:00
parent 3a9d8ba204
commit 10eb60b14f
6 changed files with 97 additions and 77 deletions

View File

@@ -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();

View File

@@ -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,6 +130,7 @@ public class OutlookServiceImpl extends ServiceImpl<OutlookMapper, Outlook> impl
/**
* 删除用户outlook
*
* @Description:
* @param: * @param github_id
* @return: int
@@ -148,42 +145,35 @@ public class OutlookServiceImpl extends ServiceImpl<OutlookMapper, Outlook> impl
}
@Override
public boolean getMailList(Outlook outlook) {
try {
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*/
/*如果出现错误是没有message中收集的那么就认为是无法刷新的情况。比如 用户取消了授权、删除了key*/
if (!errorCheck(message)) {
outlookLogService.addLog(outlook.getGithubId(), "无法刷新令牌!code:3", 0, message);
return false;
throw new Exception("无法刷新令牌!code:3" + message);
}
logger.info("令牌过期!");
/*刷新令牌*/
String token = refresh_token(outlook);
if (token == null) {
return false;
throw new Exception("刷新令牌失败! refresh_token 为空!");
}
/*再次获取邮件列表*/
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;
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;
}
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,15 +233,12 @@ 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;
throw new Exception("返回的access_token字段不存在,无法刷新令牌! 需要重新授权!");
}
outlook.setRefreshToken(jsonObject.getString("refresh_token"));
outlook.setAccessToken(jsonObject.getString("access_token"));
@@ -260,16 +247,10 @@ public class OutlookServiceImpl extends ServiceImpl<OutlookMapper, Outlook> impl
queryWrapper.eq("client_id", outlook.getClientId());
if (baseMapper.update(outlook, queryWrapper) != 1) {
logger.info("返更新行数不为1");
outlookLogService.addLog(outlook.getGithubId(), "更新数据库时发现有重复的key", 0, "");
return null;
throw new Exception("更新数据库时发现有重复的key");
}
return outlook.getAccessToken();
// 更新数据库
} catch (Exception e) {
e.printStackTrace();
outlookLogService.addLog(outlook.getGithubId(), e.getMessage(), 0, e.getMessage());
return null;
}
}
/**

View File

@@ -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) {

View File

@@ -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) {

View File

@@ -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;
}
/**

View File

@@ -0,0 +1,33 @@
server.port= 8083
<><CDA8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ<EFBFBD><D4B4><EFBFBD><EFBFBD>
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
#<23><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>С<EFBFBD><D0A1><EFBFBD><EFBFBD>
spring.rabbitmq.listener.simple.concurrency=1
#<23><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
spring.rabbitmq.listener.simple.max-concurrency=10
#<23><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ÿ<EFBFBD>δӶ<CEB4><D3B6>л<EFBFBD>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><D0B4><EFBFBD>ˣ<EFBFBD><CBA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD>ò<EFBFBD><C3B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѣ<EFBFBD><D1A3><EFBFBD><EFBFBD>ݾ<EFBFBD>һֱ<D2BB>ò<EFBFBD><C3B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
spring.rabbitmq.listener.simple.prefetch=20
#<23><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ѷ<EFBFBD><D1B6>ֶ<EFBFBD> ack
spring.rabbitmq.listener.simple.acknowledge-mode=manual
# <20><><EFBFBD><EFBFBD>
#spring.rabbitmq.listener.simple.prefetch=1
#<23><><EFBFBD><EFBFBD> confirm ȷ<>ϻ<EFBFBD><CFBB><EFBFBD>
#spring.rabbitmq.publisher-confirms=true
#<23><><EFBFBD><EFBFBD> return ȷ<>ϻ<EFBFBD><CFBB><EFBFBD>
spring.rabbitmq.publisher-returns=true
#<23><><EFBFBD><EFBFBD>Ϊ true <20><> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣû<CFA2>б<EFBFBD>·<EFBFBD>ɵ<EFBFBD><C9B5><EFBFBD><EFBFBD>ʶ<EFBFBD><CAB6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>»ᱻreturn<72><6E><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD>ɾ<EFBFBD><C9BE>
spring.rabbitmq.template.mandatory=true
github.client_id=xxxxxxxxxxxxxxxx
github.client_secret=xxxxxxxxxxxxxxxxxxx