Helpers.php 6.3 KB

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