Controller.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Http\Models\CouponLog;
  4. use App\Http\Models\UserSubscribe;
  5. use Illuminate\Foundation\Bus\DispatchesJobs;
  6. use Illuminate\Routing\Controller as BaseController;
  7. use Illuminate\Foundation\Validation\ValidatesRequests;
  8. use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
  9. use App\Http\Models\Config;
  10. use App\Http\Models\EmailLog;
  11. use App\Http\Models\Level;
  12. use App\Http\Models\SsConfig;
  13. use App\Http\Models\User;
  14. class Controller extends BaseController
  15. {
  16. use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
  17. public $systemConfig;
  18. public function __construct()
  19. {
  20. $this->systemConfig = $this->systemConfig();
  21. }
  22. // 生成订阅地址的唯一码
  23. public function makeSubscribeCode()
  24. {
  25. $code = makeRandStr(5);
  26. if (UserSubscribe::query()->where('code', $code)->exists()) {
  27. $code = $this->makeSubscribeCode();
  28. }
  29. return $code;
  30. }
  31. // 加密方式
  32. public function methodList()
  33. {
  34. return SsConfig::query()->where('type', 1)->get();
  35. }
  36. // 默认加密方式
  37. public function getDefaultMethod()
  38. {
  39. $config = SsConfig::query()->where('type', 1)->where('is_default', 1)->first();
  40. return $config ? $config->name : 'aes-192-ctr';
  41. }
  42. // 协议
  43. public function protocolList()
  44. {
  45. return SsConfig::query()->where('type', 2)->get();
  46. }
  47. // 默认协议
  48. public function getDefaultProtocol()
  49. {
  50. $config = SsConfig::query()->where('type', 2)->where('is_default', 1)->first();
  51. return $config ? $config->name : 'origin';
  52. }
  53. // 混淆
  54. public function obfsList()
  55. {
  56. return SsConfig::query()->where('type', 3)->get();
  57. }
  58. // 默认混淆
  59. public function getDefaultObfs()
  60. {
  61. $config = SsConfig::query()->where('type', 3)->where('is_default', 1)->first();
  62. return $config ? $config->name : 'plain';
  63. }
  64. // 等级
  65. public function levelList()
  66. {
  67. return Level::query()->get()->sortBy('level');
  68. }
  69. // 系统配置
  70. public function systemConfig()
  71. {
  72. $config = Config::query()->get();
  73. $data = [];
  74. foreach ($config as $vo) {
  75. $data[$vo->name] = $vo->value;
  76. }
  77. return $data;
  78. }
  79. // 获取一个随机端口
  80. public function getRandPort()
  81. {
  82. $config = $this->systemConfig();
  83. $port = mt_rand($config['min_port'], $config['max_port']);
  84. $deny_port = [1068, 1109, 1434, 3127, 3128, 3129, 3130, 3332, 4444, 5554, 6669, 8080, 8081, 8082, 8181, 8282, 9996, 17185, 24554, 35601, 60177, 60179]; // 不生成的端口
  85. $exists_port = User::query()->pluck('port')->toArray();
  86. if (in_array($port, $exists_port) || in_array($port, $deny_port)) {
  87. $port = $this->getRandPort();
  88. }
  89. return $port;
  90. }
  91. // 获取一个端口
  92. public function getOnlyPort()
  93. {
  94. $config = $this->systemConfig();
  95. $port = $config['min_port'];
  96. $deny_port = [1068, 1109, 1434, 3127, 3128, 3129, 3130, 3332, 4444, 5554, 6669, 8080, 8081, 8082, 8181, 8282, 9996, 17185, 24554, 35601, 60177, 60179]; // 不生成的端口
  97. $exists_port = User::query()->where('port', '>=', $config['min_port'])->pluck('port')->toArray();
  98. while (in_array($port, $exists_port) || in_array($port, $deny_port)) {
  99. $port = $port + 1;
  100. }
  101. return $port;
  102. }
  103. // 类似Linux中的tail命令
  104. public function tail($file, $n, $base = 5)
  105. {
  106. $fileLines = $this->countLine($file);
  107. if ($fileLines < 15000) {
  108. return false;
  109. }
  110. $fp = fopen($file, "r+");
  111. assert($n > 0);
  112. $pos = $n + 1;
  113. $lines = [];
  114. while (count($lines) <= $n) {
  115. try {
  116. fseek($fp, -$pos, SEEK_END);
  117. } catch (\Exception $e) {
  118. fseek(0);
  119. break;
  120. }
  121. $pos *= $base;
  122. while (!feof($fp)) {
  123. array_unshift($lines, fgets($fp));
  124. }
  125. }
  126. return array_slice($lines, 0, $n);
  127. }
  128. /**
  129. * 计算文件行数
  130. */
  131. public function countLine($file)
  132. {
  133. $fp = fopen($file, "r");
  134. $i = 0;
  135. while (!feof($fp)) {
  136. //每次读取2M
  137. if ($data = fread($fp, 1024 * 1024 * 2)) {
  138. //计算读取到的行数
  139. $num = substr_count($data, "\n");
  140. $i += $num;
  141. }
  142. }
  143. fclose($fp);
  144. return $i;
  145. }
  146. /**
  147. * 写入邮件发送日志
  148. *
  149. * @param int $user_id 用户ID
  150. * @param string $title 标题
  151. * @param string $content 内容
  152. * @param int $status 投递状态
  153. * @param string $error 投递失败时记录的异常信息
  154. */
  155. public function sendEmailLog($user_id, $title, $content, $status = 1, $error = '')
  156. {
  157. $emailLogObj = new EmailLog();
  158. $emailLogObj->user_id = $user_id;
  159. $emailLogObj->title = $title;
  160. $emailLogObj->content = $content;
  161. $emailLogObj->status = $status;
  162. $emailLogObj->error = $error;
  163. $emailLogObj->created_at = date('Y-m-d H:i:s');
  164. $emailLogObj->save();
  165. }
  166. /**
  167. * 添加优惠券操作日志
  168. *
  169. * @param int $couponId 优惠券ID
  170. * @param int $goodsId 商品ID
  171. * @param int $orderId 订单ID
  172. * @param string $desc 备注
  173. */
  174. public function addCouponLog($couponId, $goodsId, $orderId, $desc = '')
  175. {
  176. $couponLog = new CouponLog();
  177. $couponLog->coupon_id = $couponId;
  178. $couponLog->goods_id = $goodsId;
  179. $couponLog->order_id = $orderId;
  180. $couponLog->desc = $desc;
  181. $couponLog->save();
  182. }
  183. // 将Base64图片转换为本地图片并保存
  184. function base64ImageSaver($base64_image_content)
  185. {
  186. //匹配出图片的格式
  187. if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)) {
  188. $type = $result[2];
  189. $directory = date('Ymd');
  190. $path = '/assets/images/qrcode/' . $directory . '/';
  191. if (!file_exists(public_path($path))) { //检查是否有该文件夹,如果没有就创建,并给予最高权限
  192. mkdir(public_path($path), 0700, true);
  193. }
  194. $fileName = makeRandStr(18, true) . ".{$type}";
  195. if (file_put_contents(public_path($path . $fileName), base64_decode(str_replace($result[1], '', $base64_image_content)))) {
  196. return $path . $fileName;
  197. } else {
  198. return '';
  199. }
  200. } else {
  201. return '';
  202. }
  203. }
  204. }