Commit e51777c5 by Phạm Văn Đoan

cập nhật cảnh báo qua email

parent feb93cdd
...@@ -30,11 +30,13 @@ REDIS_PASSWORD=null ...@@ -30,11 +30,13 @@ REDIS_PASSWORD=null
REDIS_PORT=6379 REDIS_PORT=6379
MAIL_DRIVER=smtp MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io MAIL_HOST=smtp.gmail.com
MAIL_PORT=2525 MAIL_PORT=587
MAIL_USERNAME=null MAIL_USERNAME=vmusicranking@gmail.com
MAIL_PASSWORD=null MAIL_PASSWORD=cnrnjvhzryirwndr
MAIL_ENCRYPTION=null MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=vmusicranking@gmail.com
MAIL_FROM_NAME=VMusicChart.Vn
PUSHER_APP_ID= PUSHER_APP_ID=
PUSHER_APP_KEY= PUSHER_APP_KEY=
......
<?php
namespace App\Console\Commands;
use App\Models\Report\DailyReport;
use App\Models\Report\MonthlyReport;
use App\Models\Report\WeeklyReport;
use App\Models\UserLog;
use App\Repositories\MailerRepository;
use Illuminate\Console\Command;
class SendWarningCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'send:email {type?}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Gửi mail cảnh báo';
protected $mailer;
protected $receiver;
protected $admin;
/**
* Create a new command instance.
*
* SendWarningCommand constructor.
* @param MailerRepository $mailerRepository
*/
public function __construct(MailerRepository $mailerRepository)
{
parent::__construct();
$this->mailer = $mailerRepository;
$this->receiver = 'tuvt.ttdt@gmail.com';
$this->admin = 'tuvuongcms';
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$type = $this->argument('type');
if (empty($type) || !in_array($type, ['daily_checking', 'warning_daily_report', 'warning_weekly_report', 'warning_monthly_report', 'reporting'])) {
$type = $this->choice(
'Chọn loại cảnh báo ',
[
'daily_checking',
'warning_daily_report',
'warning_weekly_report',
'warning_monthly_report',
'reporting'
]
);
}
echo "\n Bắt đầu xử lý gửi mail cảnh báo.";
switch ($type) {
case 'daily_checking':
$check = UserLog::where('account_input', $this->admin)
->where('time_login', '>=', date('Y-m-d 06:00:00', time()))
->where('time_login', '<=', date('Y-m-d 09:00:00', time()))
->count();
if ($check < 1) {
$this->mailer->sendMail(
$this->receiver,
'Cảnh báo: Bạn chưa vào check CMS đầu giờ hàng ngày trước 9h sáng.',
'Cảnh báo: Bạn chưa vào check CMS đầu giờ hàng ngày trước 9h sáng.'
);
echo "\n - Vừa gửi mail cảnh báo không check hàng ngày.";
}
break;
case 'warning_daily_report':
$check = DailyReport::where('date', date('Y-m-d', time() - 86400))->count();
if ($check < 100) {
$this->mailer->sendMail(
$this->receiver,
'Cảnh báo lỗi: BXH Ngày chưa được tính toán.',
'Cảnh báo lỗi: BXH Ngày chưa được tính toán.'
);
echo "\n - Vừa gửi mail cảnh báo BXH Ngày xong.";
}
break;
case 'warning_weekly_report':
if (strtoupper(date('D')) == 'SUN') {
$check = WeeklyReport::where('week', date('W'))
->where('year', date('Y'))
->count();
if ($check < 100) {
$this->mailer->sendMail(
$this->receiver,
'Cảnh báo lỗi: BXH Tuần chưa được tính toán.',
'Cảnh báo lỗi: BXH Tuần chưa được tính toán.'
);
echo "\n - Vừa gửi mail cảnh báo BXH Tuần xong.";
}
}
break;
case 'warning_monthly_report':
if (date('d') == 21) {
$check = MonthlyReport::where('month', date('m'))
->where('year', date('Y'))
->count();
if ($check < 100) {
$this->mailer->sendMail(
$this->receiver,
'Cảnh báo lỗi: BXH Tháng chưa được tính toán.',
'Cảnh báo lỗi: BXH Tháng chưa được tính toán.'
);
echo "\n - Vừa gửi mail cảnh báo BXH Tuần xong.";
}
}
break;
case 'reporting':
break;
default:
}
echo "\n Kết thúc xử lý gửi mail cảnh báo.";
}
}
...@@ -12,6 +12,7 @@ use App\Console\Commands\Keeng\KeengCrawlerNewRealeaseCommand; ...@@ -12,6 +12,7 @@ use App\Console\Commands\Keeng\KeengCrawlerNewRealeaseCommand;
use App\Console\Commands\Keeng\KeengCrawlerTrackCommand; use App\Console\Commands\Keeng\KeengCrawlerTrackCommand;
use App\Console\Commands\Nct\NctCrawlerTrackCommand; use App\Console\Commands\Nct\NctCrawlerTrackCommand;
use App\Console\Commands\NhacVn\NhacVnCrawlerTrackCommand; use App\Console\Commands\NhacVn\NhacVnCrawlerTrackCommand;
use App\Console\Commands\SendWarningCommand;
use App\Console\Commands\Update\UpdateTrackArtistCommand; use App\Console\Commands\Update\UpdateTrackArtistCommand;
use App\Console\Commands\Zing\ZingCrawlerNewRealeaseCommand; use App\Console\Commands\Zing\ZingCrawlerNewRealeaseCommand;
use App\Console\Commands\Zing\ZingCrawlerTrackCommand; use App\Console\Commands\Zing\ZingCrawlerTrackCommand;
...@@ -46,7 +47,9 @@ class Kernel extends ConsoleKernel ...@@ -46,7 +47,9 @@ class Kernel extends ConsoleKernel
UpdateTrackArtistCommand::class, UpdateTrackArtistCommand::class,
ExportRankingCommand::class ExportRankingCommand::class,
SendWarningCommand::class
]; ];
...@@ -97,6 +100,9 @@ class Kernel extends ConsoleKernel ...@@ -97,6 +100,9 @@ class Kernel extends ConsoleKernel
$schedule->command('calculate:report weekly')->dailyAt('4:05'); $schedule->command('calculate:report weekly')->dailyAt('4:05');
$schedule->command('calculate:report monthly')->monthlyOn(21, '5:05'); $schedule->command('calculate:report monthly')->monthlyOn(21, '5:05');
// Gửi mail nhắc nhở check CMS hàng ngày trước 9h
$schedule->command('send:email daily_checking')->dailyAt('9:01');
$schedule->command('queue:work --once --timeout=120')->everyMinute()->withoutOverlapping(); $schedule->command('queue:work --once --timeout=120')->everyMinute()->withoutOverlapping();
} }
......
...@@ -36,7 +36,7 @@ class Constants ...@@ -36,7 +36,7 @@ class Constants
const TABLE_TRACK_USER = 'track_user'; const TABLE_TRACK_USER = 'track_user';
const TABLE_TRACKS = 'tracks'; const TABLE_TRACKS = 'tracks';
const TABLE_USER_LOGS = 'user_logs'; const TABLE_USER_LOG = 'user_log';
const TABLE_USERS = 'users'; const TABLE_USERS = 'users';
const VIEW_TABLE_DAILY_REPORT_VIEW = 'daily_report_view'; const VIEW_TABLE_DAILY_REPORT_VIEW = 'daily_report_view';
......
<?php
namespace App\Models;
use App\Helpers\Constants;
use Illuminate\Database\Eloquent\Model;
class UserLog extends Model
{
protected $table = Constants::TABLE_USER_LOG;
public $timestamps = false;
}
<?php
namespace App\Repositories;
use App\User;
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\PHPMailer;
class MailerRepository extends BaseRepository
{
protected $mailer = null;
public function __construct()
{
$this->mailServerSettings();
}
/**
* Lấy đối tượng mailer
* @return null
*/
public function getMailer()
{
return $this->mailer;
}
/**
* Hàm gửi 1 email
*
* @param $toEmail
* @param $subject
* @param $message
* @param null $attachFile
*
* @return int
*/
public function sendMail($toEmail, $subject, $message, $attachFile = null)
{
if (!empty($this->mailer)) {
try {
$this->mailer->addAddress($toEmail);
$this->mailer->Subject = $subject;
$this->mailer->Body = $message;
$this->mailer->send();
$this->mailer->clearAllRecipients();
$this->mailer->smtpClose();
return 1;
} catch (Exception $e) {
return -2;
}
} else {
reutrn -3;
}
}
/**
* @return null|PHPMailer
*/
private function mailServerSettings()
{
$this->mailer = new PHPMailer(true);
//$this->mailer->SMTPDebug = 2;
$this->mailer->isSMTP();
$this->mailer->CharSet = 'utf-8';
$this->mailer->SMTPKeepAlive = true;
$this->mailer->Timeout = 15;
$this->mailer->getSMTPInstance()->Timelimit = 15;
$this->mailer->Host = config('mail.host');
$this->mailer->SMTPAuth = true;
$this->mailer->SMTPSecure = config('mail.encryption');
$this->mailer->Username = config('mail.username');
$this->mailer->Password = config('mail.password');
$this->mailer->Port = config('mail.port');
$this->mailer->SMTPOptions = [
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
];
$this->mailer->isHTML(true);
$this->mailer->setFrom(config('mail.from.address'), config('mail.from.name'));
$this->mailer->addCC('doanpv@dcv.vn', 'Phạm Văn Đoan');
$this->mailer->Subject = '[VMusicChart] Cảnh báo';
return $this->mailer;
}
}
\ No newline at end of file
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
"laravel/framework": "5.4.*", "laravel/framework": "5.4.*",
"laravel/tinker": "~1.0", "laravel/tinker": "~1.0",
"maatwebsite/excel": "~2.1.0", "maatwebsite/excel": "~2.1.0",
"phpmailer/phpmailer": "^6.1",
"sunra/php-simple-html-dom-parser": "^1.5", "sunra/php-simple-html-dom-parser": "^1.5",
"yangqi/htmldom": "@dev" "yangqi/htmldom": "@dev"
}, },
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment