Helpers.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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\UserDataModifyLog;
  10. use App\Models\UserSubscribe;
  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. $data = Config::all()->pluck('value', 'name')->toArray();
  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. $port = random_int(self::systemConfig()['min_port'], self::systemConfig()['max_port']);
  101. $exists_port = User::query()->pluck('port')->toArray();
  102. if(in_array($port, $exists_port, true) || in_array($port, self::$denyPorts, true)){
  103. $port = self::getRandPort();
  104. }
  105. return $port;
  106. }
  107. // 获取一个随机端口
  108. public static function getOnlyPort() {
  109. $port = (int) self::systemConfig()['min_port'];
  110. $exists_port = User::query()->where('port', '>=', $port)->pluck('port')->toArray();
  111. while(in_array($port, $exists_port, true) || in_array($port, self::$denyPorts, true)){
  112. ++$port;
  113. }
  114. return $port;
  115. }
  116. // 获取默认加密方式
  117. public static function getDefaultMethod() {
  118. $config = SsConfig::default()->type(1)->first();
  119. return $config? $config->name : 'aes-256-cfb';
  120. }
  121. // 获取默认协议
  122. public static function getDefaultProtocol() {
  123. $config = SsConfig::default()->type(2)->first();
  124. return $config? $config->name : 'origin';
  125. }
  126. // 获取默认混淆
  127. public static function getDefaultObfs() {
  128. $config = SsConfig::default()->type(3)->first();
  129. return $config? $config->name : 'plain';
  130. }
  131. /**
  132. * 添加通知推送日志
  133. *
  134. * @param string $title 标题
  135. * @param string $content 内容
  136. * @param int $type 发送类型
  137. * @param string $address 收信方
  138. * @param int $status 投递状态
  139. * @param string $error 投递失败时记录的异常信息
  140. *
  141. * @return int
  142. */
  143. public static function addNotificationLog($title, $content, $type, $address = 'admin', $status = 1, $error = ''
  144. ): int {
  145. $log = new NotificationLog();
  146. $log->type = $type;
  147. $log->address = $address;
  148. $log->title = $title;
  149. $log->content = $content;
  150. $log->status = $status;
  151. $log->error = $error;
  152. $log->save();
  153. return $log->id;
  154. }
  155. /**
  156. * 添加优惠券操作日志
  157. *
  158. * @param int $couponId 优惠券ID
  159. * @param int $goodsId 商品ID
  160. * @param int $orderId 订单ID
  161. * @param string $description 备注
  162. *
  163. * @return int
  164. */
  165. public static function addCouponLog($couponId, $goodsId, $orderId, $description = ''): int {
  166. $log = new CouponLog();
  167. $log->coupon_id = $couponId;
  168. $log->goods_id = $goodsId;
  169. $log->order_id = $orderId;
  170. $log->description = $description;
  171. return $log->save();
  172. }
  173. /**
  174. * 记录余额操作日志
  175. *
  176. * @param int $userId 用户ID
  177. * @param string $oid 订单ID
  178. * @param int $before 记录前余额
  179. * @param int $after 记录后余额
  180. * @param int $amount 发生金额
  181. * @param string $description 描述
  182. *
  183. * @return int
  184. */
  185. public static function addUserCreditLog($userId, $oid, $before, $after, $amount, $description = ''): int {
  186. $log = new UserCreditLog();
  187. $log->user_id = $userId;
  188. $log->order_id = $oid;
  189. $log->before = $before;
  190. $log->after = $after;
  191. $log->amount = $amount;
  192. $log->description = $description;
  193. $log->created_at = date('Y-m-d H:i:s');
  194. return $log->save();
  195. }
  196. /**
  197. * 记录流量变动日志
  198. *
  199. * @param int $userId 用户ID
  200. * @param string $oid 订单ID
  201. * @param int $before 记录前的值
  202. * @param int $after 记录后的值
  203. * @param string $description 描述
  204. *
  205. * @return int
  206. */
  207. public static function addUserTrafficModifyLog($userId, $oid, $before, $after, $description = ''): int {
  208. $log = new UserDataModifyLog();
  209. $log->user_id = $userId;
  210. $log->order_id = $oid;
  211. $log->before = $before;
  212. $log->after = $after;
  213. $log->description = $description;
  214. return $log->save();
  215. }
  216. }