Helpers.php 9.0 KB

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