Helpers.php 9.1 KB

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