Helpers.php 6.1 KB

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