Helpers.php 3.9 KB

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