Skip to content

Commit 599cac3

Browse files
committed
1.定时任务
2.节点订阅
1 parent 46722f8 commit 599cac3

19 files changed

+570
-106
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace App\Console\Commands;
4+
5+
use Illuminate\Console\Command;
6+
use App\Http\Models\Goods;
7+
use App\Http\Models\OrderGoods;
8+
use App\Http\Models\User;
9+
use Log;
10+
11+
class AutoDecGoodsTrafficJob extends Command
12+
{
13+
protected $signature = 'command:autoDecGoodsTrafficJob';
14+
protected $description = '商品到期自动扣购买该商品的账号流量';
15+
16+
public function __construct()
17+
{
18+
parent::__construct();
19+
}
20+
21+
public function handle()
22+
{
23+
$goodsList = Goods::where('end_time', '<', date('Y-m-d H:i:s'))->get();
24+
foreach ($goodsList as $goods) {
25+
// 所有购买过该商品的用户
26+
$orderGoods = OrderGoods::where('goods_id', $goods->id)->get();
27+
foreach ($orderGoods as $og) {
28+
$u = User::where('id', $og->user_id)->first();
29+
if (empty($u)) {
30+
continue;
31+
}
32+
33+
if ($u->transfer_enable - $goods->traffic * 1024 * 1024 < 0) {
34+
User::where('id', $og->user_id)->update(['transfer_enable' => 0]);
35+
} else {
36+
User::where('id', $og->user_id)->decrement('transfer_enable', $goods->traffic * 1024 * 1024);
37+
}
38+
}
39+
}
40+
41+
Log::info('定时任务:' . $this->description);
42+
}
43+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace App\Console\Commands;
4+
5+
use App\Http\Models\User;
6+
use Illuminate\Console\Command;
7+
use Log;
8+
9+
class DisableExpireUserJob extends Command
10+
{
11+
protected $signature = 'command:disableExpireUserJob';
12+
protected $description = '禁用到期账号';
13+
14+
public function __construct()
15+
{
16+
parent::__construct();
17+
}
18+
19+
public function handle()
20+
{
21+
// 到期账号禁用
22+
User::where('enable', 1)->where('expire_time', '<=', date('Y-m-d'))->update(['enable' => 0]);
23+
24+
Log::info('定时任务:' . $this->description);
25+
}
26+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace App\Console\Commands;
4+
5+
use App\Http\Models\Invite;
6+
use Illuminate\Console\Command;
7+
use Log;
8+
9+
class InviteExpire extends Command
10+
{
11+
protected $signature = 'command:inviteExpire';
12+
protected $description = '邀请码过期废除';
13+
14+
public function __construct()
15+
{
16+
parent::__construct();
17+
}
18+
19+
public function handle()
20+
{
21+
$inviteList = Invite::where('status', 0)->where('dateline', '<=', date('Y-m-d H:i:s'))->get();
22+
if ($inviteList->isEmpty()) {
23+
foreach ($inviteList as $invite) {
24+
Invite::where('id', $invite->id)->update(['status' => 2]);
25+
}
26+
}
27+
28+
Log::info('定时任务:' . $this->description);
29+
}
30+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace App\Console\Commands;
4+
5+
use Illuminate\Console\Command;
6+
use App\Http\Models\Config;
7+
use App\Http\Models\User;
8+
use App\Mail\userExpireWarning;
9+
use Mail;
10+
use Log;
11+
12+
class UserExpireWarningJob extends Command
13+
{
14+
protected $signature = 'command:userExpireWarningJob';
15+
protected $description = '用户到期提醒发邮件';
16+
17+
protected static $config;
18+
19+
public function __construct()
20+
{
21+
parent::__construct();
22+
23+
$config = Config::get();
24+
$data = [];
25+
foreach ($config as $vo) {
26+
$data[$vo->name] = $vo->value;
27+
}
28+
29+
self::$config = $data;
30+
}
31+
32+
public function handle()
33+
{
34+
if (self::$config['expire_warning']) {
35+
$userList = User::where('transfer_enable', '>', 0)->whereIn('status', [0, 1])->where('enable', 1)->get();
36+
foreach ($userList as $user) {
37+
// 用户名不是邮箱的跳过
38+
if (false === filter_var($user->username, FILTER_VALIDATE_EMAIL)) {
39+
continue;
40+
}
41+
42+
$lastCanUseDays = floor(round(strtotime($user->expire_time) - strtotime(date('Y-m-d H:i:s'))) / 3600 / 24);
43+
if ($lastCanUseDays > 0 && $lastCanUseDays <= self::$config['expire_days']) {
44+
$title = '账号过期提醒';
45+
$content = '账号还剩【' . $lastCanUseDays . '】天即将过期';
46+
47+
try {
48+
Mail::to($user->username)->send(new userExpireWarning(self::$config['website_name'], $lastCanUseDays));
49+
$this->sendEmailLog($user->id, $title, $content);
50+
} catch (\Exception $e) {
51+
$this->sendEmailLog($user->id, $title, $content, 0, $e->getMessage());
52+
}
53+
}
54+
}
55+
}
56+
57+
Log::info('定时任务:' . $this->description);
58+
}
59+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace App\Console\Commands;
4+
5+
use Illuminate\Console\Command;
6+
use App\Http\Models\Config;
7+
use App\Http\Models\User;
8+
use App\Mail\userTrafficWarning;
9+
use Mail;
10+
use Log;
11+
12+
class UserTrafficWarningJob extends Command
13+
{
14+
protected $signature = 'command:userTrafficWarningJob';
15+
protected $description = '用户流量警告提醒发邮件';
16+
17+
protected static $config;
18+
19+
public function __construct()
20+
{
21+
parent::__construct();
22+
23+
$config = Config::get();
24+
$data = [];
25+
foreach ($config as $vo) {
26+
$data[$vo->name] = $vo->value;
27+
}
28+
29+
self::$config = $data;
30+
}
31+
32+
public function handle()
33+
{
34+
if (self::$config['traffic_warning']) {
35+
$userList = User::where('transfer_enable', '>', 0)->whereIn('status', [0, 1])->where('enable', 1)->get();
36+
foreach ($userList as $user) {
37+
// 用户名不是邮箱的跳过
38+
if (false === filter_var($user->username, FILTER_VALIDATE_EMAIL)) {
39+
continue;
40+
}
41+
42+
$usedPercent = round(($user->d + $user->u) / $user->transfer_enable, 2) * 100; // 已使用流量百分比
43+
if ($usedPercent >= self::$config['traffic_warning_percent']) {
44+
$title = '流量警告';
45+
$content = '流量已使用:' . $usedPercent . '%,超过设置的流量阈值' . self::$config['traffic_warning_percent'] . '%';
46+
47+
try {
48+
Mail::to($user->username)->send(new userTrafficWarning(self::$config['website_name'], $usedPercent));
49+
$this->sendEmailLog($user->id, $title, $content);
50+
} catch (\Exception $e) {
51+
$this->sendEmailLog($user->id, $title, $content, 0, $e->getMessage());
52+
}
53+
}
54+
}
55+
}
56+
57+
Log::info('定时任务:' . $this->description);
58+
}
59+
}

app/Console/Kernel.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@ class Kernel extends ConsoleKernel
1313
* @var array
1414
*/
1515
protected $commands = [
16-
//
16+
\App\Console\Commands\DisableExpireUserJob::class,
17+
\App\Console\Commands\AutoDecGoodsTrafficJob::class,
18+
\App\Console\Commands\UserTrafficWarningJob::class,
19+
\App\Console\Commands\UserExpireWarningJob::class,
20+
\App\Console\Commands\InviteExpire::class,
1721
];
1822

1923
/**
@@ -24,8 +28,11 @@ class Kernel extends ConsoleKernel
2428
*/
2529
protected function schedule(Schedule $schedule)
2630
{
27-
// $schedule->command('inspire')
28-
// ->hourly();
31+
$schedule->command('command:disableExpireUserJob')->everyMinute();
32+
$schedule->command('command:autoDecGoodsTrafficJob')->everyTenMinutes();
33+
$schedule->command('command:userTrafficWarningJob')->daily();
34+
$schedule->command('command:userExpireWarningJob')->daily();
35+
$schedule->command('command:inviteExpire')->everyThirtyMinutes();
2936
}
3037

3138
/**

app/Http/Controllers/AdminController.php

Lines changed: 0 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44

55
use App\Http\Models\Article;
66
use App\Http\Models\Config;
7-
use App\Http\Models\Goods;
87
use App\Http\Models\Invite;
9-
use App\Http\Models\OrderGoods;
108
use App\Http\Models\ReferralApply;
119
use App\Http\Models\ReferralLog;
1210
use App\Http\Models\SsConfig;
@@ -17,12 +15,9 @@
1715
use App\Http\Models\SsNodeOnlineLog;
1816
use App\Http\Models\User;
1917
use App\Http\Models\UserTrafficLog;
20-
use App\Mail\userExpireWarning;
21-
use App\Mail\userTrafficWarning;
2218
use Illuminate\Http\Request;
2319
use Redirect;
2420
use Response;
25-
use Mail;
2621

2722
class AdminController extends BaseController
2823
{
@@ -50,76 +45,6 @@ public function index(Request $request)
5045
$view['totalRefAmount'] = ReferralApply::where('status', 2)->sum('amount');
5146
$view['expireWarningUserCount'] = User::where('expire_time', '<=', date('Y-m-d', strtotime("+15 days")))->where('enable', 1)->count();
5247

53-
// 到期账号禁用 TODO:加入定时任务
54-
User::where('enable', 1)->where('expire_time', '<=', date('Y-m-d'))->update(['enable' => 0]);
55-
56-
// 商品到期自动扣购买该商品的流量 TODO:加入定时任务
57-
$goodsList = Goods::where('end_time', '<', date('Y-m-d H:i:s'))->get();
58-
foreach ($goodsList as $goods) {
59-
// 所有购买过该商品的用户
60-
$orderGoods = OrderGoods::where('goods_id', $goods->id)->get();
61-
foreach ($orderGoods as $og) {
62-
$u = User::where('id', $og->user_id)->first();
63-
if (empty($u)) {
64-
continue;
65-
}
66-
67-
if ($u->transfer_enable - $goods->traffic * 1024 * 1024 < 0) {
68-
User::where('id', $og->user_id)->update(['transfer_enable' => 0]);
69-
} else {
70-
User::where('id', $og->user_id)->decrement('transfer_enable', $goods->traffic * 1024 * 1024);
71-
}
72-
}
73-
}
74-
75-
// 用户流量警告提醒发邮件 TODO:加入定时任务
76-
if (self::$config['traffic_warning']) {
77-
$userList = User::where('transfer_enable', '>', 0)->whereIn('status', [0, 1])->where('enable', 1)->get();
78-
foreach ($userList as $user) {
79-
// 用户名不是邮箱的跳过
80-
if (false === filter_var($user->username, FILTER_VALIDATE_EMAIL)) {
81-
continue;
82-
}
83-
84-
$usedPercent = round(($user->d + $user->u) / $user->transfer_enable, 2) * 100; // 已使用流量百分比
85-
if ($usedPercent >= self::$config['traffic_warning_percent']) {
86-
$title = '流量警告';
87-
$content = '流量已使用:' . $usedPercent . '%,超过设置的流量阈值' . self::$config['traffic_warning_percent'] . '%';
88-
89-
try {
90-
Mail::to($user->username)->send(new userTrafficWarning(self::$config['website_name'], $usedPercent));
91-
$this->sendEmailLog($user->id, $title, $content);
92-
} catch (\Exception $e) {
93-
$this->sendEmailLog($user->id, $title, $content, 0, $e->getMessage());
94-
}
95-
}
96-
}
97-
}
98-
99-
// 用户到期提醒发邮件 TODO:加入定时任务
100-
if (self::$config['expire_warning']) {
101-
$userList = User::where('transfer_enable', '>', 0)->whereIn('status', [0, 1])->where('enable', 1)->get();
102-
foreach ($userList as $user) {
103-
// 用户名不是邮箱的跳过
104-
if (false === filter_var($user->username, FILTER_VALIDATE_EMAIL)) {
105-
continue;
106-
}
107-
108-
$lastCanUseDays = floor(round(strtotime($user->expire_time) - strtotime(date('Y-m-d H:i:s'))) / 3600 / 24);
109-
if ($lastCanUseDays > 0 && $lastCanUseDays <= self::$config['expire_days']) {
110-
$title = '账号过期提醒';
111-
$content = '账号还剩【' . $lastCanUseDays . '】天即将过期';
112-
113-
try {
114-
Mail::to($user->username)->send(new userExpireWarning(self::$config['website_name'], $lastCanUseDays));
115-
$this->sendEmailLog($user->id, $title, $content);
116-
} catch (\Exception $e) {
117-
$this->sendEmailLog($user->id, $title, $content, 0, $e->getMessage());
118-
}
119-
}
120-
}
121-
}
122-
12348
return Response::view('admin/index', $view);
12449
}
12550

0 commit comments

Comments
 (0)