Helpers.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <?php
  2. namespace App\Components;
  3. use App\Http\Models\Config;
  4. use App\Http\Models\CouponLog;
  5. use App\Http\Models\Level;
  6. use App\Http\Models\NotificationLog;
  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 methodList()
  23. {
  24. return SsConfig::type(1)->get();
  25. }
  26. // 协议
  27. public static function protocolList()
  28. {
  29. return SsConfig::type(2)->get();
  30. }
  31. // 混淆
  32. public static function obfsList()
  33. {
  34. return SsConfig::type(3)->get();
  35. }
  36. // 等级
  37. public static function levelList()
  38. {
  39. return Level::query()->get()->sortBy('level');
  40. }
  41. // 生成用户的订阅码
  42. public static function makeSubscribeCode()
  43. {
  44. $code = makeRandStr(5);
  45. if(UserSubscribe::query()->where('code', $code)->exists()){
  46. $code = self::makeSubscribeCode();
  47. }
  48. return $code;
  49. }
  50. /**
  51. * 添加用户
  52. *
  53. * @param string $email 用户邮箱
  54. * @param string $password 用户密码
  55. * @param string $transfer_enable 可用流量
  56. * @param int $data 可使用天数
  57. * @param int $referral_uid 邀请人
  58. * @return int
  59. */
  60. public static function addUser($email, $password, $transfer_enable, $data, $referral_uid = 0)
  61. {
  62. $user = new User();
  63. $user->username = $email;
  64. $user->email = $email;
  65. $user->password = $password;
  66. // 生成一个可用端口
  67. $user->port = self::systemConfig()['is_rand_port']? Helpers::getRandPort() : Helpers::getOnlyPort();
  68. $user->passwd = makeRandStr();
  69. $user->vmess_id = createGuid();
  70. $user->enable = 1;
  71. $user->method = Helpers::getDefaultMethod();
  72. $user->protocol = Helpers::getDefaultProtocol();
  73. $user->protocol_param = '';
  74. $user->obfs = Helpers::getDefaultObfs();
  75. $user->obfs_param = '';
  76. $user->usage = 1;
  77. $user->transfer_enable = $transfer_enable;
  78. $user->enable_time = date('Y-m-d');
  79. $user->expire_time = date('Y-m-d', strtotime("+".$data." days"));
  80. $user->reg_ip = getClientIp();
  81. $user->referral_uid = $referral_uid;
  82. $user->reset_time = NULL;
  83. $user->status = 0;
  84. $user->save();
  85. return $user->id;
  86. }
  87. // 获取系统配置
  88. public static function systemConfig()
  89. {
  90. $config = Config::query()->get();
  91. $data = [];
  92. foreach($config as $vo){
  93. $data[$vo->name] = $vo->value;
  94. }
  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()
  100. {
  101. $config = self::systemConfig();
  102. $port = mt_rand($config['min_port'], $config['max_port']);
  103. $exists_port = User::query()->pluck('port')->toArray();
  104. if(in_array($port, $exists_port) || in_array($port, self::$denyPorts)){
  105. $port = self::getRandPort();
  106. }
  107. return $port;
  108. }
  109. // 获取一个随机端口
  110. public static function getOnlyPort()
  111. {
  112. $config = self::systemConfig();
  113. $port = $config['min_port'];
  114. $exists_port = User::query()->where('port', '>=', $port)->pluck('port')->toArray();
  115. while(in_array($port, $exists_port) || in_array($port, self::$denyPorts)){
  116. $port = $port+1;
  117. }
  118. return $port;
  119. }
  120. // 获取默认加密方式
  121. public static function getDefaultMethod()
  122. {
  123. $config = SsConfig::default()->type(1)->first();
  124. return $config? $config->name : 'aes-256-cfb';
  125. }
  126. // 获取默认协议
  127. public static function getDefaultProtocol()
  128. {
  129. $config = SsConfig::default()->type(2)->first();
  130. return $config? $config->name : 'origin';
  131. }
  132. // 获取默认混淆
  133. public static function getDefaultObfs()
  134. {
  135. $config = SsConfig::default()->type(3)->first();
  136. return $config? $config->name : 'plain';
  137. }
  138. /**
  139. * 添加通知推送日志
  140. *
  141. * @param string $title 标题
  142. * @param string $content 内容
  143. * @param int $type 发送类型
  144. * @param string $address 收信方
  145. * @param int $status 投递状态
  146. * @param string $error 投递失败时记录的异常信息
  147. *
  148. * @return int
  149. */
  150. public static function addNotificationLog($title, $content, $type, $address = 'admin', $status = 1, $error = '')
  151. {
  152. $log = new NotificationLog();
  153. $log->type = $type;
  154. $log->address = $address;
  155. $log->title = $title;
  156. $log->content = $content;
  157. $log->status = $status;
  158. $log->error = $error;
  159. $log->save();
  160. return $log->id;
  161. }
  162. /**
  163. * 添加优惠券操作日志
  164. *
  165. * @param int $couponId 优惠券ID
  166. * @param int $goodsId 商品ID
  167. * @param int $orderId 订单ID
  168. * @param string $desc 备注
  169. *
  170. * @return int
  171. */
  172. public static function addCouponLog($couponId, $goodsId, $orderId, $desc = '')
  173. {
  174. $log = new CouponLog();
  175. $log->coupon_id = $couponId;
  176. $log->goods_id = $goodsId;
  177. $log->order_id = $orderId;
  178. $log->desc = $desc;
  179. return $log->save();
  180. }
  181. /**
  182. * 记录流量变动日志
  183. *
  184. * @param int $userId 用户ID
  185. * @param string $oid 订单ID
  186. * @param int $before 记录前的值
  187. * @param int $after 记录后的值
  188. * @param string $desc 描述
  189. *
  190. * @return int
  191. */
  192. public static function addUserTrafficModifyLog($userId, $oid, $before, $after, $desc = '')
  193. {
  194. $log = new UserTrafficModifyLog();
  195. $log->user_id = $userId;
  196. $log->order_id = $oid;
  197. $log->before = $before;
  198. $log->after = $after;
  199. $log->desc = $desc;
  200. return $log->save();
  201. }
  202. }