Helpers.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. <?php
  2. namespace App\Components;
  3. use App\Models\CouponLog;
  4. use App\Models\Marketing;
  5. use App\Models\NotificationLog;
  6. use App\Models\SsConfig;
  7. use App\Models\User;
  8. use App\Models\UserCreditLog;
  9. use App\Models\UserDataModifyLog;
  10. use App\Models\UserLoginLog;
  11. use App\Models\UserSubscribe;
  12. use DateTime;
  13. use Log;
  14. use Str;
  15. class Helpers
  16. {
  17. // 不生成的端口
  18. private static $denyPorts = [
  19. 1068, 1109, 1434, 3127, 3128, 3129, 3130, 3332, 4444, 5554, 6669, 8080, 8081, 8082, 8181, 8282, 9996, 17185, 24554, 35601, 60177, 60179,
  20. ];
  21. // 加密方式
  22. public static function methodList()
  23. {
  24. return SsConfig::type(1)->get();
  25. }
  26. // 协议
  27. public static function protocolList()
  28. {
  29. return SsConfig::type(2)->get();
  30. }
  31. // 混淆
  32. public static function obfsList()
  33. {
  34. return SsConfig::type(3)->get();
  35. }
  36. // 生成用户的订阅码
  37. public static function makeSubscribeCode(): string
  38. {
  39. $code = Str::random();
  40. if (UserSubscribe::whereCode($code)->exists()) {
  41. $code = self::makeSubscribeCode();
  42. }
  43. return $code;
  44. }
  45. /**
  46. * 添加用户.
  47. *
  48. * @param string $username 用户
  49. * @param string $password 用户密码
  50. * @param int $transfer_enable 可用流量
  51. * @param int|null $date 可使用天数
  52. * @param int|null $inviter_id 邀请人
  53. * @param string|null $nickname 昵称
  54. * @return User
  55. */
  56. public static function addUser(string $username, string $password, int $transfer_enable, int $date = null, int $inviter_id = null, string $nickname = null): User
  57. {
  58. return User::create([
  59. 'nickname' => $nickname ?? $username,
  60. 'username' => $username,
  61. 'password' => $password,
  62. 'port' => self::getPort(), // 生成一个可用端口
  63. 'passwd' => Str::random(),
  64. 'vmess_id' => Str::uuid(),
  65. 'method' => self::getDefaultMethod(),
  66. 'protocol' => self::getDefaultProtocol(),
  67. 'obfs' => self::getDefaultObfs(),
  68. 'transfer_enable' => $transfer_enable,
  69. 'expired_at' => date('Y-m-d', strtotime($date.' days')),
  70. 'user_group_id' => null,
  71. 'reg_ip' => IP::getClientIp(),
  72. 'inviter_id' => $inviter_id,
  73. ]);
  74. }
  75. // 获取一个有效端口
  76. public static function getPort(): int
  77. {
  78. if (sysConfig('is_rand_port')) {
  79. $port = self::getRandPort();
  80. } else {
  81. $port = (int) sysConfig('min_port');
  82. $exists_port = array_merge(User::where('port', '>=', $port)->pluck('port')->toArray(), self::$denyPorts);
  83. while (in_array($port, $exists_port, true)) {
  84. $port++;
  85. }
  86. }
  87. return $port;
  88. }
  89. // 获取一个随机端口
  90. private static function getRandPort(): int
  91. {
  92. $port = random_int(sysConfig('min_port'), sysConfig('max_port'));
  93. $exists_port = array_merge(
  94. User::where('port', '<>', 0)->pluck('port')->toArray(),
  95. self::$denyPorts
  96. );
  97. while (in_array($port, $exists_port, true)) {
  98. $port = random_int(sysConfig('min_port'), sysConfig('max_port'));
  99. }
  100. return $port;
  101. }
  102. // 获取默认加密方式
  103. public static function getDefaultMethod(): string
  104. {
  105. $config = SsConfig::default()->type(1)->first();
  106. return $config->name ?? 'aes-256-cfb';
  107. }
  108. // 获取默认协议
  109. public static function getDefaultProtocol(): string
  110. {
  111. $config = SsConfig::default()->type(2)->first();
  112. return $config->name ?? 'origin';
  113. }
  114. // 获取默认混淆
  115. public static function getDefaultObfs(): string
  116. {
  117. $config = SsConfig::default()->type(3)->first();
  118. return $config->name ?? 'plain';
  119. }
  120. public static function daysToNow($date): int
  121. {
  122. return (new DateTime())->diff(new DateTime($date))->days;
  123. }
  124. /**
  125. * 添加通知推送日志.
  126. *
  127. * @param string $title 标题
  128. * @param string $content 内容
  129. * @param int $type 发送类型
  130. * @param string $address 收信方
  131. * @param int $status 投递状态
  132. * @param string $error 投递失败时记录的异常信息
  133. * @return int
  134. */
  135. public static function addNotificationLog(string $title, string $content, int $type, string $address = 'admin', int $status = 1, string $error = ''): int
  136. {
  137. $log = new NotificationLog();
  138. $log->type = $type;
  139. $log->address = $address;
  140. $log->title = $title;
  141. $log->content = $content;
  142. $log->status = $status;
  143. $log->error = $error;
  144. $log->save();
  145. return $log->id;
  146. }
  147. /**
  148. * 添加优惠券操作日志.
  149. *
  150. * @param string $description 备注
  151. * @param int $couponId 优惠券ID
  152. * @param int|null $goodsId 商品ID
  153. * @param int|null $orderId 订单ID
  154. * @return bool
  155. */
  156. public static function addCouponLog($description, $couponId, $goodsId = null, $orderId = null): bool
  157. {
  158. $log = new CouponLog();
  159. $log->coupon_id = $couponId;
  160. $log->goods_id = $goodsId;
  161. $log->order_id = $orderId;
  162. $log->description = $description;
  163. return $log->save();
  164. }
  165. /**
  166. * 记录余额操作日志.
  167. *
  168. * @param int $userId 用户ID
  169. * @param int|null $orderId 订单ID
  170. * @param int $before 记录前余额
  171. * @param int $after 记录后余额
  172. * @param int $amount 发生金额
  173. * @param string $description 描述
  174. * @return bool
  175. */
  176. public static function addUserCreditLog($userId, $orderId, $before, $after, $amount, $description = ''): bool
  177. {
  178. $log = new UserCreditLog();
  179. $log->user_id = $userId;
  180. $log->order_id = $orderId;
  181. $log->before = $before;
  182. $log->after = $after;
  183. $log->amount = $amount;
  184. $log->description = $description;
  185. $log->created_at = date('Y-m-d H:i:s');
  186. return $log->save();
  187. }
  188. /**
  189. * 记录流量变动日志.
  190. *
  191. * @param int $userId 用户ID
  192. * @param int|null $orderId 订单ID
  193. * @param int $before 记录前的值
  194. * @param int $after 记录后的值
  195. * @param string $description 描述
  196. * @return bool
  197. */
  198. public static function addUserTrafficModifyLog($userId, $orderId, $before, $after, $description = ''): bool
  199. {
  200. $log = new UserDataModifyLog();
  201. $log->user_id = $userId;
  202. $log->order_id = $orderId;
  203. $log->before = $before;
  204. $log->after = $after;
  205. $log->description = $description;
  206. return $log->save();
  207. }
  208. /**
  209. * 推销信息推送
  210. *
  211. * @param int $type 渠道类型
  212. * @param string $title 标题
  213. * @param string $content 内容
  214. * @param int $status 状态
  215. * @param string $error 报错
  216. * @param string $receiver 收件人
  217. * @return int
  218. */
  219. public static function addMarketing(int $type, string $title, string $content, int $status = 1, string $error = '', string $receiver = ''): int
  220. {
  221. $marketing = new Marketing();
  222. $marketing->type = $type;
  223. $marketing->receiver = $receiver;
  224. $marketing->title = $title;
  225. $marketing->content = $content;
  226. $marketing->error = $error;
  227. $marketing->status = $status;
  228. return $marketing->save();
  229. }
  230. /**
  231. * 用户登录后操作.
  232. *
  233. * @param User $user 用户ID
  234. * @param string $ip IP地址
  235. */
  236. public static function userLoginAction(User $user, string $ip): void
  237. {
  238. $ipLocation = IP::getIPInfo($ip);
  239. if (empty($ipLocation) || empty($ipLocation['country'])) {
  240. Log::warning(trans('error.get_ip').':'.$ip);
  241. }
  242. $log = new UserLoginLog();
  243. $log->user_id = $user->id;
  244. $log->ip = $ip;
  245. $log->country = $ipLocation['country'] ?? '';
  246. $log->province = $ipLocation['province'] ?? '';
  247. $log->city = $ipLocation['city'] ?? '';
  248. $log->county = $ipLocation['county'] ?? '';
  249. $log->isp = $ipLocation['isp'] ?? ($ipLocation['organization'] ?? '');
  250. $log->area = $ipLocation['area'] ?? '';
  251. $log->save();
  252. $user->update(['last_login' => time()]); // 更新登录信息
  253. }
  254. }