From 601d0372016e47be6f94651ed2e8b33c2ffdc6da Mon Sep 17 00:00:00 2001 From: kuaifan Date: Thu, 7 Apr 2022 08:29:49 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BC=98=E5=8C=96=E4=BB=BB=E5=8A=A1?= =?UTF-8?q?=E5=88=B0=E6=9C=9F=E5=89=8D=E5=90=8E=E9=82=AE=E4=BB=B6=E6=8F=90?= =?UTF-8?q?=E9=86=92?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Models/ProjectTask.php | 51 --------- app/Models/ProjectTaskMailLog.php | 2 + app/Tasks/OverdueRemindEmailTask.php | 104 ++++++++++++++---- ..._add_project_task_mail_logs_send_error.php | 34 ++++++ resources/views/taskOverdueRemind.blade.php | 19 ---- 5 files changed, 118 insertions(+), 92 deletions(-) create mode 100644 database/migrations/2022_04_07_082526_add_project_task_mail_logs_send_error.php delete mode 100644 resources/views/taskOverdueRemind.blade.php 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 @@ public static function userTask($task_id, $archived = true, $trashed = true, $pe // 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 @@ * @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 @@ * @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(); + ->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); + } + }); } - $taskLists = array_merge($taskLists1, $taskLists2); - $ids = []; - foreach ($taskLists as $task) { - if (!in_array($task->id, $ids)) { - $ids[] = $task->id; - ProjectTask::overdueRemindEmail($task); + } + } + + /** + * 过期前、超期后提醒 + * @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 - -