Helpers.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. *
  60. * @return int
  61. */
  62. public static function addUser($email, $password, $transfer_enable, $data, $referral_uid = 0)
  63. {
  64. $user = new User();
  65. $user->username = $email;
  66. $user->email = $email;
  67. $user->password = $password;
  68. // 生成一个可用端口
  69. $user->port = self::systemConfig()['is_rand_port']? Helpers::getRandPort() : Helpers::getOnlyPort();
  70. $user->passwd = makeRandStr();
  71. $user->vmess_id = createGuid();
  72. $user->enable = 1;
  73. $user->method = Helpers::getDefaultMethod();
  74. $user->protocol = Helpers::getDefaultProtocol();
  75. $user->protocol_param = '';
  76. $user->obfs = Helpers::getDefaultObfs();
  77. $user->obfs_param = '';
  78. $user->usage = 1;
  79. $user->transfer_enable = $transfer_enable;
  80. $user->enable_time = date('Y-m-d');
  81. $user->expire_time = date('Y-m-d', strtotime("+".$data." days"));
  82. $user->reg_ip = getClientIp();
  83. $user->referral_uid = $referral_uid;
  84. $user->reset_time = NULL;
  85. $user->status = 0;
  86. $user->save();
  87. return $user->id;
  88. }
  89. // 获取系统配置
  90. public static function systemConfig()
  91. {
  92. $config = Config::query()->get();
  93. $data = [];
  94. foreach($config as $vo){
  95. $data[$vo->name] = $vo->value;
  96. }
  97. $data['is_onlinePay'] = ($data['is_AliPay'] || $data['is_QQPay'] || $data['is_WeChatPay'] || $data['is_otherPay'])? : 0;
  98. return $data;
  99. }
  100. // 获取一个随机端口
  101. public static function getRandPort()
  102. {
  103. $config = self::systemConfig();
  104. $port = mt_rand($config['min_port'], $config['max_port']);
  105. $exists_port = User::query()->pluck('port')->toArray();
  106. if(in_array($port, $exists_port) || in_array($port, self::$denyPorts)){
  107. $port = self::getRandPort();
  108. }
  109. return $port;
  110. }
  111. // 获取一个随机端口
  112. public static function getOnlyPort()
  113. {
  114. $config = self::systemConfig();
  115. $port = $config['min_port'];
  116. $exists_port = User::query()->where('port', '>=', $port)->pluck('port')->toArray();
  117. while(in_array($port, $exists_port) || in_array($port, self::$denyPorts)){
  118. $port = $port+1;
  119. }
  120. return $port;
  121. }
  122. // 获取默认加密方式
  123. public static function getDefaultMethod()
  124. {
  125. $config = SsConfig::default()->type(1)->first();
  126. return $config? $config->name : 'aes-256-cfb';
  127. }
  128. // 获取默认协议
  129. public static function getDefaultProtocol()
  130. {
  131. $config = SsConfig::default()->type(2)->first();
  132. return $config? $config->name : 'origin';
  133. }
  134. // 获取默认混淆
  135. public static function getDefaultObfs()
  136. {
  137. $config = SsConfig::default()->type(3)->first();
  138. return $config? $config->name : 'plain';
  139. }
  140. /**
  141. * 添加通知推送日志
  142. *
  143. * @param string $title 标题
  144. * @param string $content 内容
  145. * @param int $type 发送类型
  146. * @param string $address 收信方
  147. * @param int $status 投递状态
  148. * @param string $error 投递失败时记录的异常信息
  149. *
  150. * @return int
  151. */
  152. public static function addNotificationLog($title, $content, $type, $address = 'admin', $status = 1, $error = '')
  153. {
  154. $log = new NotificationLog();
  155. $log->type = $type;
  156. $log->address = $address;
  157. $log->title = $title;
  158. $log->content = $content;
  159. $log->status = $status;
  160. $log->error = $error;
  161. $log->save();
  162. return $log->id;
  163. }
  164. /**
  165. * 添加优惠券操作日志
  166. *
  167. * @param int $couponId 优惠券ID
  168. * @param int $goodsId 商品ID
  169. * @param int $orderId 订单ID
  170. * @param string $desc 备注
  171. *
  172. * @return int
  173. */
  174. public static function addCouponLog($couponId, $goodsId, $orderId, $desc = '')
  175. {
  176. $log = new CouponLog();
  177. $log->coupon_id = $couponId;
  178. $log->goods_id = $goodsId;
  179. $log->order_id = $orderId;
  180. $log->desc = $desc;
  181. return $log->save();
  182. }
  183. /**
  184. * 记录余额操作日志
  185. *
  186. * @param int $userId 用户ID
  187. * @param string $oid 订单ID
  188. * @param int $before 记录前余额
  189. * @param int $after 记录后余额
  190. * @param int $amount 发生金额
  191. * @param string $desc 描述
  192. *
  193. * @return int
  194. */
  195. public static function addUserBalanceLog($userId, $oid, $before, $after, $amount, $desc = '')
  196. {
  197. $log = new UserBalanceLog();
  198. $log->user_id = $userId;
  199. $log->order_id = $oid;
  200. $log->before = $before;
  201. $log->after = $after;
  202. $log->amount = $amount;
  203. $log->desc = $desc;
  204. $log->created_at = date('Y-m-d H:i:s');
  205. return $log->save();
  206. }
  207. /**
  208. * 记录流量变动日志
  209. *
  210. * @param int $userId 用户ID
  211. * @param string $oid 订单ID
  212. * @param int $before 记录前的值
  213. * @param int $after 记录后的值
  214. * @param string $desc 描述
  215. *
  216. * @return int
  217. */
  218. public static function addUserTrafficModifyLog($userId, $oid, $before, $after, $desc = '')
  219. {
  220. $log = new UserTrafficModifyLog();
  221. $log->user_id = $userId;
  222. $log->order_id = $oid;
  223. $log->before = $before;
  224. $log->after = $after;
  225. $log->desc = $desc;
  226. return $log->save();
  227. }
  228. }