diff --git a/app/Models/ProjectTask.php b/app/Models/ProjectTask.php index 6f4053c42..891ea1171 100644 --- a/app/Models/ProjectTask.php +++ b/app/Models/ProjectTask.php @@ -1264,55 +1264,4 @@ class ProjectTask extends AbstractModel // return $task; } - - /** - * 预超期任务提醒 - * @param $task - */ - public static function overdueRemindEmail($task) - { - $ownerIds = ProjectTaskUser::whereTaskId($task['id'])->whereOwner(1)->pluck('userid')->toArray(); - $users = User::whereIn('userid', $ownerIds)->get(); - if (!$users) { - throw new ApiException("ProjectTask::overdueRemindEmail--没有负责人"); - } - $type = $task['end_at'] < Carbon::now() ? 2 : 1; - $setting = Base::setting('emailSetting'); - $hours = floatval($setting['task_remind_hours']); - $hours2 = floatval($setting['task_remind_hours2']); - $time = $type === 1 ? $hours : $hours2; - UserEmailVerification::initMailConfig(); - foreach ($users as $user) { - /** @var User $user */ - if (ProjectTaskMailLog::whereTaskId($task['id'])->whereUserid($user->userid)->whereType($type)->whereIsSend(1)->exists()) { - return; - } - $email = $user->email; - $isSend = 1; - try { - $emailContent = [ - 'name' => $task['name'], - 'time' => $time, - 'type' => $type - ]; - Mail::send('taskOverdueRemind', $emailContent, function ($m) use ($email) { - $m->from(Config::get("mail.mailers.smtp.username"), env('APP_NAME')); - $m->to($email); - $m->subject("任务提醒"); - }); - } catch (\Exception $e) { - $isSend = 0; - \Log::error($email . '--邮箱发送报错:', [$e->getMessage()]); - } - $logData = [ - 'userid' => $user->userid, - 'task_id' => $task['id'], - 'email' => $email, - 'type' => $type, - 'is_send' => $isSend, - ]; - $emailLog = ProjectTaskMailLog::whereTaskId($task['id'])->whereUserid($user->userid)->whereType($type)->first(); - ProjectTaskMailLog::createInstance($logData, $emailLog->id ?? null)->save(); - } - } } diff --git a/app/Models/ProjectTaskMailLog.php b/app/Models/ProjectTaskMailLog.php index 3d852dcc2..2b5cbb78e 100644 --- a/app/Models/ProjectTaskMailLog.php +++ b/app/Models/ProjectTaskMailLog.php @@ -16,6 +16,7 @@ use Illuminate\Database\Eloquent\SoftDeletes; * @property string|null $email 电子邮箱 * @property int|null $type 提醒类型:1第一次任务提醒,2第二次任务超期提醒 * @property int|null $is_send 邮件发送是否成功:0否,1是 + * @property string|null $send_error 邮件发送错误详情 * @property \Illuminate\Support\Carbon|null $created_at * @property \Illuminate\Support\Carbon|null $updated_at * @property \Illuminate\Support\Carbon|null $deleted_at @@ -28,6 +29,7 @@ use Illuminate\Database\Eloquent\SoftDeletes; * @method static \Illuminate\Database\Eloquent\Builder|ProjectTaskMailLog whereEmail($value) * @method static \Illuminate\Database\Eloquent\Builder|ProjectTaskMailLog whereId($value) * @method static \Illuminate\Database\Eloquent\Builder|ProjectTaskMailLog whereIsSend($value) + * @method static \Illuminate\Database\Eloquent\Builder|ProjectTaskMailLog whereSendError($value) * @method static \Illuminate\Database\Eloquent\Builder|ProjectTaskMailLog whereTaskId($value) * @method static \Illuminate\Database\Eloquent\Builder|ProjectTaskMailLog whereType($value) * @method static \Illuminate\Database\Eloquent\Builder|ProjectTaskMailLog whereUpdatedAt($value) diff --git a/app/Tasks/OverdueRemindEmailTask.php b/app/Tasks/OverdueRemindEmailTask.php index 8cdfb8e39..3363cfe4a 100644 --- a/app/Tasks/OverdueRemindEmailTask.php +++ b/app/Tasks/OverdueRemindEmailTask.php @@ -1,11 +1,14 @@ 0) { - $taskLists1 = ProjectTask::whereNull('complete_at') - ->where('end_at', '>=', Carbon::now()->addMinutes($hours * 60 - 3)->rawFormat('Y-m-d H:i:s')) - ->where('end_at', '<=', Carbon::now()->addMinutes($hours * 60 + 3)->rawFormat('Y-m-d H:i:s')) + ProjectTask::whereNull('complete_at') ->whereNull('archived_at') - ->take(100) - ->get() - ->toArray(); + ->whereBetween("end_at", [ + Carbon::now()->addMinutes($hours * 60 - 3), + Carbon::now()->addMinutes($hours * 60 + 3) + ])->chunkById(100, function ($tasks) { + /** @var ProjectTask $task */ + foreach ($tasks as $task) { + $this->overdueBeforeAfterEmail($task, true); + } + }); } if ($hours2 > 0) { - $taskLists2 = ProjectTask::whereNull('complete_at') - ->where('end_at', '>=', Carbon::now()->subMinutes($hours2 * 60 + 3)->rawFormat('Y-m-d H:i:s')) - ->where('end_at', '<=', Carbon::now()->subMinutes($hours2 * 60 - 3)->rawFormat('Y-m-d H:i:s')) + ProjectTask::whereNull('complete_at') ->whereNull('archived_at') - ->take(100) - ->get() - ->toArray(); - } - $taskLists = array_merge($taskLists1, $taskLists2); - $ids = []; - foreach ($taskLists as $task) { - if (!in_array($task->id, $ids)) { - $ids[] = $task->id; - ProjectTask::overdueRemindEmail($task); - } + ->whereBetween("end_at", [ + Carbon::now()->addMinutes($hours2 * 60 + 3), + Carbon::now()->addMinutes($hours2 * 60 - 3) + ])->chunkById(100, function ($tasks) { + /** @var ProjectTask $task */ + foreach ($tasks as $task) { + $this->overdueBeforeAfterEmail($task, false); + } + }); } } } + /** + * 过期前、超期后提醒 + * @param ProjectTask $task + * @param $isBefore + * @return void + */ + private function overdueBeforeAfterEmail(ProjectTask $task, $isBefore) + { + $userids = $task->taskUser->where('owner', 1)->pluck('userid')->toArray(); + if (empty($userids)) { + return; + } + $users = User::whereIn('userid', $userids)->get(); + if (empty($users)) { + return; + } + + $setting = Base::setting('emailSetting'); + $hours = floatval($setting['task_remind_hours']); + $hours2 = floatval($setting['task_remind_hours2']); + if ($isBefore) { + $subject = "任务提醒"; + $content = "

用户您好, " . env('APP_NAME') . " 任务到期提醒。

您有一个任务【{{$task->name}}】还有{{$hours}}小时即将超时,请及时处理

"; + } else { + $subject = "任务过期提醒"; + $content = "

用户您好, " . env('APP_NAME') . " 任务到期提醒。

您的任务【{{$task->name}}】已经超时{{$hours2}}小时,请及时处理

"; + } + + /** @var User $user */ + foreach ($users as $user) { + $data = [ + 'type' => $isBefore ? 1 : 2, + 'userid' => $user->userid, + 'task_id' => $task->id, + ]; + $emailLog = ProjectTaskMailLog::where($data)->first(); + if ($emailLog) { + continue; + } + try { + if (!Base::isEmail($user->email)) { + throw new \Exception("User email '{$user->email}' address error"); + } + Factory::mailer() + ->setDsn("smtp://{$setting['account']}:{$setting['password']}@{$setting['smtp_server']}:{$setting['port']}?verify_peer=0") + ->setMessage(EmailMessage::create() + ->from(env('APP_NAME', 'Task') . " <{$setting['account']}>") + ->to($user->email) + ->subject($subject) + ->html($content)) + ->send(); + $data['is_send'] = 1; + } catch (\Exception $e) { + $data['send_error'] = $e->getMessage(); + } + $data['email'] = $user->email; + ProjectTaskMailLog::createInstance($data)->save(); + } + } } diff --git a/database/migrations/2022_04_07_082526_add_project_task_mail_logs_send_error.php b/database/migrations/2022_04_07_082526_add_project_task_mail_logs_send_error.php new file mode 100644 index 000000000..730f507ac --- /dev/null +++ b/database/migrations/2022_04_07_082526_add_project_task_mail_logs_send_error.php @@ -0,0 +1,34 @@ +text('send_error')->nullable()->after('is_send')->comment('邮件发送错误详情'); + } + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('project_task_mail_logs', function (Blueprint $table) { + $table->dropColumn("send_error"); + }); + } +} diff --git a/resources/views/taskOverdueRemind.blade.php b/resources/views/taskOverdueRemind.blade.php deleted file mode 100644 index 22bcc01b3..000000000 --- a/resources/views/taskOverdueRemind.blade.php +++ /dev/null @@ -1,19 +0,0 @@ - - - - - - - -

用户您好, {{env('APP_NAME') }} 任务到期提醒。

-@if ($type == 1) -

- 您有一个任务【{{$name}}】还有{{$time}}小时即将超时,请及时处理 -

-@else -

- 您的任务【{{$name}}】已经超时{{$time}}小时,请及时处理 -

-@endif - -