Helpers.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. // 不生成的端口
  15. private static $denyPorts = [
  16. 1068, 1109, 1434, 3127, 3128,
  17. 3129, 3130, 3332, 4444, 5554,
  18. 6669, 8080, 8081, 8082, 8181,
  19. 8282, 9996, 17185, 24554, 35601,
  20. 60177, 60179
  21. ];
  22. // 加密方式
  23. public static function methodList()
  24. {
  25. return SsConfig::type(1)->get();
  26. }
  27. // 协议
  28. public static function protocolList()
  29. {
  30. return SsConfig::type(2)->get();
  31. }
  32. // 混淆
  33. public static function obfsList()
  34. {
  35. return SsConfig::type(3)->get();
  36. }
  37. // 等级
  38. public static function levelList()
  39. {
  40. return Level::query()->get()->sortBy('level');
  41. }
  42. // 生成用户的订阅码
  43. public static function makeSubscribeCode()
  44. {
  45. $code = makeRandStr(5);
  46. if(UserSubscribe::query()->whereCode($code)->exists()){
  47. $code = self::makeSubscribeCode();
  48. }
  49. return $code;
  50. }
  51. /**
  52. * 添加用户
  53. *
  54. * @param string $email 用户邮箱
  55. * @param string $password 用户密码
  56. * @param string $transfer_enable 可用流量
  57. * @param int $data 可使用天数
  58. * @param int $referral_uid 邀请人
  59. * @return int
  60. */
  61. public static function addUser($email, $password, $transfer_enable, $data, $referral_uid = 0)
  62. {
  63. $user = new User();
  64. $user->username = $email;
  65. $user->email = $email;
  66. $user->password = $password;
  67. // 生成一个可用端口
  68. $user->port = self::systemConfig()['is_rand_port']? Helpers::getRandPort() : Helpers::getOnlyPort();
  69. $user->passwd = makeRandStr();
  70. $user->vmess_id = createGuid();
  71. $user->enable = 1;
  72. $user->method = Helpers::getDefaultMethod();
  73. $user->protocol = Helpers::getDefaultProtocol();
  74. $user->protocol_param = '';
  75. $user->obfs = Helpers::getDefaultObfs();
  76. $user->obfs_param = '';
  77. $user->usage = 1;
  78. $user->transfer_enable = $transfer_enable;
  79. $user->enable_time = date('Y-m-d');
  80. $user->expire_time = date('Y-m-d', strtotime("+".$data." days"));
  81. $user->reg_ip = getClientIp();
  82. $user->referral_uid = $referral_uid;
  83. $user->reset_time = NULL;
  84. $user->status = 0;
  85. $user->save();
  86. return $user->id;
  87. }
  88. // 获取系统配置
  89. public static function systemConfig()
  90. {
  91. $config = Config::query()->get();
  92. $data = [];
  93. foreach($config as $vo){
  94. $data[$vo->name] = $vo->value;
  95. }
  96. $data['is_onlinePay'] = ($data['is_AliPay'] || $data['is_QQPay'] || $data['is_WeChatPay'] || $data['is_otherPay'])? : 0;
  97. return $data;
  98. }
  99. // 获取一个随机端口
  100. public static function getRandPort()
  101. {
  102. $config = self::systemConfig();
  103. $port = mt_rand($config['min_port'], $config['max_port']);
  104. $exists_port = User::query()->pluck('port')->toArray();
  105. if(in_array($port, $exists_port) || in_array($port, self::$denyPorts)){
  106. $port = self::getRandPort();
  107. }
  108. return $port;
  109. }
  110. // 获取一个随机端口
  111. public static function getOnlyPort()
  112. {
  113. $config = self::systemConfig();
  114. $port = $config['min_port'];
  115. $exists_port = User::query()->where('port', '>=', $port)->pluck('port')->toArray();
  116. while(in_array($port, $exists_port) || in_array($port, self::$denyPorts)){
  117. $port = $port+1;
  118. }
  119. return $port;
  120. }
  121. // 获取默认加密方式
  122. public static function getDefaultMethod()
  123. {
  124. $config = SsConfig::default()->type(1)->first();
  125. return $config? $config->name : 'aes-256-cfb';
  126. }
  127. // 获取默认协议
  128. public static function getDefaultProtocol()
  129. {
  130. $config = SsConfig::default()->type(2)->first();
  131. return $config? $config->name : 'origin';
  132. }
  133. // 获取默认混淆
  134. public static function getDefaultObfs()
  135. {
  136. $config = SsConfig::default()->type(3)->first();
  137. return $config? $config->name : 'plain';
  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($title, $content, $type, $address = 'admin', $status = 1, $error = '')
  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 int $couponId 优惠券ID
  167. * @param int $goodsId 商品ID
  168. * @param int $orderId 订单ID
  169. * @param string $desc 备注
  170. *
  171. * @return int
  172. */
  173. public static function addCouponLog($couponId, $goodsId, $orderId, $desc = '')
  174. {
  175. $log = new CouponLog();
  176. $log->coupon_id = $couponId;
  177. $log->goods_id = $goodsId;
  178. $log->order_id = $orderId;
  179. $log->desc = $desc;
  180. return $log->save();
  181. }
  182. /**
  183. * 记录余额操作日志
  184. *
  185. * @param int $userId 用户ID
  186. * @param string $oid 订单ID
  187. * @param int $before 记录前余额
  188. * @param int $after 记录后余额
  189. * @param int $amount 发生金额
  190. * @param string $desc 描述
  191. *
  192. * @return int
  193. */
  194. public static function addUserBalanceLog($userId, $oid, $before, $after, $amount, $desc = '')
  195. {
  196. $log = new UserBalanceLog();
  197. $log->user_id = $userId;
  198. $log->order_id = $oid;
  199. $log->before = $before;
  200. $log->after = $after;
  201. $log->amount = $amount;
  202. $log->desc = $desc;
  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 string $oid 订单ID
  211. * @param int $before 记录前的值
  212. * @param int $after 记录后的值
  213. * @param string $desc 描述
  214. *
  215. * @return int
  216. */
  217. public static function addUserTrafficModifyLog($userId, $oid, $before, $after, $desc = '')
  218. {
  219. $log = new UserTrafficModifyLog();
  220. $log->user_id = $userId;
  221. $log->order_id = $oid;
  222. $log->before = $before;
  223. $log->after = $after;
  224. $log->desc = $desc;
  225. return $log->save();
  226. }
  227. }