Helpers.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. // 生成一个可用端口
  62. $user->port = self::getPort();
  63. $user->passwd = Str::random();
  64. $user->vmess_id = Str::uuid();
  65. $user->enable = 1;
  66. $user->method = self::getDefaultMethod();
  67. $user->protocol = self::getDefaultProtocol();
  68. $user->obfs = self::getDefaultObfs();
  69. $user->transfer_enable = $transfer_enable;
  70. $user->expired_at = date('Y-m-d', strtotime('+'.$data.' days'));
  71. $user->reg_ip = IP::getClientIp();
  72. $user->inviter_id = $inviter_id;
  73. $user->reset_time = null;
  74. $user->status = 0;
  75. $user->save();
  76. return $user->id;
  77. }
  78. // 获取一个有效端口
  79. public static function getPort(): int
  80. {
  81. if (sysConfig('is_rand_port')) {
  82. $port = self::getRandPort();
  83. } else {
  84. $port = (int) sysConfig('min_port');
  85. $exists_port = array_merge(User::where('port', '>=', $port)->pluck('port')->toArray(), self::$denyPorts);
  86. while (in_array($port, $exists_port, true)) {
  87. $port++;
  88. }
  89. }
  90. return $port;
  91. }
  92. // 获取一个随机端口
  93. private static function getRandPort(): int
  94. {
  95. $port = random_int(sysConfig('min_port'), sysConfig('max_port'));
  96. $exists_port = array_merge(
  97. User::where('port', '<>', 0)->pluck('port')->toArray(),
  98. self::$denyPorts
  99. );
  100. while (in_array($port, $exists_port, true)) {
  101. $port = random_int(sysConfig('min_port'), sysConfig('max_port'));
  102. }
  103. return $port;
  104. }
  105. // 获取默认加密方式
  106. public static function getDefaultMethod(): string
  107. {
  108. $config = SsConfig::default()->type(1)->first();
  109. return $config->name ?? 'aes-256-cfb';
  110. }
  111. // 获取默认协议
  112. public static function getDefaultProtocol(): string
  113. {
  114. $config = SsConfig::default()->type(2)->first();
  115. return $config->name ?? 'origin';
  116. }
  117. // 获取默认混淆
  118. public static function getDefaultObfs(): string
  119. {
  120. $config = SsConfig::default()->type(3)->first();
  121. return $config->name ?? 'plain';
  122. }
  123. // 获取系统配置
  124. public static function cacheSysConfig($name)
  125. {
  126. if ($name === 'is_onlinePay') {
  127. $value = sysConfig('is_AliPay') || sysConfig('is_QQPay') || sysConfig('is_WeChatPay') || sysConfig('is_otherPay');
  128. Cache::tags('sysConfig')->put('is_onlinePay', $value);
  129. } else {
  130. $value = Config::find($name)->value;
  131. Cache::tags('sysConfig')->put($name, $value ?? false);
  132. }
  133. return $value;
  134. }
  135. public static function daysToNow($date): int
  136. {
  137. return (new DateTime())->diff(new DateTime($date))->days;
  138. }
  139. /**
  140. * 添加通知推送日志.
  141. *
  142. * @param string $title 标题
  143. * @param string $content 内容
  144. * @param int $type 发送类型
  145. * @param string $address 收信方
  146. * @param int $status 投递状态
  147. * @param string $error 投递失败时记录的异常信息
  148. *
  149. * @return int
  150. */
  151. public static function addNotificationLog(string $title, string $content, int $type, $address = 'admin', $status = 1, $error = ''): int
  152. {
  153. $log = new NotificationLog();
  154. $log->type = $type;
  155. $log->address = $address;
  156. $log->title = $title;
  157. $log->content = $content;
  158. $log->status = $status;
  159. $log->error = $error;
  160. $log->save();
  161. return $log->id;
  162. }
  163. /**
  164. * 添加优惠券操作日志.
  165. *
  166. * @param string $description 备注
  167. * @param int $couponId 优惠券ID
  168. * @param int $goodsId 商品ID
  169. * @param int $orderId 订单ID
  170. *
  171. * @return bool
  172. */
  173. public static function addCouponLog(string $description, int $couponId, $goodsId = 0, $orderId = 0): bool
  174. {
  175. $log = new CouponLog();
  176. $log->coupon_id = $couponId;
  177. $log->goods_id = $goodsId;
  178. $log->order_id = $orderId;
  179. $log->description = $description;
  180. return $log->save();
  181. }
  182. /**
  183. * 记录余额操作日志.
  184. *
  185. * @param int $userId 用户ID
  186. * @param int $orderId 订单ID
  187. * @param int $before 记录前余额
  188. * @param int $after 记录后余额
  189. * @param int $amount 发生金额
  190. * @param string $description 描述
  191. *
  192. * @return bool
  193. */
  194. public static function addUserCreditLog(int $userId, int $orderId, int $before, int $after, int $amount, $description = ''): bool
  195. {
  196. $log = new UserCreditLog();
  197. $log->user_id = $userId;
  198. $log->order_id = $orderId;
  199. $log->before = $before;
  200. $log->after = $after;
  201. $log->amount = $amount;
  202. $log->description = $description;
  203. $log->created_at = date('Y-m-d H:i:s');
  204. return $log->save();
  205. }
  206. /**
  207. * 记录流量变动日志.
  208. *
  209. * @param int $userId 用户ID
  210. * @param int $orderId 订单ID
  211. * @param int $before 记录前的值
  212. * @param int $after 记录后的值
  213. * @param string $description 描述
  214. *
  215. * @return bool
  216. */
  217. public static function addUserTrafficModifyLog(int $userId, int $orderId, int $before, int $after, $description = ''): bool
  218. {
  219. $log = new UserDataModifyLog();
  220. $log->user_id = $userId;
  221. $log->order_id = $orderId;
  222. $log->before = $before;
  223. $log->after = $after;
  224. $log->description = $description;
  225. return $log->save();
  226. }
  227. public static function abortIfNotModified($data): string
  228. {
  229. $req = request();
  230. // Only for "GET" method
  231. if (! $req->isMethod('GET')) {
  232. return '';
  233. }
  234. $etag = sha1(json_encode($data));
  235. if ($etag == $req->header('IF-NONE-MATCH')) {
  236. abort(304);
  237. }
  238. return $etag;
  239. }
  240. }