Helpers.php 7.4 KB

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