1
0

Helpers.php 8.9 KB

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