BaseController.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Http\Models\Config;
  4. use App\Http\Models\EmailLog;
  5. use App\Http\Models\Level;
  6. use App\Http\Models\SsConfig;
  7. use App\Http\Models\User;
  8. /**
  9. * 基础控制器
  10. * Class BaseController
  11. * @package App\Http\Controllers
  12. */
  13. class BaseController extends Controller
  14. {
  15. // 生成SS密码
  16. public function makeRandStr($length = 4)
  17. {
  18. // 密码字符集,可任意添加你需要的字符
  19. $chars = 'abcdefghijkmnpqrstuvwxyz23456789';
  20. $char = '@';
  21. for ($i = 0; $i < $length; $i++) {
  22. $char .= $chars[mt_rand(0, strlen($chars) - 1)];
  23. }
  24. return $char;
  25. }
  26. // base64加密(处理URL)
  27. function base64url_encode($data)
  28. {
  29. return strtr(base64_encode($data), array('+' => '-', '/' => '_', '=' => ''));
  30. }
  31. // base64解密(处理URL)
  32. function base64url_decode($data)
  33. {
  34. return base64_decode(strtr($data, '-_', '+/'));
  35. }
  36. // 根据流量值自动转换单位输出
  37. public static function flowAutoShow($value = 0)
  38. {
  39. $kb = 1024;
  40. $mb = 1048576;
  41. $gb = 1073741824;
  42. $tb = $gb * 1024;
  43. $pb = $tb * 1024;
  44. if (abs($value) > $pb) {
  45. return round($value / $pb, 2) . "PB";
  46. } elseif (abs($value) > $tb) {
  47. return round($value / $tb, 2) . "TB";
  48. } elseif (abs($value) > $gb) {
  49. return round($value / $gb, 2) . "GB";
  50. } elseif (abs($value) > $mb) {
  51. return round($value / $mb, 2) . "MB";
  52. } elseif (abs($value) > $kb) {
  53. return round($value / $kb, 2) . "KB";
  54. } else {
  55. return round($value, 2) . "B";
  56. }
  57. }
  58. public static function toMB($traffic)
  59. {
  60. $mb = 1048576;
  61. return $traffic * $mb;
  62. }
  63. public static function toGB($traffic)
  64. {
  65. $gb = 1048576 * 1024;
  66. return $traffic * $gb;
  67. }
  68. public static function flowToGB($traffic)
  69. {
  70. $gb = 1048576 * 1024;
  71. return $traffic / $gb;
  72. }
  73. // 加密方式
  74. public function methodList()
  75. {
  76. return SsConfig::where('type', 1)->get();
  77. }
  78. // 协议
  79. public function protocolList()
  80. {
  81. return SsConfig::where('type', 2)->get();
  82. }
  83. // 混淆
  84. public function obfsList()
  85. {
  86. return SsConfig::where('type', 3)->get();
  87. }
  88. // 等级
  89. public function levelList()
  90. {
  91. return Level::get()->sortBy('level');
  92. }
  93. // 系统配置
  94. public function systemConfig()
  95. {
  96. $config = Config::get();
  97. $data = [];
  98. foreach ($config as $vo) {
  99. $data[$vo->name] = $vo->value;
  100. }
  101. return $data;
  102. }
  103. // 获取一个随机端口
  104. public function getRandPort()
  105. {
  106. $port = mt_rand(10000, 40000);
  107. $deny_port = [17185, 28281];
  108. $exists_port = User::query()->pluck('port')->toArray();
  109. if (in_array($port, $exists_port) || in_array($port, $deny_port)) {
  110. $port = $this->getRandPort();
  111. }
  112. return $port;
  113. }
  114. // 类似Linux中的tail命令
  115. public function tail($file, $n, $base = 5)
  116. {
  117. $fp = fopen($file, "r+");
  118. assert($n > 0);
  119. $pos = $n + 1;
  120. $lines = array();
  121. while (count($lines) <= $n) {
  122. try {
  123. fseek($fp, -$pos, SEEK_END);
  124. } catch (Exception $e) {
  125. fseek(0);
  126. break;
  127. }
  128. $pos *= $base;
  129. while (!feof($fp)) {
  130. array_unshift($lines, fgets($fp));
  131. }
  132. }
  133. return array_slice($lines, 0, $n);
  134. }
  135. /**
  136. * 文件大小转换
  137. *
  138. * @param int $bytes
  139. * @param int $precision
  140. *
  141. * @return string
  142. */
  143. public function formatBytes($bytes, $precision = 2)
  144. {
  145. $units = array('B', 'KB', 'MB', 'GB', 'TB');
  146. $bytes = max($bytes, 0);
  147. $pow = floor(($bytes ? log($bytes) : 0) / log(1024));
  148. $pow = min($pow, count($units) - 1);
  149. $bytes /= pow(1024, $pow);
  150. return round($bytes, $precision) . ' ' . $units[$pow];
  151. }
  152. // 浏览器类型
  153. public function browsers($tag)
  154. {
  155. $data = [
  156. 'MicroMessenger' => '微信',
  157. 'iPhone' => 'iPhone',
  158. 'Linux' => '安卓'
  159. ];
  160. }
  161. /**
  162. * 写入邮件发送日志
  163. * @param int $user_id 用户ID
  164. * @param string $title 投递类型(投递标题)
  165. * @param string $content 投递内容(简要概述)
  166. * @param int $status 投递状态
  167. * @param string $error 投递失败时记录的异常信息
  168. */
  169. public function sendEmailLog($user_id, $title, $content, $status = 1, $error = '')
  170. {
  171. $emailLogObj = new EmailLog();
  172. $emailLogObj->user_id = $user_id;
  173. $emailLogObj->title = $title;
  174. $emailLogObj->content = $content;
  175. $emailLogObj->status = $status;
  176. $emailLogObj->error = $error;
  177. $emailLogObj->created_at = date('Y-m-d H:i:s');
  178. $emailLogObj->save();
  179. }
  180. }