Controller.php 8.7 KB

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