Helpers.php 6.3 KB

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