Helpers.php 12 KB

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