Helpers.php 8.8 KB

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