123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300 |
- <?php
- namespace App\Console\Commands;
- use App\Components\Helpers;
- use App\Components\PushNotification;
- use App\Http\Models\Config;
- use App\Http\Models\Coupon;
- use App\Http\Models\Invite;
- use App\Http\Models\Order;
- use App\Http\Models\Payment;
- use App\Http\Models\SsNode;
- use App\Http\Models\SsNodeInfo;
- use App\Http\Models\User;
- use App\Http\Models\UserBanLog;
- use App\Http\Models\UserSubscribe;
- use App\Http\Models\UserSubscribeLog;
- use App\Http\Models\UserTrafficHourly;
- use App\Http\Models\VerifyCode;
- use Cache;
- use DB;
- use Exception;
- use Illuminate\Console\Command;
- use Log;
- class AutoJob extends Command
- {
- protected static $systemConfig;
- protected $signature = 'autoJob';
- protected $description = '自动化任务';
- public function __construct()
- {
- parent::__construct();
- self::$systemConfig = Helpers::systemConfig();
- }
-
- public function handle()
- {
- $jobStartTime = microtime(TRUE);
-
- $this->closePayments();
-
- $this->closeOrders();
-
- $this->expireCode();
-
- $this->blockSubscribe();
-
- $this->blockUsers();
-
- $this->unblockUsers();
-
- $this->dispatchPort();
-
- $this->checkNodeStatus();
-
- if(self::$systemConfig['maintenance_mode']){
- if(strtotime(self::$systemConfig['maintenance_time']) < time()){
- Config::query()->whereName('maintenance_mode')->update(['value' => 0]);
- Config::query()->whereName('maintenance_time')->update(['value' => '']);
- }
- }
- $jobEndTime = microtime(TRUE);
- $jobUsedTime = round(($jobEndTime-$jobStartTime), 4);
- Log::info('---【'.$this->description.'】完成---,耗时'.$jobUsedTime.'秒');
- }
-
- private function closePayments()
- {
-
- $paymentList = Payment::query()->whereStatus(0)->where('created_at', '<=', date("Y-m-d H:i:s", strtotime("-15 minutes")))->get();
- if($paymentList->isNotEmpty()){
- DB::beginTransaction();
- try{
- foreach($paymentList as $payment){
-
- Payment::query()->whereId($payment->id)->update(['status' => -1]);
-
- Order::query()->whereOid($payment->oid)->update(['status' => -1]);
-
- if($payment->order->coupon_id){
- Coupon::query()->whereId($payment->order->coupon_id)->update(['status' => 0]);
- Helpers::addCouponLog($payment->order->coupon_id, $payment->order->goods_id, $payment->oid, '在线订单超时未支付,自动退回');
- }
- }
- DB::commit();
- }catch(Exception $e){
- Log::info('【异常】自动关闭超时未支付在线订单:'.$e);
- DB::rollBack();
- }
- }
- }
-
- private function closeOrders()
- {
-
- $orderList = Order::query()->whereStatus(0)->where('created_at', '<=', date("Y-m-d H:i:s", strtotime("-30 minutes")))->get();
- if($orderList->isNotEmpty()){
- DB::beginTransaction();
- try{
- foreach($orderList as $order){
-
- Order::query()->whereOid($order->oid)->update(['status' => -1]);
-
- if($order->coupon_id){
- Coupon::query()->whereId($order->coupon_id)->update(['status' => 0]);
- Helpers::addCouponLog($order->coupon_id, $order->goods_id, $order->oid, '订单超时未支付,自动退回');
- }
- }
- DB::commit();
- }catch(Exception $e){
- Log::info('【异常】自动关闭超时未支付订单:'.$e);
- DB::rollBack();
- }
- }
- }
-
- private function expireCode()
- {
-
- VerifyCode::query()->whereStatus(0)->where('created_at', '<=', date('Y-m-d H:i:s', strtotime("-10 minutes")))->update(['status' => 2]);
-
- Coupon::query()->whereStatus(0)->where('available_end', '<=', time())->update(['status' => 2]);
-
- Invite::query()->whereStatus(0)->where('dateline', '<=', date('Y-m-d H:i:s'))->update(['status' => 2]);
- }
-
- private function blockSubscribe()
- {
- if(self::$systemConfig['is_subscribe_ban']){
- $userList = User::query()->where('status', '>=', 0)->whereEnable(1)->get();
- foreach($userList as $user){
- $subscribe = UserSubscribe::query()->whereUserId($user->id)->first();
- if($subscribe){
-
- $request_times = UserSubscribeLog::query()->whereSid($subscribe->id)->where('request_time', '>=', date("Y-m-d H:i:s", strtotime("-24 hours")))->distinct('request_ip')->count('request_ip');
- if($request_times >= self::$systemConfig['subscribe_ban_times']){
- UserSubscribe::query()->whereId($subscribe->id)->update(['status' => 0, 'ban_time' => time(), 'ban_desc' => '存在异常,自动封禁']);
-
- $this->addUserBanLog($subscribe->user_id, 0, '【完全封禁订阅】-订阅24小时内请求异常');
- }
- }
- }
- }
- }
-
- private function addUserBanLog($userId, $minutes, $desc)
- {
- $log = new UserBanLog();
- $log->user_id = $userId;
- $log->minutes = $minutes;
- $log->desc = $desc;
- $log->save();
- }
-
- private function blockUsers()
- {
-
- if(self::$systemConfig['is_traffic_ban']){
- $userList = User::query()->whereEnable(1)->where('status', '>=', 0)->whereBanTime(0)->get();
- foreach($userList as $user){
-
- if($user->is_admin){
- continue;
- }
-
- $totalTraffic = UserTrafficHourly::query()->whereUserId($user->id)->whereNodeId(0)->where('created_at', '>=', date('Y-m-d H:i:s', time()-3900))->sum('total');
- if($totalTraffic >= (self::$systemConfig['traffic_ban_value']*1073741824)){
- User::query()->whereId($user->id)->update(['enable' => 0, 'ban_time' => strtotime(date('Y-m-d H:i:s', strtotime("+".self::$systemConfig['traffic_ban_time']." minutes")))]);
-
- $this->addUserBanLog($user->id, self::$systemConfig['traffic_ban_time'], '【临时封禁代理】-1小时内流量异常');
- }
- }
- }
-
- $userList = User::query()->whereEnable(1)->where('status', '>=', 0)->whereBanTime(0)->whereRaw("u + d >= transfer_enable")->get();
- foreach($userList as $user){
- User::query()->whereId($user->id)->update(['enable' => 0]);
-
- $this->addUserBanLog($user->id, 0, '【封禁代理】-流量已用完');
- }
- }
-
- private function unblockUsers()
- {
-
- $userList = User::query()->whereEnable(0)->where('status', '>=', 0)->where('ban_time', '>', 0)->get();
- foreach($userList as $user){
- if($user->ban_time < time()){
- User::query()->whereId($user->id)->update(['enable' => 1, 'ban_time' => 0]);
-
- $this->addUserBanLog($user->id, 0, '【自动解封】-临时封禁到期');
- }
- }
-
- $userList = User::query()->whereEnable(0)->where('status', '>=', 0)->whereBanTime(0)->where('expire_time', '>=', date('Y-m-d'))->whereRaw("u + d < transfer_enable")->get();
- foreach($userList as $user){
- User::query()->whereId($user->id)->update(['enable' => 1]);
-
- $this->addUserBanLog($user->id, 0, '【自动解封】-有流量解封');
- }
- }
-
- private function dispatchPort()
- {
- if(self::$systemConfig['auto_release_port']){
-
- $userList = User::query()->whereEnable(1)->where('status', '>=', 0)->wherePort(0)->get();
- foreach($userList as $user){
- $port = self::$systemConfig['is_rand_port']? Helpers::getRandPort() : Helpers::getOnlyPort();
- User::query()->whereId($user->id)->update(['port' => $port]);
- }
-
- User::query()->whereEnable(0)->whereStatus(-1)->where('port', '!=', 0)->update(['port' => 0]);
-
- User::query()->whereEnable(0)->where('port', '!=', 0)->where('expire_time', '<=', date("Y-m-d", strtotime("-30 days")))->update(['port' => 0]);
- }
- }
-
- private function checkNodeStatus()
- {
- if(self::$systemConfig['is_node_offline']){
- $nodeList = SsNode::query()->whereIsTransit(0)->whereStatus(1)->get();
- foreach($nodeList as $node){
-
- $nodeTTL = SsNodeInfo::query()->whereNodeId($node->id)->where('log_time', '>=', strtotime("-10 minutes"))->orderBy('id', 'desc')->doesntExist();
- if($nodeTTL){
- if(self::$systemConfig['offline_check_times']){
-
- $cacheKey = 'offline_check_times'.$node->id;
- if(Cache::has($cacheKey)){
- $times = Cache::get($cacheKey);
- }else{
-
- Cache::put($cacheKey, 1, 86400);
- $times = 1;
- }
- if($times < self::$systemConfig['offline_check_times']){
- Cache::increment($cacheKey);
- PushNotification::send('节点异常警告', "节点**{$node->name}【{$node->ip}】**异常:**心跳异常,可能离线了**");
- }
- }
- }
- }
- }
- }
- }
|