Helpers.php 4.0 KB

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