Helpers.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. <?php
  2. namespace App\Components;
  3. use App\Channels\BarkChannel;
  4. use App\Channels\ServerChanChannel;
  5. use App\Models\Config;
  6. use App\Models\CouponLog;
  7. use App\Models\Marketing;
  8. use App\Models\NotificationLog;
  9. use App\Models\SsConfig;
  10. use App\Models\User;
  11. use App\Models\UserBanedLog;
  12. use App\Models\UserCreditLog;
  13. use App\Models\UserDataModifyLog;
  14. use App\Models\UserSubscribe;
  15. use Cache;
  16. use DateTime;
  17. use NotificationChannels\BearyChat\BearyChatChannel;
  18. use NotificationChannels\Telegram\TelegramChannel;
  19. use Str;
  20. class Helpers
  21. {
  22. // 不生成的端口
  23. private static $denyPorts = [
  24. 1068, 1109, 1434, 3127, 3128, 3129, 3130, 3332, 4444, 5554, 6669, 8080, 8081, 8082, 8181, 8282, 9996, 17185, 24554, 35601, 60177, 60179,
  25. ];
  26. // 加密方式
  27. public static function methodList()
  28. {
  29. return SsConfig::type(1)->get();
  30. }
  31. // 协议
  32. public static function protocolList()
  33. {
  34. return SsConfig::type(2)->get();
  35. }
  36. // 混淆
  37. public static function obfsList()
  38. {
  39. return SsConfig::type(3)->get();
  40. }
  41. // 生成用户的订阅码
  42. public static function makeSubscribeCode(): string
  43. {
  44. $code = Str::random();
  45. if (UserSubscribe::whereCode($code)->exists()) {
  46. $code = self::makeSubscribeCode();
  47. }
  48. return $code;
  49. }
  50. /**
  51. * 添加用户.
  52. *
  53. * @param string $email 用户邮箱
  54. * @param string $password 用户密码
  55. * @param int $transfer_enable 可用流量
  56. * @param int|null $date 可使用天数
  57. * @param int|null $inviter_id 邀请人
  58. * @param string|null $username 昵称
  59. * @return User
  60. */
  61. public static function addUser(string $email, string $password, int $transfer_enable, int $date = null, int $inviter_id = null, string $username = null): User
  62. {
  63. return User::create([
  64. 'username' => $username ?? $email,
  65. 'email' => $email,
  66. 'password' => $password,
  67. 'port' => self::getPort(), // 生成一个可用端口
  68. 'passwd' => Str::random(),
  69. 'vmess_id' => Str::uuid(),
  70. 'method' => self::getDefaultMethod(),
  71. 'protocol' => self::getDefaultProtocol(),
  72. 'obfs' => self::getDefaultObfs(),
  73. 'transfer_enable' => $transfer_enable,
  74. 'expired_at' => date('Y-m-d', strtotime('+'.$date.' days')),
  75. 'user_group_id' => null,
  76. 'reg_ip' => IP::getClientIp(),
  77. 'inviter_id' => $inviter_id,
  78. ]);
  79. }
  80. // 获取一个有效端口
  81. public static function getPort(): int
  82. {
  83. if (sysConfig('is_rand_port')) {
  84. $port = self::getRandPort();
  85. } else {
  86. $port = (int) sysConfig('min_port');
  87. $exists_port = array_merge(User::where('port', '>=', $port)->pluck('port')->toArray(), self::$denyPorts);
  88. while (in_array($port, $exists_port, true)) {
  89. $port++;
  90. }
  91. }
  92. return $port;
  93. }
  94. // 获取一个随机端口
  95. private static function getRandPort(): int
  96. {
  97. $port = random_int(sysConfig('min_port'), sysConfig('max_port'));
  98. $exists_port = array_merge(
  99. User::where('port', '<>', 0)->pluck('port')->toArray(),
  100. self::$denyPorts
  101. );
  102. while (in_array($port, $exists_port, true)) {
  103. $port = random_int(sysConfig('min_port'), sysConfig('max_port'));
  104. }
  105. return $port;
  106. }
  107. // 获取默认加密方式
  108. public static function getDefaultMethod(): string
  109. {
  110. $config = SsConfig::default()->type(1)->first();
  111. return $config->name ?? 'aes-256-cfb';
  112. }
  113. // 获取默认协议
  114. public static function getDefaultProtocol(): string
  115. {
  116. $config = SsConfig::default()->type(2)->first();
  117. return $config->name ?? 'origin';
  118. }
  119. // 获取默认混淆
  120. public static function getDefaultObfs(): string
  121. {
  122. $config = SsConfig::default()->type(3)->first();
  123. return $config->name ?? 'plain';
  124. }
  125. // 获取系统配置
  126. public static function cacheSysConfig($name)
  127. {
  128. $notifications = [
  129. 'account_expire_notification',
  130. 'data_anomaly_notification',
  131. 'data_exhaust_notification',
  132. 'node_blocked_notification',
  133. 'node_daily_notification',
  134. 'node_offline_notification',
  135. 'password_reset_notification',
  136. 'payment_received_notification',
  137. 'ticket_closed_notification',
  138. 'ticket_created_notification',
  139. 'ticket_replied_notification',
  140. ];
  141. if ($name === 'is_onlinePay') {
  142. $value = sysConfig('is_AliPay') || sysConfig('is_QQPay') || sysConfig('is_WeChatPay') || sysConfig('is_otherPay');
  143. Cache::tags('sysConfig')->put('is_onlinePay', $value);
  144. } else {
  145. if (in_array($name, $notifications, true)) {
  146. $value = self::setChannel(Config::find($name)->value);
  147. } else {
  148. $value = Config::find($name)->value;
  149. }
  150. Cache::tags('sysConfig')->put($name, $value ?? false);
  151. }
  152. return $value;
  153. }
  154. private static function setChannel(array $channels)
  155. {
  156. $options = [
  157. 'telegram' => TelegramChannel::class,
  158. 'beary' => BearyChatChannel::class,
  159. 'bark' => BarkChannel::class,
  160. 'serverChan' => ServerChanChannel::class,
  161. ];
  162. foreach ($options as $option => $str) {
  163. if (($key = array_search($option, $channels, true)) !== false) {
  164. $channels[$key] = $str;
  165. }
  166. }
  167. return $channels;
  168. }
  169. public static function daysToNow($date): int
  170. {
  171. return (new DateTime())->diff(new DateTime($date))->days;
  172. }
  173. /**
  174. * 添加通知推送日志.
  175. *
  176. * @param string $title 标题
  177. * @param string $content 内容
  178. * @param int $type 发送类型
  179. * @param string $address 收信方
  180. * @param int $status 投递状态
  181. * @param string $error 投递失败时记录的异常信息
  182. *
  183. * @return int
  184. */
  185. public static function addNotificationLog(string $title, string $content, int $type, $address = 'admin', $status = 1, $error = ''): int
  186. {
  187. $log = new NotificationLog();
  188. $log->type = $type;
  189. $log->address = $address;
  190. $log->title = $title;
  191. $log->content = $content;
  192. $log->status = $status;
  193. $log->error = $error;
  194. $log->save();
  195. return $log->id;
  196. }
  197. /**
  198. * 添加优惠券操作日志.
  199. *
  200. * @param string $description 备注
  201. * @param int $couponId 优惠券ID
  202. * @param int|null $goodsId 商品ID
  203. * @param int|null $orderId 订单ID
  204. *
  205. * @return bool
  206. */
  207. public static function addCouponLog($description, $couponId, $goodsId = null, $orderId = null): bool
  208. {
  209. $log = new CouponLog();
  210. $log->coupon_id = $couponId;
  211. $log->goods_id = $goodsId;
  212. $log->order_id = $orderId;
  213. $log->description = $description;
  214. return $log->save();
  215. }
  216. /**
  217. * 记录余额操作日志.
  218. *
  219. * @param int $userId 用户ID
  220. * @param int|null $orderId 订单ID
  221. * @param int $before 记录前余额
  222. * @param int $after 记录后余额
  223. * @param int $amount 发生金额
  224. * @param string $description 描述
  225. *
  226. * @return bool
  227. */
  228. public static function addUserCreditLog($userId, $orderId, $before, $after, $amount, $description = ''): bool
  229. {
  230. $log = new UserCreditLog();
  231. $log->user_id = $userId;
  232. $log->order_id = $orderId;
  233. $log->before = $before;
  234. $log->after = $after;
  235. $log->amount = $amount;
  236. $log->description = $description;
  237. $log->created_at = date('Y-m-d H:i:s');
  238. return $log->save();
  239. }
  240. /**
  241. * 记录流量变动日志.
  242. *
  243. * @param int $userId 用户ID
  244. * @param int|null $orderId 订单ID
  245. * @param int $before 记录前的值
  246. * @param int $after 记录后的值
  247. * @param string $description 描述
  248. *
  249. * @return bool
  250. */
  251. public static function addUserTrafficModifyLog($userId, $orderId, $before, $after, $description = ''): bool
  252. {
  253. $log = new UserDataModifyLog();
  254. $log->user_id = $userId;
  255. $log->order_id = $orderId;
  256. $log->before = $before;
  257. $log->after = $after;
  258. $log->description = $description;
  259. return $log->save();
  260. }
  261. /**
  262. * 添加用户封禁日志.
  263. *
  264. * @param int $userId 用户ID
  265. * @param int $time 封禁时长,单位分钟
  266. * @param string $description 封禁理由
  267. *
  268. * @return bool
  269. */
  270. public static function addUserBanLog(int $userId, int $time, string $description)
  271. {
  272. $log = new UserBanedLog();
  273. $log->user_id = $userId;
  274. $log->time = $time;
  275. $log->description = $description;
  276. return $log->save();
  277. }
  278. /**
  279. * 推销信息推送
  280. *
  281. * @param int $type 渠道类型
  282. * @param string $title 标题
  283. * @param string $content 内容
  284. * @param int $status 状态
  285. * @param string $error 报错
  286. * @param string $receiver 收件人
  287. * @return int
  288. */
  289. public static function addMarketing(int $type, string $title, string $content, int $status = 1, string $error = '', string $receiver = ''): int
  290. {
  291. $marketing = new Marketing();
  292. $marketing->type = $type;
  293. $marketing->receiver = $receiver;
  294. $marketing->title = $title;
  295. $marketing->content = $content;
  296. $marketing->error = $error;
  297. $marketing->status = $status;
  298. return $marketing->save();
  299. }
  300. }