master
cody 4 months ago
parent 8f12308f83
commit 59acfcd1af

@ -0,0 +1,70 @@
<?php
namespace App\Console\Commands;
use App\Models\EmailRecord;
use App\Models\EmailRecordUser;
use App\Models\User;
use App\Repositories\MeetRepository;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Mail;
class SendEmail extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'send_email';
/**
* The console command description.
*
* @var string
*/
protected $description = '发送邮件';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$emailRecords = EmailRecord::whereHas('emailRecordUsers', function ($query) {
$query->where('status', 0);
})->where(function ($query) {
$query->whereNull('time')->orWhere('time', '<', date('Y-m-d H:i:s'));
})->get();
foreach ($emailRecords as $records) {
// 获取模版配置
$emailTemplate = $records->emailTemplate;
// 获取未发送人员
$emailRecordUsers = $records->emailRecordUsers->where('status', 0);
foreach ($emailRecordUsers as $recordUser) {
// 替换后的标题
$title = EmailRecordUser::template($records->subject, $recordUser);
// 替换后的内容
$template = EmailRecordUser::template($emailTemplate->content, $recordUser);
// 发送邮件
EmailRecordUser::email($title, $template, $recordUser);
$recordUser->status = 1;
$recordUser->save();
}
}
return $this->info('更新完成');
}
}

@ -22,6 +22,8 @@ class Kernel extends ConsoleKernel
$schedule->command('update_appointment_total')->dailyAt('1:00');
// 生日检测
$schedule->command('check_birthday')->dailyAt('09:00');
// 邮件群发
$schedule->command('send_email')->everyMinute();
}
/**

@ -162,7 +162,7 @@ class EmailRecordController extends BaseController
* @OA\Parameter(name="time", in="query", @OA\Schema(type="string", format="date"), required=false, description="发送时间"),
* @OA\Parameter(name="subject", in="query", @OA\Schema(type="string", maxLength=255), required=false, description="主题"),
* @OA\Parameter(name="email_template_id", in="query", @OA\Schema(type="string", format="mediumtext"), required=false, description="邮件模版id"),
* @OA\Parameter(name="email_record_users", in="query", @OA\Schema(type="string", format="mediumtext"), required=false, description="二维数组,包括建明:user_id"),
* @OA\Parameter(name="email_record_users", in="query", @OA\Schema(type="string", format="mediumtext"), required=false, description="二维数组包括建明emailvar_data自定义数据"),
* @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="认证token"),
* @OA\Response(
* response="200",
@ -188,6 +188,10 @@ class EmailRecordController extends BaseController
$original = $model->getOriginal();
$model->fill($all);
$model->save();
if (isset($all['email_record_users'])) {
$model->emailRecordUsers()->delete();
$model->emailRecordUsers()->createMany($all['email_record_users']);
}
DB::commit();
// 记录日志
$this->saveLogs($original, $model);
@ -217,4 +221,50 @@ class EmailRecordController extends BaseController
return parent::destroy();
}
/**
* @OA\Post(
* path="/api/admin/email-record/excel-show",
* tags={"邮件发送配置"},
* summary="导入预览",
* description="",
* @OA\Parameter(name="file", in="query", @OA\Schema(type="string"), required=true, description="文件"),
* @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"),
* @OA\Response(
* response="200",
* description="暂无"
* )
* )
*/
public function excelShow()
{
$file = \request()->file('file');
//判断文件是否有效
if (!(\request()->hasFile('file') && $file->isValid())) {
return $this->fail([ResponseCode::ERROR_BUSINESS, '文件不存在或无效']);
}
//获取文件大小
$img_size = floor($file->getSize() / 1024);
if ($img_size >= 50 * 1024) {
return $this->fail([ResponseCode::ERROR_BUSINESS, '文件必须小于50M']);
}
//过滤文件后缀
$ext = $file->getClientOriginalExtension();
if (!in_array($ext, ['xls', 'xlsx', 'csv'])) {
return $this->fail([ResponseCode::ERROR_BUSINESS, '仅支持xls/xlsx/csv格式']);
}
$tempFile = $file->getRealPath();
$dataArray = (new FastExcel)->import($tempFile)->toArray();
$list = [];
foreach ($dataArray as $key => $value) {
if (!isset($value['邮箱']) || empty($value['邮箱'])) {
continue;
}
$list[] = [
'email' => $value['邮箱'],
'var_data' => $value
];
}
return $this->success($list);
}
}

@ -8,6 +8,12 @@ use Illuminate\Support\Facades\Cache;
class EmailRecord extends SoftDeletesModel
{
public function emailTemplate()
{
return $this->hasOne(EmailTemplate::class, 'id', 'email_template_id');
}
public function emailRecordUsers()
{
return $this->hasMany(EmailRecordUser::class, 'email_record_id', 'id');

@ -5,12 +5,37 @@ namespace App\Models;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Mail;
class EmailRecordUser extends SoftDeletesModel
{
public function user()
protected $casts = ['var_data' => 'json'];
/**
* 邮件模版内容替换
* @param $template
* @param $email_record_users
*/
public static function template($template, $record_user)
{
$var_data = $record_user->var_data;
foreach ($var_data as $key => $var) {
$template = str_replace('{' . $key . '}', $var, $template);
}
return $var_data;
}
/**
* 发送邮件
*/
public static function email($title, $template, $record_user)
{
return $this->hasOne(User::class, 'id', 'user_id');
Mail::send('email', compact('template'), function ($message) use ($record_user, $title) {
$message->from(env('MAIL_USERNAME'), '苏州科技商学院');
$message->to($record_user->email)->subject($title);
});
return true;
}
}

@ -20,6 +20,8 @@ return new class extends Migration {
$table->string('subject')->nullable()->comment('邮件主题');
// 邮件模版id
$table->integer('email_template_id')->nullable()->comment('邮件模版id');
// 状态
$table->tinyInteger('status')->nullable()->default(0)->comment('状态0:待处理1:已处理');
$table->timestamps();
$table->softDeletes();
});

@ -16,8 +16,9 @@ return new class extends Migration {
$table->id();
// 发送配置id
$table->integer('email_record_id')->nullable()->comment('发送记录id');
// 接收用户id
$table->integer('user_id')->nullable()->comment('接收用户id');
$table->json('var_data')->nullable()->comment('自定义的数据');
// 邮箱
$table->string('email')->nullable()->comment('邮箱');
// 状态
$table->tinyInteger('status')->nullable()->comment('状态0:待发送1:成功 2:失败');
// 发送时间

@ -0,0 +1,7 @@
<html>
<head>
</head>
<body>
{!! $template !!}
</body>
</html>

@ -215,7 +215,7 @@ Route::group(["namespace" => "Admin", "prefix" => "admin"], function () {
Route::get('email-record/show', [\App\Http\Controllers\Admin\EmailRecordController::class, "show"]);
Route::post('email-record/save', [\App\Http\Controllers\Admin\EmailRecordController::class, "save"]);
Route::get('email-record/destroy', [\App\Http\Controllers\Admin\EmailRecordController::class, "destroy"]);
Route::post('email-record/excel-show', [\App\Http\Controllers\Admin\EmailRecordController::class, "excelShow"]);
});
});

Loading…
Cancel
Save