Helpers.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. namespace App\Components;
  3. use App\Http\Models\Config;
  4. use App\Http\Models\CouponLog;
  5. use App\Http\Models\EmailLog;
  6. use App\Http\Models\Level;
  7. use App\Http\Models\SsConfig;
  8. use App\Http\Models\User;
  9. use App\Http\Models\UserTrafficModifyLog;
  10. class Helpers
  11. {
  12. // 不生成的端口
  13. private static $denyPorts = [
  14. 1068, 1109, 1434, 3127, 3128,
  15. 3129, 3130, 3332, 4444, 5554,
  16. 6669, 8080, 8081, 8082, 8181,
  17. 8282, 9996, 17185, 24554, 35601,
  18. 60177, 60179
  19. ];
  20. // 获取系统配置
  21. public static function systemConfig()
  22. {
  23. $config = Config::query()->get();
  24. $data = [];
  25. foreach ($config as $vo) {
  26. $data[$vo->name] = $vo->value;
  27. }
  28. return $data;
  29. }
  30. // 获取默认加密方式
  31. public static function getDefaultMethod()
  32. {
  33. $config = SsConfig::query()->where('type', 1)->where('is_default', 1)->first();
  34. return $config ? $config->name : 'aes-256-cfb';
  35. }
  36. // 获取默认混淆
  37. public static function getDefaultObfs()
  38. {
  39. $config = SsConfig::query()->where('type', 3)->where('is_default', 1)->first();
  40. return $config ? $config->name : 'plain';
  41. }
  42. // 获取默认协议
  43. public static function getDefaultProtocol()
  44. {
  45. $config = SsConfig::query()->where('type', 2)->where('is_default', 1)->first();
  46. return $config ? $config->name : 'origin';
  47. }
  48. // 获取一个随机端口
  49. public static function getRandPort()
  50. {
  51. $config = self::systemConfig();
  52. $port = mt_rand($config['min_port'], $config['max_port']);
  53. $exists_port = User::query()->pluck('port')->toArray();
  54. if (in_array($port, $exists_port) || in_array($port, self::$denyPorts)) {
  55. $port = self::getRandPort();
  56. }
  57. return $port;
  58. }
  59. // 获取一个端口
  60. public static function getOnlyPort()
  61. {
  62. $config = self::systemConfig();
  63. $port = $config['min_port'];
  64. $exists_port = User::query()->where('port', '>=', $config['min_port'])->pluck('port')->toArray();
  65. while (in_array($port, $exists_port) || in_array($port, self::$denyPorts)) {
  66. $port = $port + 1;
  67. }
  68. return $port;
  69. }
  70. // 加密方式
  71. public static function methodList()
  72. {
  73. return SsConfig::query()->where('type', 1)->get();
  74. }
  75. // 协议
  76. public static function protocolList()
  77. {
  78. return SsConfig::query()->where('type', 2)->get();
  79. }
  80. // 混淆
  81. public static function obfsList()
  82. {
  83. return SsConfig::query()->where('type', 3)->get();
  84. }
  85. // 等级
  86. public static function levelList()
  87. {
  88. return Level::query()->get()->sortBy('level');
  89. }
  90. /**
  91. * 添加邮件投递日志
  92. *
  93. * @param string $address 收信地址
  94. * @param string $title 标题
  95. * @param string $content 内容
  96. * @param int $status 投递状态
  97. * @param string $error 投递失败时记录的异常信息
  98. *
  99. * @return int
  100. */
  101. public static function addEmailLog($address, $title, $content, $status = 1, $error = '')
  102. {
  103. $log = new EmailLog();
  104. $log->type = 1;
  105. $log->address = $address;
  106. $log->title = $title;
  107. $log->content = $content;
  108. $log->status = $status;
  109. $log->error = $error;
  110. $log->created_at = date('Y-m-d H:i:s');
  111. return $log->save();
  112. }
  113. /**
  114. * 添加优惠券操作日志
  115. *
  116. * @param int $couponId 优惠券ID
  117. * @param int $goodsId 商品ID
  118. * @param int $orderId 订单ID
  119. * @param string $desc 备注
  120. *
  121. * @return int
  122. */
  123. public static function addCouponLog($couponId, $goodsId, $orderId, $desc = '')
  124. {
  125. $log = new CouponLog();
  126. $log->coupon_id = $couponId;
  127. $log->goods_id = $goodsId;
  128. $log->order_id = $orderId;
  129. $log->desc = $desc;
  130. return $log->save();
  131. }
  132. /**
  133. * 记录流量变动日志
  134. *
  135. * @param int $userId 用户ID
  136. * @param string $oid 订单ID
  137. * @param int $before 记录前的值
  138. * @param int $after 记录后的值
  139. * @param string $desc 描述
  140. *
  141. * @return int
  142. */
  143. public static function addUserTrafficModifyLog($userId, $oid, $before, $after, $desc = '')
  144. {
  145. $log = new UserTrafficModifyLog();
  146. $log->user_id = $userId;
  147. $log->order_id = $oid;
  148. $log->before = $before;
  149. $log->after = $after;
  150. $log->desc = $desc;
  151. return $log->save();
  152. }
  153. }