feat: 任务到期提醒开启邮件通知

This commit is contained in:
韦荣超 2022-03-04 09:41:36 +08:00
parent 4785cae8f0
commit 35a369a953
7 changed files with 125 additions and 31 deletions

View File

@ -5,6 +5,7 @@ namespace App\Http\Controllers;
use App\Module\Base;
use App\Tasks\AutoArchivedTask;
use App\Tasks\DeleteTmpTask;
use App\Tasks\OverdueRemindEmailTask;
use Hhxsv5\LaravelS\Swoole\Task\Task;
use Redirect;
@ -61,6 +62,8 @@ class IndexController extends InvokeController
Task::deliver(new DeleteTmpTask('tmp', 24));
// 自动归档任务
Task::deliver(new AutoArchivedTask());
// 任务到期邮件提醒
Task::deliver(new OverdueRemindEmailTask());
return "success";
}

View File

@ -1213,30 +1213,55 @@ class ProjectTask extends AbstractModel
/**
* 预超期任务提醒
* @param $ownerIds
* @param $task
*/
public static function overdueRemindEmail($ownerIds)
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--没有负责人");
}
Config::set("mail.mailers.smtp.host", Base::settingFind('emailSetting', 'smtp_server'));
Config::set("mail.mailers.smtp.port", Base::settingFind('emailSetting', 'port'));
Config::set("mail.mailers.smtp.username", Base::settingFind('emailSetting', 'account'));
Config::set("mail.mailers.smtp.password", Base::settingFind('emailSetting', 'password'));
$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;
Config::set("mail.mailers.smtp.host", Base::settingFind('emailSetting', 'smtp_server') ?: Config::get("mail.mailers.smtp.host"));
Config::set("mail.mailers.smtp.port", Base::settingFind('emailSetting', 'port') ?: Config::get("mail.mailers.smtp.port"));
Config::set("mail.mailers.smtp.username", Base::settingFind('emailSetting', 'account') ?: Config::get("mail.mailers.smtp.username"));
Config::set("mail.mailers.smtp.password", Base::settingFind('emailSetting', 'password') ?: Config::get("mail.mailers.smtp.password"));
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 {
Mail::send('taskOverdueRemind', ['url' => 'https://www.baidu.com'], function ($m) use ($email) {
$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("过期任务提醒");
$m->subject("任务提醒");
});
} catch (\Exception $e) {
\Log::error($email.'--邮箱发动报错:', [$e->getMessage()]);
$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();
}
}
}

View File

@ -0,0 +1,36 @@
<?php
namespace App\Models;
/**
* App\Models\ProjectTaskMailLog
*
* @property int $id
* @property int|null $userid 用户id
* @property int|null $task_id 任务id
* @property string|null $email 电子邮箱
* @property int|null $type 提醒类型1第一次任务提醒2第二次任务超期提醒
* @property int|null $is_send 邮件发送是否成功0否1是
* @property \Illuminate\Support\Carbon|null $created_at
* @property \Illuminate\Support\Carbon|null $updated_at
* @method static \Illuminate\Database\Eloquent\Builder|ProjectTaskMailLog newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder|ProjectTaskMailLog newQuery()
* @method static \Illuminate\Database\Eloquent\Builder|ProjectTaskMailLog query()
* @method static \Illuminate\Database\Eloquent\Builder|ProjectTaskMailLog whereCreatedAt($value)
* @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 whereTaskId($value)
* @method static \Illuminate\Database\Eloquent\Builder|ProjectTaskMailLog whereType($value)
* @method static \Illuminate\Database\Eloquent\Builder|ProjectTaskMailLog whereUpdatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|ProjectTaskMailLog whereUserid($value)
* @mixin \Eloquent
*/
class ProjectTaskMailLog extends AbstractModel
{
}

View File

@ -5,7 +5,6 @@ namespace App\Tasks;
use App\Models\ProjectTask;
use App\Models\ProjectTaskUser;
use App\Module\Base;
use Carbon\Carbon;
@ -36,10 +35,10 @@ class OverdueRemindEmailTask extends AbstractTask
->toArray();
}
if ($hours2 > 0) {
$endTime2 = Carbon::now()->addHours($hours2);
$endTime2 = Carbon::now()->subHours($hours2);
$taskLists2 = ProjectTask::whereNull('complete_at')
->where('end_at', '>=', $startTime)
->where('end_at', '<=', $endTime2)
->where('end_at', '>=', $endTime2)
->where('end_at', '<', $startTime)
->whereNull('archived_at')
->take(100)
->get()
@ -48,15 +47,8 @@ class OverdueRemindEmailTask extends AbstractTask
}
$taskLists = array_merge($taskLists1, $taskLists2);
$taskLists = Base::assoc_unique($taskLists, 'id');
$ownerIdsArr = [];
foreach ($taskLists as &$task) {
$ownerIds = ProjectTaskUser::whereTaskId($task['id'])->whereOwner(1)->pluck('userid')->toArray();
foreach ($ownerIds as $ownerId) {
$ownerIdsArr[] = $ownerId;
}
}
if (!empty($ownerIdsArr)) {
ProjectTask::overdueRemindEmail($ownerIdsArr);
foreach ($taskLists as $task) {
ProjectTask::overdueRemindEmail($task);
}
}
}

View File

@ -0,0 +1,36 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateProjectTaskMailLogsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('project_task_mail_logs', function (Blueprint $table) {
$table->id();
$table->integer('userid')->nullable()->default(0)->comment('用户id');
$table->integer('task_id')->nullable()->default(0)->comment('任务id');
$table->string('email')->nullable()->default('')->comment('电子邮箱');
$table->tinyInteger('type')->nullable()->default(0)->comment('提醒类型1第一次任务提醒2第二次任务超期提醒');
$table->tinyInteger('is_send')->nullable()->default(0)->comment('邮件发送是否成功0否1是');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('project_task_mail_logs');
}
}

View File

@ -36,7 +36,7 @@
<label>{{ $L('小时') }}</label>
</FormItem>
<FormItem :label="$L('第二次任务提醒:')" prop="task_remind_hours2">
<label>{{ $L('到期') }}</label>
<label>{{ $L('到期') }}</label>
<InputNumber v-model="formData.task_remind_hours2" :min="0.5" :step="0.5" @on-change="hours2Change"/>
<label>{{ $L('小时') }}</label>
</FormItem>

View File

@ -5,13 +5,15 @@
<meta http-equiv="X-UA-Compatible" content="IE=edge">
</head>
<body>
<p>用户您好, {{env('APP_NAME') }} 任务到期。</p>
<p>若需要重新设定账号密码,请点击下方链接:</p>
<div style="display: flex; justify-content: left;">
<a href="{{$url}}">{{$url}}</a>
</div>
<p>如果连接无法点击请复制此URL然后贴入到您浏览器的地址栏中。</p>
<p>用户您好, {{env('APP_NAME') }} 任务到期提醒。</p>
@if ($type == 1)
<p>
您有一个任务【{{$name}}】还有{{$time}}小时即将超时,请及时处理
</p>
@else
<p>
您的任务【{{$name}}】已经超时{{$time}}小时,请及时处理
</p>
@endif
</body>
<body>