Controller.php 5.0 KB

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