Controller.php 8.9 KB

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