This commit is contained in:
LuoYe_MyWork 2020-09-04 17:17:40 +08:00
parent 3a9d8ba204
commit b934572be4
5 changed files with 64 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 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(); 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.entity.Outlook;
import io.qyi.e5.outlook.mapper.OutlookMapper; import io.qyi.e5.outlook.mapper.OutlookMapper;
import io.qyi.e5.outlook.service.IOutlookService; 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.OkHttpClientUtil;
import io.qyi.e5.util.netRequest.OkHttpRequestUtils; import io.qyi.e5.util.netRequest.OkHttpRequestUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -37,8 +35,6 @@ import java.util.Map;
public class OutlookServiceImpl extends ServiceImpl<OutlookMapper, Outlook> implements IOutlookService { public class OutlookServiceImpl extends ServiceImpl<OutlookMapper, Outlook> implements IOutlookService {
private final Logger logger = LoggerFactory.getLogger(this.getClass()); private final Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
IOutlookLogService outlookLogService;
@Value("${outlook.errorMsg}") @Value("${outlook.errorMsg}")
private String[] errorMsg; private String[] errorMsg;
@ -134,12 +130,13 @@ public class OutlookServiceImpl extends ServiceImpl<OutlookMapper, Outlook> impl
/** /**
* 删除用户outlook * 删除用户outlook
* @Description: *
* @param: * @param github_id * @Description:
* @return: int * @param: * @param github_id
* @Author: 落叶随风 * @return: int
* @Date: 2020/4/17 * @Author: 落叶随风
*/ * @Date: 2020/4/17
*/
@Override @Override
public int deleteInfo(int github_id) { public int deleteInfo(int github_id) {
QueryWrapper<Outlook> outlookQueryWrapper = new QueryWrapper<>(); QueryWrapper<Outlook> outlookQueryWrapper = new QueryWrapper<>();
@ -148,42 +145,35 @@ public class OutlookServiceImpl extends ServiceImpl<OutlookMapper, Outlook> impl
} }
@Override @Override
public boolean getMailList(Outlook outlook) { public int getMailList(Outlook outlook) throws Exception {
try { String s = MailList(outlook.getAccessToken());
String s = MailList(outlook.getAccessToken()); JSONObject json = JSON.parseObject(s);
JSONObject json = JSON.parseObject(s); /*错误情况,一般是令牌过期*/
/*错误情况,一般是令牌过期*/ if (json.containsKey("error")) {
if (json.containsKey("error")) { String code = json.getJSONObject("error").getString("code");
String code = json.getJSONObject("error").getString("code"); String message = json.getJSONObject("error").getString("message");
String message = json.getJSONObject("error").getString("message"); /*如果出现的错误是没有message中收集的那么就认为是无法刷新的情况。比如 用户取消了授权、删除了key*/
/*如果出现得错误是没有message中收集的那么就认为是无法刷新的情况。比如 用户取消了授权、删除了key*/ if (!errorCheck(message)) {
if (!errorCheck(message)) { throw new Exception("无法刷新令牌!code:3" + message);
outlookLogService.addLog(outlook.getGithubId(), "无法刷新令牌!code:3", 0, message); }
return false; logger.info("令牌过期!");
} /*刷新令牌*/
logger.info("令牌过期!"); String token = refresh_token(outlook);
/*刷新令牌*/ if (token == null) {
String token = refresh_token(outlook); throw new Exception("刷新令牌失败! refresh_token 为空!");
if (token == null) { }
return false; /*再次获取邮件列表*/
} s = MailList(token);
/*再次获取邮件列表*/ json = JSON.parseObject(s);
s = MailList(token); if (json.containsKey("error")) {
json = JSON.parseObject(s); throw new Exception("无法刷新令牌!code:2,错误消息: "+ json.getJSONObject("error").getString("message"));
if (json.containsKey("error")) {
outlookLogService.addLog(outlook.getGithubId(), "无法刷新令牌!code:2", 1, json.getJSONObject("error").getString("message"));
return false;
}
} }
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<>(); Map<String, Object> head = new HashMap<>();
head.put("Content-Type", "application/x-www-form-urlencoded"); head.put("Content-Type", "application/x-www-form-urlencoded");
Map<String, Object> par = new HashMap<>(); 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("grant_type", "refresh_token");
par.put("refresh_token", outlook.getRefreshToken()); par.put("refresh_token", outlook.getRefreshToken());
String s = null; String s = null;
try { s = OkHttpClientUtil.doPost("https://login.microsoftonline.com/common/oauth2/v2.0/token", head, par);
s = OkHttpClientUtil.doPost("https://login.microsoftonline.com/common/oauth2/v2.0/token", head, par); logger.info("请求刷新列表返回数据:" + s);
logger.info("请求刷新列表返回数据:" + s); JSONObject jsonObject = JSON.parseObject(s);
JSONObject jsonObject = JSON.parseObject(s); if (!jsonObject.containsKey("access_token")) {
if (!jsonObject.containsKey("access_token")) { logger.info("返回的access_token字段不存在");
logger.info("返回的access_token字段不存在"); throw new Exception("返回的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;
} }
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();
// 更新数据库
} }
/** /**

View File

@ -73,7 +73,7 @@ public class OutlookLogController {
} }
@GetMapping("/exec111111") @GetMapping("/exec111111")
public void s(){ public void s() throws Exception {
List<Outlook> list = outlookService.findAll(); List<Outlook> list = outlookService.findAll();
logger.info(String.valueOf(list.size())); logger.info(String.valueOf(list.size()));
for (Outlook outlook :list) { for (Outlook outlook :list) {

View File

@ -36,7 +36,6 @@ public class ListenerImpl {
System.out.println("消费者1开始处理消息"+System.currentTimeMillis()); System.out.println("消费者1开始处理消息"+System.currentTimeMillis());
int github_id = Integer.valueOf(new String(message.getBody())); int github_id = Integer.valueOf(new String(message.getBody()));
boolean b = Task.executeE5(github_id); boolean b = Task.executeE5(github_id);
channel.basicAck(message.getMessageProperties().getDeliveryTag(), true); channel.basicAck(message.getMessageProperties().getDeliveryTag(), true);
/*再次进行添加任务*/ /*再次进行添加任务*/
if (b) { if (b) {

View File

@ -82,11 +82,18 @@ public class TaskImpl implements ITask {
logger.warn("未找到此用户,github_id: {}", github_id); logger.warn("未找到此用户,github_id: {}", github_id);
return false; return false;
} }
boolean mailList = outlookService.getMailList(Outlook); boolean isExecuteE5 ;
if (!mailList) { 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, "检测到错误,下次将不再自动调用,请修正错误后再授权开启续订。" ); outlookLogService.addLog(github_id, "error", 0, "检测到错误,下次将不再自动调用,请修正错误后再授权开启续订。" );
isExecuteE5 = false;
} }
return mailList; return isExecuteE5;
} }
/** /**