Helpers.php 6.1 KB

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