123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412 |
- <?php
- namespace App\Components;
- use App\Channels\BarkChannel;
- use App\Channels\ServerChanChannel;
- use App\Models\Config;
- use App\Models\CouponLog;
- use App\Models\Marketing;
- use App\Models\NotificationLog;
- use App\Models\SsConfig;
- use App\Models\User;
- use App\Models\UserBanedLog;
- use App\Models\UserCreditLog;
- use App\Models\UserDataModifyLog;
- use App\Models\UserSubscribe;
- use Cache;
- use DateTime;
- use NotificationChannels\BearyChat\BearyChatChannel;
- use NotificationChannels\Telegram\TelegramChannel;
- use Str;
- use Log;
- class Helpers
- {
- // 不生成的端口
- private static $denyPorts = [
- 1068, 1109, 1434, 3127, 3128, 3129, 3130, 3332, 4444, 5554, 6669, 8080, 8081, 8082, 8181, 8282, 9996, 17185, 24554, 35601, 60177, 60179,37886,
- ];
- // 加密方式
- public static function methodList()
- {
- return SsConfig::type(1)->get();
- }
- // 协议
- public static function protocolList()
- {
- return SsConfig::type(2)->get();
- }
- // 混淆
- public static function obfsList()
- {
- return SsConfig::type(3)->get();
- }
- // 生成用户的订阅码
- public static function makeSubscribeCode(): string
- {
- $code = Str::random();
- if (UserSubscribe::whereCode($code)->exists()) {
- $code = self::makeSubscribeCode();
- }
- return $code;
- }
- /**
- * 添加用户.
- *
- * @param string $email 用户邮箱
- * @param string $password 用户密码
- * @param int $transfer_enable 可用流量
- * @param int|null $date 可使用天数
- * @param int|null $inviter_id 邀请人
- * @param string|null $username 昵称
- * @return User
- */
- public static function addUser(string $email, string $password, int $transfer_enable, int $date = null, int $inviter_id = null, string $username = null): User
- {
- // //判断端口是否重复
- while (true){
- $port = self::getPort();
- if ($port == 37886){
- continue;
- }
- $retcount = User::where('port', $port)->count();
- if ($retcount > 0){
- continue;
- } else {
- break;
- }
- }
- return User::create([
- 'username' => $username ?? $email,
- 'email' => $email,
- 'password' => $password,
- 'port' => 0, // 生成一个可用端口
- 'passwd' => Str::random(),
- 'vmess_id' => Str::uuid(),
- 'method' => self::getDefaultMethod(),
- 'protocol' => self::getDefaultProtocol(),
- 'obfs' => self::getDefaultObfs(),
- 'transfer_enable' => $transfer_enable,
- 'enable' => 0,
- 'expired_at' => date('Y-m-d H:i:s', strtotime('+'.$date.' days')),
- 'user_group_id' => null,
- 'reg_ip' => IP::getClientIp(),
- 'inviter_id' => $inviter_id,
- ]);
- }
- public static function GetRandStr($length){
- //字符组合
- $str = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
- $len = strlen($str)-1;
- $randstr = '';
- for ($i=0;$i<$length;$i++) {
- $num=mt_rand(0,$len);
- $randstr .= $str[$num];
- }
- return $randstr;
- }
- // 获取一个有效端口
- public static function getPort(): int
- {
- if (sysConfig('is_rand_port')) {
- $port = self::getRandPort();
- } else {
- $port = (int) sysConfig('min_port');
- $exists_port = array_merge(User::where('port', '>=', $port)->pluck('port')->toArray(), self::$denyPorts);
- while (in_array($port, $exists_port, true)) {
- $port++;
- }
- }
- return $port;
- }
- // 获取一个随机端口
- private static function getRandPort(): int
- {
- $port = random_int(sysConfig('min_port'), sysConfig('max_port'));
- $exists_port = array_merge(
- User::where('port', '<>', 0)->pluck('port')->toArray(),
- self::$denyPorts
- );
- while (in_array($port, $exists_port, true)) {
- $port = random_int(sysConfig('min_port'), sysConfig('max_port'));
- }
- return $port;
- }
- // 获取默认加密方式
- public static function getDefaultMethod(): string
- {
- $config = SsConfig::default()->type(1)->first();
- return $config->name ?? 'aes-256-cfb';
- }
- // 获取默认协议
- public static function getDefaultProtocol(): string
- {
- $config = SsConfig::default()->type(2)->first();
- return $config->name ?? 'origin';
- }
- // 获取默认混淆
- public static function getDefaultObfs(): string
- {
- $config = SsConfig::default()->type(3)->first();
- return $config->name ?? 'plain';
- }
- // 获取系统配置
- public static function cacheSysConfig($name)
- {
- $notifications = [
- 'account_expire_notification',
- 'data_anomaly_notification',
- 'data_exhaust_notification',
- 'node_blocked_notification',
- 'node_daily_notification',
- 'node_offline_notification',
- 'password_reset_notification',
- 'payment_received_notification',
- 'ticket_closed_notification',
- 'ticket_created_notification',
- 'ticket_replied_notification',
- ];
- if ($name === 'is_onlinePay') {
- $value = sysConfig('is_AliPay') || sysConfig('is_QQPay') || sysConfig('is_WeChatPay') || sysConfig('is_otherPay');
- Cache::tags('sysConfig')->put('is_onlinePay', $value);
- } else {
- if (in_array($name, $notifications, true)) {
- // Log::info(json_decode(Config::find($name)->value, true));
- $value = self::setChannel(json_decode(Config::find($name)->value, true));
- } else {
- $value = Config::find($name)->value;
- }
- Cache::tags('sysConfig')->put($name, $value ?? false);
- }
- return $value;
- }
- private static function setChannel($channels)
- {
- $options = [
- 'telegram' => TelegramChannel::class,
- 'beary' => BearyChatChannel::class,
- 'bark' => BarkChannel::class,
- 'serverChan' => ServerChanChannel::class,
- ];
- foreach ($options as $option => $str) {
- if (($key = array_search($option, $channels, true)) !== false) {
- $channels[$key] = $str;
- }
- }
- return $channels;
- }
- public static function daysToNow($date): int
- {
- return (new DateTime())->diff(new DateTime($date))->days;
- }
- /**
- * 添加通知推送日志.
- *
- * @param string $title 标题
- * @param string $content 内容
- * @param int $type 发送类型
- * @param string $address 收信方
- * @param int $status 投递状态
- * @param string $error 投递失败时记录的异常信息
- *
- * @return int
- */
- public static function addNotificationLog(string $title, string $content, int $type, $address = 'admin', $status = 1, $error = ''): int
- {
- $log = new NotificationLog();
- $log->type = $type;
- $log->address = $address;
- $log->title = $title;
- $log->content = $content;
- $log->status = $status;
- $log->error = $error;
- $log->save();
- return $log->id;
- }
- /**
- * 添加优惠券操作日志.
- *
- * @param string $description 备注
- * @param int $couponId 优惠券ID
- * @param int|null $goodsId 商品ID
- * @param int|null $orderId 订单ID
- *
- * @return bool
- */
- public static function addCouponLog($description, $couponId, $goodsId = null, $orderId = null): bool
- {
- $log = new CouponLog();
- $log->coupon_id = $couponId;
- $log->goods_id = $goodsId;
- $log->order_id = $orderId;
- $log->description = $description;
- return $log->save();
- }
- /**
- * 记录余额操作日志.
- *
- * @param int $userId 用户ID
- * @param int|null $orderId 订单ID
- * @param int $before 记录前余额
- * @param int $after 记录后余额
- * @param int $amount 发生金额
- * @param string $description 描述
- *
- * @return bool
- */
- public static function addUserCreditLog($userId, $orderId, $before, $after, $amount, $description = ''): bool
- {
- $log = new UserCreditLog();
- $log->user_id = $userId;
- $log->order_id = $orderId;
- $log->before = $before;
- $log->after = $after;
- $log->amount = $amount;
- $log->description = $description;
- $log->created_at = date('Y-m-d H:i:s');
- return $log->save();
- }
- /**
- * 记录流量变动日志.
- *
- * @param int $userId 用户ID
- * @param int|null $orderId 订单ID
- * @param int $before 记录前的值
- * @param int $after 记录后的值
- * @param string $description 描述
- *
- * @return bool
- */
- public static function addUserTrafficModifyLog($userId, $orderId, $before, $after, $description = ''): bool
- {
- $log = new UserDataModifyLog();
- $log->user_id = $userId;
- $log->order_id = $orderId;
- $log->before = $before;
- $log->after = $after;
- $log->description = $description;
- return $log->save();
- }
- /**
- * 添加用户封禁日志.
- *
- * @param int $userId 用户ID
- * @param int $time 封禁时长,单位分钟
- * @param string $description 封禁理由
- *
- * @return bool
- */
- public static function addUserBanLog(int $userId, int $time, string $description)
- {
- $log = new UserBanedLog();
- $log->user_id = $userId;
- $log->time = $time;
- $log->description = $description;
- return $log->save();
- }
- /**
- * 推销信息推送
- *
- * @param int $type 渠道类型
- * @param string $title 标题
- * @param string $content 内容
- * @param int $status 状态
- * @param string $error 报错
- * @param string $receiver 收件人
- * @return int
- */
- public static function addMarketing(int $type, string $title, string $content, int $status = 1, string $error = '', string $receiver = ''): int
- {
- $marketing = new Marketing();
- $marketing->type = $type;
- $marketing->receiver = $receiver;
- $marketing->title = $title;
- $marketing->content = $content;
- $marketing->error = $error;
- $marketing->status = $status;
- return $marketing->save();
- }
- //重数组中找到最小的,和最大大
- public static function phpMaxMin($arr = [],$keys = ''){
- $max['key'] = '';
- $max['value'] = '';
- $min['key'] = '';
- $min['value'] = '';
- foreach ($arr as $key => $val){
- if($max['key'] === ''){
- $max['key'] = $key;
- $max['value'] = $val[$keys];
- }
- if((int)$max['value'] < $val[$keys]){
- $max['key'] = $key;
- $max['value'] = $val[$keys];
- }
- if($min['key'] === ''){
- $min['key'] = $key;
- $min['value'] = $val[$keys];
- }
- if((int)$min['value'] > $val[$keys]){
- $min['key'] = $key;
- $min['value'] = $val[$keys];
- }
- }
- $array['max'] = $max;
- $array['min'] = $min;
- return $array;
- }
- }
|