Helpers.php 8.0 KB

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