Helpers.php 7.5 KB

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