| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366 |
- <?php
- namespace App\Console\Commands;
- use Illuminate\Console\Command;
- use App\Http\Models\Coupon;
- use App\Http\Models\CouponLog;
- use App\Http\Models\Invite;
- use App\Http\Models\Order;
- use App\Http\Models\Payment;
- use App\Http\Models\Config;
- use App\Http\Models\User;
- use App\Http\Models\UserLabel;
- use App\Http\Models\UserBanLog;
- use App\Http\Models\UserSubscribe;
- use App\Http\Models\UserSubscribeLog;
- use App\Http\Models\UserTrafficHourly;
- use Log;
- use DB;
- class AutoJob extends Command
- {
- protected $signature = 'autoJob';
- protected $description = '自动化任务';
- protected static $config;
- public function __construct()
- {
- parent::__construct();
- self::$config = $this->systemConfig();
- }
- /*
- * 以下操作顺序如果随意挪动可能导致出现异常
- */
- public function handle()
- {
- $jobStartTime = microtime(true);
- // 优惠券到期自动置无效
- $this->expireCoupon();
- // 邀请码到期自动置无效
- $this->expireInvite();
- // 封禁访问异常的订阅链接
- $this->blockSubscribe();
- // 封禁账号
- $this->blockUsers();
- // 自动移除被封禁账号的标签
- $this->removeUserLabels();
- // 自动解封被封禁的账号
- $this->unblockUsers();
- // 端口回收与分配
- $this->dispatchPort();
- // 关闭超时未支付订单
- $this->closeOrder();
- $jobEndTime = microtime(true);
- $jobUsedTime = round(($jobEndTime - $jobStartTime), 4);
- Log::info('执行定时任务【' . $this->description . '】,耗时' . $jobUsedTime . '秒');
- }
- // 优惠券到期自动置无效
- private function expireCoupon()
- {
- $couponList = Coupon::query()->where('status', 0)->where('available_end', '<=', time())->get();
- if (!$couponList->isEmpty()) {
- foreach ($couponList as $coupon) {
- Coupon::query()->where('id', $coupon->id)->update(['status' => 2]);
- }
- }
- }
- // 邀请码到期自动置无效
- private function expireInvite()
- {
- $inviteList = Invite::query()->where('status', 0)->where('dateline', '<=', date('Y-m-d H:i:s'))->get();
- if (!$inviteList->isEmpty()) {
- foreach ($inviteList as $invite) {
- Invite::query()->where('id', $invite->id)->update(['status' => 2]);
- }
- }
- }
- // 封禁访问异常的订阅链接
- private function blockSubscribe()
- {
- if (self::$config['is_subscribe_ban']) {
- $subscribeList = UserSubscribe::query()->where('status', 1)->get();
- if (!$subscribeList->isEmpty()) {
- foreach ($subscribeList as $subscribe) {
- // 24小时内不同IP的请求次数
- $request_times = UserSubscribeLog::query()->where('sid', $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::$config['subscribe_ban_times']) {
- UserSubscribe::query()->where('id', $subscribe->id)->update(['status' => 0, 'ban_time' => time(), 'ban_desc' => '存在异常,自动封禁']);
- // 记录封禁日志
- $this->addUserBanLog($subscribe->user_id, 0, '【完全封禁订阅】-订阅24小时内请求异常');
- }
- }
- }
- }
- }
- // 封禁账号
- private function blockUsers()
- {
- // 过期用户处理
- $userList = User::query()->where('status', '>=', 0)->where('enable', 1)->where('expire_time', '<', date('Y-m-d'))->get();
- if (!$userList->isEmpty()) {
- foreach ($userList as $user) {
- if (self::$config['is_ban_status']) {
- User::query()->where('id', $user->id)->update([
- 'u' => 0,
- 'd' => 0,
- 'transfer_enable' => 0,
- 'enable' => 0,
- 'traffic_reset_day' => 0,
- 'ban_time' => 0,
- 'status' => -1
- ]);
- $this->addUserBanLog($user->id, 0, '【禁止登录,清空账户】-账号已过期');
- } else {
- User::query()->where('id', $user->id)->update([
- 'u' => 0,
- 'd' => 0,
- 'transfer_enable' => 0,
- 'enable' => 0,
- 'traffic_reset_day' => 0,
- 'ban_time' => 0
- ]);
- $this->addUserBanLog($user->id, 0, '【封禁代理,清空账户】-账号已过期');
- }
- }
- }
- // 封禁1小时内流量异常账号
- if (self::$config['is_traffic_ban']) {
- $userList = User::query()->where('status', '>=', 0)->where('enable', 1)->where('ban_time', 0)->get();
- if (!$userList->isEmpty()) {
- foreach ($userList as $user) {
- // 多往前取5分钟,防止数据统计任务执行时间过长导致没有数据
- $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');
- if ($totalTraffic >= (self::$config['traffic_ban_value'] * 1024 * 1024 * 1024)) {
- User::query()->where('id', $user->id)->update(['enable' => 0, 'ban_time' => strtotime(date('Y-m-d H:i:s', strtotime("+" . self::$config['traffic_ban_time'] . " minutes")))]);
- // 写入日志
- $this->addUserBanLog($user->id, self::$config['traffic_ban_time'], '【临时封禁代理】-1小时内流量异常');
- }
- }
- }
- }
- // 禁用流量超限用户
- $userList = User::query()->where('status', '>=', 0)->where('enable', 1)->where('ban_time', 0)->whereRaw("u + d >= transfer_enable")->get();
- if (!$userList->isEmpty()) {
- foreach ($userList as $user) {
- User::query()->where('id', $user->id)->update(['enable' => 0]);
- // 写入日志
- $this->addUserBanLog($user->id, 0, '【封禁代理】-流量已用完');
- }
- }
- }
- // 自动清空过期的账号的标签和流量(临时封禁不移除)
- private function removeUserLabels()
- {
- $userList = User::query()->where('enable', 0)->where('ban_time', 0)->where('expire_time', '<', date('Y-m-d'))->get();
- if (!$userList->isEmpty()) {
- foreach ($userList as $user) {
- UserLabel::query()->where('user_id', $user->id)->delete();
- User::query()->where('id', $user->id)->update([
- 'u' => 0,
- 'd' => 0,
- 'transfer_enable' => 0,
- 'traffic_reset_day' => 0
- ]);
- }
- }
- }
- // 解封账号
- private function unblockUsers()
- {
- // 解封被临时封禁的账号
- $userList = User::query()->where('status', '>=', 0)->where('enable', 0)->where('ban_time', '>', 0)->get();
- foreach ($userList as $user) {
- if ($user->ban_time < time()) {
- User::query()->where('id', $user->id)->update(['enable' => 1, 'ban_time' => 0]);
- // 写入操作日志
- $this->addUserBanLog($user->id, 0, '【自动解封】-临时封禁到期');
- }
- }
- // 可用流量大于已用流量也解封(比如:邀请返利自动加了流量)
- $userList = User::query()->where('status', '>=', 0)->where('enable', 0)->where('ban_time', 0)->where('expire_time', '>=', date('Y-m-d'))->whereRaw("u + d < transfer_enable")->get();
- if (!$userList->isEmpty()) {
- foreach ($userList as $user) {
- User::query()->where('id', $user->id)->update(['enable' => 1]);
- // 写入操作日志
- $this->addUserBanLog($user->id, 0, '【自动解封】-有流量解封');
- }
- }
- }
- // 端口回收与分配
- private function dispatchPort()
- {
- if (self::$config['auto_release_port']) {
- ## 自动分配端口
- $userList = User::query()->where('status', '>=', 0)->where('enable', 1)->where('port', 0)->get();
- if (!$userList->isEmpty()) {
- foreach ($userList as $user) {
- $port = self::$config['is_rand_port'] ? $this->getRandPort() : $this->getOnlyPort();
- User::query()->where('id', $user->id)->update(['port' => $port]);
- }
- }
- ## 被封禁的账号自动释放端口
- $userList = User::query()->where('status', -1)->where('enable', 0)->get();
- if (!$userList->isEmpty()) {
- foreach ($userList as $user) {
- if ($user->port) {
- User::query()->where('id', $user->id)->update(['port' => 0]);
- }
- }
- }
- ## 过期一个月的账户自动释放端口
- $userList = User::query()->where('enable', 0)->get();
- if (!$userList->isEmpty()) {
- foreach ($userList as $user) {
- if ($user->port) {
- $overdueDays = floor((strtotime(date('Y-m-d H:i:s')) - strtotime($user->expire_time)) / 86400);
- if ($overdueDays > 30) {
- User::query()->where('id', $user->id)->update(['port' => 0]);
- }
- }
- }
- }
- }
- }
- // 自动关闭超时未支付订单
- private function closeOrder()
- {
- // 自动关闭超时未支付的有赞云订单(有赞云收款二维码超过30分钟自动关闭,我们限制15分钟内必须付款)
- $paymentList = Payment::query()->with(['order', 'order.coupon'])->where('status', 0)->where('created_at', '<=', date("Y-m-d H:i:s", strtotime("-15 minutes")))->get();
- if (!$paymentList->isEmpty()) {
- DB::beginTransaction();
- try {
- foreach ($paymentList as $payment) {
- // 关闭支付单
- Payment::query()->where('id', $payment->id)->update(['status' => -1]);
- // 关闭订单
- Order::query()->where('oid', $payment->oid)->update(['status' => -1]);
- // 退回优惠券
- if ($payment->order->coupon_id) {
- Coupon::query()->where('id', $payment->order->coupon_id)->update(['status' => 0]);
- $this->addCouponLog($payment->order->coupon_id, $payment->order->goods_id, $payment->oid, '订单超时未支付,自动退回');
- }
- }
- DB::commit();
- } catch (\Exception $e) {
- Log::info('【异常】自动关闭超时未支付订单:' . $e);
- DB::rollBack();
- }
- }
- }
- /**
- * 添加用户封禁日志
- *
- * @param int $userId 用户ID
- * @param int $minutes 封禁时长,单位分钟
- * @param string $desc 封禁理由
- */
- private function addUserBanLog($userId, $minutes, $desc)
- {
- $log = new UserBanLog();
- $log->user_id = $userId;
- $log->minutes = $minutes;
- $log->desc = $desc;
- $log->save();
- }
- /**
- * 添加优惠券操作日志
- *
- * @param int $couponId 优惠券ID
- * @param int $goodsId 商品ID
- * @param int $orderId 订单ID
- * @param string $desc 备注
- */
- private function addCouponLog($couponId, $goodsId, $orderId, $desc = '')
- {
- $couponLog = new CouponLog();
- $couponLog->coupon_id = $couponId;
- $couponLog->goods_id = $goodsId;
- $couponLog->order_id = $orderId;
- $couponLog->desc = $desc;
- $couponLog->save();
- }
- // 系统配置
- private function systemConfig()
- {
- $config = Config::query()->get();
- $data = [];
- foreach ($config as $vo) {
- $data[$vo->name] = $vo->value;
- }
- return $data;
- }
- // 获取一个随机端口
- public function getRandPort()
- {
- $config = $this->systemConfig();
- $port = mt_rand($config['min_port'], $config['max_port']);
- $deny_port = [1068, 1109, 1434, 3127, 3128, 3129, 3130, 3332, 4444, 5554, 6669, 8080, 8081, 8082, 8181, 8282, 9996, 17185, 24554, 35601, 60177, 60179]; // 不生成的端口
- $exists_port = User::query()->pluck('port')->toArray();
- if (in_array($port, $exists_port) || in_array($port, $deny_port)) {
- $port = $this->getRandPort();
- }
- return $port;
- }
- // 获取一个端口
- public function getOnlyPort()
- {
- $config = $this->systemConfig();
- $port = $config['min_port'];
- $deny_port = [1068, 1109, 1434, 3127, 3128, 3129, 3130, 3332, 4444, 5554, 6669, 8080, 8081, 8082, 8181, 8282, 9996, 17185, 24554, 35601, 60177, 60179]; // 不生成的端口
- $exists_port = User::query()->where('port', '>=', $config['min_port'])->where('port', '<=', $config['max_port'])->pluck('port')->toArray();
- while (in_array($port, $exists_port) || in_array($port, $deny_port)) {
- $port = $port + 1;
- }
- return $port;
- }
- }
|