Helpers.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. <?php
  2. namespace App\Components;
  3. use App\Models\Config;
  4. use App\Models\CouponLog;
  5. use App\Models\NotificationLog;
  6. use App\Models\SsConfig;
  7. use App\Models\User;
  8. use App\Models\UserBanedLog;
  9. use App\Models\UserCreditLog;
  10. use App\Models\UserDataModifyLog;
  11. use App\Models\UserSubscribe;
  12. use Cache;
  13. use DateTime;
  14. use Str;
  15. class Helpers
  16. {
  17. // 不生成的端口
  18. private static $denyPorts = [
  19. 1068, 1109, 1434, 3127, 3128, 3129, 3130, 3332, 4444, 5554, 6669, 8080, 8081, 8082, 8181, 8282, 9996, 17185, 24554, 35601, 60177, 60179,
  20. ];
  21. // 加密方式
  22. public static function methodList()
  23. {
  24. return SsConfig::type(1)->get();
  25. }
  26. // 协议
  27. public static function protocolList()
  28. {
  29. return SsConfig::type(2)->get();
  30. }
  31. // 混淆
  32. public static function obfsList()
  33. {
  34. return SsConfig::type(3)->get();
  35. }
  36. // 生成用户的订阅码
  37. public static function makeSubscribeCode(): string
  38. {
  39. $code = Str::random();
  40. if (UserSubscribe::whereCode($code)->exists()) {
  41. $code = self::makeSubscribeCode();
  42. }
  43. return $code;
  44. }
  45. /**
  46. * 添加用户.
  47. *
  48. * @param string $email 用户邮箱
  49. * @param string $password 用户密码
  50. * @param int $transfer_enable 可用流量
  51. * @param int|null $date 可使用天数
  52. * @param int|null $inviter_id 邀请人
  53. * @param string|null $username 昵称
  54. * @return User
  55. */
  56. public static function addUser(string $email, string $password, int $transfer_enable, int $date = null, int $inviter_id = null, string $username = null): User
  57. {
  58. return User::create([
  59. 'username' => $username ?? $email,
  60. 'email' => $email,
  61. 'password' => $password,
  62. 'port' => self::getPort(), // 生成一个可用端口
  63. 'passwd' => Str::random(),
  64. 'vmess_id' => Str::uuid(),
  65. 'method' => self::getDefaultMethod(),
  66. 'protocol' => self::getDefaultProtocol(),
  67. 'obfs' => self::getDefaultObfs(),
  68. 'transfer_enable' => $transfer_enable,
  69. 'expired_at' => date('Y-m-d', strtotime('+'.$date.' days')),
  70. 'user_group_id' => null,
  71. 'reg_ip' => IP::getClientIp(),
  72. 'inviter_id' => $inviter_id,
  73. ]);
  74. }
  75. // 获取一个有效端口
  76. public static function getPort(): int
  77. {
  78. if (sysConfig('is_rand_port')) {
  79. $port = self::getRandPort();
  80. } else {
  81. $port = (int) sysConfig('min_port');
  82. $exists_port = array_merge(User::where('port', '>=', $port)->pluck('port')->toArray(), self::$denyPorts);
  83. while (in_array($port, $exists_port, true)) {
  84. $port++;
  85. }
  86. }
  87. return $port;
  88. }
  89. // 获取一个随机端口
  90. private static function getRandPort(): int
  91. {
  92. $port = random_int(sysConfig('min_port'), sysConfig('max_port'));
  93. $exists_port = array_merge(
  94. User::where('port', '<>', 0)->pluck('port')->toArray(),
  95. self::$denyPorts
  96. );
  97. while (in_array($port, $exists_port, true)) {
  98. $port = random_int(sysConfig('min_port'), sysConfig('max_port'));
  99. }
  100. return $port;
  101. }
  102. // 获取默认加密方式
  103. public static function getDefaultMethod(): string
  104. {
  105. $config = SsConfig::default()->type(1)->first();
  106. return $config->name ?? 'aes-256-cfb';
  107. }
  108. // 获取默认协议
  109. public static function getDefaultProtocol(): string
  110. {
  111. $config = SsConfig::default()->type(2)->first();
  112. return $config->name ?? 'origin';
  113. }
  114. // 获取默认混淆
  115. public static function getDefaultObfs(): string
  116. {
  117. $config = SsConfig::default()->type(3)->first();
  118. return $config->name ?? 'plain';
  119. }
  120. // 获取系统配置
  121. public static function cacheSysConfig($name)
  122. {
  123. if ($name === 'is_onlinePay') {
  124. $value = sysConfig('is_AliPay') || sysConfig('is_QQPay') || sysConfig('is_WeChatPay') || sysConfig('is_otherPay');
  125. Cache::tags('sysConfig')->put('is_onlinePay', $value);
  126. } else {
  127. $value = Config::find($name)->value;
  128. Cache::tags('sysConfig')->put($name, $value ?? false);
  129. }
  130. return $value;
  131. }
  132. public static function daysToNow($date): int
  133. {
  134. return (new DateTime())->diff(new DateTime($date))->days;
  135. }
  136. /**
  137. * 添加通知推送日志.
  138. *
  139. * @param string $title 标题
  140. * @param string $content 内容
  141. * @param int $type 发送类型
  142. * @param string $address 收信方
  143. * @param int $status 投递状态
  144. * @param string $error 投递失败时记录的异常信息
  145. *
  146. * @return int
  147. */
  148. public static function addNotificationLog(string $title, string $content, int $type, $address = 'admin', $status = 1, $error = ''): int
  149. {
  150. $log = new NotificationLog();
  151. $log->type = $type;
  152. $log->address = $address;
  153. $log->title = $title;
  154. $log->content = $content;
  155. $log->status = $status;
  156. $log->error = $error;
  157. $log->save();
  158. return $log->id;
  159. }
  160. /**
  161. * 添加优惠券操作日志.
  162. *
  163. * @param string $description 备注
  164. * @param int $couponId 优惠券ID
  165. * @param int|null $goodsId 商品ID
  166. * @param int|null $orderId 订单ID
  167. *
  168. * @return bool
  169. */
  170. public static function addCouponLog($description, $couponId, $goodsId = null, $orderId = null): bool
  171. {
  172. $log = new CouponLog();
  173. $log->coupon_id = $couponId;
  174. $log->goods_id = $goodsId;
  175. $log->order_id = $orderId;
  176. $log->description = $description;
  177. return $log->save();
  178. }
  179. /**
  180. * 记录余额操作日志.
  181. *
  182. * @param int $userId 用户ID
  183. * @param int|null $orderId 订单ID
  184. * @param int $before 记录前余额
  185. * @param int $after 记录后余额
  186. * @param int $amount 发生金额
  187. * @param string $description 描述
  188. *
  189. * @return bool
  190. */
  191. public static function addUserCreditLog($userId, $orderId, $before, $after, $amount, $description = ''): bool
  192. {
  193. $log = new UserCreditLog();
  194. $log->user_id = $userId;
  195. $log->order_id = $orderId;
  196. $log->before = $before;
  197. $log->after = $after;
  198. $log->amount = $amount;
  199. $log->description = $description;
  200. $log->created_at = date('Y-m-d H:i:s');
  201. return $log->save();
  202. }
  203. /**
  204. * 记录流量变动日志.
  205. *
  206. * @param int $userId 用户ID
  207. * @param int|null $orderId 订单ID
  208. * @param int $before 记录前的值
  209. * @param int $after 记录后的值
  210. * @param string $description 描述
  211. *
  212. * @return bool
  213. */
  214. public static function addUserTrafficModifyLog($userId, $orderId, $before, $after, $description = ''): bool
  215. {
  216. $log = new UserDataModifyLog();
  217. $log->user_id = $userId;
  218. $log->order_id = $orderId;
  219. $log->before = $before;
  220. $log->after = $after;
  221. $log->description = $description;
  222. return $log->save();
  223. }
  224. /**
  225. * 添加用户封禁日志.
  226. *
  227. * @param int $userId 用户ID
  228. * @param int $time 封禁时长,单位分钟
  229. * @param string $description 封禁理由
  230. *
  231. * @return bool
  232. */
  233. public static function addUserBanLog(int $userId, int $time, string $description)
  234. {
  235. $log = new UserBanedLog();
  236. $log->user_id = $userId;
  237. $log->time = $time;
  238. $log->description = $description;
  239. return $log->save();
  240. }
  241. }