Helpers.php 12 KB

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