Helpers.php 6.1 KB

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