AutoJob.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Components\Helpers;
  4. use App\Components\ServerChan;
  5. use App\Http\Models\Coupon;
  6. use App\Http\Models\Invite;
  7. use App\Http\Models\Order;
  8. use App\Http\Models\Payment;
  9. use App\Http\Models\SsNode;
  10. use App\Http\Models\SsNodeInfo;
  11. use App\Http\Models\User;
  12. use App\Http\Models\UserBanLog;
  13. use App\Http\Models\UserSubscribe;
  14. use App\Http\Models\UserSubscribeLog;
  15. use App\Http\Models\UserTrafficHourly;
  16. use App\Http\Models\VerifyCode;
  17. use DB;
  18. use Exception;
  19. use Illuminate\Console\Command;
  20. use Log;
  21. class AutoJob extends Command
  22. {
  23. protected $signature = 'autoJob';
  24. protected $description = '自动化任务';
  25. protected static $systemConfig;
  26. public function __construct()
  27. {
  28. parent::__construct();
  29. self::$systemConfig = Helpers::systemConfig();
  30. }
  31. /*
  32. * 警告:除非熟悉业务流程,否则不推荐更改以下执行顺序,随意变更以下顺序可能导致系统异常
  33. */
  34. public function handle()
  35. {
  36. $jobStartTime = microtime(TRUE);
  37. // 关闭超时未支付订单
  38. $this->closeOrders();
  39. //过期验证码、优惠券、邀请码无效化
  40. $this->expireCode();
  41. // 封禁访问异常的订阅链接
  42. $this->blockSubscribe();
  43. // 封禁账号
  44. $this->blockUsers();
  45. // 解封被封禁的账号
  46. $this->unblockUsers();
  47. // 端口回收与分配
  48. $this->dispatchPort();
  49. // 检测节点是否离线
  50. $this->checkNodeStatus();
  51. $jobEndTime = microtime(TRUE);
  52. $jobUsedTime = round(($jobEndTime-$jobStartTime), 4);
  53. Log::info('【'.$this->description.'】执行定时任务,耗时'.$jobUsedTime.'秒');
  54. }
  55. // 关闭超时未支付订单
  56. private function closeOrders()
  57. {
  58. // 关闭超时未支付的在线支付订单(在线支付收款二维码超过30分钟自动关闭,关闭后无法再支付,所以我们限制15分钟内必须付款)
  59. $paymentList = Payment::query()->with(['order', 'order.coupon'])->where('status', 0)->where('created_at', '<=', date("Y-m-d H:i:s", strtotime("-15 minutes")))->get();
  60. if($paymentList->isNotEmpty()){
  61. DB::beginTransaction();
  62. try{
  63. foreach($paymentList as $payment){
  64. // 关闭支付单
  65. Payment::query()->where('id', $payment->id)->update(['status' => -1]);
  66. // 关闭订单
  67. Order::query()->where('oid', $payment->oid)->update(['status' => -1]);
  68. // 退回优惠券
  69. if($payment->order->coupon_id){
  70. Coupon::query()->where('id', $payment->order->coupon_id)->update(['status' => 0]);
  71. Helpers::addCouponLog($payment->order->coupon_id, $payment->order->goods_id, $payment->oid, '订单超时未支付,自动退回');
  72. }
  73. }
  74. DB::commit();
  75. } catch(Exception $e){
  76. Log::info('【异常】自动关闭超时未支付订单:'.$e);
  77. DB::rollBack();
  78. }
  79. }
  80. }
  81. // 注册验证码自动置无效
  82. private function expireCode()
  83. {
  84. // 注册验证码自动置无效
  85. VerifyCode::query()->where('status', 0)->where('created_at', '<=', date('Y-m-d H:i:s', strtotime("-10 minutes")))->update(['status' => 2]);
  86. // 优惠券到期自动置无效
  87. Coupon::query()->where('status', 0)->where('available_end', '<=', time())->update(['status' => 2]);
  88. // 邀请码到期自动置无效
  89. Invite::query()->where('status', 0)->where('dateline', '<=', date('Y-m-d H:i:s'))->update(['status' => 2]);
  90. }
  91. // 封禁访问异常的订阅链接
  92. private function blockSubscribe()
  93. {
  94. if(self::$systemConfig['is_subscribe_ban']){
  95. $userList = User::query()->where('status', '>=', 0)->where('enable', 1)->get();
  96. foreach($userList as $user){
  97. $subscribe = UserSubscribe::query()->where('user_id', $user->id)->first();
  98. if($subscribe){
  99. // 24小时内不同IP的请求次数
  100. $request_times = UserSubscribeLog::query()->where('sid', $subscribe->id)->where('request_time', '>=', date("Y-m-d H:i:s", strtotime("-24 hours")))->distinct()->count('request_ip');
  101. if($request_times >= self::$systemConfig['subscribe_ban_times']){
  102. UserSubscribe::query()->where('id', $subscribe->id)->update(['status' => 0, 'ban_time' => time(), 'ban_desc' => '存在异常,自动封禁']);
  103. // 记录封禁日志
  104. $this->addUserBanLog($subscribe->user_id, 0, '【完全封禁订阅】-订阅24小时内请求异常');
  105. }
  106. }
  107. }
  108. }
  109. }
  110. // 封禁账号
  111. private function blockUsers()
  112. {
  113. // 封禁1小时内流量异常账号
  114. if(self::$systemConfig['is_traffic_ban']){
  115. $userList = User::query()->where('enable', 1)->where('status', '>=', 0)->where('ban_time', 0)->get();
  116. foreach($userList as $user){
  117. // 对管理员豁免
  118. if($user->is_admin){
  119. continue;
  120. }
  121. // 多往前取5分钟,防止数据统计任务执行时间过长导致没有数据
  122. $totalTraffic = UserTrafficHourly::query()->where('user_id', $user->id)->where('node_id', 0)->where('created_at', '>=', date('Y-m-d H:i:s', time()-3900))->sum('total');
  123. if($totalTraffic >= (self::$systemConfig['traffic_ban_value']*1073741824)){
  124. User::query()->where('id', $user->id)->update(['enable' => 0, 'ban_time' => strtotime(date('Y-m-d H:i:s', strtotime("+".self::$systemConfig['traffic_ban_time']." minutes")))]);
  125. // 写入日志
  126. $this->addUserBanLog($user->id, self::$systemConfig['traffic_ban_time'], '【临时封禁代理】-1小时内流量异常');
  127. }
  128. }
  129. }
  130. // 禁用流量超限用户
  131. $userList = User::query()->where('enable', 1)->where('status', '>=', 0)->where('ban_time', 0)->whereRaw("u + d >= transfer_enable")->get();
  132. foreach($userList as $user){
  133. User::query()->where('id', $user->id)->update(['enable' => 0]);
  134. // 写入日志
  135. $this->addUserBanLog($user->id, 0, '【封禁代理】-流量已用完');
  136. }
  137. }
  138. // 解封被临时封禁的账号
  139. private function unblockUsers()
  140. {
  141. // 解封被临时封禁的账号
  142. $userList = User::query()->where('enable', 0)->where('status', '>=', 0)->where('ban_time', '>', 0)->get();
  143. foreach($userList as $user){
  144. if($user->ban_time < time()){
  145. User::query()->where('id', $user->id)->update(['enable' => 1, 'ban_time' => 0]);
  146. // 写入操作日志
  147. $this->addUserBanLog($user->id, 0, '【自动解封】-临时封禁到期');
  148. }
  149. }
  150. // 可用流量大于已用流量也解封(比如:邀请返利自动加了流量)
  151. $userList = User::query()->where('enable', 0)->where('status', '>=', 0)->where('ban_time', 0)->where('expire_time', '>=', date('Y-m-d'))->whereRaw("u + d < transfer_enable")->get();
  152. foreach($userList as $user){
  153. User::query()->where('id', $user->id)->update(['enable' => 1]);
  154. // 写入操作日志
  155. $this->addUserBanLog($user->id, 0, '【自动解封】-有流量解封');
  156. }
  157. }
  158. // 端口回收与分配
  159. private function dispatchPort()
  160. {
  161. if(self::$systemConfig['auto_release_port']){
  162. ## 自动分配端口
  163. $userList = User::query()->where('enable', 1)->where('status', '>=', 0)->where('port', 0)->get();
  164. foreach($userList as $user){
  165. $port = self::$systemConfig['is_rand_port']? Helpers::getRandPort() : Helpers::getOnlyPort();
  166. User::query()->where('id', $user->id)->update(['port' => $port]);
  167. }
  168. ## 被封禁的账号自动释放端口
  169. User::query()->where('enable', 0)->where('status', -1)->where('port', '!=', 0)->update(['port' => 0]);
  170. ## 过期一个月的账户自动释放端口
  171. User::query()->where('enable', 0)->where('port', '!=', 0)->where('expire_time', '<=', date("Y-m-d", strtotime("-30 days")))->update(['port' => 0]);
  172. }
  173. }
  174. // 检测节点是否离线
  175. private function checkNodeStatus()
  176. {
  177. if(Helpers::systemConfig()['is_node_crash_warning']){
  178. $nodeList = SsNode::query()->where('is_transit', 0)->where('status', 1)->get();
  179. foreach($nodeList as $node){
  180. // 10分钟内无节点负载信息则认为是后端炸了
  181. $nodeTTL = SsNodeInfo::query()->where('node_id', $node->id)->where('log_time', '>=', strtotime("-10 minutes"))->orderBy('id', 'desc')->first();
  182. if(!$nodeTTL){
  183. ServerChan::send('节点异常警告', "节点**{$node->name}【{$node->ip}】**异常:**心跳异常,可能离线了**");
  184. }
  185. }
  186. }
  187. }
  188. /**
  189. * 添加用户封禁日志
  190. *
  191. * @param int $userId 用户ID
  192. * @param int $minutes 封禁时长,单位分钟
  193. * @param string $desc 封禁理由
  194. */
  195. private function addUserBanLog($userId, $minutes, $desc)
  196. {
  197. $log = new UserBanLog();
  198. $log->user_id = $userId;
  199. $log->minutes = $minutes;
  200. $log->desc = $desc;
  201. $log->save();
  202. }
  203. }