XcxWarningCrontab.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. namespace App\Crontab;
  3. use EasySwoole\Crontab\JobInterface;
  4. use App\Com\SmtpSend;
  5. use EasySwoole\EasySwoole\Config;
  6. use App\Models\WarningBody;
  7. use EasySwoole\Mysqli\QueryBuilder;
  8. use EasySwoole\ORM\DbManager;
  9. class XcxWarningCrontab implements JobInterface
  10. {
  11. public function jobName(): string
  12. {
  13. // 定时任务的名称
  14. return 'XcxWarningCrontab';
  15. }
  16. public function crontabRule(): string
  17. {
  18. // 定义执行规则 根据 Crontab 来定义
  19. // 这里是每分钟执行 1 次
  20. return '*/1 * * * *';
  21. }
  22. public function run()
  23. {
  24. //定时任务的执行逻辑
  25. $xcx_warn_set = Config::getInstance()->getConf('XCX_WARNING');
  26. //获取需要发送邮件的警告日志
  27. $this->processWarning();
  28. //发送邮件
  29. //$smtp = new SmtpSend();
  30. //$smtp->send($xcx_warn_set['accept_email'],'福利商城小程序关键节点告警信息','这是一份测试邮件,请勿回复');
  31. }
  32. //获取警告信息发送并处理发送结果
  33. protected function processWarning()
  34. {
  35. $where = ' xwb.`send_status` <> 2 and xwb.`create_time`>'.(time()-3600); //1个小时内未发送和发送失败的消息
  36. $que_sql = "SELECT xwb.`id`,xwb.`member_id`,xwb.`mobile`,xwb.`create_time`,xwb.`type`,xwb.`ip`,xwb.`url`,xwb.`param`,xwt.`desc` as type_desc
  37. FROM ims_superdesk_shop_xcx_warning_body xwb
  38. LEFT JOIN ims_superdesk_shop_xcx_warning_type AS xwt ON xwb.`type` = xwt.`id`
  39. WHERE {$where};
  40. ";
  41. $queryBuild = new QueryBuilder();
  42. $queryBuild->raw($que_sql);
  43. $res_data = DbManager::getInstance()->query($queryBuild, true, 'default')->toArray();
  44. $formatData = $this->formatMessage($res_data['result']);
  45. //发送邮件同时更新日志状态
  46. $this->send_mail($formatData);
  47. }
  48. /**
  49. * 获取需要发送的日志记录,每10条记录合成一份邮件内容
  50. * @param $warn_msg 未发送的警告记录
  51. * @return Array [content]邮件内容 [warn_id]数组,包含邮件里对应日志记录的ID
  52. *
  53. */
  54. protected function formatMessage($warn_msg){
  55. $msg_count = (int)(count($warn_msg)/10);
  56. if(count($warn_msg)%10>0){$msg_count++;}
  57. $formatData = Array();
  58. for($i=0;$i<$msg_count;$i++){
  59. $start_index = $i*10;
  60. $end_index = $i*10+9;
  61. $tmp_content = '';
  62. $tmp_warn_id = Array();
  63. for($j=$start_index;$j<=$end_index;$j++){
  64. if(!empty($warn_msg[$j])){
  65. $tmp_content .= "<br>用户ID:{$warn_msg[$j]['member_id']}<br>
  66. 手机号:{$warn_msg[$j]['mobile']}<br>
  67. 时间:" . date("Y-m-d H:i:s",$warn_msg[$j]['create_time']) . "<br>
  68. 内容:{$warn_msg[$j]['type_desc']}<br>
  69. IP:{$warn_msg[$j]['ip']}<br>
  70. URL:{$warn_msg[$j]['url']}<br>
  71. 参数:{$warn_msg[$j]['param']}<br><br>";
  72. $tmp_warn_id[] = $warn_msg[$j]['id'];
  73. }
  74. }
  75. $formatData[] = ['content'=>$tmp_content,'warn_id'=>$tmp_warn_id];
  76. }
  77. return $formatData;
  78. }
  79. //发送邮件更新日志状态
  80. protected function send_mail($formatData){
  81. //定时任务的执行逻辑
  82. $xcx_warn_set = Config::getInstance()->getConf('XCX_WARNING');
  83. $smtp = new SmtpSend();
  84. foreach($formatData as $value){
  85. $is_send = $smtp->send($xcx_warn_set['accept_email'],'福利商城小程序关键节点告警信息',$value['content']);
  86. //批量更新状态
  87. $status = $is_send?2:3;
  88. $res = WarningBody::create()->update([
  89. 'send_status' => $status
  90. ], $value['warn_id']);
  91. }
  92. }
  93. public function onException(\Throwable $throwable)
  94. {
  95. // 捕获 run 方法内所抛出的异常
  96. }
  97. }