Controller.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Http\Models\ReferralLog;
  4. use App\Http\Models\SensitiveWords;
  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. class Controller extends BaseController
  13. {
  14. use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
  15. // 生成随机密码
  16. public function makePasswd()
  17. {
  18. exit(makeRandStr());
  19. }
  20. // 生成VmessId
  21. public function makeVmessId()
  22. {
  23. exit(createGuid());
  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. // 类似Linux中的tail命令
  35. public function tail($file, $n, $base = 5)
  36. {
  37. $fileLines = $this->countLine($file);
  38. if ($fileLines < 15000) {
  39. return false;
  40. }
  41. $fp = fopen($file, "r+");
  42. assert($n > 0);
  43. $pos = $n + 1;
  44. $lines = [];
  45. while (count($lines) <= $n) {
  46. try {
  47. fseek($fp, -$pos, SEEK_END);
  48. } catch (\Exception $e) {
  49. fseek(0);
  50. break;
  51. }
  52. $pos *= $base;
  53. while (!feof($fp)) {
  54. array_unshift($lines, fgets($fp));
  55. }
  56. }
  57. return array_slice($lines, 0, $n);
  58. }
  59. /**
  60. * 计算文件行数
  61. */
  62. public function countLine($file)
  63. {
  64. $fp = fopen($file, "r");
  65. $i = 0;
  66. while (!feof($fp)) {
  67. //每次读取2M
  68. if ($data = fread($fp, 1024 * 1024 * 2)) {
  69. //计算读取到的行数
  70. $num = substr_count($data, "\n");
  71. $i += $num;
  72. }
  73. }
  74. fclose($fp);
  75. return $i;
  76. }
  77. /**
  78. * 记录余额操作日志
  79. *
  80. * @param int $userId 用户ID
  81. * @param string $oid 订单ID
  82. * @param int $before 记录前余额
  83. * @param int $after 记录后余额
  84. * @param int $amount 发生金额
  85. * @param string $desc 描述
  86. *
  87. * @return int
  88. */
  89. public function addUserBalanceLog($userId, $oid, $before, $after, $amount, $desc = '')
  90. {
  91. $log = new UserBalanceLog();
  92. $log->user_id = $userId;
  93. $log->order_id = $oid;
  94. $log->before = $before;
  95. $log->after = $after;
  96. $log->amount = $amount;
  97. $log->desc = $desc;
  98. $log->created_at = date('Y-m-d H:i:s');
  99. return $log->save();
  100. }
  101. /**
  102. * 添加返利日志
  103. *
  104. * @param int $userId 用户ID
  105. * @param int $refUserId 返利用户ID
  106. * @param int $oid 订单ID
  107. * @param int $amount 发生金额
  108. * @param int $refAmount 返利金额
  109. *
  110. * @return int
  111. */
  112. public function addReferralLog($userId, $refUserId, $oid, $amount, $refAmount)
  113. {
  114. $log = new ReferralLog();
  115. $log->user_id = $userId;
  116. $log->ref_user_id = $refUserId;
  117. $log->order_id = $oid;
  118. $log->amount = $amount;
  119. $log->ref_amount = $refAmount;
  120. $log->status = 0;
  121. return $log->save();
  122. }
  123. /**
  124. * 添加积分日志
  125. *
  126. * @param int $userId 用户ID
  127. * @param int $before 记录前余额
  128. * @param int $after 记录后余额
  129. * @param int $score 发生值
  130. * @param string $desc 描述
  131. *
  132. * @return int
  133. */
  134. public function addUserScoreLog($userId, $before, $after, $score, $desc = '')
  135. {
  136. $log = new UserScoreLog();
  137. $log->user_id = $userId;
  138. $log->before = $before;
  139. $log->after = $after;
  140. $log->score = $score;
  141. $log->desc = $desc;
  142. $log->created_at = date('Y-m-d H:i:s');
  143. return $log->save();
  144. }
  145. // 获取敏感词
  146. public function sensitiveWords()
  147. {
  148. return SensitiveWords::query()->get()->pluck('words')->toArray();
  149. }
  150. // 将Base64图片转换为本地图片并保存
  151. function base64ImageSaver($base64_image_content)
  152. {
  153. // 匹配出图片的格式
  154. if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)) {
  155. $type = $result[2];
  156. $directory = date('Ymd');
  157. $path = '/assets/images/qrcode/' . $directory . '/';
  158. if (!file_exists(public_path($path))) { // 检查是否有该文件夹,如果没有就创建,并给予最高权限
  159. mkdir(public_path($path), 0755, true);
  160. }
  161. $fileName = makeRandStr(18, true) . ".{$type}";
  162. if (file_put_contents(public_path($path . $fileName), base64_decode(str_replace($result[1], '', $base64_image_content)))) {
  163. chmod(public_path($path . $fileName), 0744);
  164. return $path . $fileName;
  165. } else {
  166. return '';
  167. }
  168. } else {
  169. return '';
  170. }
  171. }
  172. }