Helpers.php 9.1 KB

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