AutoJob.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Components\Helpers;
  4. use App\Models\Config;
  5. use App\Models\Coupon;
  6. use App\Models\Invite;
  7. use App\Models\Order;
  8. use App\Models\User;
  9. use App\Models\VerifyCode;
  10. use Illuminate\Console\Command;
  11. use Log;
  12. class AutoJob extends Command
  13. {
  14. protected $signature = 'autoJob';
  15. protected $description = '自动化任务';
  16. /*
  17. * 警告:除非熟悉业务流程,否则不推荐更改以下执行顺序,随意变更以下顺序可能导致系统异常
  18. */
  19. public function handle(): void
  20. {
  21. $jobStartTime = microtime(true);
  22. // 关闭超时未支付本地订单
  23. Order::query()->recentUnPay()->update(['status' => -1]);
  24. //过期验证码、优惠券、邀请码无效化
  25. $this->expireCode();
  26. // 封禁访问异常的订阅链接
  27. $this->blockSubscribe();
  28. // 封禁账号
  29. $this->blockUsers();
  30. // 解封被封禁的账号
  31. $this->unblockUsers();
  32. // 端口回收与分配
  33. if (sysConfig('auto_release_port')) {
  34. $this->dispatchPort();
  35. }
  36. // 检查维护模式
  37. if (sysConfig('maintenance_mode') && sysConfig('maintenance_time') && sysConfig('maintenance_time') <= date('c')) {
  38. Config::whereIn('name', ['maintenance_mode', 'maintenance_content', 'maintenance_time'])->update(['value' => null]);
  39. }
  40. $jobEndTime = microtime(true);
  41. $jobUsedTime = round(($jobEndTime - $jobStartTime), 4);
  42. Log::info('---【'.$this->description.'】完成---,耗时'.$jobUsedTime.'秒');
  43. }
  44. // 注册验证码自动置无效 & 优惠券无效化
  45. private function expireCode(): void
  46. {
  47. // 注册验证码自动置无效
  48. VerifyCode::recentUnused()->update(['status' => 2]);
  49. // 优惠券到期 / 用尽的 自动置无效
  50. Coupon::withTrashed()->whereStatus(0)->where('end_time', '<=', time())->orWhereIn('type', [1, 2])->whereUsableTimes(0)->update(['status' => 2]);
  51. // 邀请码到期自动置无效
  52. Invite::whereStatus(0)->where('dateline', '<=', date('Y-m-d H:i:s'))->update(['status' => 2]);
  53. }
  54. // 封禁访问异常的订阅链接
  55. private function blockSubscribe(): void
  56. {
  57. if (sysConfig('is_subscribe_ban')) {
  58. $subscribe_ban_times = sysConfig('subscribe_ban_times');
  59. foreach (User::activeUser()->with('subscribe')->get() as $user) {
  60. if (! $user->subscribe || $user->subscribe->status === 0) { // 无订阅链接 或 已封
  61. continue;
  62. }
  63. // 24小时内不同IP的请求次数
  64. $request_times = $user->subscribeLogs()
  65. ->where('request_time', '>=', date('Y-m-d H:i:s', strtotime('-1 days')))
  66. ->distinct()
  67. ->count('request_ip');
  68. if ($request_times >= $subscribe_ban_times) {
  69. $user->subscribe->update([
  70. 'status' => 0,
  71. 'ban_time' => strtotime('+'.sysConfig('traffic_ban_time').' minutes'),
  72. 'ban_desc' => '存在异常,自动封禁',
  73. ]);
  74. // 记录封禁日志
  75. Helpers::addUserBanLog($user->id, 0, '【完全封禁订阅】-订阅24小时内请求异常');
  76. }
  77. }
  78. }
  79. }
  80. // 封禁账号
  81. private function blockUsers(): void
  82. {
  83. // 禁用流量超限用户
  84. foreach (User::activeUser()->whereRaw('u + d >= transfer_enable')->get() as $user) {
  85. $user->update(['enable' => 0]);
  86. // 写入日志
  87. Helpers::addUserBanLog($user->id, 0, '【封禁代理】-流量已用完');
  88. }
  89. // 封禁1小时内流量异常账号
  90. if (sysConfig('is_traffic_ban')) {
  91. $trafficBanTime = sysConfig('traffic_ban_time');
  92. foreach (User::activeUser()->whereBanTime(null)->get() as $user) {
  93. // 多往前取5分钟,防止数据统计任务执行时间过长导致没有数据
  94. if ($user->isTrafficWarning()) {
  95. $user->update([
  96. 'enable' => 0,
  97. 'ban_time' => strtotime('+'.$trafficBanTime.' minutes'),
  98. ]);
  99. // 写入日志
  100. Helpers::addUserBanLog($user->id, $trafficBanTime, '【临时封禁代理】-1小时内流量异常');
  101. }
  102. }
  103. }
  104. }
  105. // 解封被临时封禁的账号
  106. private function unblockUsers(): void
  107. {
  108. // 解封被临时封禁的账号
  109. $userList = User::whereEnable(0)->where('status', '>=', 0)->whereNotNull('ban_time')->where('ban_time', '<', time())->get();
  110. foreach ($userList as $user) {
  111. $user->update(['enable' => 1, 'ban_time' => null]);
  112. // 写入操作日志
  113. Helpers::addUserBanLog($user->id, 0, '【自动解封】-临时封禁到期');
  114. }
  115. // 可用流量大于已用流量也解封(比如:邀请返利自动加了流量)
  116. $userList = User::whereEnable(0)
  117. ->where('status', '>=', 0)
  118. ->whereBanTime(null)
  119. ->where('expired_at', '>=', date('Y-m-d'))
  120. ->whereRaw('u + d < transfer_enable')
  121. ->get();
  122. foreach ($userList as $user) {
  123. $user->update(['enable' => 1]);
  124. // 写入操作日志
  125. Helpers::addUserBanLog($user->id, 0, '【自动解封】-有流量解封');
  126. }
  127. }
  128. // 端口回收与分配
  129. private function dispatchPort(): void
  130. {
  131. // 自动分配端口
  132. User::activeUser()->wherePort(0)->get()->each(function ($user) {
  133. $user->update(['port' => Helpers::getPort()]);
  134. });
  135. // 被封禁 / 过期一个月 的账号自动释放端口
  136. User::where('port', '<>', 0)
  137. ->whereStatus(-1)
  138. ->orWhere('expired_at', '<=', date('Y-m-d', strtotime('-1 months')))
  139. ->update(['port' => 0]);
  140. }
  141. }