Helpers.php 6.1 KB

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